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

Last change on this file since 2217 was 2217, checked in by Bruno Cornec, 7 years ago

Fix an error whith external repo

When using an external repo the second time we evaluate the list of deps
remaining to be installaed, we should not use the forcerepo flag as it will
make the operation fail despite a real success (seen with FOSSology and stop
at error)

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