File: cmdsel-debs

package info (click to toggle)
debram 1.0.3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,796 kB
  • ctags: 437
  • sloc: perl: 2,953; ansic: 1,901; makefile: 169; sh: 14
file content (233 lines) | stat: -rwxr-xr-x 6,654 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#! /usr/bin/perl
use warnings;
use strict;
use integer;
use FindBin;
use lib $FindBin::RealBin;
use Def;
use Cmdsel;

# This helper script lists commands and packages for the body of
# cmdsel-debs.txt.  It requires the path to the big Contents.gz file.
# It optionally also accepts the names of one or more files containing
# lists of selected binary packages, one name per line; if so, the
# script outputs no package not listed.

our $cmd_zcat       = 'zcat';
our $mark_contents  = qr/^FILE\s+LOCATION\s*$/;
our $width          = 24;
our $file_cmdsel    = $Def::cmdsel_txt;
our $file_underride = "${FindBin::RealBin}/cmdsel-debs.data";

our $usage = <<END;
usage: $0 {-t cmdsel.txt} {-d cmdsel-debs.data} [-Dh] \\
[Contents.gz file] {[selections file]...}
    -t cmdsel.txt
    -d cmdsel-debs.data
           use alternate data files (these options must appear first)
    -D ignore cmdsel-debs.data
    -h print this usage message
END

our %f = (
  '1'     => [
    { a => 'usr/bin/'            , b => ''       },
    { a => 'bin/'                , b => ''       },
    { a => 'usr/share/man/man1/' , b => '.1.gz'  },
    { a => 'usr/X11R6/bin/'      , b => ''       },
    { a => 'usr/X11R6/man/man1/' , b => '.1x.gz' },
    { a => 'usr/share/man/man1/' , b => '.1x.gz' },
    { a => 'usr/X11R6/man/man1/' , b => '.1.gz'  },
  ],
  '1x'    => [
    { a => 'usr/X11R6/bin/'      , b => ''       },
    { a => 'usr/bin/'            , b => ''       },
    { a => 'bin/'                , b => ''       },
    { a => 'usr/X11R6/man/man1/' , b => '.1x.gz' },
    { a => 'usr/share/man/man1/' , b => '.1.gz'  },
    { a => 'usr/share/man/man1/' , b => '.1x.gz' },
    { a => 'usr/X11R6/man/man1/' , b => '.1.gz'  },
  ],
  '8'     => [
    { a => 'usr/sbin/'           , b => ''       },
    { a => 'usr/bin/'            , b => ''       },
    { a => 'bin/'                , b => ''       },
    { a => 'sbin/'               , b => ''       },
    { a => 'usr/share/man/man8/' , b => '.8.gz'  },
  ],
);
for my $s ( qw( 4 5 7 ) ) {
  my $x = "${s}x";
  $f{$s} = [
    { a => "usr/share/man/man$s/", b => ".$s.gz" },
    { a => "usr/X11R6/man/man$s/", b => ".$x.gz" },
    { a => "usr/share/man/man$s/", b => ".$x.gz" },
    { a => "usr/X11R6/man/man$s/", b => ".$s.gz" },
  ];
  $f{$x} = [
    { a => "usr/X11R6/man/man$s/", b => ".$x.gz" },
    { a => "usr/share/man/man$s/", b => ".$s.gz" },
    { a => "usr/share/man/man$s/", b => ".$x.gz" },
    { a => "usr/X11R6/man/man$s/", b => ".$s.gz" },
  ];
}

my $width1 = $width - 1;

# Read command-line arguments and options.
while ( @ARGV >= 2 && ( $ARGV[0] eq '-t' || $ARGV[0] eq '-m' ) ) {
  my( $opt, $file ) = splice @ARGV, 0, 2;
  if ( $opt eq '-t' ) { $file_cmdsel    = $file }
  if ( $opt eq '-d' ) { $file_underride = $file }
}
my @opt;
my @arg;
push @{ /^-\S/ ? \@opt : \@arg }, $_ for @ARGV;
my %opt = map {
  my $o = $_;
  map { substr( $o, $_, 1 ) => 1 } 1 .. length($o)-1
} @opt;
if ( @arg < 1 || $opt{'?'} || $opt{h} ) {
  print $usage;
  exit 0;
}
my $file_contents = shift(@arg);
# (At this point, @arg contains the names of the selections files,
# if any.)

# Subroutine: eliminate duplicates from a list, without otherwise
# affecting the list ordering.
sub elimdup (@) {
  my @ret;
  my %already;
  for ( @_ ) {
    push @ret, $_ unless $already{$_};
    ++$already{$_};
  }
  return @ret;
}

# Read in and process cmdsel.txt.
my $parse;
open  F, '<', $file_cmdsel;
  $parse = Cmdsel::parse <F>;
close F;

# Read in the selections.
my %sel;
for ( @arg ) {
  open  F, '<', $_;
    while (<F>) {
      chomp;
      $sel{$_} = 1 if /\S/ && !/^#/;
    }
  close F;
}
print "$_\n" for keys %sel;

# Read in Contents.gz.
my %pkg;
open  F, "-|", "$cmd_zcat $file_contents";
  1 until <F> =~ $mark_contents;
  while (<F>) {
    my( $file, $pkgs ) = /^(\S+)\s+(\S+)\s*$/
      or  die "$0: badly formatted Contents line\n$_\n";
    exists $pkg{$file}
      and die "$0: file $file listed twice\n";
    $pkg{$file} = $pkgs eq '->'
      ? [] : [ elimdup split /\s*,\s*/, $pkgs ];
    $pkg{$file}[$_] =~ /\// or splice @{ $pkg{$file} }, $_, 1
      for reverse 0 .. $#{ $pkg{$file} };
    s/^.*\/// or die "$0: impossible" for @{ $pkg{$file} };
    !@arg || $sel{ $pkg{$file}[$_] } or splice @{ $pkg{$file} }, $_, 1
      for reverse 0 .. $#{ $pkg{$file} };
  }
close F;

# Read in underrides.
my %underride;
if ( !$opt{D} && -e $file_underride ) {
  open  F, '<', $file_underride;
    while (<F>) {
      my( $cmdsect, $pkg ) = /^(\S+)\s+(\S+)$/;
      $underride{$cmdsect} = $pkg;
    }
  close F;
}

# Create an output record for each appropriate cmdsel.txt entry.
my @out0;
for my $ram ( keys %$parse ) {
  my $ram1 = $parse->{$ram};
  my $x    = $ram1->{x};
  for my $cmd ( keys %$x ) {
    next if $cmd eq '...';
    my $cmd1    = $x->{$cmd};
    my $c       = $cmd;
    $c =~ s/^.*\///;
    $c =~ s/\s+-.*$//;
    my $sect    = $cmd1->{sect};
    my $isx     = $cmd1->{isx };
    next if $sect eq $Cmdsel::shell_sect;
    my $sx      = $sect . $isx;
    my $cmdsect = "$cmd($sx)";
    my $f;
    {
      # Find the best candidate file.  A file is a candidate if listed
      # in %pkg.  It is a better candidate if it is also provided by at
      # least one selected package.  It is the best candidate if
      # provided by exactly one selected package.
      my $quality = 0;
      for my $i ( 0 .. $#{ $f{$sx} } ) {
        my $fa = $f{$sx}[$i]{a};
        my $fb = $f{$sx}[$i]{b};
        my $f0 = "$fa$c$fb";
        $pkg{$f0} or next;
        my $q0 = 1;
        my $n0 = @{ $pkg{$f0} };
        if ( $n0 ) {
          ++$q0;
          ++$q0 if $n0 == 1;
        }
        if ( $q0 > $quality ) {
          $f       = $f0;
          $quality = $q0;
        }
        last if $quality >= 3;
      }
    }
    my $pkgs  ; $pkgs   = $pkg{$f} if defined $f;
    my $pkgstr;
    if    ( defined($pkgs) && @$pkgs == 1 ) {
      $pkgstr = $pkgs->[0];
      $underride{$cmdsect}
        and warn "$0: underride for $cmdsect ignored\n";
    }
    elsif ( defined($underride{$cmdsect}) ) {
      $pkgstr = $underride{$cmdsect};
    }
    else {
      $pkgstr = defined($pkgs) && @$pkgs
        ? join( ',', @$pkgs ) : '?';
      warn "$0: missing underride for $cmdsect\n";
    }
    push @out0, {
      sx      => $sx     ,
      cmd     => $cmd    ,
      cmdsect => $cmdsect,
      pkgs    => $pkgs   ,
      pkgstr  => $pkgstr ,
    };
  }
}

my @out = sort {
  $a->{sx     } cmp $b->{sx     } or
  $a->{cmd    } cmp $b->{cmd    } or
  $a->{cmdsect} cmp $b->{cmdsect} or
  $a->{pkgstr } cmp $b->{pkgstr }
} @out0;

printf "%-${width1}s %s\n", "$_->{cmd}($_->{sx})", $_->{pkgstr}
  for @out;