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

# $Id$

#- Copyright (C) 2002, 2003, 2004, 2005 MandrakeSoft SA
#- Copyright (C) 2005, 2006 Mandriva SA
#-
#- 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;

sub usage() {
    print N("urpmf version %s
Copyright (C) 2002-2006 Mandriva.
This is free software and may be redistributed under the terms of the GNU GPL.

usage: urpmf [options] pattern-expression
", $urpm::VERSION)
   . 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("  --media        - use only the given media, separated by comma.
") . N("  --sortmedia    - sort media according to substrings separated by comma.
") . 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("  --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("  --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(0);
}

#- 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 $synthesis = '';
our $uniq = '';		# --uniq
our $update = 0;	# --update
our $verbose = 0;	# --verbose

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

#- parse arguments list.
urpm::args::parse_cmdline() or exit(1);

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/ }

my $urpm = new urpm;
$verbose or $urpm->{log} = sub {};

#- build the callback matching the expression.
#- count multi-valued tags
my $multi = 0;
my $multitag = '';
my %multitags = map { $_ => 1 } qw(conffiles conflicts files obsoletes provides requires);
my %usedtags;

(my $proto = $qf) =~ s/%([-\d]*)(\w+)/%${1}s/g;
my $sprintfargs = join(', ', map {
    $usedtags{$1} = 1;
    if ($_ eq 'media') {
	'$urpm::currentmedia->{name}';
    } elsif ($_ eq 'fullname') {
	'scalar($pkg->fullname)';
    } elsif ($multitags{$_}) {
	$multi++;
	$multitag = $_;
	'$mt';
    } else {
	'$pkg->' . $_;
    }
} $qf =~ /%[-\d]*(\w+)/g);

$multi > 1
    and $urpm->{fatal}->(1, N("Incorrect format: you may use only one multi-valued tag"));

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

if ($multi) {
    @inner = (
	"for my \$mt (\$pkg->$multitag) {", 
	(map { "    $_" } @inner), 
	"}",
    );
}

my $callback = join("\n", 
    "sub {", 
    (map { "    $_" } 
       'my ($urpm, $pkg) = @_;',
       @inner,
       '1;'),
    "}");

our $debug;
$urpm->{error}("qf:[$qf]\ncallback:\n$callback") if $debug;
$callback = eval $callback;
if ($@) {
    $debug and 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;
}

{
    #- lock to avoid concurrent media updates,
    #- but don't die if it doesn't work
    local $urpm->{fatal} = sub { printf STDERR "%s\n", $_[1] };
    $urpm->shlock_urpmi_db;
}
my $use_hdlist = grep { $usedtags{$_} } qw(
    buildhost
    buildtime
    conf_files
    description
    distribution
    files
    packager
    sourcerpm
    url
    vendor
);
$urpm->configure(
    nocheck_access => 1,
    noskipping => 1,
    media => $media,
    excludemedia => $excludemedia,
    sortmedia => $sortmedia,
    synthesis => $synthesis,
    update => $update,
    callback => $callback,
    call_back_only_once => 1,
    hdlist => $use_hdlist,
);
$urpm->unlock_urpmi_db;

if ($use_hdlist) {
    # @hdmedia is the list of all media searched that use hdlists
    my @hdmedia = grep {
	!$_->{synthesis} && !$_->{removable} && !$_->{ignore};
    } @{ $urpm->{media} };
    if (!@hdmedia) {
	print N("Note: since no media searched uses hdlists, urpmf was unable to return any result\n");
	print N("You may want to use --name to search for package names.\n") if !$usedtags{name};
    }
}