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