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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6193 - (hide annotations) (download)
Thu Oct 11 15:49:57 2012 UTC (11 years, 6 months ago) by tv
File size: 16334 byte(s)
(valid_statedir) introduce it

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

  ViewVC Help
Powered by ViewVC 1.1.30