File: apt-file

package info (click to toggle)
apt-file 2.0.3-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 112 kB
  • ctags: 6
  • sloc: perl: 299; makefile: 61
file content (371 lines) | stat: -rw-r--r-- 9,037 bytes parent folder | download
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/perl -w

#
# apt-file - APT package searching utility -- command-line interface
#
# (c) 2001 Sebastien J. Gross <seb@debian.org>
# $Id: apt-file,v 1.34 2004/01/12 12:13:36 sjg_ Exp $
#
#
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#

use strict;
use ConfigFile "read_config_file";
use Getopt::Long qw/:config no_ignore_case/;
use Data::Dumper;
use File::Basename;
use AptPkg::Config '$_config';
use constant VERSION => "2.0.3";


my $Conf;
my $Version;

sub error($) {
    print STDERR "E: ", shift, $! ? ": $!" : "" ,"\n";
    undef $!;
    exit 1;
}

sub warning($) {
    print STDERR "W: ", shift, $! ? ": $!" : "" ,"\n";
    undef $!;
}

sub debug($) {
    return if ! defined $Conf->{verbose};
    print STDERR "D: ", shift, $! ? ": $!" : "" ,"\n";
    undef $!;
}

sub debug_line($){
    return if ! defined $Conf->{verbose};
    print STDERR shift;
}

sub unique($) {
    my $seen = ();
    return [ grep { ! $seen->{$_}++ } @{(shift)} ];
}

sub reverse_hash($) {
    my $hash = shift;
    my $ret = ();
    foreach my $key (keys %$hash) {
	foreach (@{$hash->{$key}}) {
	    push @{$ret->{$_}}, $key;
	}
    }
    return $ret;
}

sub parse_sources_list($) {
    my $file = shift;
    my $uri;
    my @uri_items;
    my @tmp;
    my $line;
    my $ret;

    my ($cmd, $dest);

    open(SOURCE, "< $file") || error "Can't open $file";
    while(<SOURCE>) {
	next if /^\s*(?:$|\#|(?:deb-|rpm-))/xo;
	chomp;
	my $line = $_;
	debug "got \'$line\'";
	$line =~ s/([^\/])\#.*$/$1/o;
	$line =~ s/^(\S+\s+)\[\S+\]/$1/o;
	$line =~ s/\s+/ /go;
	$line =~ s/^\s+//o;

	# CDROM entry
	if (@tmp = $line =~ m/^([^\[]*)\[([^\]]*)\](.*)$/o) {
	    $tmp[1] =~ s/ /_/g;
	    $line = $tmp[0].'['.$tmp[1].']'.$tmp[2];
	}

	# Handle $(ARCH) in sources.list
	$line =~ s/\$\(ARCH\)/$Conf->{arch}/g;
	debug "kept \'$line\'";

	my( $pkg, $uri, $dist, @extra) = split /\s+/, $line;
	my($scheme, $user, $passwd, $host, $port, $path, $query,
	   $fragment) =
	       $uri =~
	       m|^
	       (?:([^:/?\#]+):)?           # scheme
	       (?://
		(?:
		 ([^:@]*)                  #username
		 (?::([^@]*))?             #passwd
		 @)?
		([^:/?\#]*)                # host
		(?::(\d+))?                # port
		)?
		([^?\#]*)			# path
		(?:\?([^\#]*))?		# query
		(?:\#(.*))?			# fragment
		|ox;

#	print "$scheme, $user, $passwd, $host, $port, $path, $query, $fragment\n";

	my $fetch=[];

	foreach (@extra) {
	    push @$fetch,  m/(.*?)\/(?:.*)/o ? "$dist/$1" : "$dist";
	}

	foreach (@{(unique $fetch)}) {
	    if (!defined $Conf->{"${scheme}"}) {
		warning "Don't know how to handle $scheme";
		next;
	    }
	    $dist = $_;
	    $cmd = $Conf->{"${scheme}"};
	    $dest = $Conf->{destination};
	    my $cache = $Conf->{cache};
	    my $arch = $Conf->{arch};
	    my $cdrom = $Conf->{cdrom_mount};
	    foreach my $var (qw/host port user passwd path dist pkg
			     cache arch uri cdrom/) {
		map {
		    $_ =~ s{<$var(?:\|(.+?))?>}{
			defined eval "\$$var" ? eval "\$$var" :
			    defined $1 ? $1 : "";
		    }gsex;
		} ($cmd, $dest)
	    }
	    $dest =~ s/(\/|_)+/_/go;
	    $cmd =~ s/<dest>/$dest/g;
	    my $hash;
	    foreach (qw/host port user passwd path dist pkg uri line
		     dest cmd/) {
		$hash->{$_} = eval "\$$_";
	    }
	    push @$ret, $hash;
	};
    }
    close SOURCE;
    return $ret;
}

sub fetch_files ($) {
    mkdir $Conf->{cache} if ! -d $Conf->{cache};
    error "Can't write in $Conf->{cache}" if ! -w $Conf->{cache};
    foreach (@{(shift)}) {
	debug $_->{cmd};
	qx|$_->{cmd}| if ! defined $Conf->{dummy};
    }
}

sub do_grep($$) {
    my ($data, $pattern) = @_;
    my $ret;
    my ($pack, $file);
    debug "regexp: $pattern";
    $|=1;
    my $regexp = eval { $Conf->{ignore_case} ? qr/$pattern/i : qr/$pattern/ };
    error($@) if $@;
    foreach(@$data) {
	my $file = "$Conf->{cache}/$_->{dest}";
	next if (! -f $file);
	$file = quotemeta $file;
	debug "Search in $file";
	open (ZCAT, "zcat $file |") ||
	    warning "Can't zcat $file";
	while(<ZCAT>) {
	    next if ! (($pack, $file) = /$regexp/);
	    debug_line ".";
	    foreach (split /,/, $file) {
		push @{$ret->{$pack}}, basename $_;
	    }
	}
	close ZCAT;
	debug_line "\n";
    }
    $ret = reverse_hash($ret);
    if (!defined $Conf->{package_only}) {
	foreach my $key (sort keys %$ret) {
	    foreach (sort @{$ret->{$key}}) {
		print "$key: $_\n";
	    }
	}
    } else {
	print map {"$_\n"} (sort keys %$ret);
    }
    exit 0;
}

sub grep_file($) {
    my $data = shift;
    my $pattern = join "", (
			    '^(.*?',
			    $Conf->{pattern},
			    defined $Conf->{fixed_strings} ?
			    ")" : '[^\s]*)',
			    '\s+(\S+)\s*$',
			    );
    do_grep $data, $pattern;
}

sub grep_package($) {
    my $data = shift;
    my $pattern = join "", (
			    '^(\S+)\s+',
			    '(\S*/',
			    $Conf->{pattern},
			    defined $Conf->{fixed_strings} ?
			    "" : ".*",
			    ")",
			    '$',
			    );
    do_grep $data, $pattern;
}

sub purge_cache($) {
    my $data = shift;
    foreach (@$data) {
	debug "Purging $Conf->{cache}/$_->{dest}";
	next if defined $Conf->{dummy};
	unlink "$Conf->{cache}/$_->{dest}" ||
	    warning "Can't remove $Conf->{cache}/$_->{dest}";
    }
}

sub print_version {
    print <<EOF;
apt-file version $Version
(c) 2002 Sebastien J. Gross <sjg\@debian.org>

EOF
    ;
}

sub print_help {
    my $err_code = shift || 0;

    print_version;
    print <<"EOF";

apt-file [options] action [pattern]

Configuration options:
    --sources-list	-s  <file>	sources.list location
    --cache		-c  <dir>	Cache directory
    --architecture	-a  <arch>	Use specific architecture
    --cdrom-mount	-d  <cdrom>	Use specific cdrom mountpoint
    --package-only	-l		Only display packages name
    --fixed-string	-F		Do not expand pattern
    --ignore-case	-i		Ignore case distinctions
    --regexp		-x		pattern is a regular expression
    --verbose		-v		run in verbose mode
    --dummy		-y		run in dummy mode (no action)
    --help		-h		Show this help.
    --version		-V		Show version number

Action:
    update			Fetch Contents files from apt-sources.
    search	<pattern>	Search files in packages
    list	<pattern>	List files in packages
    purge			Remove cache files
EOF
;
    exit $err_code;
}

sub get_options() {
    my %options = (
		   "sources-list|s=s" => \$Conf->{sources_list},
		   "cache|c=s" => \$Conf->{cache},
		   "architecture|a=s" => \$Conf->{architecture},
		   "cdrom-mount|d=s" => \$Conf->{cdrom_mount},
		   "verbose|v" => \$Conf->{verbose},
		   "ignore-case|i" => \$Conf->{ignore_case},
		   "regexp|x" => \$Conf->{is_regexp},
		   "dummy|y" => \$Conf->{dummy},
		   "package-only|l" => \$Conf->{package_only},
		   "fixed-string|F" => \$Conf->{fixed_strings},
		   "help|h" => \$Conf->{help},
		   "version|V" => \$Conf->{version},
		   );
    GetOptions(%options) || print_help 1;
}

sub main {
    my $conf_file;
    map { $conf_file = $_ if -f $_ } ("/etc/apt/apt-file.conf",
				      "apt-file.conf",
				      "$ENV{HOME}/.apt-file.conf");

    error "No config file found\n" if ! defined $conf_file;
    debug "Using $conf_file";

    $Conf=read_config_file $conf_file;
    get_options();
    if (defined $Conf->{version}) {
	print_version;
	exit 0;
    }

    $_config->init;
    $Conf->{arch} ||= $_config->{'APT::Architecture'};
    $Conf->{sources_list} ||=  $_config->{'Dir'} .
	$_config->{'Dir::Etc'} . $_config->{'Dir::Etc::sourcelist'};
    $Conf->{cache} ||= $_config->{'Dir'} . $_config->{'Dir::Cache'};
    $Conf->{cache} =~ s/\/\s*$//;
    $Conf->{cdrom_mount} ||= $_config->{'Acquire::cdrom::Mount'} ||
	"/cdrom";

    $Conf->{action} = shift @ARGV || "none";
    $Conf->{pattern} = shift @ARGV;
    if(defined $Conf->{pattern}) {
	$Conf->{pattern} =~ s/^\///;
	$Conf->{pattern} = quotemeta($Conf->{pattern}) unless $Conf->{is_regexp};
    }
    undef $!;

    my $actions = {
	update => \&fetch_files,
	search => \&grep_file,
	list => \&grep_package,
	purge => \&purge_cache,
    };

    $Conf->{help}=2 if $Conf->{action} =~ m/search|list/ &&
    	! defined $Conf->{pattern};
    $Conf->{help}=2 if ! defined $actions->{$Conf->{action}} &&
	! defined $Conf->{help};
    print_help($Conf->{help}-1) if defined $Conf->{help};

    my $sources = parse_sources_list $Conf->{sources_list};
    error "No valid sources in $Conf->{sources_list}" if ! defined
	$sources;

    $actions->{$Conf->{action}}->($sources);
}

BEGIN {
    $Version = VERSION;
    main();
}

END {

}

__END__