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

Last change on this file since 1678 was 1678, checked in by Bruno Cornec, 11 years ago
  • Exports function pb_vcs_add_if_not_in
  • replace most die by confess
File size: 22.0 KB
Line 
1#!/usr/bin/perl -w
2#
3# Creates common environment for distributions
4#
5# Copyright B. Cornec 2007-2012
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_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_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
215foreach $d (reverse keys %$ambiguous_rel_files) {
216 $r = $ambiguous_rel_files->{$d};
217 if (-f "$r" && !-l "$r") {
218 # Found one possibility.
219 # Get all distros concerned by that file
220 my $tmp=pb_get_content("$r");
221 my $found = 0;
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 if ($found == 0) {
239 print STDERR "Unable to find $d version in $r (ambiguous)\n";
240 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
241 $release = "unknown";
242 } else {
243 return($distro,$release);
244 }
245 }
246}
247return("unknown","unknown");
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 || undef;
326my $pbos = shift;
327my $deps = shift || undef;
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/));
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 || undef;
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 # Remove conditions in the middle and at the end for deb
408 s/\(\s*[><=]+.*\)[^,]*,/,/g;
409 s/\(\s*[><=]+.*$//g;
410 # Same for rpm
411 s/[><=]+[^,]*,/,/g;
412 s/[><=]+.*$//g;
413 # Improve string format (remove , and spaces at start, end and in double
414 s/,/ /g;
415 s/^\s*//;
416 s/\s*$//;
417 s/\s+/ /g;
418 $deps .= " ".$_;
419
420 # Support multi-lines deps for .deb (fwup)
421 if (defined $nextline) {
422 $_ = $nextline;
423 redo;
424 }
425}
426close(DESC);
427$/ = $oldsep;
428pb_log(2,"now deps: $deps\n");
429my $deps2 = pb_distro_only_deps_needed($pbos,$deps);
430return($deps2);
431}
432
433
434=item B<pb_distro_only_deps_needed>
435
436This function returns only the dependencies not yet installed
437
438=cut
439
440sub pb_distro_only_deps_needed {
441
442my $pbos = shift;
443my $deps = shift || undef;
444
445return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
446my $deps2 = "";
447# Avoid to install what is already there
448delete $ENV{COLUMNS};
449foreach my $p (split(/\s+/,$deps)) {
450 next if $p =~ /^\s*$/o;
451 if ($pbos->{'type'} eq "rpm") {
452 my $rpmcmd = "rpm -q --whatprovides --quiet";
453 # whatprovides doesn't work for RH6.2
454 $rpmcmd = "rpm -q --quiet" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /6/));
455 my $res = pb_system("$rpmcmd $p","Looking for $p","mayfail");
456 next if ($res eq 0);
457 pb_log(1, "INFO: missing dependency $p\n");
458 } elsif ($pbos->{'type'} eq "deb") {
459 my $res = pb_system("dpkg -L $p","Looking for $p","mayfail");
460 next if ($res eq 0);
461 open(CMD,"dpkg -l $p |") or confess "Unable to run dpkg -l $p: $!";
462 my $ok = 0;
463 while (<CMD>) {
464 $ok = 1 if /^ii\s+$p/;
465 }
466 close(CMD);
467 next if $ok;
468 pb_log(1, "INFO: missing dependency $p\n");
469 } elsif ($pbos->{'type'} eq "ebuild") {
470 } else {
471 # Not reached
472 }
473 pb_log(2,"found deps2: $p\n");
474 $deps2 .= " $p";
475}
476
477$deps2 =~ s/^\s*//;
478pb_log(2,"now deps2: $deps2\n");
479return($deps2);
480}
481
482=item B<pb_distro_setuposrepo>
483
484This function sets up potential additional repository for the setup phase
485
486=cut
487
488sub pb_distro_setuposrepo {
489
490my $pbos = shift;
491
492pb_distro_setuprepo_gen($pbos,pb_distro_conffile(),"osrepo");
493}
494
495=item B<pb_distro_setuprepo>
496
497This function sets up potential additional repository to the build environment
498
499=cut
500
501sub pb_distro_setuprepo {
502
503my $pbos = shift;
504
505pb_distro_setuprepo_gen($pbos,"$ENV{'PBDESTDIR'}/pbrc","addrepo");
506}
507
508# Internal
509sub pb_distro_compare_repo {
510
511my $src = shift;
512my $dest = shift;
513
514if (not -f $dest) {
515 pb_log(1, "INFO: Creating new file $dest\n");
516} elsif (-f $dest && -s $dest == 0) {
517 pb_log(1, "INFO: Overwriting empty file $dest\n");
518} elsif (-f $dest && compare("$src", $dest) == 0) {
519 pb_log(1, "INFO: Overwriting identical file $dest\n");
520} else {
521 pb_log(0, "ERROR: destination file $dest exists and is different than source $src\n");
522 pb_system("cat $dest","INFO: Dest...\n");
523 pb_system("cat $src","INFO: New...\n");
524 pb_log("INFO: Returning...\n");
525 return(0);
526}
527# TRUE
528return(1);
529}
530
531=item B<pb_distro_setuprepo_gen>
532
533This function sets up in a generic way potential additional repository
534
535=cut
536
537sub pb_distro_setuprepo_gen {
538
539my $pbos = shift;
540my $pbconf = shift || undef;
541my $pbkey = shift || undef;
542
543return if (not defined $pbconf);
544return if (not defined $pbkey);
545my ($addrepo) = pb_conf_read($pbconf,$pbkey);
546return if (not defined $addrepo);
547
548my $param = pb_distro_get_param($pbos,$addrepo);
549return if ($param eq "");
550
551pb_apply_conf_proxy($pbos);
552
553# Loop on the list of additional repo
554foreach my $i (split(/,/,$param)) {
555
556 my ($scheme, $account, $host, $port, $path) = pb_get_uri($i);
557 my $bn = basename($i);
558
559 # The repo file can be local or remote. download or copy at the right place
560 if (($scheme eq "ftp") || ($scheme eq "http")) {
561 pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Downloading additional repository file $i");
562 } else {
563 copy($i,$ENV{'PBTMP'}/$bn);
564 }
565
566 # The repo file can be a real file or a package
567 if ($pbos->{'type'} eq "rpm") {
568 if ($bn =~ /\.rpm$/) {
569 my $pn = $bn;
570 $pn =~ s/\.rpm//;
571 if (pb_system("rpm -q --quiet $pn","","mayfail") != 0) {
572 pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package to setup repository");
573 }
574 } elsif ($bn =~ /\.repo$/) {
575 my $dirdest = "";
576 my $reponame = "";
577 # TODO: could go in pb.conf in fact
578 if ($pbos->{install} =~ /\byum\b/) {
579 $reponame="yum";
580 $dirdest = "/etc/yum.repos.d";
581 } elsif ($pbos->{install} =~ /\bzypper\b/) {
582 $reponame="zypper";
583 $dirdest = "/etc/zypp/repos.d";
584 } else {
585 confess "Unknown location for repository file for '$pbos->{install}' command";
586 }
587 my $dest = "$dirdest/$bn";
588 return if (pb_distro_compare_repo($ENV{'PBTMP'}/$bn,$dest));
589 confess "Missing directory $dirdest ($reponame)" unless (-d $dirdest);
590 pb_system("sudo mv $ENV{'PBTMP'}/$bn $dirdest/$bn","Adding $reponame repository") if (not -f "$dirdest/$bn");
591 } elsif ($bn =~ /\.addmedia/) {
592 # URPMI repo
593 # We should test that it's not already a urpmi repo
594 pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository");
595 } else {
596 pb_log(0,"ERROR: Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
597 }
598 } elsif ($pbos->{'type'} eq "deb") {
599 if ($bn =~ /\.sources.list$/) {
600 my $dest = "/etc/apt/sources.list.d/$bn";
601 return if (pb_distro_compare_repo($ENV{'PBTMP'}/$bn,$dest));
602 pb_system("sudo mv $ENV{'PBTMP'}/$bn /etc/apt/sources.list.d","Adding apt repository");
603 pb_system("sudo apt-get update","Updating apt repository");
604 } else {
605 pb_log(0,"ERROR: Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
606 }
607 } else {
608 pb_log(0,"ERROR: Unable to deal with repository file $i on that distro ! Please report to dev team\n");
609 }
610}
611return;
612}
613
614=item B<pb_distro_to_keylist>
615
616Given a pbos object (first param) and the generic key (second param), get the list of possible keys for looking up variable for
617filter names. The list will be sorted most-specific to least specific.
618
619=cut
620
621sub pb_distro_to_keylist ($$) {
622
623my ($pbos, $generic) = @_;
624
625foreach my $key (qw/name version arch family type os/) {
626 confess "missing pbos key $key" unless (defined $pbos->{$key});
627}
628
629my @keylist = ("$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}", "$pbos->{'name'}-$pbos->{'version'}");
630
631# Loop to include also previous minor versions
632# if configured so
633if ((defined $pbos->{'useminor'}) && ($pbos->{'useminor'} eq "true") && ($pbos->{'version'} =~ /^(\d+)\.(\d+)$/o)) {
634 my ($major, $minor) = ($1, $2);
635 while ($minor > 0) {
636 $minor--;
637 push (@keylist, "$pbos->{'name'}-${major}.$minor");
638 }
639 push (@keylist, "$pbos->{'name'}-$major");
640}
641
642push (@keylist, $pbos->{'name'}, $pbos->{'family'}, $pbos->{'type'}, $pbos->{'os'}, $generic);
643return @keylist;
644}
645
646=item B<pb_distro_get_param>
647
648This function gets the parameter in the conf file from the most precise tuple up to default
649
650=cut
651
652sub pb_distro_get_param {
653
654my @param = ();
655my $pbos = shift;
656
657my @keylist = pb_distro_to_keylist($pbos,"default");
658pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} for ".Dumper(@_)."\n");
659foreach my $opt (@_) {
660 my $param = "";
661 foreach my $key (@keylist) {
662 if (defined $opt->{$key}) {
663 $param = $opt->{$key};
664 last;
665 }
666 }
667 # Allow replacement of variables inside the parameter such as name, version, arch for rpmbootstrap
668 # but not shell variable which are backslashed
669 if ($param =~ /[^\\]\$/) {
670 pb_log(3,"Expanding variable on $param\n");
671 eval { $param =~ s/(\$\w+->{\'\w+\'})/$1/eeg };
672 }
673 push @param,$param;
674}
675
676pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} returns ==".Dumper(@param)."==\n");
677
678# Return one param if user only asked for one lookup, an array if not.
679my $nb = @param;
680if ($nb eq 1) {
681 return($param[0]);
682} else {
683 return(@param);
684}
685}
686
687=item B<pb_distro_get_context>
688
689This function gets the OS context passed as parameter and return the corresponding distribution hash
690If passed undef or "" then auto-detects
691
692=cut
693
694
695sub pb_distro_get_context {
696
697my $os = shift;
698my $pbos;
699
700if ((defined $os) && ($os ne "")) {
701 my ($name,$ver,$darch) = split(/-/,$os);
702 pb_log(0,"Bad format for $os") if ((not defined $name) || (not defined $ver) || (not defined $darch)) ;
703 chomp($darch);
704 $pbos = pb_distro_init($name,$ver,$darch);
705} else {
706 $pbos = pb_distro_init();
707}
708return($pbos);
709}
710
711=back
712
713=head1 WEB SITES
714
715The 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/>.
716
717=head1 USER MAILING LIST
718
719None exists for the moment.
720
721=head1 AUTHORS
722
723The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
724
725=head1 COPYRIGHT
726
727Project-Builder.org is distributed under the GPL v2.0 license
728described in the file C<COPYING> included with the distribution.
729
730=cut
731
732
7331;
Note: See TracBrowser for help on using the repository browser.