1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | $| = 1; |
---|
4 | use warnings; |
---|
5 | use strict; |
---|
6 | use locale; # for sort |
---|
7 | |
---|
8 | use Getopt::Std; |
---|
9 | use AptPkg::Config '$_config'; |
---|
10 | use AptPkg::Cache; |
---|
11 | |
---|
12 | our ($opt_n, $opt_d, $opt_c) = (0, |
---|
13 | '/afs/athena.mit.edu/system/athena10/thirdparty', |
---|
14 | '/var/lib/debathena-thirdparty'); |
---|
15 | |
---|
16 | getopts('nd:c:'); |
---|
17 | |
---|
18 | sub debug { |
---|
19 | if ($opt_n) { |
---|
20 | print join(' ', 'D:', @_, "\n"); |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | |
---|
25 | my $codename = `/usr/bin/lsb_release -sc`; |
---|
26 | die "Can't determine codename" unless ($? == 0); |
---|
27 | chomp($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; |
---|
34 | my $cache = AptPkg::Cache->new; |
---|
35 | my $policy = $cache->policy; |
---|
36 | |
---|
37 | my %packages = (); |
---|
38 | my %depends = (); |
---|
39 | |
---|
40 | open(COMMON, join('/', $opt_d, 'common')) || die "Can't open $opt_d/common"; |
---|
41 | debug("Reading 'common' file..."); |
---|
42 | while (<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 | } |
---|
77 | close(COMMON); |
---|
78 | |
---|
79 | open(DIST, join('/', $opt_d, $codename)) || die "Can't open $opt_d/$codename"; |
---|
80 | debug("Reading distro-specific file"); |
---|
81 | while (<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 | } |
---|
102 | close(DIST); |
---|
103 | |
---|
104 | foreach 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 | |
---|
130 | debug("Writing out lists"); |
---|
131 | open(DEPS, '>', join('/', $opt_c, 'dependencies')) || die "Can't open $opt_c/dependencies for writing"; |
---|
132 | open(RECS, '>', join('/', $opt_c, 'recommendations')) || die "Can't open $opt_c/dependencies for writing"; |
---|
133 | foreach (sort(keys(%packages))) { |
---|
134 | if ($packages{$_} == 2) { |
---|
135 | print RECS "$_\n"; |
---|
136 | } else { |
---|
137 | print DEPS "$_\n"; |
---|
138 | } |
---|
139 | } |
---|
140 | close(DEPS); |
---|
141 | close(RECS); |
---|
142 | exit 0; |
---|
143 | |
---|