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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6191 - (hide annotations) (download)
Thu Oct 11 15:49:51 2012 UTC (11 years, 6 months ago) by tv
File size: 16173 byte(s)
(check_cache_dir) rename as check_dir() which is more meaningful
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 dmorgan 1928 sub init_cache_dir {
147     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     sub userdir {
163     #mdkonline uses userdir because it runs as user
164     my ($urpm) = @_;
165     $< or return;
166    
167     my $dir = ($urpm->{urpmi_root} || '') . userdir_prefix($urpm) . $<;
168     init_cache_dir($urpm, $dir);
169     }
170     sub ensure_valid_cachedir {
171     my ($urpm) = @_;
172     if (my $dir = userdir($urpm)) {
173     $urpm->{cachedir} = $dir;
174     }
175     -w "$urpm->{cachedir}/partial" or $urpm->{fatal}(1, N("Can not download packages into %s", "$urpm->{cachedir}/partial"));
176     }
177     sub valid_cachedir {
178     my ($urpm) = @_;
179     userdir($urpm) || $urpm->{cachedir};
180     }
181    
182     sub is_temporary_file {
183     my ($urpm, $f) = @_;
184    
185     begins_with($f, $urpm->{cachedir});
186     }
187    
188     sub set_env {
189     my ($urpm, $env) = @_;
190     -d $env or $urpm->{fatal}(8, N("Environment directory %s does not exist", $env));
191     print N("using specific environment on %s\n", $env);
192     #- setting new environment.
193     $urpm->{config} = "$env/urpmi.cfg";
194     if (cat_($urpm->{config}) =~ /^\s*virtual\s*$/m) {
195     print "dropping virtual from $urpm->{config}\n";
196     system(q(perl -pi -e 's/^\s*virtual\s*$//' ) . $urpm->{config});
197     }
198     $urpm->{mediacfgdir} = "$env/mediacfg.d";
199     $urpm->{skiplist} = "$env/skip.list";
200     $urpm->{instlist} = "$env/inst.list";
201     $urpm->{prefer_list} = "$env/prefer.list";
202     $urpm->{prefer_vendor_list} = "$env/prefer.vendor.list";
203     $urpm->{statedir} = $env;
204     $urpm->{env_rpmdb} = "$env/rpmdb.cz";
205     $urpm->{env_dir} = $env;
206     }
207    
208     sub set_files {
209     my ($urpm, $urpmi_root) = @_;
210    
211     $urpmi_root and $urpmi_root = file2absolute_file($urpmi_root);
212    
213     my %h = (
214     config => "$urpmi_root/etc/urpmi/urpmi.cfg",
215     mediacfgdir => "$urpmi_root/etc/urpmi/mediacfg.d",
216     skiplist => prefer_rooted($urpmi_root, '/etc/urpmi/skip.list'),
217     instlist => prefer_rooted($urpmi_root, '/etc/urpmi/inst.list'),
218     prefer_list => prefer_rooted($urpmi_root, '/etc/urpmi/prefer.list'),
219     prefer_vendor_list =>
220     prefer_rooted($urpmi_root, '/etc/urpmi/prefer.vendor.list'),
221     private_netrc => "$urpmi_root/etc/urpmi/netrc",
222     statedir => "$urpmi_root/var/lib/urpmi",
223     cachedir => "$urpmi_root/var/cache/urpmi",
224     root => $urpmi_root,
225 tv 3856 $urpmi_root ? (urpmi_root => $urpmi_root) : @{[]},
226 dmorgan 1928 );
227     $urpm->{$_} = $h{$_} foreach keys %h;
228    
229     create_var_lib_rpm($urpm, %h);
230    
231     # policy is too use chroot environment only for --urpmi-root, not for --root:
232     if ($urpmi_root && -e "$urpmi_root/etc/rpm/macros") {
233     URPM::loadmacrosfile("$urpmi_root/etc/rpm/macros");
234     }
235     }
236    
237     sub create_var_lib_rpm {
238     my ($urpm, %h) = @_;
239     require File::Path;
240     File::Path::mkpath([ $h{statedir},
241     (map { "$h{cachedir}/$_" } qw(partial rpms)),
242     dirname($h{config}),
243     "$urpm->{root}/var/lib/rpm",
244     "$urpm->{root}/var/tmp",
245     ]);
246     }
247    
248     sub modify_rpm_macro {
249     my ($name, $to_remove, $to_add) = @_;
250    
251     my $val = URPM::expand('%' . $name);
252     $val =~ s/$to_remove/$to_add/ or $val = join(' ', grep { $_ } $val, $to_add);
253     URPM::add_macro("$name $val");
254     }
255    
256     sub set_tune_rpm {
257     my ($urpm, $para) = @_;
258    
259     my %h = map { $_ => 1 } map {
260     if ($_ eq 'all') {
261     ('nofsync', 'private');
262     } else {
263     $_;
264     }
265     } split(',', $para);
266    
267     $urpm->{tune_rpm} = \%h;
268     }
269    
270     sub tune_rpm {
271     my ($urpm) = @_;
272    
273     if ($urpm->{tune_rpm}{nofsync}) {
274     modify_rpm_macro('__dbi_other', 'fsync', 'nofsync');
275     }
276     if ($urpm->{tune_rpm}{private}) {
277     urpm::sys::clean_rpmdb_shared_regions($urpm->{root});
278     modify_rpm_macro('__dbi_other', 'usedbenv', 'private');
279     }
280     }
281    
282     sub _blist_pkg_to_urls {
283     my ($blist, @pkgs) = @_;
284     my $base_url = $blist->{medium}{url} . '/';
285     map { $base_url . $_->filename } @pkgs;
286     }
287     sub blist_pkg_to_url {
288     my ($blist, $pkg) = @_;
289     my ($url) = _blist_pkg_to_urls($blist, $pkg);
290     $url;
291     }
292     sub blist_to_urls {
293     my ($blist) = @_;
294     _blist_pkg_to_urls($blist, values %{$blist->{pkgs}});
295     }
296     sub blist_to_filenames {
297     my ($blist) = @_;
298     map { $_->filename } values %{$blist->{pkgs}};
299     }
300    
301     sub protocol_from_url {
302     my ($url) = @_;
303     $url =~ m!^(\w+)(_[^:]*)?:! && $1;
304     }
305     sub file_from_local_url {
306     my ($url) = @_;
307     $url =~ m!^(?:removable[^:]*:/|file:/)?(/.*)! && $1;
308     }
309     sub file_from_local_medium {
310     my ($medium, $o_url) = @_;
311     my $url = $o_url || $medium->{url};
312     if ($url =~ m!^cdrom://(.*)!) {
313     my $rel = $1;
314     $medium->{mntpoint} or do { require Carp; Carp::confess("cdrom is not mounted yet!\n") };
315     "$medium->{mntpoint}/$rel";
316     } else {
317     file_from_local_url($url);
318     }
319     }
320     sub is_local_url {
321     my ($url) = @_;
322     file_from_local_url($url) || is_cdrom_url($url);
323     }
324     sub is_local_medium {
325     my ($medium) = @_;
326     is_local_url($medium->{url});
327     }
328     sub is_cdrom_url {
329     my ($url) = @_;
330     protocol_from_url($url) eq 'cdrom';
331     }
332    
333 tv 6162 =item db_open_or_die($urpm, $b_write_perm)
334    
335     Open RPM database (RW or not) and die if it fails
336    
337     =cut
338    
339 dmorgan 1928 sub db_open_or_die_ {
340     my ($urpm, $b_write_perm) = @_;
341     my $db;
342     if ($urpm->{env_rpmdb}) {
343     #- URPM has same methods as URPM::DB and empty URPM will be seen as empty URPM::DB.
344 tv 3547 $db = URPM->new;
345 dmorgan 1928 $db->parse_synthesis($urpm->{env_rpmdb});
346     } else {
347     $db = db_open_or_die($urpm, $urpm->{root}, $b_write_perm);
348     }
349     $db;
350     }
351    
352     # please use higher level function db_open_or_die_()
353     sub db_open_or_die {
354     my ($urpm, $root, $b_write_perm) = @_;
355    
356     $urpm->{debug} and $urpm->{debug}("opening rpmdb (root=$root, write=$b_write_perm)");
357    
358     my $db = URPM::DB::open($root, $b_write_perm || 0)
359     or $urpm->{fatal}(9, N("unable to open rpmdb"));
360    
361     $db;
362     }
363    
364 tv 6160 =item register_rpms($urpm, @files)
365    
366     Register local packages for being installed, keep track of source.
367    
368     =cut
369    
370 dmorgan 1928 sub register_rpms {
371     my ($urpm, @files) = @_;
372     my ($start, $id, $error, %requested);
373    
374     #- examine each rpm and build the depslist for them using current
375     #- depslist and provides environment.
376     $start = @{$urpm->{depslist}};
377     foreach (@files) {
378     /\.(?:rpm|spec)$/ or $error = 1, $urpm->{error}(N("invalid rpm file name [%s]", $_)), next;
379    
380     #- if that's an URL, download.
381     if (protocol_from_url($_)) {
382     my $basename = basename($_);
383     unlink "$urpm->{cachedir}/partial/$basename";
384     $urpm->{log}(N("retrieving rpm file [%s] ...", $_));
385     if (urpm::download::sync_url($urpm, $_, quiet => 1)) {
386     $urpm->{log}(N("...retrieving done"));
387     $_ = "$urpm->{cachedir}/partial/$basename";
388     } else {
389     $urpm->{error}(N("...retrieving failed: %s", $@));
390     unlink "$urpm->{cachedir}/partial/$basename";
391     next;
392     }
393     } else {
394     -r $_ or $error = 1, $urpm->{error}(N("unable to access rpm file [%s]", $_)), next;
395     }
396    
397     if (/\.spec$/) {
398     my $pkg = URPM::spec2srcheader($_)
399     or $error = 1, $urpm->{error}(N("unable to parse spec file %s [%s]", $_, $!)), next;
400     $id = @{$urpm->{depslist}};
401     $urpm->{depslist}[$id] = $pkg;
402     $pkg->set_id($id); #- sets internal id to the depslist id.
403     $urpm->{source}{$id} = $_;
404     } else {
405     ($id) = $urpm->parse_rpm($_);
406     my $pkg = defined $id && $urpm->{depslist}[$id];
407     $pkg or $error = 1, $urpm->{error}(N("unable to register rpm file")), next;
408     $pkg->arch eq 'src' || $pkg->is_arch_compat
409     or $error = 1, $urpm->{error}(N("Incompatible architecture for rpm [%s]", $_)), next;
410     $urpm->{source}{$id} = $_;
411     }
412     }
413     $error and $urpm->{fatal}(2, N("error registering local packages"));
414     defined $id && $start <= $id and @requested{($start .. $id)} = (1) x ($id-$start+1);
415    
416     #- distribute local packages to distant nodes directly in cache of each machine.
417     if (@files && $urpm->{parallel_handler}) {
418     $urpm->{parallel_handler}->parallel_register_rpms($urpm, @files);
419     }
420    
421     %requested;
422     }
423    
424 tv 6160 =item is_delta_installable($urpm, $pkg, $root)
425    
426     checks whether the delta RPM represented by $pkg is installable wrt the
427     RPM DB on $root. For this, it extracts the rpm version to which the
428     delta applies from the delta rpm filename itself. So naming conventions
429     do matter :)
430    
431     =cut
432    
433 dmorgan 1928 sub is_delta_installable {
434     my ($urpm, $pkg, $root) = @_;
435     $pkg->flag_installed or return 0;
436     my $f = $pkg->filename;
437     my $n = $pkg->name;
438     my ($v_match) = $f =~ /^\Q$n\E-(.*)_.+\.delta\.rpm$/;
439     my $db = db_open_or_die($urpm, $root);
440     my $v_installed;
441     $db->traverse(sub {
442     my ($p) = @_;
443     $p->name eq $n and $v_installed = $p->version . '-' . $p->release;
444     });
445     $v_match eq $v_installed;
446     }
447    
448 tv 6160
449     =item extract_packages_to_install($urpm, $sources)
450    
451     Extract package that should be installed instead of upgraded,
452     installing instead of upgrading is useful
453     - for inst.list (cf flag disable_obsolete)
454    
455     Sources is a hash of id -> source rpm filename.
456    
457     =cut
458    
459 dmorgan 1928 sub extract_packages_to_install {
460 tv 5481 my ($urpm, $sources) = @_;
461 dmorgan 1928 my %inst;
462    
463     foreach (keys %$sources) {
464     my $pkg = $urpm->{depslist}[$_] or next;
465     $pkg->flag_disable_obsolete
466     and $inst{$pkg->id} = delete $sources->{$pkg->id};
467     }
468    
469     \%inst;
470     }
471    
472 tv 6156 #- deprecated, use find_candidate_packages_() directly
473     #-
474     #- side-effects: none
475     sub find_candidate_packages_ {
476     my ($urpm, $id_prop) = @_;
477    
478     my %packages;
479     foreach ($urpm->find_candidate_packages($id_prop)) {
480     push @{$packages{$_->name}}, $_;
481     }
482     values %packages;
483     }
484    
485 tv 6160 =item get_updates_description($urpm, @update_medias)
486    
487     Get reason of update for packages to be updated.
488     Use all update medias if none given.
489    
490     =cut
491    
492 dmorgan 1928 sub get_updates_description {
493     my ($urpm, @update_medias) = @_;
494     my %update_descr;
495     my ($cur, $section);
496    
497     @update_medias or @update_medias = urpm::media::non_ignored_media($urpm, 'update');
498    
499     foreach my $medium (@update_medias) {
500     # fix not taking into account the last %package token of each descrptions file: '%package dummy'
501     foreach (cat_utf8(urpm::media::statedir_descriptions($urpm, $medium)),
502     ($::env ? cat_utf8("$::env/descriptions") : ()), '%package dummy') {
503     /^%package +(.+)/ and do {
504     # fixes not parsing descriptions file when MU adds itself the security source:
505     if (exists $cur->{importance} && !member($cur->{importance}, qw(security bugfix))) {
506     $cur->{importance} = 'normal';
507     }
508     $update_descr{$medium->{name}}{$_} = $cur foreach @{$cur->{pkgs} || []};
509     $cur = { pkgs => [ split /\s/, $1 ] };
510     $section = 'pkg';
511     next;
512     };
513     /^Updated?: +(.+)/ && $section eq 'pkg' and do { $cur->{updated} = $1; next };
514     /^Importance: +(.+)/ && $section eq 'pkg' and do { $cur->{importance} = $1; next };
515     /^(ID|URL): +(.+)/ && $section eq 'pkg' and do { $cur->{$1} = $2; next };
516     /^%(pre|description)/ and do { $section = $1; next };
517     $section =~ /^(pre|description)\z/ and $cur->{$1} .= $_;
518     }
519     }
520     \%update_descr;
521     }
522    
523     sub error_restricted ($) {
524     my ($urpm) = @_;
525     $urpm->{fatal}(2, N("This operation is forbidden while running in restricted mode"));
526     }
527    
528     sub DESTROY {}
529    
530     1;
531    
532     __END__
533    
534     =back
535    
536     =head1 SEE ALSO
537    
538 tv 6165 The L<URPM> package is used to manipulate at a lower level synthesis and rpm
539 dmorgan 1928 files.
540    
541 tv 6164 See also submodules: L<urpm::args>, L<urpm::bug_report>,
542     L<urpm::cdrom>, L<urpm::cfg>, L<urpm::download>, L<urpm::get_pkgs>,
543     L<urpm::install>, L<urpm::ldap>, L<urpm::lock>, L<urpm::main_loop>,
544     L<urpm::md5sum>, L<urpm::media>, L<urpm::mirrors>, L<urpm::msg>,
545     L<urpm::orphans>, L<urpm::parallel_ka_run>, L<urpm::parallel>,
546     L<urpm::parallel_ssh>, L<urpm::prompt>, L<urpm::removable>,
547     L<urpm::select>, L<urpm::signature>, L<urpm::sys>, L<urpm::util>,
548     L<urpm::xml_info_pkg>, L<urpm::xml_info>
549    
550 dmorgan 1928 =head1 COPYRIGHT
551    
552     Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA
553    
554     Copyright (C) 2005-2010 Mandriva SA
555    
556 tv 6163 Copyright (C) 2011-2012 Mageia SA
557    
558 dmorgan 1928 This program is free software; you can redistribute it and/or modify
559     it under the terms of the GNU General Public License as published by
560     the Free Software Foundation; either version 2, or (at your option)
561     any later version.
562    
563     This program is distributed in the hope that it will be useful,
564     but WITHOUT ANY WARRANTY; without even the implied warranty of
565     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
566     GNU General Public License for more details.
567    
568     You should have received a copy of the GNU General Public License
569     along with this program; if not, write to the Free Software
570     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
571    
572     =cut
573    
574     # ex: set ts=8 sts=4 sw=4 noet:

  ViewVC Help
Powered by ViewVC 1.1.30