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

Last change on this file since 2136 was 2136, checked in by Bruno Cornec, 8 years ago

Fix #115

The standard conf file is now under /usr/share/pb and the one under
/etc/pn is modifyable by the sysadmin is contains only comments by
default.

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