Debian Codegraph @

helper/new-debram-body

raw
1 #! /usr/bin/perl
2 # This script's documentation stands below. Scroll down.
3 use warnings;
4 use strict;
5 use integer;
6 use bytes;
7 use FindBin;
8 use lib $FindBin::RealBin;
9 use Def;
10 use Bnm;
11 use Alpha;
12
13 our $file_maint = $Def::maint_txt;
14 our $file_debram = $Def::debram_txt;
15
16 our $usage = <<END;
17 usage: $0 {-t debram.txt} {-m maint.txt} [-TPwWkch] \\
18 [Packages file]... { - [Packages file]... }
19 -t debram.txt
20 -m maint.txt
21 use alternate data files (these options must appear first)
22 -T output a complete debram.txt
23 -P instead of a new debram.txt body, output a modified Packages file
24 -w within each ram in the output, separate the new pkgs from the old
25 -W output only the new packages
26 -k keep packages already ramified in any Packages file (even after
27 the optional dash `-'; see below)
28 -c output contrib packages, too
29 -h print this usage message
30 Packages listed only in files after the optional dash `-' are excluded
31 from the output (but see -k above, which partially overrides).
32 END
33
34 our $rule = '-' x $Def::width . "\n";
35 our $skip_to_deb = $Def::w_maint + $Def::w_pri;
36
37 # You do not need this optional helper script to install, use, modify,
38 # develop, build, package or distribute `debram'. If you are developing
39 # the debram, however, you may find the script convenient.
40 #
41 # SYNOPSIS
42 #
43 # new-debram-body {-t debram.txt} {-m maint.txt} [-TPwWkch]
44 # [Packages file]... { - [Packages file]... }
45 #
46 # DESCRIPTION
47 #
48 # This script builds a new debram.txt body from one or more new Packages
49 # files and from the old debram.txt. It prints the new debram.txt body
50 # to stdout.
51 #
52 # OPTIONS
53 #
54 # -t debram.txt
55 # -m maint.txt
56 # use alternate data files (these options must appear first)
57 #
58 # -T output a complete debram.txt; not just a body
59 #
60 # -P instead of a new debram.txt body, output a sorted, modified
61 # Packages file
62 #
63 # -w within each ram in the output, separate the new packages from the
64 # old (a new package is defined to be one which appears only in a
65 # Packages file, not in the old debram.txt)
66 #
67 # -W output only the new packages
68 #
69 # -k keep packages already ramified in any Packages file (even after
70 # the optional dash `-'; see below)
71 #
72 # -c output contrib packages, too (contribs are filtered out by default)
73 #
74 # -h (or -?) print a usage message
75 #
76 # INPUT PRECEDENCE
77 #
78 # Packages files named leftmost on the command line take precedence.
79 # However, if the left file lacks a ramno for a certain package but a
80 # file further right has one, the right ramno is read and integrated
81 # with the left package info. Files to the right of a dash `-' are
82 # special: they never add packages to the output (except if the -k
83 # option is given), but they do contribute ramnos for unramified
84 # packages already added (this is useful when we have an old Packages
85 # file with some obsolete packages; we want to lose the obsoletes while
86 # otherwise saving the old ramnos).
87 #
88 # PACKAGES FILES
89 #
90 # A Packages file consists of a list of binary control paragraphs, each
91 # of which resembles the following example.
92 #
93 # Package: apt-zip
94 # Priority: extra
95 # Section: admin
96 # Installed-Size: 104
97 # Maintainer: Giacomo Catenazzi <[email protected]>
98 # Architecture: all
99 # Version: 0.13.2
100 # Depends: apt (>= 0.3.10)
101 # Filename: pool/main/a/apt-zip/apt-zip_0.13.2_all.deb
102 # Size: 14752
103 # MD5sum: 78b979ac3ea325e1a2d4b154f14f5eb4
104 # Description: Update a non-networked computer using apt and removable media
105 # These scripts simplify the process of using dselect and apt on a
106 # non-networked Debian box, using removable media like ZIP floppies.
107 # One generates a `fetch' script (supporting backends such as wget and
108 # lftp, in a modular, extensible way) to be run on a host with better
109 # connectivity, check space constraints of your removable media, and
110 # then install the package on your Debian box.
111 # .
112 # Note on current version: space-checking is not done and spanning
113 # multiple disks is not yet supported.
114 #
115 # You probably have a Packages file on your system at
116 #
117 # /var/lib/dpkg/available
118 #
119 # At the time of this writing (Jan. 2004, revised July 2006), the
120 # various current Packages files are found in Debian's on-line archive
121 # in places like
122 #
123 # dists/testing/main/binary-i386/Packages.gz
124 #
125 # When multiple Packages files are given, the leftmost takes precedence.
126 #
127 # THE RAMIFICATION SPECIAL CONTROL FIELD
128 #
129 # You may find it useful to edit a copy of the Packages file to include
130 # the special control field `Ramification:' in some or all of the binary
131 # control paragraphs. This is no standard control field, of course, but
132 # the script understands it to identify a package's ramification. For
133 # example,
134 #
135 # Ramification: 8212
136 # Package: apt-zip
137 # Priority: extra
138 # Section: admin
139 # Installed-Size: 104
140 # Maintainer: Giacomo Catenazzi <[email protected]>
141 # Architecture: all
142 # Version: 0.13.2
143 # Depends: apt (>= 0.3.10)
144 # Filename: pool/main/a/apt-zip/apt-zip_0.13.2_all.deb
145 # Size: 14752
146 # MD5sum: 78b979ac3ea325e1a2d4b154f14f5eb4
147 # Description: Update a non-networked computer using apt and removable media
148 # These scripts simplify the process of using dselect and apt on a
149 # non-networked Debian box, using removable media like ZIP floppies.
150 # One generates a `fetch' script (supporting backends such as wget and
151 # lftp, in a modular, extensible way) to be run on a host with better
152 # connectivity, check space constraints of your removable media, and
153 # then install the package on your Debian box.
154 # .
155 # Note on current version: space-checking is not done and spanning
156 # multiple disks is not yet supported.
157 #
158 # Extra text may follow the ram number. If so, the script ignores the
159 # extra text. For example,
160 #
161 # Ramification: 8212 APT
162 #
163 # has the same effect as does
164 #
165 # Ramification: 8212
166 #
167 # When no Ramification control field is given, the script uses the
168 # ramification number from the old debram.txt.
169 #
170 # The script's -P option can help you to generate an initial
171 # Ramification-identifying copy of the Packages file.
172 #
173 # PACKAGE SORTING ORDER
174 #
175 # To group related packages together, this script sorts packages in a
176 # peculiar order:
177 #
178 # 1. by maintainer's name; then
179 # 2. by source package name; then
180 # 3. by binary package name with the source's flagship binary first.
181 #
182 # Maintainers are sorted last name first. Refer to `Alpha.pm' for the
183 # precise name-sorting algorithm.
184 #
185 # Package names are sorted first without a leading "lib". This means
186 # that "libncurses5" sorts near "ncurses-bin". (It also means that
187 # "libtool" sorts near "tool", which is wrong. In almost every other
188 # case, though, it is right to sort without the leading "lib"; so this
189 # is how the sort is done.)
190 #
191 # Experience shows such a sort to be surprisingly useful.
192 #
193 # PACKAGES NOT OUTPUT
194 #
195 # Packages not named in the Packages files are not output, even when
196 # they appear in the old debram.txt. Packages whose ramifications
197 # cannot be determined are output only when the -P option has been
198 # given. Contrib packages are output only when the -c option has been
199 # given. (As to non-free packages, their control paragraphs do not
200 # occur in the standard Packages files. If non-free are fed to the
201 # script nevertheless, the script's output is undefined.)
202 #
203 # THE UNKNOWN-MAINTAINER ERROR
204 #
205 # This script often exits with an "unknown maintainer" error. Usually
206 # this error is no sign of trouble; it usually merely means that the
207 # script has seen a new Debian maintainer mentioned somewhere in a
208 # Packages file, a maintainer not yet in maint.txt. Sometimes it means
209 # that the script has found a new spelling of an existing maintainer's
210 # name. In either case, the error is usually simple to fix.
211 #
212 # If you are developing the debram, when you see an "unknown maintainer"
213 # error,
214 #
215 # 1. examine the Packages files to decide whether the unknown
216 # maintainer is really a new maintainer or is an existing
217 # maintainer with an alternate spelling.
218 #
219 # 2. update `maint.txt' accordingly, and
220 #
221 # 3. run this script again.
222 #
223 # Although it is not necessary that `maint.txt' always be kept in
224 # perfect alphabetical order, after making several updates to it you
225 # probably want to `helper/sort-maint' it for neatness' and correctness'
226 # sake.
227 #
228 # FILES
229 #
230 # (See the global parameters at this script's head. The files include
231 # debram.txt and maint.txt.)
232 #
233 # BUGS
234 #
235 # This script uses a lot of memory at runtime: it stores a data
236 # structure duplicating piecewise almost the Packages files' entire
237 # combined text. This is convenient for the script writer but is
238 # technically unnecessary. With additional scripting effort, the memory
239 # usage could be dramatically reduced.
240 #
241 # The -t and -m options should be respected even when they do not appear
242 # first on the command line.
243 #
244 # On the other hand, this is just a helper script. To make such
245 # improvements therefore has low priority.
246 #
247 #
248
249 # The general iconv(3) function does more than is wanted here, so a
250 # limited local iconv function is provided. It converts utf-8
251 # characters directly to Latin-1, except that utf-8 characters not
252 # present in Latin-1 are converted to $utf8_unk (probably '?').
253 sub iconv ($) {
254 local $_ = shift;
255 my $out = '';
256 my( $c1, $c2, $c3, $c4 );
257 while (
258 ( $c1, $c2, $c3, $c4 ) =
259 /^([\000-\177]*)([^\000-\177])([\200-\277]*)(.*)/s
260 ) {
261 if (
262 length($c3) == 1
263 && ( $c2 eq "\302" || $c2 eq "\303" )
264 && ( $c3 ge "\200" && $c3 le "\277" )
265 ) {
266 $c2 = chr( 0100*(ord($c2)-0302) + ord($c3) );
267 }
268 else { $c2 = $Def::utf8_unk }
269 $c3 = '';
270 $out .= "$c1$c2$c3";
271 $_ = "$c4";
272 }
273 $_ = $out . $_;
274 return $_;
275 }
276
277 # Read command-line arguments and options.
278 while ( @ARGV >= 2 && ( $ARGV[0] eq '-t' || $ARGV[0] eq '-m' ) ) {
279 my( $opt, $file ) = splice @ARGV, 0, 2;
280 if ( $opt eq '-t' ) { $file_debram = $file }
281 elsif ( $opt eq '-m' ) { $file_maint = $file }
282 }
283 my @opt;
284 my @arg;
285 push @{ /^-\S/ ? \@opt : \@arg }, $_ for @ARGV;
286 my %opt = map {
287 my $o = $_;
288 map { substr( $o, $_, 1 ) => 1 } 1 .. length($o)-1
289 } @opt;
290 if ( @arg < 1 || $opt{'?'} || $opt{h} ) {
291 print $usage;
292 exit 0;
293 }
294
295 # Read the maintainer names.
296 my %mnstd; # debram-standard maintainer names
297 my %maint; # full maintainer names
298 {
299 open F, '<', $file_maint;
300 local $/ = '';
301 while ( <F> ) {
302 my @m = grep { /\S/ } split "\n";
303 my $ms = shift @m;
304 $mnstd{$ms} and die "$0: std name $ms appears twice\n";
305 $mnstd{$ms} = 1;
306 for ( @m ) {
307 defined $maint{$_}
308 and die "$0: full maint name $_ appears twice\n";
309 $maint{$_} = $ms;
310 };
311 }
312 close F;
313 }
314
315 # Read the available packages from the Packages files.
316 my %deb; # Debian packages
317 my $after_dash = 0;
318 my %maint_unk;
319 for my $file_avail ( @arg ) {
320 $file_avail eq '-' and $after_dash = 1, next;
321 open F, '<', $file_avail;
322 local $/ = '';
323 FILE_LOOP: while ( <F> ) {
324 {
325 my $psect = qr/(?:${Def::contrib}\/|.*\/${Def::contrib}$)/om;
326 next FILE_LOOP if !$opt{c} && /^${Def::Section}:[ \t]+$psect/om;
327 }
328 my( $ram ) = /^${Def::Ramification}:[ \t]+(\d{4})/om;
329 my $mnt0;
330 ( $mnt0 ) = /^${Def::Maintainer}:[ \t]+([^<>]+?)[ \t]+</om or
331 ( $mnt0 ) = /^${Def::Maintainer}:[ \t]+(\S+@\S+)/om
332 or die
333 "$0: control paragraph lacks a proper " .
334 "${Def::Maintainer} field\n$_\n";
335 defined $maint{$mnt0} or $maint_unk{$mnt0} = 1;
336 my $maint = $maint {$mnt0};
337 my( $pri0 ) = /^${Def::Priority}:[ \t]+(\S+)/om
338 or die
339 "$0: control paragraph lacks a proper " .
340 "${Def::Priority} field\n$_\n";
341 my $pri = $Def::pri{$pri0}
342 or die "$0: unknown priority $pri0\n";
343 my( $deb ) = /^${Def::Package}:[ \t]+(\S+)/om
344 or die
345 "$0: control paragraph lacks a proper " .
346 "${Def::Package} field\n$_\n";
347 if ( $deb{$deb} ) {
348 $deb{$deb}{ram} = $ram unless defined $deb{$deb}{ram};
349 next;
350 }
351 next if $after_dash && !( $opt{k} && defined($ram) );
352 my( $src ) = /^${Def::Source}:[ \t]+(\S+)/om;
353 my $lead;
354 if ( defined( $src ) && $deb ne $src ) {
355 $lead = 0;
356 }
357 else {
358 $lead = 1;
359 $src = $deb;
360 }
361 my( $desc ) = /^${Def::Description}:[ \t]+(.+)/om
362 or die
363 "$0: control paragraph lacks a proper " .
364 "${Def::Description} field\n$_\n";
365 $desc = iconv $desc if $desc =~ /[^\000-\177]/;
366 $deb{$deb} = {
367 maint => $maint,
368 pri => $pri ,
369 src => $src ,
370 lead => $lead , # is this a flagship package?
371 desc => $desc ,
372 ram => $ram ,
373 control => $_ ,
374 old => 0 ,
375 # had this package been ramified
376 # in the old debram.txt?
377 reramified
378 => 0 ,
379 # had this package been ramified under a different ramno
380 # in the old debram.txt?
381 };
382 }
383 close F;
384 }
385 if ( keys %maint_unk ) {
386 warn "$0: unknown maintainer(s):\n\n";
387 warn "$_\n" . iconv($_) . "\n\n" for keys %maint_unk;
388 exit 1;
389 }
390
391 # Read the section titles. Record package ramifications not already
392 # known.
393 my %title; # ram titles
394 my @head = (); # debram.txt head
395 my @tail = (); # debram.txt tail
396 {
397 open F, '<', $file_debram;
398 while ( <F> ) {
399 push @head, $_;
400 last if $_ eq $Def::mark_main_body;
401 }
402 push @head, scalar <F>;
403 local $/ = '';
404 while ( <F> ) {
405 s/\n?\z/\n/s;
406 my @c0 = /.*\n/g;
407 my @c = grep { /\S/ } @c0;
408 chomp @c;
409 shift @c;
410 my( $ram, $title ) =
411 shift( @c ) =~ /^(\d{${Def::ndig}}) (.+?) \(\d+\)$/o
412 or do {
413 push @tail, @c0;
414 last;
415 };
416 defined $title{$ram} and die "$0: ram $ram is titled twice\n";
417 $title{$ram} = $title;
418 shift @c;
419 for ( @c ) {
420 my( $deb ) = /^.{$skip_to_deb}(\S+)/o
421 or die "$0: badly formed old debram line\n$_\n";
422 $deb{$deb} or next;
423 if ( defined $deb{$deb}{ram} ) {
424 $deb{$deb}{reramified} = 1
425 if $ram ne $deb{$deb}{ram};
426 }
427 else {
428 $deb{$deb}{ram} = $ram;
429 }
430 $deb{$deb}{old} = 1;
431 }
432 }
433 push @tail, <F>;
434 close F;
435 }
436
437 # Sort the packages in two ways: by ramification then
438 # maintainer (%debram); by maintainer (@pkg).
439 my @pkg = sort {
440 Alpha::cmp_std_mn
441 ( $deb{$a}{maint}, $deb{$b}{maint} )
442 || Bnm::cmp_prefixless
443 ( $deb{$a}{src }, $deb{$b}{src } )
444 || $deb{$b}{lead} <=> $deb{$a}{lead}
445 || Bnm::cmp_prefixless
446 ( $a , $b )
447 } keys %deb;
448 my %debram = map { $_ => [] } keys %title;
449 for my $deb ( @pkg ) {
450 my $ram = $deb{$deb}{ram};
451 defined( $ram ) or next;
452 $debram{$ram} or die "$0: unknown ram $ram\n";
453 push @{ $debram{$ram} }, $deb;
454 }
455
456 # Subroutine: fill a field with dots.
457 sub dot ($$) {
458 my( $c, $w ) = @_;
459 $c .= ' ';
460 if ( length( $c ) < $w - 1 )
461 { $c .= "\267" x ( $w - length( $c ) - 1 ) }
462 if ( length( $c ) < $w ) { $c .= ' ' }
463 return $c;
464 }
465
466 # Subroutine: output one line describing a single deb.
467 sub debout (;$) {
468 local $_ = @_ ? $_[0] : $_;
469 my $out;
470 $out .= dot $deb{$_}{maint}, $Def::w_maint;
471 $out .= dot $deb{$_}{pri }, $Def::w_pri ;
472 $out .= dot $_ , $Def::w_deb ;
473 $out .= $deb{$_}{desc } ;
474 $out =~ s/^(.{0,${Def::width}}).*$/$1/o;
475 print "$out\n";
476 }
477
478 # Output the reformed ramification.
479 unless ( $opt{P} ) {
480 print @head if $opt{T};
481 for my $ram ( sort keys %debram ) {
482 print $rule;
483 print "$ram $title{$ram} ("
484 . scalar( @{ $debram{$ram} } ) . ")\n";
485 print $rule;
486 if ( $opt{w} || $opt{W} ) {
487 my @old;
488 my @new;
489 push @{
490 $deb{$_}{old} && !$deb{$_}{reramified}
491 ? \@old : \@new
492 }, $_ for @{ $debram{$ram} };
493 if ( !$opt{W} ) {
494 debout for @old;
495 print $rule;
496 }
497 debout for @new;
498 }
499 else { debout for @{ $debram{$ram} } }
500 print "\n";
501 }
502 print @tail if $opt{T};
503 }
504
505 # Output a working Packages file.
506 else {
507 for ( @pkg ) {
508 $opt{W} && $deb{$_}{old} && !$deb{$_}{reramified} and next;
509 my $control = $deb{$_}{control};
510 $control =~ s/^${Def::Ramification}:[ \t]+.*\n?//omg;
511 print "${Def::Ramification}: "
512 . "$deb{$_}{ram} $title{ $deb{$_}{ram} }\n"
513 if defined $deb{$_}{ram};
514 print $control;
515 }
516 }
517