summaryrefslogtreecommitdiffstats
path: root/urpmf
blob: 92a959b05d04f729727052ca84779c178570fdf1 (plain)
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
#!/usr/bin/perl


#- Copyright (C) 2002, 2003, 2004, 2005 MandrakeSoft SA
#- Copyright (C) 2005-2010 Mandriva SA
#- Copyright (C) 2011-2020 Mageia
#-
#- This program 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; either version 2, or (at your option)
#- any later version.
#-
#- This program 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 program; if not, write to the Free Software
#- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use strict;
use urpm;
use urpm::args;
use urpm::msg;
use urpm::media;

sub usage() {
    print urpm::args::copyright('urpmf', [ '2002-2010', 'Mandriva' ], [ '2011-2020', 'Mageia' ])
   . N("  --help         - print this help message.
") . N("  --version      - print this tool's version number.
") . N("  --env          - use specific environment (typically a bug report).
") . N("  --excludemedia - do not use the given media, separated by comma.
") . N("  --literal, -l  - don't match patterns, use argument as a literal string.
") . N("  --urpmi-root   - use another root for urpmi db & rpm installation.
") . N("  --media        - use only the given media, separated by comma.
") . N("  --sortmedia    - sort media according to substrings separated by comma.
") . N("  --use-distrib  - use the given path to access media
") . N("  --synthesis    - use the given synthesis instead of urpmi db.
") . N("  --uniq         - do not print identical lines twice.
") . N("  --update       - use only update media.
") . N("  --verbose      - verbose mode.
") . N("  -i             - ignore case distinctions in patterns.
") . N("  -I             - honor case distinctions in patterns (default).
") . N("  -F<str>        - change field separator (defaults to ':').
") . N("Pattern expressions:
") . N("  text           - any text is parsed as a regexp, unless -l is used.
") . N("  -e             - include perl code directly as perl -e.
") . N("  -a             - binary AND operator.
") . N("  -o             - binary OR operator.
") . N("  !              - unary NOT.
") . N("  ( )            - left and right parentheses.
") . N("List of tags:
") . N("  --qf           - specify a printf-like output format
") . N("                   example: '%%name:%%files'
") . N("  --arch         - architecture
") . N("  --buildhost    - build host
") . N("  --buildtime    - build time
") . N("  --conffiles    - configuration files
") . N("  --conflicts    - conflict tags
") . N("  --description  - package description
") . N("  --distribution - distribution
") . N("  --epoch        - epoch
") . N("  --filename     - filename of the package
") . N("  --files        - list of files contained in the package
") . N("  --group        - group
") . N("  --license      - license
") . N("  --name         - package name
") . N("  --obsoletes    - obsoletes tags
") . N("  --packager     - packager
") . N("  --provides     - provides tags
") . N("  --requires     - requires tags
") . N("  --size         - installed size
") . N("  --sourcerpm    - source rpm name
") . N("  --recommends   - recommends tags
") . N("  --summary      - summary
") . N("  --url          - url
") . N("  --vendor       - vendor
") . N("  -m             - the media in which the package was found
") . N("  -f             - print version, release and arch with name.
");
    exit(1);
}

my %tags_per_media_info = (
  everywhere => [ qw(
    arch
    epoch
    filename
    name
    release
    version
   ) ],

   synthesis => [ qw(
    conflicts
    group
    obsoletes
    provides
    recommends
    requires
    size
    suggests
    summary
   ) ],

   xml_info__info => [ qw(
    description
    license
    sourcerpm
    url
   ) ],

   xml_info__files => [ qw(
    files
   ) ],

   hdlist => [ qw(
    buildhost
    buildtime
    conf_files
    distribution
    packager
    vendor
   ) ],
);

urpm::args::add_urpmf_cmdline_tags(map { @$_ } values %tags_per_media_info);

#- default options.
our $env;		# bug report directory
our $excludemedia = '';
our $full = '';		# -f : print rpm fullname instead of rpm name
our $literal = 0;	# should we quotemeta the pattern
our $media = '';
our $pattern = '';	# regexp match flags ("i" or "")
our $qf = '%default';	# format string
our $separator = ':';	# default field separator
our $sortmedia = '';
our $uniq = '';		# --uniq
our $update = 0;	# --update

#- globals used in callback
our ($expr, $left_expr); # regexp to match against
our %uniq;

#- parse arguments list.
my $urpm = urpm->new_parse_cmdline or exit(1);
defined $left_expr and $urpm->{fatal}(1, N("unterminated expression (%s)", $left_expr));
defined $expr or usage();

if ($qf eq '%default') {
    #- nothing on the command-line : default is to search on file names
    $qf = '%name' . $separator . '%files';
} else {
    #- else default to a leading %name
    $qf =~ s/%default\b/%name/;
}

#- replace first %name by %fullname if -f was specified
if ($full) { $qf =~ s/%name\b/%fullname/ }

#- we really don't want logs on stdout, and remove verbose if not asked.
$urpm->{info} = sub { print STDERR "$_[0]\n" };
$urpm->{log} = sub { print STDERR "$_[0]\n" } if $options{verbose} > 0;

my $only_simple_files_search;
if ($qf eq '%name:%files') {
    if ($::literal) {
	$only_simple_files_search = $expr !~ /:/;	
    } elsif (@::raw_non_literals == 1) {
	my $s = $::raw_non_literals[0];
	$s =~ s!/.*!!; # things after "/" won't match pkg name for sure	 
	$only_simple_files_search = $s !~ m![:.*?\[\]]!;
    }
    $only_simple_files_search and $urpm->{log}("using fast algorithm");    
}


my $multitag = '';
my %multitags = map { $_ => 1 } qw(conffiles conflicts files obsoletes provides requires recommends suggests);
my %usedtags;

(my $proto = $qf) =~ s/%([-\d]*)(\w+)/%${1}s/g;
my $sprintfargs = join(', ', map {
    $usedtags{$_} = 1;
    if ($_ eq 'media') {
	'$medium->{name}';
    } elsif ($_ eq 'fullname') {
	'scalar($pkg->fullname)';
    } elsif ($_ eq 'description') {
	'do { my $d = $pkg->description; $d =~ s/^/\t/mg; "\n$d" }';
    } elsif ($multitags{$_}) {
	$multitag and $urpm->{fatal}->(1, N("Incorrect format: you may use only one multi-valued tag"));
	$multitag = $_;
	"'%s'";
    } else {
	'$pkg->' . $_;
    }
} $qf =~ /%[-\d]*(\w+)/g);

my ($proto_cooked, $sprintfargs_cooked);
if ($multitag) {
    ($proto_cooked, $sprintfargs_cooked) = ($proto, $sprintfargs);
    ($proto, $sprintfargs) = ('$proto_cooked', '$mt');    
}

my $next_st = $multitag ? 'next' : 'return 0';
my @inner = (
    'local $_;',
    "\$_ = sprintf(qq{$proto}, $sprintfargs);",
    "$expr or $next_st;",
    $uniq ? ('$uniq{$_} and ' . $next_st . ';', '$uniq{$_} = 1;') : (),
    'print $_, "\n";',
);

if ($multitag) {
    @inner = (
	"my \$proto_cooked = sprintf(qq{$proto_cooked}, $sprintfargs_cooked);",
	"foreach my \$mt (\$pkg->$multitag) {", 
	(map { "    $_" } @inner), 
	"}",
    );
}

#- build the callback matching the expression.
my $callback = join("\n", 
    "sub {", 
    (map { "    $_" } 
       'my ($urpm, $pkg) = @_;',
       @inner,
       '0;'),
    "}");

$urpm->{debug}("qf:[$qf]\ncallback:\n$callback") if $urpm->{debug} && !$only_simple_files_search;
our $medium;
$callback = eval $callback;
if ($@) {
    warn "Internal error: $@\n";
    exit(1);
}

if ($env) {
    print N("using specific environment on %s\n", $env);
    #- setting new environment.
    $urpm->{config} = "$env/urpmi.cfg";
    $urpm->{skiplist} = "$env/skip.list";
    $urpm->{instlist} = "$env/inst.list";
    $urpm->{statedir} = $env;
}

my $_lock = urpm::lock::urpmi_db($urpm, '', nofatal => 1, wait => $options{wait_lock});

my %needed_media_info = map { $_ => 1 } grep {
    my $l = $tags_per_media_info{$_};
    int(grep { $usedtags{$_} } @$l);
} keys %tags_per_media_info;

my @needed_xml_info = map { s/xml_info__// ? $_ : @{[]} } keys %needed_media_info;
if (@needed_xml_info > 1) {
    # we don't handle parallel parsing of xml files, default to hdlist
    $needed_media_info{hdlist} = 1;
}

my %fullname2pkg;
urpm::media::configure($urpm,
    no_skiplist => 1,
    media => $media,
    excludemedia => $excludemedia,
    sortmedia => $sortmedia,
    synthesis => $options{synthesis},
    usedistrib => $urpm::args::options{usedistrib},
    update => $update,
    @needed_xml_info && $needed_media_info{synthesis} && !$needed_media_info{hdlist} ?
      # in that case, we need to have both synthesis and xml_info
      (callback => sub {
	   my ($_urpm, $pkg) = @_;
	   $fullname2pkg{$pkg->fullname} = $pkg;
	   1;
       }) : (nodepslist => 1)
);

# nb: we don't "my" $medium since it is used for $callback
if ($needed_media_info{hdlist}) {
    foreach $medium (urpm::media::non_ignored_media($urpm)) {
	my $hdlist = urpm::media::any_hdlist($urpm, $medium, $options{verbose} < 0) or 
	  $urpm->{error}(N("no hdlist available for medium \"%s\"", $medium->{name})), next;
	$urpm->{log}("getting information from $hdlist");
	$urpm->parse_hdlist($hdlist, callback => $callback);
    }
} elsif (!@needed_xml_info) {
    foreach $medium (urpm::media::non_ignored_media($urpm)) {
	my $synthesis = urpm::media::any_synthesis($urpm, $medium) or 
	  $urpm->{error}(N("no synthesis available for medium \"%s\"", $medium->{name})), next;
	$urpm->{log}("getting information from $synthesis");
	$urpm->parse_synthesis($synthesis, callback => $callback);
    }
} elsif (my ($xml_info) = @needed_xml_info) {
    foreach $medium (urpm::media::non_ignored_media($urpm)) {
	my $xml_info_file = urpm::media::any_xml_info($urpm, $medium, $xml_info, $options{verbose} < 0);
	if (!$xml_info_file) {
	    my $hdlist = urpm::media::any_hdlist($urpm, $medium, $options{verbose} < 0) or 
	      $urpm->{error}(N("no xml-info available for medium \"%s\"", $medium->{name})), next;
	    $urpm->{log}("getting information from $hdlist");
	    $urpm->parse_hdlist($hdlist, callback => $callback);
	    next;
	}
	require urpm::xml_info;
	require urpm::xml_info_pkg;

	my $cooked_callback = $needed_media_info{synthesis} ? 
	  sub {
	      my ($node) = @_;
	      my $pkg = $fullname2pkg{$node->{fn}} or warn "can't find $node->{fn} in synthesis\n";
	      $pkg and $callback->($urpm, urpm::xml_info_pkg->new($node, $pkg));
	  } : sub {
	      my ($node) = @_;
	      $callback->($urpm, urpm::xml_info_pkg->new($node, undef));
	  };
	$urpm->{log}("getting information from $xml_info_file");
	if ($only_simple_files_search) {
	    # special version for speed (3x faster), hopefully fully compatible
	    my $code = sprintf(<<'EOF', $expr, $expr);
	    my $F = urpm::xml_info::open_lzma($xml_info_file);
	    my $fn;    
	    local $_;
	    while (<$F>) {
		if (m!^<!) { 
		    ($fn) = /fn="(.*)"/;
		} elsif (%s || ($fn =~ %s)) {
                    $fn or $urpm->{fatal}(1, "fast algorithm is broken, please report a bug");
                    my $pkg = urpm::xml_info_pkg->new({ fn => $fn });
		    print $pkg->name, ':', $_;		    
		}
	    }
EOF
	    $urpm->{debug} and $urpm->{debug}($code);
	    eval $code;
	    $@ and $urpm->{fatal}(1, $@);
	} else {
	    urpm::xml_info::do_something_with_nodes(
		$xml_info,
		$xml_info_file,
		$cooked_callback,
	    );
	}	
    }
}