File Coverage

lib/ProjectBuilder/Distribution.pm
Criterion Covered Total %
statement 33 349 9.4
branch 0 180 0.0
condition 0 78 0.0
subroutine 11 27 40.7
pod 14 16 87.5
total 58 650 8.9


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2             #
3             # Creates common environment for distributions
4             #
5             # Copyright B. Cornec 2007-2016
6             # Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
7             # Provided under the GPL v2
8             #
9             # $Id$
10             #
11              
12             package ProjectBuilder::Distribution;
13              
14 1     1   359 use strict;
  1         1  
  1         29  
15 1     1   3 use Data::Dumper;
  1         1  
  1         34  
16 1     1   3 use Carp 'confess';
  1         1  
  1         27  
17 1     1   5 use ProjectBuilder::Version;
  1         1  
  1         55  
18 1     1   5 use ProjectBuilder::Base;
  1         1  
  1         129  
19 1     1   4 use ProjectBuilder::Conf;
  1         0  
  1         55  
20 1     1   3 use File::Basename;
  1         1  
  1         59  
21 1     1   444 use File::Copy;
  1         1732  
  1         51  
22             # requires perl 5.004 minimum in VM/VE
23 1     1   398 use File::Compare;
  1         681  
  1         43  
24              
25             # Global vars
26             # Inherit from the "Exporter" module which handles exporting functions.
27            
28 1     1   5 use vars qw($VERSION $REVISION @ISA @EXPORT);
  1         1  
  1         45  
29 1     1   3 use Exporter;
  1         1  
  1         3368  
30            
31             # Export, by default, all the functions into the namespace of
32             # any code which uses this module.
33            
34             our @ISA = qw(Exporter);
35             our @EXPORT = qw(pb_distro_init pb_distro_conffile pb_distro_get pb_distro_getlsb pb_distro_installdeps pb_distro_getdeps pb_distro_only_deps_needed pb_distro_setuprepo pb_distro_setuposrepo pb_distro_get_param pb_distro_get_context pb_distro_to_keylist pb_distro_conf_print pb_apply_conf_proxy);
36             ($VERSION,$REVISION) = pb_version_init();
37              
38             =pod
39              
40             =head1 NAME
41              
42             ProjectBuilder::Distribution, part of the project-builder.org - module dealing with distribution detection
43              
44             =head1 DESCRIPTION
45              
46             This modules provides functions to allow detection of Linux distributions, and giving back some attributes concerning them.
47              
48             =head1 SYNOPSIS
49              
50             use ProjectBuilder::Distribution;
51              
52             #
53             # Return information on the running distro
54             #
55             my $pbos = pb_distro_get_context();
56             print "distro tuple: ".Dumper($pbos->name, $pbos->ver, $pbos->fam, $pbos->type, $pbos->pbsuf, $pbos->pbupd, $pbos->pbins, $pbos->arch)."\n";
57             #
58             # Return information on the requested distro
59             #
60             my $pbos = pb_distro_get_context("ubuntu-7.10-x86_64");
61             print "distro tuple: ".Dumper($pbos->name, $pbos->ver, $pbos->fam, $pbos->type, $pbos->pbsuf, $pbos->pbupd, $pbos->pbins, $pbos->arch)."\n";
62             #
63             # Return information on the running distro
64             #
65             my ($ddir,$dver) = pb_distro_get();
66              
67             =head1 USAGE
68              
69             =over 4
70              
71             =item B
72              
73             This function returns the mandatory configuration file used for distribution/OS detection
74              
75             =cut
76              
77             sub pb_distro_conffile {
78              
79 0     0 1   return("/usr/local/etc/pb/pb.conf");
80             }
81              
82              
83             =item B
84              
85             This function returns a hash of parameters indicating the distribution name, version, family, type of build system, suffix of packages, update command line, installation command line and architecture of the underlying Linux distribution. The value of the fields may be "unknown" in case the function was unable to recognize on which distribution it is running.
86              
87             As an example, Ubuntu and Debian are in the same "du" family. As well as RedHat, RHEL, CentOS, fedora are on the same "rh" family.
88             Mandriva, Open SuSE and Fedora have all the same "rpm" type of build system. Ubuntu and Debian have the same "deb" type of build system.
89             And "fc" is the extension generated for all Fedora packages (Version will be added by pb).
90             All this information is stored in an external configuration file typically at /etc/pb/pb.conf
91              
92             When passing the distribution name and version as parameters, the B function returns the parameter of that distribution instead of the underlying one.
93              
94             Cf: http://linuxmafia.com/faq/Admin/release-files.html
95             Ideas taken from http://search.cpan.org/~kerberus/Linux-Distribution-0.14/lib/Linux/Distribution.pm
96              
97             =cut
98              
99              
100             sub pb_distro_init {
101              
102 0     0 1   my $pbos = {
103             'name' => undef,
104             'version' => undef,
105             'arch' => undef,
106             'family' => "unknown",
107             'suffix' => "unknown",
108             'update' => "unknown",
109             'install' => "unknown",
110             'type' => "unknown",
111             'os' => "unknown",
112             'nover' => "false",
113             'rmdot' => "false",
114             'useminor' => "false",
115             };
116 0           $pbos->{'name'} = shift;
117 0           $pbos->{'version'} = shift;
118 0           $pbos->{'arch'} = shift;
119              
120             # Adds conf file for distribution description
121             # the location of the conf file is finalyzed at install time
122             # depending whether we deal with package install or tar file install
123 0           pb_conf_add(pb_distro_conffile());
124              
125             # If we don't know which distribution we're on, then guess it
126 0 0 0       ($pbos->{'name'},$pbos->{'version'}) = pb_distro_get() if ((not defined $pbos->{'name'}) || (not defined $pbos->{'version'}));
127              
128             # For some rare cases, typically nover ones
129 0 0         $pbos->{'name'} = "unknown" if (not defined $pbos->{'name'});
130 0 0         $pbos->{'version'} = "unknown" if (not defined $pbos->{'version'});
131              
132             # Initialize arch
133 0 0         $pbos->{'arch'} = pb_get_arch() if (not defined $pbos->{'arch'});
134             # Solves a bug on Red Hat 6.x where real arch is not detected when using setarch and a chroot
135             # As it was only i386 forcing it here.
136 0 0 0       $pbos->{'arch'} = "i386" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /^6\./));
137              
138             # Dig into the tuple to find the best answer
139             # Do NOT factorize here, as it won't work as of now for hash creation
140             # Do NOT change order without caution
141 0           $pbos->{'useminor'} = pb_distro_get_param($pbos,pb_conf_get("osuseminorrel"));
142 0           $pbos->{'family'} = pb_distro_get_param($pbos,pb_conf_get("osfamily"));
143 0           $pbos->{'type'} = pb_distro_get_param($pbos,pb_conf_get("ostype"));
144 0           ($pbos->{'os'},$pbos->{'install'},$pbos->{'suffix'},$pbos->{'nover'},$pbos->{'rmdot'},$pbos->{'update'}) = pb_distro_get_param($pbos,pb_conf_get("os","osins","ossuffix","osnover","osremovedotinver","osupd"));
145             #($pbos->{'family'},$pbos->{'type'},$pbos->{'os'},$pbos->{'install'},$pbos->{'suffix'},$pbos->{'nover'},$pbos->{'rmdot'},$pbos->{'update'}) = pb_distro_get_param($pbos,pb_conf_get("osfamily","ostype","os","osins","ossuffix","osnover","osremovedotinver","osupd"));
146              
147             # Some OS have no interesting version
148 0 0 0       $pbos->{'version'} = "nover" if ((defined $pbos->{'nover'}) && ($pbos->{'nover'} eq "true"));
149              
150             # For some OS remove the . in version name for extension
151 0           my $dver2 = $pbos->{'version'};
152 0 0 0       $dver2 =~ s/\.//g if ((defined $pbos->{'rmdot'}) && ($pbos->{'rmdot'} eq "true"));
153              
154 0 0 0       if ((not defined $pbos->{'suffix'}) || ($pbos->{'suffix'} eq "")) {
155             # By default suffix is a concatenation of name and version
156 0           $pbos->{'suffix'} = ".$pbos->{'name'}$dver2"
157             } else {
158             # concat just the version to what has been found
159 0           $pbos->{'suffix'} = ".$pbos->{'suffix'}$dver2";
160             }
161              
162             # if ($arch eq "x86_64") {
163             # $opt="--exclude=*.i?86";
164             # }
165 0           pb_log(2,"DEBUG: pb_distro_init: ".Dumper($pbos)."\n");
166              
167 0           return($pbos);
168             }
169              
170             =item B
171              
172             This function returns a list of 2 parameters indicating the distribution name and version of the underlying Linux distribution. The value of those 2 fields may be "unknown" in case the function was unable to recognize on which distribution it is running.
173              
174             On my home machine it would currently report ("mandriva","2010.2").
175              
176             =cut
177              
178             sub pb_distro_get {
179              
180             # 1: List of files that unambiguously indicates what distro we have
181             # 2: List of files that ambiguously indicates what distro we have
182             # 3: Should have the same keys as the previous one. If ambiguity, which other distributions should be checked
183             # 4: Matching Rg. Expr to detect distribution and version
184 0     0 1   my ($single_rel_files, $ambiguous_rel_files,$distro_similar,$distro_match) = pb_conf_get("osrelfile","osrelambfile","osambiguous","osrelexpr");
185              
186 0           my $release;
187             my $distro;
188              
189             # Begin to test presence of non-ambiguous files
190             # that way we reduce the choice
191 0           my ($d,$r);
192 0           while (($d,$r) = each %$single_rel_files) {
193 0 0         if (defined $ambiguous_rel_files->{$d}) {
194 0           print STDERR "The key $d is considered as both unambiguous and ambigous.\n";
195 0           print STDERR "Please fix your configuration file.\n"
196             }
197 0 0 0       if (-f "$r" && ! -l "$r") {
198 0           my $tmp=pb_get_content("$r");
199             # Found the only possibility.
200             # Try to get version and return
201 0 0         if (defined ($distro_match->{$d})) {
202 0           ($release) = $tmp =~ m/$distro_match->{$d}/m;
203             } else {
204 0           print STDERR "Unable to find $d version in $r (non-ambiguous)\n";
205 0           print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
206 0           $release = "unknown";
207             }
208 0           return($d,$release);
209             }
210             }
211              
212             # Now look at ambiguous files
213             # Ubuntu before 10.04 includes a /etc/debian_version file that creates an ambiguity with debian
214             # So we need to look at distros in reverse alphabetic order to treat ubuntu always first via lsb
215 0           my $found = 0;
216 0           foreach $d (reverse keys %$ambiguous_rel_files) {
217 0           $r = $ambiguous_rel_files->{$d};
218 0 0 0       if (-f "$r" && !-l "$r") {
219             # Found one possibility.
220             # Get all distros concerned by that file
221 0           my $tmp=pb_get_content("$r");
222 0           my $ptr = $distro_similar->{$d};
223 0           pb_log(2,"amb: ".Dumper($ptr)."\n");
224 0           $release = "unknown";
225 0           foreach my $dd (split(/,/,$ptr)) {
226 0           pb_log(2,"check $dd\n");
227             # Try to check pattern
228 0 0         if (defined $distro_match->{$dd}) {
229 0           pb_log(2,"cmp: $distro_match->{$dd} - vs - $tmp\n");
230 0           ($release) = $tmp =~ m/$distro_match->{$dd}/m;
231 0 0 0       if ((defined $release) && ($release ne "unknown")) {
232 0           $distro = $dd;
233 0           $found = 1;
234 0           last;
235             }
236             }
237             }
238 0 0         last if ($found == 1);
239             }
240             }
241 0 0         if ($found == 0) {
242 0           print STDERR "Unable to find a version in ".join(' ',keys %$ambiguous_rel_files)." (ambiguous)\n";
243 0           print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
244 0           return("unknown","unknown");
245             } else {
246 0           return($distro,$release);
247             }
248             }
249              
250             =item B
251              
252             This function returns the 5 lsb values LSB version, distribution ID, Description, release and codename.
253             As entry it takes an optional parameter to specify whether the output is short or not.
254              
255             =cut
256              
257             sub pb_distro_getlsb {
258              
259 0     0 1   my $s = shift;
260 0           pb_log(3,"Entering pb_distro_getlsb\n");
261              
262 0           my ($ambiguous_rel_files) = pb_conf_get("osrelambfile");
263 0           my $lsbf = $ambiguous_rel_files->{"lsb"};
264              
265             # LSB has not been configured.
266 0 0         if (not defined $lsbf) {
267 0           print STDERR "no lsb entry defined for osrelambfile\n";
268 0           confess "You modified upstream delivery and lost !\n";
269             }
270              
271 0 0         if (-r $lsbf) {
272 0           my $rep = pb_get_content($lsbf);
273             # Create elementary fields
274 0           my ($c, $r, $d, $i, $l) = ("", "", "", "", "");
275 0           for my $f (split(/\n/,$rep)) {
276 0           pb_log(3,"Reading file part ***$f***\n");
277 0 0         $c = $f if ($f =~ /^DISTRIB_CODENAME/);
278 0           $c =~ s/DISTRIB_CODENAME=/Codename:\t/;
279 0 0         $r = $f if ($f =~ /^DISTRIB_RELEASE/);
280 0           $r =~ s/DISTRIB_RELEASE=/Release:\t/;
281 0 0         $d = $f if ($f =~ /^DISTRIB_DESCRIPTION/);
282 0           $d =~ s/DISTRIB_DESCRIPTION=/Description:\t/;
283 0           $d =~ s/"//g;
284 0 0         $i = $f if ($f =~ /^DISTRIB_ID/);
285 0           $i =~ s/DISTRIB_ID=/Distributor ID:\t/;
286 0 0         $l = $f if ($f =~ /^LSB_VERSION/);
287 0           $l =~ s/LSB_VERSION=/LSB Version:\t/;
288             }
289 0           my $regexp = "^[A-z ]*:[\t ]*";
290 0 0         $c =~ s/$regexp// if (defined $s);
291 0 0         $r =~ s/$regexp// if (defined $s);
292 0 0         $d =~ s/$regexp// if (defined $s);
293 0 0         $i =~ s/$regexp// if (defined $s);
294 0 0         $l =~ s/$regexp// if (defined $s);
295 0           return($l, $i, $d, $r, $c);
296             } else {
297 0           print STDERR "Unable to read $lsbf file\n";
298 0           confess "Please report to the maintainer bruno_at_project-builder.org\n";
299             }
300             }
301              
302             # Internal function
303              
304             sub pb_apply_conf_proxy {
305 0     0 0   my ($pbos) = @_;
306              
307 0           my $ftp_proxy = pb_distro_get_param($pbos,pb_conf_get_if("ftp_proxy"));
308 0           my $http_proxy = pb_distro_get_param($pbos,pb_conf_get_if("http_proxy"));
309              
310             # We do not overwrite shell settings
311 0 0 0       $ENV{ftp_proxy} ||= $ftp_proxy if ((defined $ftp_proxy) && ($ftp_proxy ne ""));
      0        
312 0 0 0       $ENV{http_proxy} ||= $http_proxy if ((defined $http_proxy) && ($http_proxy ne ""));
      0        
313             }
314              
315             =item B
316              
317             This function install the dependencies required to build the package on a distro.
318             Dependencies can be passed as a parameter in which case they are not computed
319              
320             =cut
321              
322             sub pb_distro_installdeps {
323              
324             # SPEC file
325 0     0 1   my $f = shift;
326 0           my $pbos = shift;
327 0           my $deps = shift;
328              
329             # Protection
330 0 0 0       confess "Missing install command for $pbos->{name}-$pbos->{version}-$pbos->{arch}" unless (defined $pbos->{install} && $pbos->{install} =~ /\w/);
331 0           pb_apply_conf_proxy($pbos);
332              
333             # Get dependencies in the build file if not forced
334 0 0         $deps = pb_distro_getdeps($f,$pbos) if (not defined $deps);
335 0 0         pb_log(1, "ftp_proxy=$ENV{ftp_proxy}\n") if (defined $ENV{ftp_proxy});
336 0 0         pb_log(1, "http_proxy=$ENV{http_proxy}\n") if (defined $ENV{http_proxy});
337 0           pb_log(2,"deps: $deps\n");
338 0 0 0       return if ((not defined $deps) || ($deps =~ /^\s*$/));
339              
340             # This may not be // proof. We should test for availability of repo and sleep if not
341 0           my $cmd = "$pbos->{'install'} $deps";
342 0           my $ret = pb_system($cmd, "Installing dependencies ($cmd)","mayfail");
343             # Try to accomodate deficient proxies
344 0 0         if ($ret != 0) {
345 0           pb_system($cmd, "Re-trying installing dependencies ($cmd)");
346             }
347             # Check that all deps have been installed correctly
348 0           $deps = pb_distro_getdeps($f, $pbos);
349 0 0 0       confess "Some dependencies did not install ($deps)" if ((defined $deps) && ($deps =~ /\S/) && ($Global::pb_stop_on_error));
      0        
350             }
351              
352             =item B
353              
354             This function computes the dependencies indicated in the build file and return them as a string of packages to install
355              
356             =cut
357              
358             sub pb_distro_getdeps {
359              
360 0     0 1   my $f = shift;
361 0           my $pbos = shift;
362              
363 0           my $regexp = "";
364 0           my $deps = "";
365 0           my $sep = $/;
366              
367             # Protection
368 0 0         return("") if (not defined $pbos->{'type'});
369 0 0         return("") if (not defined $f);
370              
371 0           pb_log(3,"entering pb_distro_getdeps: $pbos->{'type'} - $f\n");
372 0 0         if ($pbos->{'type'} eq "rpm") {
    0          
    0          
373             # In RPM this could include files, but we do not handle them atm.
374 0           $regexp = '^BuildRequires:(.*)$';
375             } elsif ($pbos->{'type'} eq "deb") {
376 0           $regexp = '^Build-Depends:(.*)$';
377             } elsif ($pbos->{'type'} eq "ebuild") {
378 0           $sep = '"'.$/;
379 0           $regexp = '^DEPEND="(.*)"\n'
380             } else {
381             # No idea
382 0           return("");
383             }
384 0           pb_log(2,"regexp: $regexp\n");
385              
386             # Preserve separator before using the one we need
387 0           my $oldsep = $/;
388 0           $/ = $sep;
389 0 0         open(DESC,"$f") || confess "Unable to open $f";
390 0           while () {
391 0           pb_log(4,"read: $_\n");
392 0 0         next if (! /$regexp/);
393 0           chomp();
394              
395 0           my $nextline;
396             # Support multi-lines deps for .deb
397 0 0         if ($pbos->{type} eq 'deb') {
398 0           while ($nextline = ) {
399 0 0         last unless $nextline =~ /^\s+(.+)$/o;
400 0           $_ .= $1;
401             }
402             }
403              
404             # What we found with the regexp is the list of deps.
405 0           pb_log(2,"found deps: $_\n");
406 0           s/$regexp/$1/i;
407 0           pb_log(4,"found deps 1: $_\n");
408             # Remove conditions in the middle and at the end for deb
409 0           s/\(\s*[><=]+.*\)[^,]*,/,/g;
410 0           pb_log(4,"found deps 2: $_\n");
411 0           s/\(\s*[><=]+.*$//g;
412 0           pb_log(4,"found deps 3: $_\n");
413             # Same for rpm
414 0           s/[><=]+[^,]*,/,/g;
415 0           pb_log(4,"found deps 4: $_\n");
416 0           s/[><=]+.*$//g;
417 0           pb_log(4,"found deps 5: $_\n");
418             # Improve string format (remove , and spaces at start, end and in double
419 0           s/,/ /g;
420 0           pb_log(4,"found deps 6: $_\n");
421 0           s/^\s*//;
422 0           pb_log(4,"found deps 7: $_\n");
423             # $ here removes the \n
424 0           s/\s*$//;
425 0           pb_log(4,"found deps 8: $_\n");
426 0           s/\s+/ /g;
427 0           pb_log(4,"found deps 9: $_\n");
428 0           $deps .= " ".$_;
429 0           pb_log(4,"found deps end: $deps\n");
430              
431             # Support multi-lines deps for .deb (fwup)
432 0 0         if (defined $nextline) {
433 0           $_ = $nextline;
434 0           redo;
435             }
436             }
437 0           close(DESC);
438 0           $/ = $oldsep;
439 0           pb_log(2,"now deps: $deps\n");
440 0           my $deps2 = pb_distro_only_deps_needed($pbos,$deps);
441 0           return($deps2);
442             }
443              
444              
445             =item B
446              
447             This function returns only the dependencies not yet installed
448              
449             =cut
450              
451             sub pb_distro_only_deps_needed {
452              
453 0     0 1   my $pbos = shift;
454 0           my $deps = shift;
455              
456 0 0 0       return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
457 0           my $deps2 = "";
458             # Avoid to install what is already there
459 0           delete $ENV{COLUMNS};
460 0           foreach my $p (split(/\s+/,$deps)) {
461 0 0         next if $p =~ /^\s*$/o;
462 0 0         if ($pbos->{'type'} eq "rpm") {
    0          
    0          
463 0           my $rpmcmd = "rpm -q --whatprovides --quiet";
464             # whatprovides doesn't work for RH6.2
465 0 0 0       $rpmcmd = "rpm -q --quiet" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /6/));
466 0           my $res = pb_system("$rpmcmd $p","Looking for $p","mayfail");
467 0 0         next if ($res eq 0);
468 0           pb_log(1, "INFO: missing dependency $p\n");
469             } elsif ($pbos->{'type'} eq "deb") {
470 0           my $res = pb_system("dpkg -L $p","Looking for $p","mayfail");
471 0 0         next if ($res eq 0);
472 0 0         open(CMD,"dpkg -l $p |") or confess "Unable to run dpkg -l $p: $!";
473 0           my $ok = 0;
474 0           while () {
475 0 0         $ok = 1 if /^ii\s+$p/;
476             }
477 0           close(CMD);
478 0 0         next if $ok;
479 0           pb_log(1, "INFO: missing dependency $p\n");
480             } elsif ($pbos->{'type'} eq "ebuild") {
481             } else {
482             # Not reached
483             }
484 0           pb_log(2,"found deps2: $p\n");
485 0           $deps2 .= " $p";
486             }
487              
488 0           $deps2 =~ s/^\s*//;
489 0           pb_log(2,"now deps2: $deps2\n");
490 0           return($deps2);
491             }
492              
493             =item B
494              
495             This function sets up potential additional repository for the setup phase
496              
497             =cut
498              
499             sub pb_distro_setuposrepo {
500              
501 0     0 1   my $pbos = shift;
502              
503 0           pb_distro_setuprepo_gen($pbos,pb_distro_conffile(),"osrepo");
504             }
505              
506             =item B
507              
508             This function sets up potential additional repository to the build environment
509              
510             =cut
511              
512             sub pb_distro_setuprepo {
513              
514 0     0 1   my $pbos = shift;
515              
516 0           pb_distro_setuprepo_gen($pbos,"$ENV{'PBDESTDIR'}/pbrc","addrepo");
517             }
518              
519             # Internal
520             sub pb_distro_compare_repo {
521              
522 0     0 0   my $src = shift;
523 0           my $dest = shift;
524              
525 0 0 0       if (not -f $dest) {
    0 0        
    0          
526 0           pb_log(1, "INFO: Creating new file $dest\n");
527             } elsif (-f $dest && -s $dest == 0) {
528 0           pb_log(1, "INFO: Overwriting empty file $dest\n");
529             } elsif (-f $dest && compare("$src", $dest) == 0) {
530 0           pb_log(1, "INFO: Overwriting identical file $dest\n");
531             } else {
532 0           pb_log(0, "ERROR: destination file $dest exists and is different than source $src\n");
533 0           pb_system("cat $dest","INFO: Dest...\n");
534 0           pb_system("cat $src","INFO: New...\n");
535 0           pb_log("INFO: Returning...\n");
536 0           return(0);
537             }
538             # TRUE
539 0           return(1);
540             }
541              
542             =item B
543              
544             This function sets up in a generic way potential additional repository
545              
546             =cut
547              
548             sub pb_distro_setuprepo_gen {
549              
550 0     0 1   my $pbos = shift;
551 0           my $pbconf = shift;
552 0           my $pbkey = shift;
553              
554 0 0         return if (not defined $pbconf);
555 0 0         return if (not defined $pbkey);
556 0           my ($addrepo) = pb_conf_read($pbconf,$pbkey);
557 0 0         return if (not defined $addrepo);
558              
559 0           my $param = pb_distro_get_param($pbos,$addrepo);
560 0 0         return if ($param eq "");
561              
562 0           pb_apply_conf_proxy($pbos);
563              
564             # Loop on the list of additional repo
565 0           foreach my $i (split(/,/,$param)) {
566              
567 0           my ($scheme, $account, $host, $port, $path) = pb_get_uri($i);
568 0           my $bn = basename($i);
569              
570             # The repo file can be local or remote. download or copy at the right place
571 0 0 0       if (($scheme eq "ftp") || ($scheme eq "http")) {
572 0           pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Downloading additional repository file $i");
573             } else {
574 0           copy($i,"$ENV{'PBTMP'}/$bn");
575             }
576              
577             # The repo file can be a real file or a package
578 0 0         if ($pbos->{'type'} eq "rpm") {
    0          
579 0 0         if ($bn =~ /\.rpm$/) {
    0          
    0          
580 0           my $pn = $bn;
581 0           $pn =~ s/\.rpm//;
582 0 0         if (pb_system("rpm -q --quiet $pn","","mayfail") != 0) {
583 0           pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package to setup repository");
584             }
585             } elsif ($bn =~ /\.repo$/) {
586 0           my $dirdest = "";
587 0           my $reponame = "";
588             # TODO: could go in pb.conf in fact
589 0 0         if ($pbos->{install} =~ /\byum\b/) {
    0          
    0          
590 0           $reponame="yum";
591 0           $dirdest = "/etc/yum.repos.d";
592             } elsif ($pbos->{install} =~ /\bdnf\b/) {
593 0           $reponame="dnf";
594 0           $dirdest = "/etc/yum.repos.d";
595             } elsif ($pbos->{install} =~ /\bzypper\b/) {
596 0           $reponame="zypper";
597 0           $dirdest = "/etc/zypp/repos.d";
598             } else {
599 0           confess "Unknown location for repository file for '$pbos->{install}' command";
600             }
601 0           my $dest = "$dirdest/$bn";
602 0 0         return if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest));
603 0 0         confess "Missing directory $dirdest ($reponame)" unless (-d $dirdest);
604 0 0         pb_system("sudo mv $ENV{'PBTMP'}/$bn $dirdest/$bn","Adding $reponame repository") if (not -f "$dirdest/$bn");
605             } elsif ($bn =~ /\.addmedia/) {
606             # URPMI repo
607             # We should test that it's not already a urpmi repo
608 0           pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository");
609             } else {
610 0           pb_log(0,"ERROR: Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
611             }
612             } elsif ($pbos->{'type'} eq "deb") {
613 0 0         if ($bn =~ /\.sources.list$/) {
614 0           my $dest = "/etc/apt/sources.list.d/$bn";
615 0 0         return if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest));
616 0           pb_system("sudo mv $ENV{'PBTMP'}/$bn /etc/apt/sources.list.d","Adding apt repository");
617 0           pb_system("sudo apt-get update","Updating apt repository");
618             } else {
619 0           pb_log(0,"ERROR: Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
620             }
621             } else {
622 0           pb_log(0,"ERROR: Unable to deal with repository file $i on that distro ! Please report to dev team\n");
623             }
624             }
625 0           return;
626             }
627              
628             =item B
629              
630             Given a pbos object (first param) and the generic key (second param), get the list of possible keys for looking up variable for
631             filter names. The list will be sorted most-specific to least specific.
632              
633             =cut
634              
635             sub pb_distro_to_keylist ($$) {
636              
637 0     0 1   my ($pbos, $generic) = @_;
638              
639 0           foreach my $key (qw/name version arch family type os/) {
640 0 0         confess "missing pbos key $key" unless (defined $pbos->{$key});
641             }
642              
643 0           my @keylist = ("$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}", "$pbos->{'name'}-$pbos->{'version'}");
644              
645             # Loop to include also previous minor versions
646             # if configured so
647 0 0 0       if ((defined $pbos->{'useminor'}) && ($pbos->{'useminor'} eq "true") && ($pbos->{'version'} =~ /^(\d+)\.(\d+)$/o)) {
      0        
648 0           my ($major, $minor) = ($1, $2);
649 0           while ($minor > 0) {
650 0           $minor--;
651 0           push (@keylist, "$pbos->{'name'}-${major}.$minor");
652             }
653 0           push (@keylist, "$pbos->{'name'}-$major");
654             }
655              
656 0           push (@keylist, $pbos->{'name'}, $pbos->{'family'}, $pbos->{'type'}, $pbos->{'os'}, $generic);
657 0           return @keylist;
658             }
659              
660             =item B
661              
662             This function gets the parameter in the conf file from the most precise tuple up to default
663              
664             =cut
665              
666             sub pb_distro_get_param {
667              
668 0     0 1   my @param = ();
669 0           my $pbos = shift;
670              
671 0           my @keylist = pb_distro_to_keylist($pbos,"default");
672 0           pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} for ".Dumper(@_)."\n");
673 0           foreach my $opt (@_) {
674 0           my $param = "";
675 0           foreach my $key (@keylist) {
676 0 0         if (defined $opt->{$key}) {
677 0           $param = $opt->{$key};
678 0           last;
679             }
680             }
681             # Allow replacement of variables inside the parameter such as name, version, arch for rpmbootstrap
682             # but not shell variable which are backslashed
683 0 0         if ($param =~ /[^\\]\$/) {
684 0           pb_log(3,"Expanding variable on $param\n");
685 0           eval { $param =~ s/(\$\w+->\{\'\w+\'\})/$1/eeg };
  0            
  0            
686             }
687 0           push @param,$param;
688             }
689              
690 0           pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} returns ==".Dumper(@param)."==\n");
691              
692             # Return one param if user only asked for one lookup, an array if not.
693 0           my $nb = @param;
694 0 0         if ($nb eq 1) {
695 0           return($param[0]);
696             } else {
697 0           return(@param);
698             }
699             }
700              
701             =item B
702              
703             This function gets the OS context passed as parameter and return the corresponding distribution hash
704             If passed undef or "" then auto-detects
705              
706             =cut
707              
708              
709             sub pb_distro_get_context {
710              
711 0     0 1   my $os = shift;
712 0           my $pbos;
713              
714 0 0 0       if ((defined $os) && ($os ne "")) {
715 0           my ($name,$ver,$darch) = split(/-/,$os);
716 0 0 0       pb_log(0,"Bad format for $os") if ((not defined $name) || (not defined $ver) || (not defined $darch)) ;
      0        
717 0           chomp($darch);
718 0           $pbos = pb_distro_init($name,$ver,$darch);
719             } else {
720 0           $pbos = pb_distro_init();
721             }
722 0           return($pbos);
723             }
724              
725             =item B
726              
727             This function prints every configuration parameter in order to help debug stacking issues with conf files. If a VM/VE/RM is given restrict display to this distribution. Ifparameters are passed, restrict again the display to these values only.
728              
729             =cut
730              
731             sub pb_distro_conf_print {
732              
733 0     0 1   my $pbos = shift;
734 0           my @keys = @_;
735              
736 0 0         if ($#keys == -1) {
737 0           pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
738 0           pb_log(0,"================================================\n");
739             }
740 0 0         if (defined $ENV{'PBV'}) {
741 0           pb_log(0,"Distribution $ENV{'PBV'}\n");
742 0           pb_log(0,"========================\n");
743             } else {
744 0           pb_log(0,"Local Distribution\n");
745 0           pb_log(0,"==================\n");
746             }
747              
748 0 0         if ($#keys == -1) {
749 0           foreach my $k (pb_conf_get_all()) {
750 0           pb_log(0,"$k => ".Dumper(pb_conf_get($k))."\n");
751             }
752             } else {
753 0           foreach my $k (@keys) {
754 0           pb_log(0,"$k=".pb_distro_get_param($pbos,pb_conf_get($k))."\n");
755             }
756             }
757             }
758              
759              
760             =back
761              
762             =head1 WEB SITES
763              
764             The main Web site of the project is available at L. Bug reports should be filled using the trac instance of the project at L.
765              
766             =head1 USER MAILING LIST
767              
768             None exists for the moment.
769              
770             =head1 AUTHORS
771              
772             The Project-Builder.org team L lead by Bruno Cornec L.
773              
774             =head1 COPYRIGHT
775              
776             Project-Builder.org is distributed under the GPL v2.0 license
777             described in the file C included with the distribution.
778              
779             =cut
780              
781              
782             1;