Debian Codegraph @

helper/cmdsel-debs

raw
1 #! /usr/bin/perl
2 use warnings;
3 use strict;
4 use integer;
5 use FindBin;
6 use lib $FindBin::RealBin;
7 use Def;
8 use Cmdsel;
9
10 # This helper script lists commands and packages for the body of
11 # cmdsel-debs.txt. It requires the path to the big Contents.gz file.
12 # It optionally also accepts the names of one or more files containing
13 # lists of selected binary packages, one name per line; if so, the
14 # script outputs no package not listed.
15
16 our $cmd_zcat = 'zcat';
17 our $mark_contents = qr/^FILE\s+LOCATION\s*$/;
18 our $width = 24;
19 our $file_cmdsel = $Def::cmdsel_txt;
20 our $file_underride = "${FindBin::RealBin}/cmdsel-debs.data";
21
22 our $usage = <<END;
23 usage: $0 {-t cmdsel.txt} {-d cmdsel-debs.data} [-Dh] \\
24 [Contents.gz file] {[selections file]...}
25 -t cmdsel.txt
26 -d cmdsel-debs.data
27 use alternate data files (these options must appear first)
28 -D ignore cmdsel-debs.data
29 -h print this usage message
30 END
31
32 our %f = (
33 '1' => [
34 { a => 'usr/bin/' , b => '' },
35 { a => 'bin/' , b => '' },
36 { a => 'usr/share/man/man1/' , b => '.1.gz' },
37 { a => 'usr/X11R6/bin/' , b => '' },
38 { a => 'usr/X11R6/man/man1/' , b => '.1x.gz' },
39 { a => 'usr/share/man/man1/' , b => '.1x.gz' },
40 { a => 'usr/X11R6/man/man1/' , b => '.1.gz' },
41 ],
42 '1x' => [
43 { a => 'usr/X11R6/bin/' , b => '' },
44 { a => 'usr/bin/' , b => '' },
45 { a => 'bin/' , b => '' },
46 { a => 'usr/X11R6/man/man1/' , b => '.1x.gz' },
47 { a => 'usr/share/man/man1/' , b => '.1.gz' },
48 { a => 'usr/share/man/man1/' , b => '.1x.gz' },
49 { a => 'usr/X11R6/man/man1/' , b => '.1.gz' },
50 ],
51 '8' => [
52 { a => 'usr/sbin/' , b => '' },
53 { a => 'usr/bin/' , b => '' },
54 { a => 'bin/' , b => '' },
55 { a => 'sbin/' , b => '' },
56 { a => 'usr/share/man/man8/' , b => '.8.gz' },
57 ],
58 );
59 for my $s ( qw( 4 5 7 ) ) {
60 my $x = "${s}x";
61 $f{$s} = [
62 { a => "usr/share/man/man$s/", b => ".$s.gz" },
63 { a => "usr/X11R6/man/man$s/", b => ".$x.gz" },
64 { a => "usr/share/man/man$s/", b => ".$x.gz" },
65 { a => "usr/X11R6/man/man$s/", b => ".$s.gz" },
66 ];
67 $f{$x} = [
68 { a => "usr/X11R6/man/man$s/", b => ".$x.gz" },
69 { a => "usr/share/man/man$s/", b => ".$s.gz" },
70 { a => "usr/share/man/man$s/", b => ".$x.gz" },
71 { a => "usr/X11R6/man/man$s/", b => ".$s.gz" },
72 ];
73 }
74
75 my $width1 = $width - 1;
76
77 # Read command-line arguments and options.
78 while ( @ARGV >= 2 && ( $ARGV[0] eq '-t' || $ARGV[0] eq '-m' ) ) {
79 my( $opt, $file ) = splice @ARGV, 0, 2;
80 if ( $opt eq '-t' ) { $file_cmdsel = $file }
81 if ( $opt eq '-d' ) { $file_underride = $file }
82 }
83 my @opt;
84 my @arg;
85 push @{ /^-\S/ ? \@opt : \@arg }, $_ for @ARGV;
86 my %opt = map {
87 my $o = $_;
88 map { substr( $o, $_, 1 ) => 1 } 1 .. length($o)-1
89 } @opt;
90 if ( @arg < 1 || $opt{'?'} || $opt{h} ) {
91 print $usage;
92 exit 0;
93 }
94 my $file_contents = shift(@arg);
95 # (At this point, @arg contains the names of the selections files,
96 # if any.)
97
98 # Subroutine: eliminate duplicates from a list, without otherwise
99 # affecting the list ordering.
100 sub elimdup (@) {
101 my @ret;
102 my %already;
103 for ( @_ ) {
104 push @ret, $_ unless $already{$_};
105 ++$already{$_};
106 }
107 return @ret;
108 }
109
110 # Read in and process cmdsel.txt.
111 my $parse;
112 open F, '<', $file_cmdsel;
113 $parse = Cmdsel::parse <F>;
114 close F;
115
116 # Read in the selections.
117 my %sel;
118 for ( @arg ) {
119 open F, '<', $_;
120 while (<F>) {
121 chomp;
122 $sel{$_} = 1 if /\S/ && !/^#/;
123 }
124 close F;
125 }
126 print "$_\n" for keys %sel;
127
128 # Read in Contents.gz.
129 my %pkg;
130 open F, "-|", "$cmd_zcat $file_contents";
131 1 until <F> =~ $mark_contents;
132 while (<F>) {
133 my( $file, $pkgs ) = /^(\S+)\s+(\S+)\s*$/
134 or die "$0: badly formatted Contents line\n$_\n";
135 exists $pkg{$file}
136 and die "$0: file $file listed twice\n";
137 $pkg{$file} = $pkgs eq '->'
138 ? [] : [ elimdup split /\s*,\s*/, $pkgs ];
139 $pkg{$file}[$_] =~ /\// or splice @{ $pkg{$file} }, $_, 1
140 for reverse 0 .. $#{ $pkg{$file} };
141 s/^.*\/// or die "$0: impossible" for @{ $pkg{$file} };
142 !@arg || $sel{ $pkg{$file}[$_] } or splice @{ $pkg{$file} }, $_, 1
143 for reverse 0 .. $#{ $pkg{$file} };
144 }
145 close F;
146
147 # Read in underrides.
148 my %underride;
149 if ( !$opt{D} && -e $file_underride ) {
150 open F, '<', $file_underride;
151 while (<F>) {
152 my( $cmdsect, $pkg ) = /^(\S+)\s+(\S+)$/;
153 $underride{$cmdsect} = $pkg;
154 }
155 close F;
156 }
157
158 # Create an output record for each appropriate cmdsel.txt entry.
159 my @out0;
160 for my $ram ( keys %$parse ) {
161 my $ram1 = $parse->{$ram};
162 my $x = $ram1->{x};
163 for my $cmd ( keys %$x ) {
164 next if $cmd eq '...';
165 my $cmd1 = $x->{$cmd};
166 my $c = $cmd;
167 $c =~ s/^.*\///;
168 $c =~ s/\s+-.*$//;
169 my $sect = $cmd1->{sect};
170 my $isx = $cmd1->{isx };
171 next if $sect eq $Cmdsel::shell_sect;
172 my $sx = $sect . $isx;
173 my $cmdsect = "$cmd($sx)";
174 my $f;
175 {
176 # Find the best candidate file. A file is a candidate if listed
177 # in %pkg. It is a better candidate if it is also provided by at
178 # least one selected package. It is the best candidate if
179 # provided by exactly one selected package.
180 my $quality = 0;
181 for my $i ( 0 .. $#{ $f{$sx} } ) {
182 my $fa = $f{$sx}[$i]{a};
183 my $fb = $f{$sx}[$i]{b};
184 my $f0 = "$fa$c$fb";
185 $pkg{$f0} or next;
186 my $q0 = 1;
187 my $n0 = @{ $pkg{$f0} };
188 if ( $n0 ) {
189 ++$q0;
190 ++$q0 if $n0 == 1;
191 }
192 if ( $q0 > $quality ) {
193 $f = $f0;
194 $quality = $q0;
195 }
196 last if $quality >= 3;
197 }
198 }
199 my $pkgs ; $pkgs = $pkg{$f} if defined $f;
200 my $pkgstr;
201 if ( defined($pkgs) && @$pkgs == 1 ) {
202 $pkgstr = $pkgs->[0];
203 $underride{$cmdsect}
204 and warn "$0: underride for $cmdsect ignored\n";
205 }
206 elsif ( defined($underride{$cmdsect}) ) {
207 $pkgstr = $underride{$cmdsect};
208 }
209 else {
210 $pkgstr = defined($pkgs) && @$pkgs
211 ? join( ',', @$pkgs ) : '?';
212 warn "$0: missing underride for $cmdsect\n";
213 }
214 push @out0, {
215 sx => $sx ,
216 cmd => $cmd ,
217 cmdsect => $cmdsect,
218 pkgs => $pkgs ,
219 pkgstr => $pkgstr ,
220 };
221 }
222 }
223
224 my @out = sort {
225 $a->{sx } cmp $b->{sx } or
226 $a->{cmd } cmp $b->{cmd } or
227 $a->{cmdsect} cmp $b->{cmdsect} or
228 $a->{pkgstr } cmp $b->{pkgstr }
229 } @out0;
230
231 printf "%-${width1}s %s\n", "$_->{cmd}($_->{sx})", $_->{pkgstr}
232 for @out;
233