Debian Codegraph @

tools/my-dpkg-scanpackages

raw
1 #! /usr/bin/perl
2
3 $version="1.9.17"; # diverted from dpkg 1.9.17
4
5 %kmap= ('optional','suggests',
6 'recommended','recommends',
7 'class','priority',
8 'package_revision','revision');
9
10 @fieldpri= ('Package',
11 'Version',
12 'Priority',
13 'Section',
14 'Essential',
15 'Maintainer',
16 'Pre-Depends',
17 'Depends',
18 'Recommends',
19 'Suggests',
20 'Conflicts',
21 'Provides',
22 'Replaces',
23 'Architecture',
24 'Filename',
25 'Size',
26 'MD5sum',
27 'SHA256',
28 'Description');
29
30 $written=0;
31 $i=100; grep($pri{$_}=$i--,@fieldpri);
32
33 if ($#ARGV > -1 && $ARGV[0] eq "-m") {
34 shift(@ARGV);
35 $opt_medium = shift(@ARGV); }
36 if ($#ARGV > -1 && $ARGV[0] eq "-a") {
37 shift(@ARGV);
38 $opt_arch = shift(@ARGV); }
39 $#ARGV == 1 || $#ARGV == 2
40 or die "Usage: dpkg-scanpackages [-m medium] [-a architecture ] binarypath overridefile [pathprefix] > Packages
41 \te.g. dpkg-scanpackages -m 'Debian GNU/Linux -a i386 binary-i386' \\
42 \t\tbinary-i386 /pub/debian/indices/override.hamm.gz \\
43 \t\tdists/stable/ > binary-i386/Packages\n";
44
45 ($binarydir, $override, $pathprefix) = @ARGV;
46 -d $binarydir or die "Binary dir $binarydir not found\n";
47 -e $override or die "Override file $override not found\n";
48
49 sub vercmp {
50 ($a,$b)=@_;
51 return $vercache{$a,$b} if defined($vercache{$a,$b});
52 system("dpkg --compare-versions $a le $b");
53 $vercache{$a,$a}=$?;
54 return $?;
55 }
56
57 # The extra slash causes symlinks to be followed.
58 open(F,"find $binarydir/ -follow -name '*.deb' -print |")
59 or die "Couldn't open pipe to find: $!\n";
60
61 # Note: contrib and other sections are written as contrib/foo in
62 # the overrides and control file
63 ($sectdir) = "$pathprefix/$binarydir" =~ m:dists/[^\\]+/([^/]+/):;
64 $sectdir =~ s:(main|hamm)/::;
65
66 while (<F>) {
67 chomp($fn=$_);
68 substr($fn,0,length($binarydir)) eq $binarydir
69 or die "$fn not in binary dir $binarydir\n";
70 $t= `dpkg-deb -I $fn control`;
71 if ($t eq "") {
72 warn "Couldn't call dpkg-deb on $fn: $!, skipping package\n";
73 next;
74 }
75 if ($?) {
76 warn "\`dpkg-deb -I $fn control' exited with $?, skipping package\n";
77 next;
78 }
79
80 undef %tv;
81 $o= $t;
82 while ($t =~ s/^\n*(\S+):[ \t]*(.*(\n[ \t].*)*)\n//) {
83 $k= lc $1; $v= $2;
84 if (defined($kmap{$k})) { $k= $kmap{$k}; }
85 if (@kn= grep($k eq lc $_, @fieldpri)) {
86 @kn==1 || die $k;
87 $k= $kn[0];
88 }
89 $v =~ s/\s+$//;
90 $tv{$k}= $v;
91 }
92 $t =~ /^\n*$/
93 or die "Unprocessed text from $fn control file; info:\n$o / $t\n";
94
95 defined($tv{'Package'})
96 or die "No Package field in control file of $fn\n";
97 $p= $tv{'Package'}; delete $tv{'Package'};
98
99 $arch=$tv{'Architecture'};
100 if (defined($opt_arch)) {
101 if ($arch =~ $opt_arch || $arch =~ "all") {
102 # print(STDERR " ! Package $p ($fn) is for the correct architecture $opt_arch: $arch\n");
103 } else {
104 # print(STDERR " ! Package $p ($fn) is not for the correct architecture $opt_arch: $arch\n");
105 next;
106 }
107 }
108
109 if (defined($p1{$p})) {
110 if (&vercmp($tv{'Version'}, $pv{$p,'Version'})) {
111 print(STDERR " ! Package $p (filename $fn) is repeat but newer version;\n".
112 " used that one and ignored data from $pfilename{$p} !\n")
113 || die $!;
114 delete $p1{$p};
115 for $k (keys %k1) {
116 delete $pv{$p,$k};
117 }
118 } else {
119 print(STDERR " ! Package $p (filename $fn) is repeat;\n".
120 " ignored that one and using data from $pfilename{$p} !\n")
121 || die $!;
122 next;
123 }
124 }
125 print(STDERR " ! Package $p (filename $fn) has Filename field!\n") || die $!
126 if defined($tv{'Filename'});
127
128 $tv{'Filename'}= "$pathprefix$fn";
129
130 open(C,"md5sum $fn |") || die "$fn $!";
131 chop($_=<C>); close(C); $? and die "\`md5sum < $fn' exited with $?\n";
132 /^[0-9a-f]{32}/ or die "Strange text from \`md5sum < $fn': \`$_'\n";
133 s/\ .*$//;
134 $tv{'MD5sum'}= $_;
135
136 open(C,"sha256sum $fn |") || die "$fn $!";
137 chop($_=<C>); close(C); $? and die "\`sha256sum < $fn' exited with $?\n";
138 /^[0-9a-f]{32}/ or die "Strange text from \`sha256sum < $fn': \`$_'\n";
139 s/\ .*$//;
140 $tv{'SHA256'}= $_;
141
142 @stat= stat($fn) or die "Couldn't stat $fn: $!\n";
143 $stat[7] or die "$fn is empty\n";
144 $tv{'Size'}= $stat[7];
145
146 if (length($tv{'Revision'})) {
147 $tv{'Version'}.= '-'.$tv{'Revision'};
148 delete $tv{'Revision'};
149 }
150
151 for $k (keys %tv) {
152 $pv{$p,$k}= $tv{$k};
153 $k1{$k}= 1;
154 $p1{$p}= 1;
155 }
156
157 $_= substr($fn,length($binarydir));
158 s#/[^/]+$##; s#^/*##;
159 $psubdir{$p}= $_;
160 $pfilename{$p}= $fn;
161 }
162 close(F);
163 $? and warn "find exited with $?\n";
164
165 select(STDERR); $= = 1000; select(STDOUT);
166
167 format STDERR =
168 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
169 $packages
170 .
171
172 sub writelist {
173 $title= shift(@_);
174 return unless @_;
175 print(STDERR " $title\n") || die $!;
176 $packages= join(' ',sort @_);
177 while (length($packages)) { write(STDERR) || die $!; }
178 print(STDERR "\n") || die $!;
179 }
180
181 @samemaint=();
182
183 if ($override =~ /\.gz$/) {
184 open(O, "zcat $override|")
185 or die "Couldn't open override file $override: $!\n";
186 } else {
187 open(O, $override)
188 or die "Couldn't open override file $override: $!\n";
189 }
190 while (<O>) {
191 s/\#.*//;
192 s/\s+$//;
193 ($p,$priority,$section,$maintainer)= split(/\s+/,$_,4);
194 next unless defined($p1{$p});
195 if (length($maintainer)) {
196 if ($maintainer =~ m/\s*=\>\s*/) {
197 $oldmaint= $`; $newmaint= $'; $debmaint= $pv{$p,'Maintainer'};
198 if (!grep($debmaint eq $_, split(m:\s*//\s*:, $oldmaint))) {
199 push(@changedmaint,
200 " $p (package says $pv{$p,'Maintainer'}, not $oldmaint)\n");
201 } else {
202 $pv{$p,'Maintainer'}= $newmaint;
203 }
204 } elsif ($pv{$p,'Maintainer'} eq $maintainer) {
205 push(@samemaint," $p ($maintainer)\n");
206 } else {
207 print(STDERR " * Unconditional maintainer override for $p *\n") || die $!;
208 $pv{$p,'Maintainer'}= $maintainer;
209 }
210 }
211 $pv{$p,'Priority'}= $priority;
212 $pv{$p,'Section'}= $section;
213 ($sectioncut = $section) =~ s:^[^/]*/::;
214 if (length($psubdir{$p}) && $section ne $psubdir{$p} &&
215 $sectioncut ne $psubdir{$p}) {
216 print(STDERR " !! Package $p has \`Section: $section',".
217 " but file is in \`$psubdir{$p}' !!\n") || die $!;
218 $ouches++;
219 }
220 $o1{$p}= 1;
221 }
222 close(O);
223 print(STDERR "\n") || die $! if $ouches;
224
225 $k1{'Maintainer'}= 1;
226 $k1{'Priority'}= 1;
227 $k1{'Section'}= 1;
228
229 @missingover=();
230
231 for $p (sort keys %p1) {
232 if (!defined($o1{$p})) {
233 push(@missingover,$p);
234 }
235 $r= "Package: $p\n";
236 for $k (sort { $pri{$b} <=> $pri{$a} } keys %k1) {
237 next unless length($pv{$p,$k});
238 $r.= "$k: $pv{$p,$k}\n";
239 }
240 $r.= "X-Medium: $opt_medium\n" if (defined $opt_medium);
241 $r.= "\n";
242 $written++;
243 $p1{$p}= 1;
244 print(STDOUT $r) or die "Failed when writing stdout: $!\n";
245 }
246 close(STDOUT) or die "Couldn't close stdout: $!\n";
247
248 @spuriousover= grep(!defined($p1{$_}),sort keys %o1);
249
250 &writelist("** Packages in archive but missing from override file: **",
251 @missingover);
252 if (@changedmaint) {
253 print(STDERR
254 " ++ Packages in override file with incorrect old maintainer value: ++\n",
255 @changedmaint,
256 "\n") || die $!;
257 }
258 if (@samemaint) {
259 print(STDERR
260 " -- Packages specifying same maintainer as override file: --\n",
261 @samemaint,
262 "\n") || die $!;
263 }
264 if (@spuriousover) {
265 print(STDERR
266 " -- Packages in override file but not in archive: --\n",
267 @spuriousover,
268 "\n") || die $!;
269 }
270
271 print(STDERR " Wrote $written entries to output Packages file.\n") || die $!;