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

Last change on this file since 2468 was 2468, checked in by Bruno Cornec, 4 years ago

Fix #180 by adding the pb_check_v function

File size: 31.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;
[2362]16use Carp qw/cluck 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);
[2427]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_installpkgs 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
[2426]71=item B<pb_distro_api>
[891]72
[2426]73This function returns the mandatory configuration file used for api
[891]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}) {
[2362]230 print STDERR "The key $d is considered as both unambiguous and ambiguous.\n";
[2361]231 confess "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";
[2361]242 confess "Please report to the maintainer bruno_at_project-builder.org\n";
[2333]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
[2360]259 my $tmp = pb_get_content("$r");
[869]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}
[2394]279if ($found == 0) {
280 #
281 # Now look at the os-release file to see if we have a std distribution description
282 #
283 foreach my $r ("/usr/lib/os-release","/etc/os-release") {
284 if (-r $r) {
285 my $tmp = pb_get_content("$r");
286 ($release) = $tmp =~ m/.*\nVERSION_ID=[\"\']*([0-9a-z\._-]+)[\"\']*\n/m;
287 ($distro) = $tmp =~ m/.*\nID=[\"\']*([0-9A-z\._-]+)[\"\']*\n/m;
288 # Remove the leap suffix if present (OpenSUSE)
289 $distro =~ s/-leap//;
290 if ((defined $release) && (defined $distro)) {
291 $found = 1;
292 last;
293 }
[2390]294 }
295 }
[2360]296}
[1969]297if ($found == 0) {
298 print STDERR "Unable to find a version in ".join(' ',keys %$ambiguous_rel_files)." (ambiguous)\n";
[2360]299 confess "Please report to the maintainer bruno_at_project-builder.org\n";
[1969]300} else {
301 return($distro,$release);
[24]302}
[1969]303}
[23]304
[1071]305=item B<pb_distro_getlsb>
[621]306
[1071]307This function returns the 5 lsb values LSB version, distribution ID, Description, release and codename.
308As entry it takes an optional parameter to specify whether the output is short or not.
309
310=cut
311
312sub pb_distro_getlsb {
313
314my $s = shift;
315pb_log(3,"Entering pb_distro_getlsb\n");
316
317my ($ambiguous_rel_files) = pb_conf_get("osrelambfile");
318my $lsbf = $ambiguous_rel_files->{"lsb"};
319
320# LSB has not been configured.
321if (not defined $lsbf) {
322 print STDERR "no lsb entry defined for osrelambfile\n";
[1678]323 confess "You modified upstream delivery and lost !\n";
[1071]324}
325
326if (-r $lsbf) {
327 my $rep = pb_get_content($lsbf);
328 # Create elementary fields
329 my ($c, $r, $d, $i, $l) = ("", "", "", "", "");
330 for my $f (split(/\n/,$rep)) {
331 pb_log(3,"Reading file part ***$f***\n");
332 $c = $f if ($f =~ /^DISTRIB_CODENAME/);
333 $c =~ s/DISTRIB_CODENAME=/Codename:\t/;
334 $r = $f if ($f =~ /^DISTRIB_RELEASE/);
335 $r =~ s/DISTRIB_RELEASE=/Release:\t/;
336 $d = $f if ($f =~ /^DISTRIB_DESCRIPTION/);
337 $d =~ s/DISTRIB_DESCRIPTION=/Description:\t/;
338 $d =~ s/"//g;
339 $i = $f if ($f =~ /^DISTRIB_ID/);
340 $i =~ s/DISTRIB_ID=/Distributor ID:\t/;
341 $l = $f if ($f =~ /^LSB_VERSION/);
342 $l =~ s/LSB_VERSION=/LSB Version:\t/;
343 }
[1177]344 my $regexp = "^[A-z ]*:[\t ]*";
345 $c =~ s/$regexp// if (defined $s);
346 $r =~ s/$regexp// if (defined $s);
347 $d =~ s/$regexp// if (defined $s);
348 $i =~ s/$regexp// if (defined $s);
349 $l =~ s/$regexp// if (defined $s);
[1071]350 return($l, $i, $d, $r, $c);
351} else {
352 print STDERR "Unable to read $lsbf file\n";
[1678]353 confess "Please report to the maintainer bruno_at_project-builder.org\n";
[1071]354}
355}
356
[1517]357# Internal function
358
[1713]359sub pb_apply_conf_proxy {
[1517]360my ($pbos) = @_;
361
362my $ftp_proxy = pb_distro_get_param($pbos,pb_conf_get_if("ftp_proxy"));
363my $http_proxy = pb_distro_get_param($pbos,pb_conf_get_if("http_proxy"));
[2234]364my $https_proxy = pb_distro_get_param($pbos,pb_conf_get_if("https_proxy"));
[1517]365
366# We do not overwrite shell settings
[2287]367$ENV{'ftp_proxy'} ||= $ftp_proxy if ((defined $ftp_proxy) && ($ftp_proxy ne ""));
368$ENV{'http_proxy'} ||= $http_proxy if ((defined $http_proxy) && ($http_proxy ne ""));
369$ENV{'https_proxy'} ||= $https_proxy if ((defined $https_proxy) && ($https_proxy ne ""));
[1517]370}
371
[2427]372=item B<pb_distro_installpkgs>
373
374This function install the packages passed as parameters on a distribution.
375
376=cut
377
378sub pb_distro_installpkgs {
379
380my $pbos = shift;
381my $pkgs = shift; # list of pkgs to install
382my $local = shift; # optional should we install local packages or remote (for deb command is different)
383
384# Protection
385confess "Missing install command for $pbos->{name}-$pbos->{version}-$pbos->{arch}" unless (defined $pbos->{install} && $pbos->{install} =~ /\w/);
386pb_apply_conf_proxy($pbos);
387pb_log(1, "ftp_proxy=$ENV{'ftp_proxy'}\n") if (defined $ENV{'ftp_proxy'});
388pb_log(1, "http_proxy=$ENV{'http_proxy'}\n") if (defined $ENV{'http_proxy'});
389pb_log(1, "https_proxy=$ENV{'https_proxy'}\n") if (defined $ENV{'https_proxy'});
390
391# This may not be // proof. We should test for availability of repo and sleep if not
392my $cmd = "$pbos->{'install'} $pkgs";
393$cmd = "$pbos->{'localinstall'} $pkgs" if ((defined $local) && (defined $pbos->{'localinstall'}) && ($pbos->{'localinstall'} !~ /[ ]*/));
394my $ret = pb_system($cmd, "Installing packages ($cmd)","mayfail");
395# Try to accomodate deficient proxies
396if ($ret != 0) {
397 $ret = pb_system($cmd, "Re-trying installing packages ($cmd)");
398}
399confess "Some packages did not install" if (($ret != 0) && ($Global::pb_stop_on_error));
400}
401
402
[621]403=item B<pb_distro_installdeps>
404
[1177]405This function install the dependencies required to build the package on a distro.
[2189]406If $forcerepo is defined then do not assume packages are alredy installed, but reinstall them
[2182]407(useful if you add a repo which contains more up to date packages that you need)
408Dependencies can be passed as the 4th parameter in which case they are not computed
[621]409
410=cut
411
412sub pb_distro_installdeps {
413
414# SPEC file
[1907]415my $f = shift;
[1177]416my $pbos = shift;
[2182]417my $forcerepo = shift;
418my $deps = shift; # optional list of deps to install
[2324]419my $local = shift; # optional should we install local packages or remote (for deb command is different)
[621]420
[1177]421# Get dependencies in the build file if not forced
[2182]422$deps = pb_distro_getdeps($f,$pbos, $forcerepo) if ((not defined $deps) || (defined $forcerepo));
[621]423pb_log(2,"deps: $deps\n");
[623]424return if ((not defined $deps) || ($deps =~ /^\s*$/));
[1505]425
[2427]426pb_distro_installpkgs($pbos,$deps,$local);
[1517]427# Check that all deps have been installed correctly
[2217]428# This time we don't forcerepo to avoid getting a list as a return as we have
429# already forced it previously, and this time we just want to check
430$deps = pb_distro_getdeps($f, $pbos, undef);
[1879]431confess "Some dependencies did not install ($deps)" if ((defined $deps) && ($deps =~ /\S/) && ($Global::pb_stop_on_error));
[1515]432}
[621]433
434=item B<pb_distro_getdeps>
435
436This function computes the dependencies indicated in the build file and return them as a string of packages to install
437
438=cut
439
440sub pb_distro_getdeps {
441
[1907]442my $f = shift;
[1177]443my $pbos = shift;
[2182]444my $forcerepo = shift;
[621]445
446my $regexp = "";
447my $deps = "";
448my $sep = $/;
449
450# Protection
[1177]451return("") if (not defined $pbos->{'type'});
[899]452return("") if (not defined $f);
453
[1177]454pb_log(3,"entering pb_distro_getdeps: $pbos->{'type'} - $f\n");
455if ($pbos->{'type'} eq "rpm") {
[621]456 # In RPM this could include files, but we do not handle them atm.
457 $regexp = '^BuildRequires:(.*)$';
[1177]458} elsif ($pbos->{'type'} eq "deb") {
[621]459 $regexp = '^Build-Depends:(.*)$';
[2333]460} elsif ($pbos->{'type'} eq "apk") {
[2334]461 $regexp = '^makedepends=(.*)$';
[1177]462} elsif ($pbos->{'type'} eq "ebuild") {
[621]463 $sep = '"'.$/;
[2333]464 $regexp = '^DEPEND="(.*)"\n';
[621]465} else {
466 # No idea
[622]467 return("");
[621]468}
469pb_log(2,"regexp: $regexp\n");
470
471# Preserve separator before using the one we need
472my $oldsep = $/;
473$/ = $sep;
[2434]474open(DESC,"$f") || (cluck "Unable to open $f" && return(""));
[621]475while (<DESC>) {
476 pb_log(4,"read: $_\n");
477 next if (! /$regexp/);
478 chomp();
[1514]479
480 my $nextline;
481 # Support multi-lines deps for .deb
482 if ($pbos->{type} eq 'deb') {
483 while ($nextline = <DESC>) {
484 last unless $nextline =~ /^\s+(.+)$/o;
485 $_ .= $1;
486 }
487 }
488
[621]489 # What we found with the regexp is the list of deps.
490 pb_log(2,"found deps: $_\n");
[681]491 s/$regexp/$1/i;
[1972]492 pb_log(4,"found deps 1: $_\n");
[698]493 # Remove conditions in the middle and at the end for deb
[2189]494 s/\([><=]+[^,]*,/,/g;
[1972]495 pb_log(4,"found deps 2: $_\n");
[2189]496 s/\([><=]+[^,]*$//g;
[1972]497 pb_log(4,"found deps 3: $_\n");
[698]498 # Same for rpm
[931]499 s/[><=]+[^,]*,/,/g;
[1972]500 pb_log(4,"found deps 4: $_\n");
[652]501 s/[><=]+.*$//g;
[1972]502 pb_log(4,"found deps 5: $_\n");
[621]503 # Improve string format (remove , and spaces at start, end and in double
[652]504 s/,/ /g;
[1972]505 pb_log(4,"found deps 6: $_\n");
[621]506 s/^\s*//;
[1972]507 pb_log(4,"found deps 7: $_\n");
508 # $ here removes the \n
[621]509 s/\s*$//;
[1972]510 pb_log(4,"found deps 8: $_\n");
[621]511 s/\s+/ /g;
[1972]512 pb_log(4,"found deps 9: $_\n");
[621]513 $deps .= " ".$_;
[1972]514 pb_log(4,"found deps end: $deps\n");
[1514]515
516 # Support multi-lines deps for .deb (fwup)
517 if (defined $nextline) {
518 $_ = $nextline;
519 redo;
520 }
[621]521}
522close(DESC);
523$/ = $oldsep;
524pb_log(2,"now deps: $deps\n");
[2182]525if (defined $forcerepo) {
526 # We want to force installation of all pkgs
527 # because a repo was setup in between, which may contains updated versions
528 pb_log(0,"Forcing installation of all packages due to previous repo setup\n");
529 return($deps);
530} else {
531 pb_log(0,"Installation of only necessary packages\n");
532 my $deps2 = pb_distro_only_deps_needed($pbos,$deps);
533 return($deps2);
[622]534}
[2182]535}
[622]536
537
538=item B<pb_distro_only_deps_needed>
539
540This function returns only the dependencies not yet installed
541
542=cut
543
544sub pb_distro_only_deps_needed {
545
[1177]546my $pbos = shift;
[1907]547my $deps = shift;
[622]548
[623]549return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
[621]550my $deps2 = "";
551# Avoid to install what is already there
[2287]552delete $ENV{'COLUMNS'};
[1505]553foreach my $p (split(/\s+/,$deps)) {
554 next if $p =~ /^\s*$/o;
[1177]555 if ($pbos->{'type'} eq "rpm") {
[1653]556 my $rpmcmd = "rpm -q --whatprovides --quiet";
557 # whatprovides doesn't work for RH6.2
558 $rpmcmd = "rpm -q --quiet" if (($pbos->{'name'} eq "redhat") && ($pbos->{'version'} =~ /6/));
559 my $res = pb_system("$rpmcmd $p","Looking for $p","mayfail");
[621]560 next if ($res eq 0);
[1524]561 pb_log(1, "INFO: missing dependency $p\n");
[1177]562 } elsif ($pbos->{'type'} eq "deb") {
[1597]563 my $res = pb_system("dpkg -L $p","Looking for $p","mayfail");
[621]564 next if ($res eq 0);
[2434]565 open(CMD,"dpkg -l $p |") || (cluck "Unable to run dpkg -l $p: $!" && next);
[1516]566 my $ok = 0;
567 while (<CMD>) {
568 $ok = 1 if /^ii\s+$p/;
569 }
[1530]570 close(CMD);
[1516]571 next if $ok;
[1524]572 pb_log(1, "INFO: missing dependency $p\n");
[1177]573 } elsif ($pbos->{'type'} eq "ebuild") {
[2334]574 } elsif ($pbos->{'type'} eq "apk") {
[2337]575 my $res = pb_system("apk -e info $p","Looking for $p","mayfail");
576 next if ($res eq 0);
[2334]577 pb_log(1, "INFO: missing dependency $p\n");
[621]578 } else {
579 # Not reached
580 }
581 pb_log(2,"found deps2: $p\n");
582 $deps2 .= " $p";
583}
584
585$deps2 =~ s/^\s*//;
586pb_log(2,"now deps2: $deps2\n");
587return($deps2);
588}
589
[2410]590# Internal
591sub pb_distro_compare_repo {
592
593my $src = shift;
594my $dest = shift;
595
596if (not -f $dest) {
597 pb_log(1, "INFO: Creating new file $dest\n");
598} elsif (-f $dest && -s $dest == 0) {
599 pb_log(1, "INFO: Overwriting empty file $dest\n");
600} elsif (-f $dest && compare("$src", $dest) == 0) {
601 pb_log(1, "INFO: Overwriting identical file $dest\n");
602} else {
603 pb_log(0, "ERROR: destination file $dest exists and is different than source $src\n");
604 pb_system("cat $dest","INFO: Dest...\n","verbose");
605 pb_system("cat $src","INFO: New...\n","verbose");
606 pb_log(1, "INFO: Returning...\n");
607 return(1);
608}
609return(0);
610}
611
612
[1132]613=item B<pb_distro_setuposrepo>
614
615This function sets up potential additional repository for the setup phase
616
617=cut
618
619sub pb_distro_setuposrepo {
620
[1177]621my $pbos = shift;
[1132]622
[2410]623pb_log(3, "INFO: Adding osrepo from config file\n");
[2426]624my %h;
625my $h = \%h;
626# Adds conf file for availability of conf elements either from the local build env or from a VE/VM/RM in which the conf file has been passed
627$h = pb_conf_cache(pb_distro_conffile(),$h);
628my ($osrepo) = pb_conf_get_in_hash_if($h,"osrepo");
[2429]629my $repo = pb_distro_get_param($pbos,$osrepo);
630return(pb_distro_setuprepo_gen($pbos,$repo));
[1132]631}
632
[702]633=item B<pb_distro_setuprepo>
634
[2426]635This function sets up potential additional repository to the build/install/test environment
[2465]636If done, it returns forcerepo if a repo was added, if not undef
[702]637
638=cut
639
640sub pb_distro_setuprepo {
641
[1177]642my $pbos = shift;
[2426]643my $repotype = shift;
[702]644
[2426]645my %h;
646my $h = \%h;
647# Adds conf file for availability of conf elements either from the local build env or from a VE/VM/RM in which the conf file has been passed
648$h = pb_conf_cache("$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.yml",$h) if ((defined $ENV{'PBROOTDIR'}) && (-f "$ENV{'PBROOTDIR'}/$ENV{'PBPROJ'}.yml"));
649$h = pb_conf_cache("$ENV{'PBDESTDIR'}/pbrc.yml",$h) if ((defined $ENV{'PBDESTDIR'}) && (-f "$ENV{'PBDESTDIR'}/pbrc.yml"));
[1132]650
[2462]651my ($addrepo) = pb_conf_get_in_hash_if($h,"add".$repotype."repo");
652my $repo = pb_distro_get_param($pbos,$addrepo);
[2465]653# If no repo then set it up as undef so the return value is correct from pb_distro_setuprepo_gen
654$repo = undef if ($repo eq "");
[2426]655if ($repotype =~ /install/) {
656 # Give a probable default if nothing is provided to avoid overloading conf files
657 #
[2437]658 if ((not defined $addrepo) || (not defined $repo)) {
[2426]659 my ($pbrepo) = pb_conf_get_in_hash_if($h,"pbrepo");
[2465]660 if (not defined $pbrepo) {
661 cluck("No pbrepo parameter defined in your project configuration file, please fix !");
662 return(undef);
663 }
[2426]664 my $url = "$pbrepo->{$ENV{'PBPROJ'}}";
665 my ($testver,$delivery) = pb_conf_get_in_hash_if($h,"testver","delivery");
666 $delivery->{$ENV{'PBPROJ'}} = "" if (not defined $delivery->{$ENV{'PBPROJ'}});
667 $url .= "/$delivery->{$ENV{'PBPROJ'}}/";
668 my $repotag = "";
669 $repotag = "-$delivery->{$ENV{'PBPROJ'}}" if ($delivery->{$ENV{'PBPROJ'}} ne "");
670 $url .= "/$pbos->{'name'}/$pbos->{'version'}/$pbos->{'arch'}/$ENV{PBPROJ}$repotag.";
671 my $ext = "";
672 if ($pbos->{'type'} eq "rpm") {
673 $ext = "repo";
674 if ($pbos->{'family'} eq "md") {
675 $ext = "addmedia";
676 }
677 } elsif ($pbos->{'type'} eq "deb") {
678 $ext = ".sources.list";
679 }
[2462]680 $repo = $url.$ext;
[2426]681 }
[2187]682}
[2468]683pb_log(1, "INFO: Adding $repo from config file for $repotype step\n") if (defined $repo);
[2462]684return(pb_distro_setuprepo_gen($pbos,$repo));
[2426]685}
[2187]686
687=item B<pb_distro_setuprepo_gen>
688
[2465]689This functionthe sets up in a generic way potential additional repository passed as a param
690It returns forcerepo if one was added, else undef
[2187]691
692=cut
693
694sub pb_distro_setuprepo_gen {
695
696my $pbos = shift;
697my $param = shift;
698
[2427]699return(undef) if (not defined $param);
[2187]700
[1517]701pb_apply_conf_proxy($pbos);
702
[702]703# Loop on the list of additional repo
704foreach my $i (split(/,/,$param)) {
705
[2171]706 pb_log(1,"Adding repository defined by $i\n");
[702]707 my ($scheme, $account, $host, $port, $path) = pb_get_uri($i);
708 my $bn = basename($i);
709
710 # The repo file can be local or remote. download or copy at the right place
[2169]711 if (($scheme eq "ftp") || ($scheme =~ /http/)) {
[1507]712 pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Downloading additional repository file $i");
[702]713 } else {
[1834]714 copy($i,"$ENV{'PBTMP'}/$bn");
[702]715 }
716
717 # The repo file can be a real file or a package
[1177]718 if ($pbos->{'type'} eq "rpm") {
[702]719 if ($bn =~ /\.rpm$/) {
[721]720 my $pn = $bn;
721 $pn =~ s/\.rpm//;
[1595]722 if (pb_system("rpm -q --quiet $pn","","mayfail") != 0) {
[2177]723 pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package $bn to setup repository");
[721]724 }
725 } elsif ($bn =~ /\.repo$/) {
[1521]726 my $dirdest = "";
727 my $reponame = "";
[2289]728 # TODO: could go in pb.yml in fact
[1521]729 if ($pbos->{install} =~ /\byum\b/) {
730 $reponame="yum";
731 $dirdest = "/etc/yum.repos.d";
[2012]732 } elsif ($pbos->{install} =~ /\bdnf\b/) {
733 $reponame="dnf";
734 $dirdest = "/etc/yum.repos.d";
[1521]735 } elsif ($pbos->{install} =~ /\bzypper\b/) {
736 $reponame="zypper";
737 $dirdest = "/etc/zypp/repos.d";
738 } else {
[2362]739 cluck "Unknown location for repository file for '$pbos->{install}' command";
740 next;
[1521]741 }
742 my $dest = "$dirdest/$bn";
[2427]743 return(undef) if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
[2362]744 if (! -d $dirdest) {
745 cluck "Missing directory $dirdest ($reponame)";
[2427]746 return(undef);
[2362]747 }
[2171]748 pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding $reponame repository") if (not -f "$dest");
[2178]749 # OpenSUSE does't seem to import keys automatically
750 # :-(
751 if ($pbos->{install} =~ /\bzypper\b/) {
752 my $keyfile = undef;
[2434]753 open(REPO,"$dest") || (cluck "Unable to open $dest" && next);
[2178]754 while (<REPO>) {
[2181]755 $keyfile = $_;
756 if ($keyfile =~ /^gpgkey=/) {
757 $keyfile =~ s/gpgkey=//;
758 last;
[2178]759 }
760 }
761 close(REPO);
762 if (defined $keyfile) {
763 pb_system("wget -O $ENV{'PBTMP'}/$bn $keyfile","Downloading GPG key file $keyfile");
[2181]764 pb_system("sudo rpm --import $ENV{'PBTMP'}/$bn","Importing GPG key file $keyfile");
[2178]765 unlink("$ENV{'PBTMP'}/$bn");
766 }
767 }
[702]768 } elsif ($bn =~ /\.addmedia/) {
769 # URPMI repo
[2427]770 # We should test that it's not an already setup urpmi repo
[2434]771 open(URPMI,"/etc/urpmi/urpmi.cfg") || (cluck "Unable to open /etc/urpmi/urpmi.cfg" && next);
[2427]772 my $found = 0;
773 my $entry = $bn;
774 $entry =~ s/.addmedia$//;
775 while (<URPMI>) {
776 $found = 1 if ($_ =~ /^$entry /);
777 }
778 pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository") if ($found == 0);
779 pb_log(0,"INFO urpmi $bn already set up\n") if ($found == 1);
[702]780 } else {
[1507]781 pb_log(0,"ERROR: Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
[702]782 }
[1177]783 } elsif ($pbos->{'type'} eq "deb") {
[1507]784 if ($bn =~ /\.sources.list$/) {
785 my $dest = "/etc/apt/sources.list.d/$bn";
[2427]786 return(undef) if (pb_distro_compare_repo("$ENV{'PBTMP'}/$bn",$dest) == 1);
[2188]787 pb_system("sudo mv $ENV{'PBTMP'}/$bn $dest","Adding apt repository $dest");
788 # Check whether GPG keys for this repo are already known and if
789 # not add them
[2434]790 open(REPO,"$dest") || (cluck "Unable to open $dest" && next);
[2188]791 my $debrepo;
792 while (<REPO>) {
793 if (/^deb\s/) {
794 $debrepo = $_;
795 chomp($debrepo);
796 $debrepo =~ s|^deb ([^\s]+)\s([^\s]+)\s([^\s]+)|$1/dists/$2|;
797 last;
798 }
799 }
800 close(REPO);
[2427]801 return(undef) if (not defined $debrepo);
[2188]802
803 pb_system("wget -O $ENV{'PBTMP'}/Release $debrepo/Release","Downloading $debrepo/Release");
804 pb_system("wget -O $ENV{'PBTMP'}/Release.gpg $debrepo/Release.gpg","Downloading $debrepo/Release.gpg");
[2410]805 my $signature = undef;
806 my ($pbgpgserver) = pb_conf_get("pbgpgserver");
807 confess "Unable to find a GPG server in configuration, please define pbgpgserver" if (not defined $pbgpgserver);
808 my $keyserver = $pbgpgserver->{$ENV{'PBPROJ'}};
809 $keyserver = $pbgpgserver->{'default'} if (not defined $keyserver);
810 confess "Unable to find a GPG server in configuration, please define correctly pbgpgserver" if (not defined $keyserver);
[2434]811 open(SIGN,"LANGUAGE=C LANG=C gpg --verify --keyid-format=long $ENV{'PBTMP'}/Release.gpg $ENV{'PBTMP'}/Release 2>&1 |") || (cluck "Unable to verify GPG signature from Release.gpg\n" && next);
[2188]812 while(<SIGN>) {
813 chomp();
[2410]814 if (/^gpg: .*key /) {
[2188]815 $signature = $_;
[2410]816 $signature =~ s/^gpg: .*key [ID ]*([A-Z0-9]+)/$1/;
817 pb_system("gpg --recv-keys --keyserver $keyserver $signature","Importing GPG signature for $signature");
[2188]818 last;
819 }
820 }
821 close(SIGN);
[2427]822 return(undef) if (not defined $signature);
[2188]823
824 pb_log(3, "GnuPG repo verify returned: $signature\n");
825 pb_system("gpg -a --export -o $ENV{'PBTMP'}/apt.sig \'$signature\'","Exporting GnuPG signature of $signature");
826 pb_system("sudo apt-key add $ENV{'PBTMP'}/apt.sig","Adding GnuPG signature of $signature to APT key ring");
[711]827 pb_system("sudo apt-get update","Updating apt repository");
[702]828 } else {
[1507]829 pb_log(0,"ERROR: Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
[702]830 }
831 } else {
[1507]832 pb_log(0,"ERROR: Unable to deal with repository file $i on that distro ! Please report to dev team\n");
[702]833 }
834}
[2182]835return("forcerepo");
[702]836}
837
[1553]838=item B<pb_distro_to_keylist>
[1523]839
[1553]840Given a pbos object (first param) and the generic key (second param), get the list of possible keys for looking up variable for
[1523]841filter names. The list will be sorted most-specific to least specific.
842
843=cut
844
[1553]845sub pb_distro_to_keylist ($$) {
[1523]846
847my ($pbos, $generic) = @_;
848
849foreach my $key (qw/name version arch family type os/) {
[1529]850 confess "missing pbos key $key" unless (defined $pbos->{$key});
[1523]851}
852
853my @keylist = ("$pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'}", "$pbos->{'name'}-$pbos->{'version'}");
854
855# Loop to include also previous minor versions
856# if configured so
[1530]857if ((defined $pbos->{'useminor'}) && ($pbos->{'useminor'} eq "true") && ($pbos->{'version'} =~ /^(\d+)\.(\d+)$/o)) {
[1523]858 my ($major, $minor) = ($1, $2);
859 while ($minor > 0) {
860 $minor--;
[1530]861 push (@keylist, "$pbos->{'name'}-${major}.$minor");
[1523]862 }
[1530]863 push (@keylist, "$pbos->{'name'}-$major");
[1523]864}
865
[1530]866push (@keylist, $pbos->{'name'}, $pbos->{'family'}, $pbos->{'type'}, $pbos->{'os'}, $generic);
[1523]867return @keylist;
868}
869
[702]870=item B<pb_distro_get_param>
871
872This function gets the parameter in the conf file from the most precise tuple up to default
873
874=cut
875
876sub pb_distro_get_param {
877
[1540]878my @param = ();
[1177]879my $pbos = shift;
[702]880
[1553]881my @keylist = pb_distro_to_keylist($pbos,"default");
[1177]882pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} for ".Dumper(@_)."\n");
883foreach my $opt (@_) {
[1523]884 my $param = "";
885 foreach my $key (@keylist) {
886 if (defined $opt->{$key}) {
887 $param = $opt->{$key};
888 last;
889 }
[1177]890 }
891 # Allow replacement of variables inside the parameter such as name, version, arch for rpmbootstrap
892 # but not shell variable which are backslashed
893 if ($param =~ /[^\\]\$/) {
894 pb_log(3,"Expanding variable on $param\n");
[2063]895 eval { $param =~ s/(\$\w+->\{\'\w+\'\})/$1/eeg };
[1177]896 }
897 push @param,$param;
[702]898}
[974]899
[1177]900pb_log(2,"DEBUG: pb_distro_get_param on $pbos->{'name'}-$pbos->{'version'}-$pbos->{'arch'} returns ==".Dumper(@param)."==\n");
901
[1523]902# Return one param if user only asked for one lookup, an array if not.
[1177]903my $nb = @param;
904if ($nb eq 1) {
[1523]905 return($param[0]);
[1177]906} else {
907 return(@param);
[982]908}
[1177]909}
[974]910
[1177]911=item B<pb_distro_get_context>
[702]912
[1177]913This function gets the OS context passed as parameter and return the corresponding distribution hash
[1402]914If passed undef or "" then auto-detects
[1177]915
916=cut
917
918
919sub pb_distro_get_context {
920
921my $os = shift;
922my $pbos;
923
[1402]924if ((defined $os) && ($os ne "")) {
[1177]925 my ($name,$ver,$darch) = split(/-/,$os);
926 pb_log(0,"Bad format for $os") if ((not defined $name) || (not defined $ver) || (not defined $darch)) ;
927 chomp($darch);
928 $pbos = pb_distro_init($name,$ver,$darch);
929} else {
930 $pbos = pb_distro_init();
[702]931}
[1177]932return($pbos);
933}
[749]934
[1795]935=item B<pb_distro_conf_print>
936
[2152]937This 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]938
939=cut
940
941sub pb_distro_conf_print {
942
943my $pbos = shift;
944my @keys = @_;
[2350]945my $all = 0;
[1795]946
947if ($#keys == -1) {
948 pb_log(0,"Full pb configuration for project $ENV{'PBPROJ'}\n");
949 pb_log(0,"================================================\n");
[2153]950 @keys = pb_conf_get_all();
[2350]951 $all = 1;
[1795]952}
953if (defined $ENV{'PBV'}) {
[2161]954 pb_log(1,"Distribution $ENV{'PBV'}\n");
955 pb_log(1,"========================\n");
[1795]956} else {
[2161]957 pb_log(1,"Local Distribution\n");
958 pb_log(1,"==================\n");
[1795]959}
960
[2153]961my %rep;
962my $i = 0;
[2155]963# Index on distro
964foreach my $r (pb_distro_get_param($pbos,pb_conf_get(@keys))) {
965 $rep{$keys[$i]} = $r if (defined $keys[$i]);
966 $i++;
967}
968$i = 0;
969# Then Index on prj to overwrite previous value if needed
[2153]970foreach my $r (pb_conf_get(@keys)) {
971 $rep{$keys[$i]} = $r->{'default'} if (defined $r->{'default'});
972 $rep{$keys[$i]} = $r->{$ENV{'PBPROJ'}} if (defined $r->{$ENV{'PBPROJ'}});
973 $i++;
[1795]974}
[2153]975foreach my $r (keys %rep) {
[2161]976 pb_log(1, "$r => ");
[2350]977 pb_log(0, "$rep{$r}\n") if ($all == 0);
978 pb_log(0, "$r = $rep{$r}\n") if ($all == 1);
[2153]979}
980}
[1795]981
982
[395]983=back
[23]984
[395]985=head1 WEB SITES
[23]986
[395]987The 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/>.
988
989=head1 USER MAILING LIST
990
991None exists for the moment.
992
993=head1 AUTHORS
994
995The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
996
997=head1 COPYRIGHT
998
999Project-Builder.org is distributed under the GPL v2.0 license
1000described in the file C<COPYING> included with the distribution.
1001
1002=cut
1003
1004
[11]10051;
Note: See TracBrowser for help on using the repository browser.