source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/Distribution.pm@ 2012

Last change on this file since 2012 was 2012, checked in by Bruno Cornec, 9 years ago

Adds dnf support

Starting with Fedora 22 the package manager is dnf and not yum anymore
pb needed some adaptations to support it, which have been mostly been
coded in this patch. Tests to be done.

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