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

Last change on this file since 2338 was 2338, checked in by bruno, 22 months ago

the API conf file is now under etc and in pb-modules

File size: 28.8 KB
RevLine 
[11]1#!/usr/bin/perl -w
[2]2#
[11]3# Creates common environment for distributions
[2]4#
[2032]5# Copyright B. Cornec 2007-2016
[1528]6# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
7# Provided under the GPL v2
8#
[2]9# $Id$
10#
11
[329]12package ProjectBuilder::Distribution;
13
[11]14use strict;
[423]15use Data::Dumper;
[1515]16use Carp 'confess';
[1148]17use ProjectBuilder::Version;
[395]18use ProjectBuilder::Base;
[702]19use ProjectBuilder::Conf;
20use File::Basename;
21use File::Copy;
[1507]22# requires perl 5.004 minimum in VM/VE
23use File::Compare;
[2]24
[1148]25# Global vars
[329]26# Inherit from the "Exporter" module which handles exporting functions.
27 
[2279]28use vars qw($VERSION $REVISION $PBCONFVER @ISA @EXPORT);
[329]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);
[2338]35our @EXPORT = qw(pb_distro_init pb_distro_conffile pb_distro_sysconffile pb_distro_api 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_setuprepo_gen pb_distro_get_param pb_distro_get_context pb_distro_to_keylist pb_distro_conf_print pb_apply_conf_proxy);
[2279]36($VERSION,$REVISION,$PBCONFVER) = pb_version_init();
[329]37
[391]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  #
[1177]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";
[391]57  #
58  # Return information on the requested distro
59  #
[1177]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";
[391]62  #
63  # Return information on the running distro
64  #
[622]65  my ($ddir,$dver) = pb_distro_get();
[391]66
67=head1 USAGE
68
69=over 4
70
[891]71=item B<pb_distro_conffile>
72
73This function returns the mandatory configuration file used for distribution/OS detection
74
75=cut
76
[2338]77sub pb_distro_api {
78
79return("CCCC/api.yml");
80}
81
82
83=item B<pb_distro_conffile>
84
85This function returns the mandatory configuration file used for distribution/OS detection
86
87=cut
88
[891]89sub pb_distro_conffile {
90
[2279]91if ($PBCONFVER < 1) {
92    return("CCCC/pb.conf");
93} else {
94    return("CCCC/pb.yml");
[891]95}
[2279]96}
[891]97
[2136]98=item B<pb_distro_sysconffile>
[891]99
[2136]100This function returns the optional configuration file used for local customization
101
102=cut
103
104sub pb_distro_sysconffile {
105
[2279]106if ($PBCONFVER < 1) {
107    return("SSSS/pb.conf");
108} else {
109    return("SSSS/pb.yml");
[2136]110}
[2279]111}
[2136]112
113
[756]114=item B<pb_distro_init>
[395]115
[1177]116This 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.
[391]117
[756]118As an example, Ubuntu and Debian are in the same "du" family. As well as RedHat, RHEL, CentOS, fedora are on the same "rh" family.
[1177]119Mandriva, Open SuSE and Fedora have all the same "rpm" type of build system. Ubuntu and Debian have the same "deb" type of build system.
[756]120And "fc" is the extension generated for all Fedora packages (Version will be added by pb).
[2289]121All this information is stored in an external configuration file typically at /etc/pb/pb.yml
[391]122
[756]123When 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.
[391]124
[756]125Cf: http://linuxmafia.com/faq/Admin/release-files.html
126Ideas taken from http://search.cpan.org/~kerberus/Linux-Distribution-0.14/lib/Linux/Distribution.pm
127
[391]128=cut
129
[756]130
[74]131sub pb_distro_init {
[2]132
[1177]133my $pbos = {
134    'name' => undef,
135    'version' => undef,
136    'arch' => undef,
137    'family' => "unknown",
138    'suffix' => "unknown",
139    'update' => "unknown",
140    'install' => "unknown",
141    'type' => "unknown",
142    'os' => "unknown",
143    'nover' => "false",
144    'rmdot' => "false",
[1540]145    'useminor' => "false",
[1177]146    };
147$pbos->{'name'} = shift;
148$pbos->{'version'} = shift;
149$pbos->{'arch'} = shift;
[2]150
[869]151# Adds conf file for distribution description
152# the location of the conf file is finalyzed at install time
153# depending whether we deal with package install or tar file install
154
[2136]155pb_conf_add(pb_distro_sysconffile());
156
[2152]157# Similarly for the local file available for sysadmin. After the previous one to allow overwrite to work
158pb_conf_add(pb_distro_conffile());
159
[11]160# If we don't know which distribution we're on, then guess it
[1177]161($pbos->{'name'},$pbos->{'version'}) = pb_distro_get() if ((not defined $pbos->{'name'}) || (not defined $pbos->{'version'}));
[2]162
[1159]163# For some rare cases, typically nover ones
[1177]164$pbos->{'name'} = "unknown" if (not defined $pbos->{'name'});
165$pbos->{'version'} = "unknown" if (not defined $pbos->{'version'});
[1159]166
[757]167# Initialize arch
[1177]168$pbos->{'arch'} = pb_get_arch() if (not defined $pbos->{'arch'});
[1652]169# Solves a bug on Red Hat 6.x where real arch is not detected when using setarch and a chroot
170# As it was only i386 forcing it here.
171$pbos->{'arch'} = "i386" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /^6\./));
[757]172
[969]173# Dig into the tuple to find the best answer
[1177]174# Do NOT factorize here, as it won't work as of now for hash creation
[1530]175# Do NOT change order without caution
176$pbos->{'useminor'} = pb_distro_get_param($pbos,pb_conf_get("osuseminorrel"));
[1177]177$pbos->{'family'} = pb_distro_get_param($pbos,pb_conf_get("osfamily"));
178$pbos->{'type'} = pb_distro_get_param($pbos,pb_conf_get("ostype"));
[2330]179($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"));
180($pbos->{'localinstall'}) = pb_distro_get_param($pbos,pb_conf_get_if("oslocalins"));
[1177]181#($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"));
[867]182
183# Some OS have no interesting version
[1177]184$pbos->{'version'} = "nover" if ((defined $pbos->{'nover'}) && ($pbos->{'nover'} eq "true"));
[867]185
[1167]186# For some OS remove the . in version name for extension
[1177]187my $dver2 = $pbos->{'version'};
188$dver2 =~ s/\.//g if ((defined $pbos->{'rmdot'}) && ($pbos->{'rmdot'} eq "true"));
[867]189
[1177]190if ((not defined $pbos->{'suffix'}) || ($pbos->{'suffix'} eq "")) {
191    # By default suffix is a concatenation of name and version
192    $pbos->{'suffix'} = ".$pbos->{'name'}$dver2" 
[11]193} else {
[867]194    # concat just the version to what has been found
[1177]195    $pbos->{'suffix'} = ".$pbos->{'suffix'}$dver2";
[11]196}
197
[867]198#   if ($arch eq "x86_64") {
199#   $opt="--exclude=*.i?86";
200#   }
[1177]201pb_log(2,"DEBUG: pb_distro_init: ".Dumper($pbos)."\n");
[867]202
[1177]203return($pbos);
[11]204}
[23]205
[756]206=item B<pb_distro_get>
[395]207
[756]208This 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.
[395]209
[1156]210On my home machine it would currently report ("mandriva","2010.2").
[395]211
212=cut
213
[622]214sub pb_distro_get {
[23]215
[869]216# 1: List of files that unambiguously indicates what distro we have
217# 2: List of files that ambiguously indicates what distro we have
218# 3: Should have the same keys as the previous one. If ambiguity, which other distributions should be checked
219# 4: Matching Rg. Expr to detect distribution and version
[2333]220my ($single_rel_files, $ambiguous_rel_files,$distro_similar,$distro_match,$nover) = pb_conf_get("osrelfile","osrelambfile","osambiguous","osrelexpr","osnover");
[23]221
222my $release;
223my $distro;
224
[391]225# Begin to test presence of non-ambiguous files
[23]226# that way we reduce the choice
[24]227my ($d,$r);
[869]228while (($d,$r) = each %$single_rel_files) {
[1027]229    if (defined $ambiguous_rel_files->{$d}) {
230        print STDERR "The key $d is considered as both unambiguous and ambigous.\n";
[1029]231        print STDERR "Please fix your configuration file.\n"
[1027]232    }
[869]233    if (-f "$r" && ! -l "$r") {
234        my $tmp=pb_get_content("$r");
[23]235        # Found the only possibility.
236        # Try to get version and return
[869]237        if (defined ($distro_match->{$d})) {
238            ($release) = $tmp =~ m/$distro_match->{$d}/m;
[23]239        } else {
[2333]240            if (not defined ($nover->{$d})) {
241                print STDERR "Unable to find $d version in $r (non-ambiguous)\n";
242                print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
243            }
[23]244            $release = "unknown";
245        }
246        return($d,$release);
247    }
248}
249
[423]250# Now look at ambiguous files
[1027]251# Ubuntu before 10.04 includes a /etc/debian_version file that creates an ambiguity with debian
[423]252# So we need to look at distros in reverse alphabetic order to treat ubuntu always first via lsb
[1969]253my $found = 0;
[869]254foreach $d (reverse keys %$ambiguous_rel_files) {
255    $r = $ambiguous_rel_files->{$d};
256    if (-f "$r" && !-l "$r") {
[23]257        # Found one possibility.
258        # Get all distros concerned by that file
[869]259        my $tmp=pb_get_content("$r");
260        my $ptr = $distro_similar->{$d};
[423]261        pb_log(2,"amb: ".Dumper($ptr)."\n");
[24]262        $release = "unknown";
[869]263        foreach my $dd (split(/,/,$ptr)) {
[423]264            pb_log(2,"check $dd\n");
[23]265            # Try to check pattern
[869]266            if (defined $distro_match->{$dd}) {
267                pb_log(2,"cmp: $distro_match->{$dd} - vs - $tmp\n");
268                ($release) = $tmp =~ m/$distro_match->{$dd}/m;
[24]269                if ((defined $release) && ($release ne "unknown")) {
270                    $distro = $dd;
271                    $found = 1;
272                    last;
273                }
[23]274            }
275        }
[1969]276        last if ($found == 1);
[23]277    }
278}
[1969]279if ($found == 0) {
280    print STDERR "Unable to find a version in ".join(' ',keys %$ambiguous_rel_files)." (ambiguous)\n";
281    print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
282    return("unknown","unknown");
283} else {
284    return($distro,$release);
[24]285}
[1969]286}
[23]287
[1071]288=item B<pb_distro_getlsb>
[621]289
[1071]290This function returns the 5 lsb values LSB version, distribution ID, Description, release and codename.
291As entry it takes an optional parameter to specify whether the output is short or not.
292
293=cut
294
295sub pb_distro_getlsb {
296
297my $s = shift;
298pb_log(3,"Entering pb_distro_getlsb\n");
299
300my ($ambiguous_rel_files) = pb_conf_get("osrelambfile");
301my $lsbf = $ambiguous_rel_files->{"lsb"};
302
303# LSB has not been configured.
304if (not defined $lsbf) {
305    print STDERR "no lsb entry defined for osrelambfile\n";
[1678]306    confess "You modified upstream delivery and lost !\n";
[1071]307}
308
309if (-r $lsbf) {
310    my $rep = pb_get_content($lsbf);
311    # Create elementary fields
312    my ($c, $r, $d, $i, $l) = ("", "", "", "", "");
313    for my $f (split(/\n/,$rep)) {
314        pb_log(3,"Reading file part ***$f***\n");
315        $c = $f if ($f =~ /^DISTRIB_CODENAME/);
316        $c =~ s/DISTRIB_CODENAME=/Codename:\t/;
317        $r = $f if ($f =~ /^DISTRIB_RELEASE/);
318        $r =~ s/DISTRIB_RELEASE=/Release:\t/;
319        $d = $f if ($f =~ /^DISTRIB_DESCRIPTION/);
320        $d =~ s/DISTRIB_DESCRIPTION=/Description:\t/;
321        $d =~ s/"//g;
322        $i = $f if ($f =~ /^DISTRIB_ID/);
323        $i =~ s/DISTRIB_ID=/Distributor ID:\t/;
324        $l = $f if ($f =~ /^LSB_VERSION/);
325        $l =~ s/LSB_VERSION=/LSB Version:\t/;
326    }
[1177]327    my $regexp = "^[A-z ]*:[\t ]*";
328    $c =~ s/$regexp// if (defined $s);
329    $r =~ s/$regexp// if (defined $s);
330    $d =~ s/$regexp// if (defined $s);
331    $i =~ s/$regexp// if (defined $s);
332    $l =~ s/$regexp// if (defined $s);
[1071]333    return($l, $i, $d, $r, $c);
334} else {
335    print STDERR "Unable to read $lsbf file\n";
[1678]336    confess "Please report to the maintainer bruno_at_project-builder.org\n";
[1071]337}
338}
339
[1517]340# Internal function
341
[1713]342sub pb_apply_conf_proxy {
[1517]343my ($pbos) = @_;
344
345my $ftp_proxy = pb_distro_get_param($pbos,pb_conf_get_if("ftp_proxy"));
346my $http_proxy = pb_distro_get_param($pbos,pb_conf_get_if("http_proxy"));
[2234]347my $https_proxy = pb_distro_get_param($pbos,pb_conf_get_if("https_proxy"));
[1517]348
349# We do not overwrite shell settings
[2287]350$ENV{'ftp_proxy'} ||= $ftp_proxy if ((defined $ftp_proxy) && ($ftp_proxy ne ""));
351$ENV{'http_proxy'} ||= $http_proxy if ((defined $http_proxy) && ($http_proxy ne ""));
352$ENV{'https_proxy'} ||= $https_proxy if ((defined $https_proxy) && ($https_proxy ne ""));
[1517]353}
354
[621]355=item B<pb_distro_installdeps>
356
[1177]357This function install the dependencies required to build the package on a distro.
[2189]358If $forcerepo is defined then do not assume packages are alredy installed, but reinstall them
[2182]359(useful if you add a repo which contains more up to date packages that you need)
360Dependencies can be passed as the 4th parameter in which case they are not computed
[621]361
362=cut
363
364sub pb_distro_installdeps {
365
366# SPEC file
[1907]367my $f = shift;
[1177]368my $pbos = shift;
[2182]369my $forcerepo = shift;
370my $deps = shift;   # optional list of deps to install
[2324]371my $local = shift;   # optional should we install local packages or remote (for deb command is different)
[621]372
[1519]373# Protection
[1678]374confess "Missing install command for $pbos->{name}-$pbos->{version}-$pbos->{arch}" unless (defined $pbos->{install} && $pbos->{install} =~ /\w/);
[1517]375pb_apply_conf_proxy($pbos);
[2287]376pb_log(1, "ftp_proxy=$ENV{'ftp_proxy'}\n") if (defined $ENV{'ftp_proxy'});
377pb_log(1, "http_proxy=$ENV{'http_proxy'}\n")  if (defined $ENV{'http_proxy'});
378pb_log(1, "https_proxy=$ENV{'https_proxy'}\n")  if (defined $ENV{'https_proxy'});
[621]379
[1177]380# Get dependencies in the build file if not forced
[2182]381$deps = pb_distro_getdeps($f,$pbos, $forcerepo) if ((not defined $deps) || (defined $forcerepo));
[621]382pb_log(2,"deps: $deps\n");
[623]383return if ((not defined $deps) || ($deps =~ /^\s*$/));
[1505]384
385# This may not be // proof. We should test for availability of repo and sleep if not
386my $cmd = "$pbos->{'install'} $deps";
[2330]387$cmd = "$pbos->{'localinstall'} $deps" if ((defined $local) && (defined $pbos->{'localinstall'}) && ($pbos->{'localinstall'} !~ /[ ]*/));
[1595]388my $ret = pb_system($cmd, "Installing dependencies ($cmd)","mayfail");
[1515]389# Try to accomodate deficient proxies
390if ($ret != 0) {
391    pb_system($cmd, "Re-trying installing dependencies ($cmd)");
[621]392}
[1517]393# Check that all deps have been installed correctly
[2217]394# This time we don't forcerepo to avoid getting a list as a return as we have
395# already forced it previously, and this time we just want to check
396$deps = pb_distro_getdeps($f, $pbos, undef);
[1879]397confess "Some dependencies did not install ($deps)" if ((defined $deps) && ($deps =~ /\S/) && ($Global::pb_stop_on_error));
[1515]398}
[621]399
400=item B<pb_distro_getdeps>
401
402This function computes the dependencies indicated in the build file and return them as a string of packages to install
403
404=cut
405
406sub pb_distro_getdeps {
407
[1907]408my $f = shift;
[1177]409my $pbos = shift;
[2182]410my $forcerepo = shift;
[621]411
412my $regexp = "";
413my $deps = "";
414my $sep = $/;
415
416# Protection
[1177]417return("") if (not defined $pbos->{'type'});
[899]418return("") if (not defined $f);
419
[1177]420pb_log(3,"entering pb_distro_getdeps: $pbos->{'type'} - $f\n");
421if ($pbos->{'type'} eq  "rpm") {
[621]422    # In RPM this could include files, but we do not handle them atm.
423    $regexp = '^BuildRequires:(.*)$';
[1177]424} elsif ($pbos->{'type'} eq "deb") {
[621]425    $regexp = '^Build-Depends:(.*)$';
[2333]426} elsif ($pbos->{'type'} eq "apk") {
[2334]427    $regexp = '^makedepends=(.*)$';
[1177]428} elsif ($pbos->{'type'} eq "ebuild") {
[621]429    $sep = '"'.$/;
[2333]430    $regexp = '^DEPEND="(.*)"\n';
[621]431} else {
432    # No idea
[622]433    return("");
[621]434}
435pb_log(2,"regexp: $regexp\n");
436
437# Preserve separator before using the one we need
438my $oldsep = $/;
439$/ = $sep;
[1678]440open(DESC,"$f") || confess "Unable to open $f";
[621]441while (<DESC>) {
442    pb_log(4,"read: $_\n");
443    next if (! /$regexp/);
444    chomp();
[1514]445
446    my $nextline;
447    # Support multi-lines deps for .deb
448    if ($pbos->{type} eq 'deb') {
449        while ($nextline = <DESC>) {
450            last unless $nextline =~ /^\s+(.+)$/o;
451            $_ .= $1;
452        }
453    }
454
[621]455    # What we found with the regexp is the list of deps.
456    pb_log(2,"found deps: $_\n");
[681]457    s/$regexp/$1/i;
[1972]458    pb_log(4,"found deps 1: $_\n");
[698]459    # Remove conditions in the middle and at the end for deb
[2189]460    s/\([><=]+[^,]*,/,/g;
[1972]461    pb_log(4,"found deps 2: $_\n");
[2189]462    s/\([><=]+[^,]*$//g;
[1972]463    pb_log(4,"found deps 3: $_\n");
[698]464    # Same for rpm
[931]465    s/[><=]+[^,]*,/,/g;
[1972]466    pb_log(4,"found deps 4: $_\n");
[652]467    s/[><=]+.*$//g;
[1972]468    pb_log(4,"found deps 5: $_\n");
[621]469    # Improve string format (remove , and spaces at start, end and in double
[652]470    s/,/ /g;
[1972]471    pb_log(4,"found deps 6: $_\n");
[621]472    s/^\s*//;
[1972]473    pb_log(4,"found deps 7: $_\n");
474    # $ here removes the \n
[621]475    s/\s*$//;
[1972]476    pb_log(4,"found deps 8: $_\n");
[621]477    s/\s+/ /g;
[1972]478    pb_log(4,"found deps 9: $_\n");
[621]479    $deps .= " ".$_;
[1972]480    pb_log(4,"found deps end: $deps\n");
[1514]481
482    # Support multi-lines deps for .deb (fwup)
483    if (defined $nextline) {
484        $_ = $nextline;
485        redo;
486    }
[621]487}
488close(DESC);
489$/ = $oldsep;
490pb_log(2,"now deps: $deps\n");
[2182]491if (defined $forcerepo) {
492    # We want to force installation of all pkgs
493    # because a repo was setup in between, which may contains updated versions
494    pb_log(0,"Forcing installation of all packages due to previous repo setup\n");
495    return($deps);
496} else {
497    pb_log(0,"Installation of only necessary packages\n");
498    my $deps2 = pb_distro_only_deps_needed($pbos,$deps);
499    return($deps2);
[622]500}
[2182]501}
[622]502
503
504=item B<pb_distro_only_deps_needed>
505
506This function returns only the dependencies not yet installed
507
508=cut
509
510sub pb_distro_only_deps_needed {
511
[1177]512my $pbos = shift;
[1907]513my $deps = shift;
[622]514
[623]515return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
[621]516my $deps2 = "";
517# Avoid to install what is already there
[2287]518delete $ENV{'COLUMNS'};
[1505]519foreach my $p (split(/\s+/,$deps)) {
520    next if $p =~ /^\s*$/o;
[1177]521    if ($pbos->{'type'} eq  "rpm") {
[1653]522        my $rpmcmd = "rpm -q --whatprovides --quiet";
523        # whatprovides doesn't work for RH6.2
524        $rpmcmd = "rpm -q --quiet" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /6/));
525        my $res = pb_system("$rpmcmd $p","Looking for $p","mayfail");
[621]526        next if ($res eq 0);
[1524]527        pb_log(1, "INFO: missing dependency $p\n");
[1177]528    } elsif ($pbos->{'type'} eq "deb") {
[1597]529        my $res = pb_system("dpkg -L $p","Looking for $p","mayfail");
[621]530        next if ($res eq 0);
[1678]531        open(CMD,"dpkg -l $p |") or confess "Unable to run dpkg -l $p: $!";
[1516]532        my $ok = 0;
533        while (<CMD>) {
534            $ok = 1 if /^ii\s+$p/;
535        }
[1530]536        close(CMD);
[1516]537        next if $ok;
[1524]538        pb_log(1, "INFO: missing dependency $p\n");
[1177]539    } elsif ($pbos->{'type'} eq "ebuild") {
[2334]540    } elsif ($pbos->{'type'} eq "apk") {
[2337]541        my $res = pb_system("apk -e info $p","Looking for $p","mayfail");
542        next if ($res eq 0);
[2334]543        pb_log(1, "INFO: missing dependency $p\n");
[621]544    } else {
545        # Not reached
546    }
547    pb_log(2,"found deps2: $p\n");
548    $deps2 .= " $p";
549}
550
551$deps2 =~ s/^\s*//;
552pb_log(2,"now deps2: $deps2\n");
553return($deps2);
554}
555
[1132]556=item B<pb_distro_setuposrepo>
557
558This function sets up potential additional repository for the setup phase
559
560=cut
561
562sub pb_distro_setuposrepo {
563
[1177]564my $pbos = shift;
[1132]565
[2187]566return(pb_distro_setuprepo_gen_conf($pbos,pb_distro_conffile(),"osrepo"));
[1132]567}
568
[702]569=item B<pb_distro_setuprepo>
570
571This function sets up potential additional repository to the build environment
572
573=cut
574
575sub pb_distro_setuprepo {
576
[1177]577my $pbos = shift;
[702]578
[2261]579return(pb_distro_setuprepo_gen_conf($pbos,"$ENV{'PBDESTDIR'}/pbrc.yml","addrepo"));
[1132]580}
581
[1507]582# Internal
583sub pb_distro_compare_repo {
584
585my $src = shift;
586my $dest = shift;
587
[1514]588if (not -f $dest) {
[1524]589    pb_log(1, "INFO: Creating new file $dest\n");
[1514]590} elsif (-f $dest && -s $dest == 0) {
[1524]591    pb_log(1, "INFO: Overwriting empty file $dest\n");
[1507]592} elsif (-f $dest && compare("$src", $dest) == 0) {
[1524]593    pb_log(1, "INFO: Overwriting identical file $dest\n");
[1507]594} else {
595    pb_log(0, "ERROR: destination file $dest exists and is different than source $src\n");
[2222]596    pb_system("cat $dest","INFO: Dest...\n","verbose");
597    pb_system("cat $src","INFO: New...\n","verbose");
[2221]598    pb_log(1, "INFO: Returning...\n");
[2176]599    return(1);
[1507]600}
[2176]601return(0);
[1507]602}
603
[2187]604=item B<pb_distro_setuprepo_gen_conf>
[1132]605
[2187]606This function sets up in a generic way potential additional repository using conf files
[1132]607
608=cut
609
[2187]610sub pb_distro_setuprepo_gen_conf {
[1132]611
[1177]612my $pbos = shift;
[1907]613my $pbconf = shift;
614my $pbkey = shift;
[1132]615
[2182]616return undef if (not defined $pbconf);
617return undef if (not defined $pbkey);
[1132]618my ($addrepo) = pb_conf_read($pbconf,$pbkey);
[2182]619return undef if (not defined $addrepo);
[702]620
[1177]621my $param = pb_distro_get_param($pbos,$addrepo);
[2182]622return undef if ($param eq "");
[702]623
[2187]624return(pb_distro_setuprepo_gen($pbos,$param));
625}
626
627
628=item B<pb_distro_setuprepo_gen>
629
630This function sets up in a generic way potential additional repository passed as a param
631
632=cut
633
634sub pb_distro_setuprepo_gen {
635
636my $pbos = shift;
637my $param = shift;
638
639return if (not defined $param);
640
[1517]641pb_apply_conf_proxy($pbos);
642
[702]643# Loop on the list of additional repo
644foreach my $i (split(/,/,$param)) {
645
[2171]646    pb_log(1,"Adding repository defined by $i\n");
[702]647    my ($scheme, $account, $host, $port, $path) = pb_get_uri($i);
648    my $bn = basename($i);
649
650    # The repo file can be local or remote. download or copy at the right place
[2169]651    if (($scheme eq "ftp") || ($scheme =~ /http/)) {
[1507]652        pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Downloading additional repository file $i");
[702]653    } else {
[1834]654        copy($i,"$ENV{'PBTMP'}/$bn");
[702]655    }
656
657    # The repo file can be a real file or a package
[1177]658    if ($pbos->{'type'} eq "rpm") {
[702]659        if ($bn =~ /\.rpm$/) {
[721]660            my $pn = $bn;
661            $pn =~ s/\.rpm//;
[1595]662            if (pb_system("rpm -q --quiet $pn","","mayfail") != 0) {
[2177]663                pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package $bn to setup repository");
[721]664            }
665        } elsif ($bn =~ /\.repo$/) {
[1521]666            my $dirdest = "";
667            my $reponame = "";
[2289]668            # TODO: could go in pb.yml in fact
[1521]669            if ($pbos->{install} =~ /\byum\b/) {
670                $reponame="yum";
671                $dirdest = "/etc/yum.repos.d";
[2012]672            } elsif ($pbos->{install} =~ /\bdnf\b/) {
673                $reponame="dnf";
674                $dirdest = "/etc/yum.repos.d";
[1521]675            } elsif ($pbos->{install} =~ /\bzypper\b/) {
676                $reponame="zypper";
677                $dirdest = "/etc/zypp/repos.d";
678            } else {
[1678]679                confess "Unknown location for repository file for '$pbos->{install}' command";
[1521]680            }
681            my $dest = "$dirdest/$bn";
[2182]682            return undef if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
[1678]683            confess "Missing directory $dirdest ($reponame)" unless (-d $dirdest);
[2171]684            pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding $reponame repository") if (not -f "$dest");
[2178]685            # OpenSUSE does't seem to import keys automatically
686            # :-(
687            if ($pbos->{install} =~ /\bzypper\b/) {
688                my $keyfile = undef;
689                open(REPO,"$dest") || confess "Unable to open $dest";
690                while (<REPO>) {
[2181]691                    $keyfile = $_;
692                    if ($keyfile =~ /^gpgkey=/) {
693                        $keyfile =~ s/gpgkey=//;
694                        last;
[2178]695                    }
696                }
697                close(REPO);
698                if (defined $keyfile) {
699                    pb_system("wget -O $ENV{'PBTMP'}/$bn $keyfile","Downloading GPG key file $keyfile");
[2181]700                    pb_system("sudo rpm --import $ENV{'PBTMP'}/$bn","Importing GPG key file $keyfile");
[2178]701                    unlink("$ENV{'PBTMP'}/$bn");
702                }
703            }
[702]704        } elsif ($bn =~ /\.addmedia/) {
705            # URPMI repo
[721]706            # We should test that it's not already a urpmi repo
[702]707            pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository");
708        } else {
[1507]709            pb_log(0,"ERROR: Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
[702]710        }
[1177]711    } elsif ($pbos->{'type'} eq "deb") {
[1507]712        if ($bn =~ /\.sources.list$/) {
713            my $dest = "/etc/apt/sources.list.d/$bn";
[2176]714            return if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
[2188]715            pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding apt repository $dest");
716            # Check whether GPG keys for this repo are already known and if
717            # not add them
718            open(REPO,"$dest") || confess "Unable to open $dest";
719            my $debrepo;
720            while (<REPO>) {
721                if (/^deb\s/) {
722                    $debrepo = $_;
723                    chomp($debrepo);
724                    $debrepo =~ s|^deb ([^\s]+)\s([^\s]+)\s([^\s]+)|$1/dists/$2|;
725                    last;
726                }
727            }
728            close(REPO);
729            return if (not defined $debrepo);
730
731            pb_system("wget -O $ENV{'PBTMP'}/Release $debrepo/Release","Downloading $debrepo/Release");
732            pb_system("wget -O $ENV{'PBTMP'}/Release.gpg $debrepo/Release.gpg","Downloading $debrepo/Release.gpg");
733            my $signature;
734            open(SIGN,"LANGUAGE=C LANG=C gpg --verify $ENV{'PBTMP'}/Release.gpg $ENV{'PBTMP'}/Release 2>&1 |") || confess "Unable to verify GPG signature from Release.gpg\n";
735            while(<SIGN>) {
736                chomp();
737                if (/^gpg: .*key ID/) {
738                    $signature = $_;
739                    $signature =~ s/^gpg: .*key ID ([A-Z0-9]+)/$1/;
740                    #TODO: create a pbkeyserver conf var for that
741                    pb_system("gpg --recv-keys --keyserver hkp://pgp.mit.edu $signature","Importing GPG signature for $signature");
742                    $signature = undef;
743                    last;
744                }
745            }
746            close(SIGN);
747            open(SIGN,"LANGUAGE=C LANG=C gpg --verify $ENV{'PBTMP'}/Release.gpg $ENV{'PBTMP'}/Release 2>&1 |") || confess "Unable to verify GPG signature from $debrepo/Release.gpg\n";
748            while(<SIGN>) {
749                chomp();
750                if (/^gpg: Good signature/) {
751                    $signature = $_;
752                    $signature =~ s/^gpg: Good signature from "([^\"]+)"/$1/;
753                }
754            }
755            close(SIGN);
756
757            return if (not defined $signature);
758            pb_log(3, "GnuPG repo verify returned: $signature\n");
759            unlink("$ENV{'PBTMP'}/apt.sig");
760            pb_system("gpg -a --export -o $ENV{'PBTMP'}/apt.sig \'$signature\'","Exporting GnuPG signature of $signature");
761            pb_system("sudo apt-key add $ENV{'PBTMP'}/apt.sig","Adding GnuPG signature of $signature to APT key ring");
[711]762            pb_system("sudo apt-get update","Updating apt repository");
[702]763        } else {
[1507]764            pb_log(0,"ERROR: Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
[702]765        }
766    } else {
[1507]767        pb_log(0,"ERROR: Unable to deal with repository file $i on that distro ! Please report to dev team\n");
[702]768    }
769}
[2182]770return("forcerepo");
[702]771}
772
[1553]773=item B<pb_distro_to_keylist>
[1523]774
[1553]775Given a pbos object (first param) and the generic key (second param), get the list of possible keys for looking up variable for
[1523]776filter names.  The list will be sorted most-specific to least specific.
777
778=cut
779
[1553]780sub pb_distro_to_keylist ($$) {
[1523]781
782my ($pbos, $generic) = @_;
783
784foreach my $key (qw/name version arch family type os/) {
[1529]785    confess "missing pbos key $key" unless (defined $pbos->{$key});
[1523]786}
787
788my @keylist = ("$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}", "$pbos->{'name'}-$pbos->{'version'}");
789
790# Loop to include also previous minor versions
791# if configured so
[1530]792if ((defined $pbos->{'useminor'}) && ($pbos->{'useminor'} eq "true") && ($pbos->{'version'} =~ /^(\d+)\.(\d+)$/o)) {
[1523]793        my ($major, $minor) = ($1, $2);
794        while ($minor > 0) {
795                $minor--;
[1530]796                push (@keylist, "$pbos->{'name'}-${major}.$minor");
[1523]797        }
[1530]798        push (@keylist, "$pbos->{'name'}-$major");
[1523]799}
800
[1530]801push (@keylist, $pbos->{'name'}, $pbos->{'family'}, $pbos->{'type'}, $pbos->{'os'}, $generic);
[1523]802return @keylist;
803}
804
[702]805=item B<pb_distro_get_param>
806
807This function gets the parameter in the conf file from the most precise tuple up to default
808
809=cut
810
811sub pb_distro_get_param {
812
[1540]813my @param = ();
[1177]814my $pbos = shift;
[702]815
[1553]816my @keylist = pb_distro_to_keylist($pbos,"default");
[1177]817pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} for ".Dumper(@_)."\n");
818foreach my $opt (@_) {
[1523]819    my $param = "";
820    foreach my $key (@keylist) {
821        if (defined $opt->{$key}) {
822            $param = $opt->{$key};
823            last;
824        }
[1177]825    }
826    # Allow replacement of variables inside the parameter such as name, version, arch for rpmbootstrap
827    # but not shell variable which are backslashed
828    if ($param =~ /[^\\]\$/) {
829        pb_log(3,"Expanding variable on $param\n");
[2063]830        eval { $param =~ s/(\$\w+->\{\'\w+\'\})/$1/eeg };
[1177]831    }
832    push @param,$param;
[702]833}
[974]834
[1177]835pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} returns ==".Dumper(@param)."==\n");
836
[1523]837# Return one param if user only asked for one lookup, an array if not.
[1177]838my $nb = @param;
839if ($nb eq 1) {
[1523]840    return($param[0]);
[1177]841} else {
842    return(@param);
[982]843}
[1177]844}
[974]845
[1177]846=item B<pb_distro_get_context>
[702]847
[1177]848This function gets the OS context passed as parameter and return the corresponding distribution hash
[1402]849If passed undef or "" then auto-detects
[1177]850
851=cut
852
853
854sub pb_distro_get_context {
855
856my $os = shift;
857my $pbos;
858
[1402]859if ((defined $os) && ($os ne "")) {
[1177]860    my ($name,$ver,$darch) = split(/-/,$os);
861    pb_log(0,"Bad format for $os") if ((not defined $name) || (not defined $ver) || (not defined $darch)) ;
862    chomp($darch);
863    $pbos = pb_distro_init($name,$ver,$darch);
864} else {
865    $pbos = pb_distro_init();
[702]866}
[1177]867return($pbos);
868}
[749]869
[1795]870=item B<pb_distro_conf_print>
871
[2152]872This 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. If parameters are passed, restrict again the display to these values only.
[1795]873
874=cut
875
876sub pb_distro_conf_print {
877
878my $pbos = shift;
879my @keys = @_;
880
881if ($#keys == -1) {
882    pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
883    pb_log(0,"================================================\n");
[2153]884    @keys = pb_conf_get_all();
[1795]885}
886if (defined $ENV{'PBV'}) {
[2161]887    pb_log(1,"Distribution $ENV{'PBV'}\n");
888    pb_log(1,"========================\n");
[1795]889} else {
[2161]890    pb_log(1,"Local Distribution\n");
891    pb_log(1,"==================\n");
[1795]892}
893
[2153]894my %rep;
895my $i = 0;
[2155]896# Index on distro
897foreach my $r (pb_distro_get_param($pbos,pb_conf_get(@keys))) {
898    $rep{$keys[$i]} = $r if (defined $keys[$i]);
899    $i++;
900}
901$i = 0;
902# Then Index on prj to overwrite previous value if needed
[2153]903foreach my $r (pb_conf_get(@keys)) {
904    $rep{$keys[$i]} = $r->{'default'} if (defined  $r->{'default'});
905    $rep{$keys[$i]} = $r->{$ENV{'PBPROJ'}} if (defined  $r->{$ENV{'PBPROJ'}});
906    $i++;
[1795]907}
[2153]908foreach my $r (keys %rep) {
[2161]909    pb_log(1, "$r => ");
910    pb_log(0, "$rep{$r}\n");
[2153]911}
912}
[1795]913
914
[395]915=back
[23]916
[395]917=head1 WEB SITES
[23]918
[395]919The 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/>.
920
921=head1 USER MAILING LIST
922
923None exists for the moment.
924
925=head1 AUTHORS
926
927The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
928
929=head1 COPYRIGHT
930
931Project-Builder.org is distributed under the GPL v2.0 license
932described in the file C<COPYING> included with the distribution.
933
934=cut
935
936
[11]9371;
Note: See TracBrowser for help on using the repository browser.