source: trunk/debathena/debathena/thirdparty-installer/debian/generate-package-list.pl @ 25654

Revision 25654, 3.3 KB checked in by jdreed, 12 years ago (diff)
In thirdparty-installer: * Initial release.
  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2
3$| = 1;
4use warnings;
5use strict;
6use locale; # for sort
7
8use Getopt::Std;
9use AptPkg::Config '$_config';
10use AptPkg::Cache;
11
12our ($opt_n, $opt_d, $opt_c) = (0,
13                                '/afs/athena.mit.edu/system/athena10/thirdparty',
14                                '/var/lib/debathena-thirdparty');
15
16getopts('nd:c:');
17
18sub debug {
19    if ($opt_n) {
20        print join(' ', 'D:', @_, "\n");
21    }
22}
23
24
25my $codename = `/usr/bin/lsb_release -sc`;
26die "Can't determine codename" unless ($? == 0);
27chomp($codename);
28
29(-d $opt_d) || die "$opt_d is not a directory";
30(-d $opt_c) || die "$opt_c is not a directory";
31
32# Initialize the APT configuration
33$_config->init;
34my $cache = AptPkg::Cache->new;
35my $policy = $cache->policy;
36
37my %packages = ();
38my %depends = ();
39
40open(COMMON, join('/', $opt_d, 'common')) || die "Can't open $opt_d/common";
41debug("Reading 'common' file...");
42while (<COMMON>) {
43    chomp;
44    s/^\s+//;
45    s/\s+$//;
46    next if /^#/;
47    next unless /\S/;
48    if (/^-/) {
49        warn "Ignoring invalid package exclusion in the common file, line $.";
50        next;
51    }
52    if (/^(\S+) \| (\S+)$/) {
53        debug("Examining conditional line: $_");
54        foreach my $p ($1, $2) {
55            debug("Checking for $p");
56            if ($cache->{$p} && $cache->{$p}->{VersionList}) {
57                debug("Adding $p to dependencies");
58                $packages{$p} = 1;
59                last;
60            }
61        }
62        unless (exists($packages{$1}) || exists($packages{$2})) {
63            warn "Could not satisfy conditional dependency: $_!";
64        }
65    } elsif (/^(\S+)(?: (\S+))+$/) {
66        my ($pkg1, @rest) = (split(' ', $_));
67        $packages{$pkg1} = 1;
68        $depends{$pkg1} = \@rest;
69    } elsif (/^\?(\S+)$/) {
70        debug("Adding $1 to recommendations");
71        $packages{$1} = 2;
72    } else {
73        debug("Adding $_ to dependencies");
74        $packages{$_} = 1;
75    }
76}
77close(COMMON);
78
79open(DIST, join('/', $opt_d, $codename)) || die "Can't open $opt_d/$codename";
80debug("Reading distro-specific file");
81while (<DIST>) {
82    chomp;
83    s/^\s+//;
84    s/\s+$//;
85    next if /^#/;
86    next unless /\S/;
87    if (/^-(\S+)$/) {
88        if (exists($packages{$1})) {
89            debug("Deleting $1 from package list.");
90            delete($packages{$1});
91        } else {
92            warn("Package $1 is not in package list, so can't remove it.");
93        }
94    } elsif (/^\?(\S+)$/) {
95        debug("Adding $1 to recommendations");
96        $packages{$1} = 2;
97    } else {
98        debug("Adding $_ to dependencies");
99        $packages{$_} = 1;
100    }
101}
102close(DIST);
103
104foreach my $pkgname (sort(keys(%packages))) {
105    my $pkg = $cache->{$pkgname};
106    if (! $pkg) {
107        debug("Removing $pkgname as it can't be found in the APT cache.");
108        delete($packages{$pkgname});
109        if (exists($depends{$pkgname})) {
110            foreach (@{$depends{$pkgname}}) {
111                debug("Removing $_ because we removed $pkgname");
112                delete($packages{$_});
113            }
114        }
115        next;
116    }
117    if (! $pkg->{VersionList}) {
118        debug("Removing $pkgname as it has no version candidate");
119        delete($packages{$pkgname});
120        if (exists($depends{$pkgname})) {
121            foreach (@{$depends{$pkgname}}) {
122                debug("Removing $_ because we removed $pkgname");
123                delete($packages{$_});
124            }
125        }
126        next;
127    }
128}
129
130debug("Writing out lists");
131open(DEPS, '>', join('/', $opt_c, 'dependencies')) || die "Can't open $opt_c/dependencies for writing";
132open(RECS, '>', join('/', $opt_c, 'recommendations')) || die "Can't open $opt_c/dependencies for writing";
133foreach (sort(keys(%packages))) {
134    if ($packages{$_} == 2) {
135        print RECS "$_\n";
136    } else {
137        print DEPS "$_\n";
138    }
139}
140close(DEPS);
141close(RECS);
142exit 0;
143       
Note: See TracBrowser for help on using the repository browser.