/[soft]/rpm/urpmi/trunk/urpm.pm
ViewVC logotype

Annotation of /rpm/urpmi/trunk/urpm.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2915 - (hide annotations) (download)
Tue Feb 7 21:30:21 2012 UTC (12 years, 2 months ago) by tv
File size: 14841 byte(s)
6.44
1 dmorgan 1928 package urpm;
2    
3     # $Id: urpm.pm 271301 2010-11-22 00:50:49Z eugeni $
4    
5     no warnings 'utf8';
6     use strict;
7     use File::Find ();
8     use urpm::msg;
9     use urpm::download;
10     use urpm::util;
11     use urpm::sys;
12     use urpm::cfg;
13     use urpm::md5sum;
14 tv 2877 # perl_checker: require urpm::args
15     # perl_checker: require urpm::media
16     # perl_checker: require urpm::parallel
17 dmorgan 1928
18 tv 2915 our $VERSION = '6.44';
19 dmorgan 1928 our @ISA = qw(URPM Exporter);
20     our @EXPORT_OK = ('file_from_local_url', 'file_from_local_medium', 'is_local_medium');
21    
22     use URPM;
23     use URPM::Resolve;
24    
25     #- this violently overrides is_arch_compat() to always return true.
26     sub shunt_ignorearch {
27     eval q( sub URPM::Package::is_arch_compat { 1 } );
28     }
29    
30     sub xml_info_policies() { qw(never on-demand update-only always) }
31    
32     sub default_options {
33     {
34     'split-level' => 1,
35     'split-length' => 8,
36     'verify-rpm' => 1,
37     'post-clean' => 1,
38     'xml-info' => 'on-demand',
39     'max-round-robin-tries' => 5,
40     'max-round-robin-probes' => 2,
41     'days-between-mirrorlist-update' => 5,
42     'nb-of-new-unrequested-pkgs-between-auto-select-orphans-check' => 10,
43     };
44     }
45    
46     #- create a new urpm object.
47     sub new {
48     my ($class) = @_;
49     my $self;
50     $self = bless {
51     # from URPM
52     depslist => [],
53     provides => {},
54     obsoletes => {},
55    
56     media => undef,
57     options => {},
58    
59     fatal => sub { printf STDERR "%s\n", $_[1]; exit($_[0]) },
60     error => sub { printf STDERR "%s\n", $_[0] },
61     info => sub { printf "%s\n", $_[0] }, #- displayed unless --quiet
62     log => sub { printf "%s\n", $_[0] }, #- displayed if --verbose
63     print => sub { printf "%s\n", $_[0] }, #- always displayed, enable to redirect output for eg: installer
64     }, $class;
65    
66     set_files($self, '');
67     $self->set_nofatal(1);
68     $self;
69     }
70    
71     sub new_parse_cmdline {
72     my ($class) = @_;
73     my $urpm = $class->new;
74     urpm::args::parse_cmdline(urpm => $urpm);
75     get_global_options($urpm);
76     $urpm;
77     }
78    
79     sub _add2hash { my ($a, $b) = @_; while (my ($k, $v) = each %{$b || {}}) { defined $a->{$k} or $a->{$k} = $v } $a }
80    
81     sub get_global_options {
82     my ($urpm) = @_;
83    
84     my $config = urpm::cfg::load_config($urpm->{config})
85     or $urpm->{fatal}(6, $urpm::cfg::err);
86    
87     if (my $global = $config->{global}) {
88     _add2hash($urpm->{options}, $global);
89     }
90     #- remember global options for write_config
91     $urpm->{global_config} = $config->{global};
92    
93     _add2hash($urpm->{options}, default_options());
94     }
95    
96     sub prefer_rooted {
97     my ($root, $file) = @_;
98     -e "$root$file" ? "$root$file" : $file;
99     }
100    
101     sub init_cache_dir {
102     my ($urpm, $dir) = @_;
103    
104     mkdir $dir, 0755; # try to create it
105    
106     -d $dir && ! -l $dir or $urpm->{fatal}(1, N("fail to create directory %s", $dir));
107     -o $dir && -w $dir or $urpm->{fatal}(1, N("invalid owner for directory %s", $dir));
108    
109     mkdir "$dir/partial";
110     mkdir "$dir/rpms";
111    
112     $dir;
113     }
114     sub userdir_prefix {
115     my ($_urpm) = @_;
116     '/tmp/.urpmi-';
117     }
118     sub userdir {
119     #mdkonline uses userdir because it runs as user
120     my ($urpm) = @_;
121     $< or return;
122    
123     my $dir = ($urpm->{urpmi_root} || '') . userdir_prefix($urpm) . $<;
124     init_cache_dir($urpm, $dir);
125     }
126     sub ensure_valid_cachedir {
127     my ($urpm) = @_;
128     if (my $dir = userdir($urpm)) {
129     $urpm->{cachedir} = $dir;
130     }
131     -w "$urpm->{cachedir}/partial" or $urpm->{fatal}(1, N("Can not download packages into %s", "$urpm->{cachedir}/partial"));
132     }
133     sub valid_cachedir {
134     my ($urpm) = @_;
135     userdir($urpm) || $urpm->{cachedir};
136     }
137    
138     sub is_temporary_file {
139     my ($urpm, $f) = @_;
140    
141     begins_with($f, $urpm->{cachedir});
142     }
143    
144     sub set_env {
145     my ($urpm, $env) = @_;
146     -d $env or $urpm->{fatal}(8, N("Environment directory %s does not exist", $env));
147     print N("using specific environment on %s\n", $env);
148     #- setting new environment.
149     $urpm->{config} = "$env/urpmi.cfg";
150     if (cat_($urpm->{config}) =~ /^\s*virtual\s*$/m) {
151     print "dropping virtual from $urpm->{config}\n";
152     system(q(perl -pi -e 's/^\s*virtual\s*$//' ) . $urpm->{config});
153     }
154     $urpm->{mediacfgdir} = "$env/mediacfg.d";
155     $urpm->{skiplist} = "$env/skip.list";
156     $urpm->{instlist} = "$env/inst.list";
157     $urpm->{prefer_list} = "$env/prefer.list";
158     $urpm->{prefer_vendor_list} = "$env/prefer.vendor.list";
159     $urpm->{statedir} = $env;
160     $urpm->{env_rpmdb} = "$env/rpmdb.cz";
161     $urpm->{env_dir} = $env;
162     }
163    
164     sub set_files {
165     my ($urpm, $urpmi_root) = @_;
166    
167     $urpmi_root and $urpmi_root = file2absolute_file($urpmi_root);
168    
169     my %h = (
170     config => "$urpmi_root/etc/urpmi/urpmi.cfg",
171     mediacfgdir => "$urpmi_root/etc/urpmi/mediacfg.d",
172     skiplist => prefer_rooted($urpmi_root, '/etc/urpmi/skip.list'),
173     instlist => prefer_rooted($urpmi_root, '/etc/urpmi/inst.list'),
174     prefer_list => prefer_rooted($urpmi_root, '/etc/urpmi/prefer.list'),
175     prefer_vendor_list =>
176     prefer_rooted($urpmi_root, '/etc/urpmi/prefer.vendor.list'),
177     private_netrc => "$urpmi_root/etc/urpmi/netrc",
178     statedir => "$urpmi_root/var/lib/urpmi",
179     cachedir => "$urpmi_root/var/cache/urpmi",
180     root => $urpmi_root,
181     $urpmi_root ? (urpmi_root => $urpmi_root) : (),
182     );
183     $urpm->{$_} = $h{$_} foreach keys %h;
184    
185     create_var_lib_rpm($urpm, %h);
186    
187     # policy is too use chroot environment only for --urpmi-root, not for --root:
188     if ($urpmi_root && -e "$urpmi_root/etc/rpm/macros") {
189     URPM::loadmacrosfile("$urpmi_root/etc/rpm/macros");
190     }
191     }
192    
193     sub create_var_lib_rpm {
194     my ($urpm, %h) = @_;
195     require File::Path;
196     File::Path::mkpath([ $h{statedir},
197     (map { "$h{cachedir}/$_" } qw(partial rpms)),
198     dirname($h{config}),
199     "$urpm->{root}/var/lib/rpm",
200     "$urpm->{root}/var/tmp",
201     ]);
202     }
203    
204     sub modify_rpm_macro {
205     my ($name, $to_remove, $to_add) = @_;
206    
207     my $val = URPM::expand('%' . $name);
208     $val =~ s/$to_remove/$to_add/ or $val = join(' ', grep { $_ } $val, $to_add);
209     URPM::add_macro("$name $val");
210     }
211    
212     sub set_tune_rpm {
213     my ($urpm, $para) = @_;
214    
215     my %h = map { $_ => 1 } map {
216     if ($_ eq 'all') {
217     ('nofsync', 'private');
218     } else {
219     $_;
220     }
221     } split(',', $para);
222    
223     $urpm->{tune_rpm} = \%h;
224     }
225    
226     sub tune_rpm {
227     my ($urpm) = @_;
228    
229     if ($urpm->{tune_rpm}{nofsync}) {
230     modify_rpm_macro('__dbi_other', 'fsync', 'nofsync');
231     }
232     if ($urpm->{tune_rpm}{private}) {
233     urpm::sys::clean_rpmdb_shared_regions($urpm->{root});
234     modify_rpm_macro('__dbi_other', 'usedbenv', 'private');
235     }
236     }
237    
238     sub _blist_pkg_to_urls {
239     my ($blist, @pkgs) = @_;
240     my $base_url = $blist->{medium}{url} . '/';
241     map { $base_url . $_->filename } @pkgs;
242     }
243     sub blist_pkg_to_url {
244     my ($blist, $pkg) = @_;
245     my ($url) = _blist_pkg_to_urls($blist, $pkg);
246     $url;
247     }
248     sub blist_to_urls {
249     my ($blist) = @_;
250     _blist_pkg_to_urls($blist, values %{$blist->{pkgs}});
251     }
252     sub blist_to_filenames {
253     my ($blist) = @_;
254     map { $_->filename } values %{$blist->{pkgs}};
255     }
256    
257     sub protocol_from_url {
258     my ($url) = @_;
259     $url =~ m!^(\w+)(_[^:]*)?:! && $1;
260     }
261     sub file_from_local_url {
262     my ($url) = @_;
263     $url =~ m!^(?:removable[^:]*:/|file:/)?(/.*)! && $1;
264     }
265     sub file_from_local_medium {
266     my ($medium, $o_url) = @_;
267     my $url = $o_url || $medium->{url};
268     if ($url =~ m!^cdrom://(.*)!) {
269     my $rel = $1;
270     $medium->{mntpoint} or do { require Carp; Carp::confess("cdrom is not mounted yet!\n") };
271     "$medium->{mntpoint}/$rel";
272     } else {
273     file_from_local_url($url);
274     }
275     }
276     sub is_local_url {
277     my ($url) = @_;
278     file_from_local_url($url) || is_cdrom_url($url);
279     }
280     sub is_local_medium {
281     my ($medium) = @_;
282     is_local_url($medium->{url});
283     }
284     sub is_cdrom_url {
285     my ($url) = @_;
286     protocol_from_url($url) eq 'cdrom';
287     }
288    
289     sub db_open_or_die_ {
290     my ($urpm, $b_write_perm) = @_;
291     my $db;
292     if ($urpm->{env_rpmdb}) {
293     #- URPM has same methods as URPM::DB and empty URPM will be seen as empty URPM::DB.
294     $db = new URPM;
295     $db->parse_synthesis($urpm->{env_rpmdb});
296     } else {
297     $db = db_open_or_die($urpm, $urpm->{root}, $b_write_perm);
298     }
299     $db;
300     }
301    
302     # please use higher level function db_open_or_die_()
303     sub db_open_or_die {
304     my ($urpm, $root, $b_write_perm) = @_;
305    
306     $urpm->{debug} and $urpm->{debug}("opening rpmdb (root=$root, write=$b_write_perm)");
307    
308     my $db = URPM::DB::open($root, $b_write_perm || 0)
309     or $urpm->{fatal}(9, N("unable to open rpmdb"));
310    
311     $db;
312     }
313    
314     #- register local packages for being installed, keep track of source.
315     sub register_rpms {
316     my ($urpm, @files) = @_;
317     my ($start, $id, $error, %requested);
318    
319     #- examine each rpm and build the depslist for them using current
320     #- depslist and provides environment.
321     $start = @{$urpm->{depslist}};
322     foreach (@files) {
323     /\.(?:rpm|spec)$/ or $error = 1, $urpm->{error}(N("invalid rpm file name [%s]", $_)), next;
324    
325     #- if that's an URL, download.
326     if (protocol_from_url($_)) {
327     my $basename = basename($_);
328     unlink "$urpm->{cachedir}/partial/$basename";
329     $urpm->{log}(N("retrieving rpm file [%s] ...", $_));
330     if (urpm::download::sync_url($urpm, $_, quiet => 1)) {
331     $urpm->{log}(N("...retrieving done"));
332     $_ = "$urpm->{cachedir}/partial/$basename";
333     } else {
334     $urpm->{error}(N("...retrieving failed: %s", $@));
335     unlink "$urpm->{cachedir}/partial/$basename";
336     next;
337     }
338     } else {
339     -r $_ or $error = 1, $urpm->{error}(N("unable to access rpm file [%s]", $_)), next;
340     }
341    
342     if (/\.spec$/) {
343     my $pkg = URPM::spec2srcheader($_)
344     or $error = 1, $urpm->{error}(N("unable to parse spec file %s [%s]", $_, $!)), next;
345     $id = @{$urpm->{depslist}};
346     $urpm->{depslist}[$id] = $pkg;
347     $pkg->set_id($id); #- sets internal id to the depslist id.
348     $urpm->{source}{$id} = $_;
349     } else {
350     ($id) = $urpm->parse_rpm($_);
351     my $pkg = defined $id && $urpm->{depslist}[$id];
352     $pkg or $error = 1, $urpm->{error}(N("unable to register rpm file")), next;
353     $pkg->arch eq 'src' || $pkg->is_arch_compat
354     or $error = 1, $urpm->{error}(N("Incompatible architecture for rpm [%s]", $_)), next;
355     $urpm->{source}{$id} = $_;
356     }
357     }
358     $error and $urpm->{fatal}(2, N("error registering local packages"));
359     defined $id && $start <= $id and @requested{($start .. $id)} = (1) x ($id-$start+1);
360    
361     #- distribute local packages to distant nodes directly in cache of each machine.
362     if (@files && $urpm->{parallel_handler}) {
363     $urpm->{parallel_handler}->parallel_register_rpms($urpm, @files);
364     }
365    
366     %requested;
367     }
368    
369     #- checks whether the delta RPM represented by $pkg is installable wrt the
370     #- RPM DB on $root. For this, it extracts the rpm version to which the
371     #- delta applies from the delta rpm filename itself. So naming conventions
372     #- do matter :)
373     sub is_delta_installable {
374     my ($urpm, $pkg, $root) = @_;
375     $pkg->flag_installed or return 0;
376     my $f = $pkg->filename;
377     my $n = $pkg->name;
378     my ($v_match) = $f =~ /^\Q$n\E-(.*)_.+\.delta\.rpm$/;
379     my $db = db_open_or_die($urpm, $root);
380     my $v_installed;
381     $db->traverse(sub {
382     my ($p) = @_;
383     $p->name eq $n and $v_installed = $p->version . '-' . $p->release;
384     });
385     $v_match eq $v_installed;
386     }
387    
388     #- extract package that should be installed instead of upgraded,
389     #- installing instead of upgrading is useful
390     #- - for inst.list (cf flag disable_obsolete)
391     #- sources is a hash of id -> source rpm filename.
392     sub extract_packages_to_install {
393     my ($urpm, $sources, $_state) = @_;
394     my %inst;
395    
396     foreach (keys %$sources) {
397     my $pkg = $urpm->{depslist}[$_] or next;
398     $pkg->flag_disable_obsolete
399     and $inst{$pkg->id} = delete $sources->{$pkg->id};
400     }
401    
402     \%inst;
403     }
404    
405     #- deprecated
406     sub install { require urpm::install; &urpm::install::install }
407    
408     #- deprecated
409     sub parallel_remove { &urpm::parallel::remove }
410    
411     #- get reason of update for packages to be updated
412     #- use all update medias if none given
413     sub get_updates_description {
414     my ($urpm, @update_medias) = @_;
415     my %update_descr;
416     my ($cur, $section);
417    
418     @update_medias or @update_medias = urpm::media::non_ignored_media($urpm, 'update');
419    
420     foreach my $medium (@update_medias) {
421     # fix not taking into account the last %package token of each descrptions file: '%package dummy'
422     foreach (cat_utf8(urpm::media::statedir_descriptions($urpm, $medium)),
423     ($::env ? cat_utf8("$::env/descriptions") : ()), '%package dummy') {
424     /^%package +(.+)/ and do {
425     # fixes not parsing descriptions file when MU adds itself the security source:
426     if (exists $cur->{importance} && !member($cur->{importance}, qw(security bugfix))) {
427     $cur->{importance} = 'normal';
428     }
429     $update_descr{$medium->{name}}{$_} = $cur foreach @{$cur->{pkgs} || []};
430     $cur = { pkgs => [ split /\s/, $1 ] };
431     $section = 'pkg';
432     next;
433     };
434     /^Updated?: +(.+)/ && $section eq 'pkg' and do { $cur->{updated} = $1; next };
435     /^Importance: +(.+)/ && $section eq 'pkg' and do { $cur->{importance} = $1; next };
436     /^(ID|URL): +(.+)/ && $section eq 'pkg' and do { $cur->{$1} = $2; next };
437     /^%(pre|description)/ and do { $section = $1; next };
438     $section =~ /^(pre|description)\z/ and $cur->{$1} .= $_;
439     }
440     }
441     \%update_descr;
442     }
443    
444     sub error_restricted ($) {
445     my ($urpm) = @_;
446     $urpm->{fatal}(2, N("This operation is forbidden while running in restricted mode"));
447     }
448    
449     sub DESTROY {}
450    
451     1;
452    
453     __END__
454    
455     =head1 NAME
456    
457     urpm - Mageia perl tools to handle the urpmi database
458    
459     =head1 DESCRIPTION
460    
461     C<urpm> is used by urpmi executables to manipulate packages and media
462     on a Mageia Linux distribution.
463    
464     =head2 The urpm class
465    
466     =over 4
467    
468     =item urpm->new()
469    
470     The constructor creates a new urpm object. It's a blessed hash that
471     contains fields from C<URPM>, and also the following fields:
472    
473     B<source>: { id => src_rpm_file|spec_file }
474    
475     B<media>: [ {
476     start => int, end => int, name => string, url => string,
477     virtual => bool, media_info_dir => string, with_synthesis => string,
478     no-media-info => bool,
479     iso => string, downloader => string,
480     ignore => bool, update => bool, modified => bool, really_modified => bool,
481     unknown_media_info => bool,
482     } ],
483    
484     =back
485    
486     =head1 SEE ALSO
487    
488     The C<URPM> package is used to manipulate at a lower level synthesis and rpm
489     files.
490    
491     =head1 COPYRIGHT
492    
493     Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA
494    
495     Copyright (C) 2005-2010 Mandriva SA
496    
497     This program is free software; you can redistribute it and/or modify
498     it under the terms of the GNU General Public License as published by
499     the Free Software Foundation; either version 2, or (at your option)
500     any later version.
501    
502     This program is distributed in the hope that it will be useful,
503     but WITHOUT ANY WARRANTY; without even the implied warranty of
504     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
505     GNU General Public License for more details.
506    
507     You should have received a copy of the GNU General Public License
508     along with this program; if not, write to the Free Software
509     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
510    
511     =cut
512    
513     # ex: set ts=8 sts=4 sw=4 noet:

  ViewVC Help
Powered by ViewVC 1.1.30