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

Last change on this file since 1156 was 1156, checked in by Bruno Cornec, 13 years ago
  • Adds a global variable REVISION for version management
  • Do not return in pb_send2target if pb file not available in order to shutdown VM in all cases
  • Code and test sbx2setupvm
  • setup_v now needs a fake pb_version_init fct added at the end of the script
  • Fix pbdistrocheck install comand printing
  • Fix mandralinux old distro build in pb.conf (Note only non symlink release files are important)
File size: 17.3 KB
RevLine 
[11]1#!/usr/bin/perl -w
[2]2#
[11]3# Creates common environment for distributions
[2]4#
5# $Id$
6#
7
[329]8package ProjectBuilder::Distribution;
9
[11]10use strict;
[423]11use Data::Dumper;
[1148]12use ProjectBuilder::Version;
[395]13use ProjectBuilder::Base;
[702]14use ProjectBuilder::Conf;
15use File::Basename;
16use File::Copy;
[2]17
[1148]18# Global vars
[329]19# Inherit from the "Exporter" module which handles exporting functions.
20
[1156]21use vars qw($VERSION $REVISION @ISA @EXPORT);
[329]22use Exporter;
23
24# Export, by default, all the functions into the namespace of
25# any code which uses this module.
26
27our @ISA = qw(Exporter);
[1132]28our @EXPORT = qw(pb_distro_conffile pb_distro_init pb_distro_get pb_distro_getlsb pb_distro_installdeps pb_distro_getdeps pb_distro_only_deps_needed pb_distro_setuprepo pb_distro_setuposrepo pb_distro_get_param);
[1156]29($VERSION,$REVISION) = pb_version_init();
[329]30
[391]31=pod
32
33=head1 NAME
34
35ProjectBuilder::Distribution, part of the project-builder.org - module dealing with distribution detection
36
37=head1 DESCRIPTION
38
39This modules provides functions to allow detection of Linux distributions, and giving back some attributes concerning them.
40
41=head1 SYNOPSIS
42
43 use ProjectBuilder::Distribution;
44
45 #
46 # Return information on the running distro
47 #
[1111]48 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $dos, $pbupd, $pbins, $arch) = pb_distro_init();
49 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $pbins, $arch)."\n";
[391]50 #
51 # Return information on the requested distro
52 #
[1111]53 my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $pbins, $arch) = pb_distro_init("ubuntu","7.10","x86_64");
54 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $pbins, $arch)."\n";
[391]55 #
56 # Return information on the running distro
57 #
[622]58 my ($ddir,$dver) = pb_distro_get();
[1111]59 my ($ddir, $dver, $dfam, $dtype, $dos, $pbsuf, $pbupd, $pbins, $arch) = pb_distro_init($ddir,$dver);
60 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $pbins, $arch)."\n";
[391]61
62=head1 USAGE
63
64=over 4
65
[891]66=item B<pb_distro_conffile>
67
68This function returns the mandatory configuration file used for distribution/OS detection
69
70=cut
71
72sub pb_distro_conffile {
73
74return("CCCC/pb.conf");
75}
76
77
[756]78=item B<pb_distro_init>
[395]79
[1111]80This function returns a list of 8 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 8 fields may be "unknown" in case the function was unable to recognize on which distribution it is running.
[391]81
[756]82As an example, Ubuntu and Debian are in the same "du" family. As well as RedHat, RHEL, CentOS, fedora are on the same "rh" family.
83Mandriva, Open SuSE and Fedora have all the same "rpm" type of build system. Ubuntu ad Debian have the same "deb" type of build system.
84And "fc" is the extension generated for all Fedora packages (Version will be added by pb).
[869]85All these information are stored in an external configuration file typically at /etc/pb/pb.conf
[391]86
[756]87When 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]88
[756]89Cf: http://linuxmafia.com/faq/Admin/release-files.html
90Ideas taken from http://search.cpan.org/~kerberus/Linux-Distribution-0.14/lib/Linux/Distribution.pm
91
[391]92=cut
93
[756]94
[74]95sub pb_distro_init {
[2]96
[23]97my $ddir = shift || undef;
98my $dver = shift || undef;
[11]99my $dfam = "unknown";
100my $dtype = "unknown";
[1064]101my $dos = "unknown";
[11]102my $dsuf = "unknown";
[620]103my $dupd = "unknown";
[1111]104my $dins = "unknown";
[867]105my $darch = shift || undef;
106my $dnover = "false";
107my $drmdot = "false";
[2]108
[869]109# Adds conf file for distribution description
110# the location of the conf file is finalyzed at install time
111# depending whether we deal with package install or tar file install
[891]112pb_conf_add(pb_distro_conffile());
[869]113
[11]114# If we don't know which distribution we're on, then guess it
[622]115($ddir,$dver) = pb_distro_get() if ((not defined $ddir) || (not defined $dver));
[2]116
[757]117# Initialize arch
[867]118$darch=pb_get_arch() if (not defined $darch);
[757]119
[1111]120my ($osfamily,$ostype,$osupd,$osins,$ossuffix,$osnover,$osremovedotinver,$os) = pb_conf_get("osfamily","ostype","osupd","osins","ossuffix","osnover","osremovedotinver","os");
[620]121
[969]122# Dig into the tuple to find the best answer
[867]123$dfam = pb_distro_get_param($ddir,$dver,$darch,$osfamily);
[1067]124$dtype = pb_distro_get_param($ddir,$dver,$darch,$ostype,$dfam);
125$dos = pb_distro_get_param($ddir,$dver,$darch,$os,$dfam,$dtype);
126$dupd = pb_distro_get_param($ddir,$dver,$darch,$osupd,$dfam,$dtype,$dos);
[1111]127$dins = pb_distro_get_param($ddir,$dver,$darch,$osins,$dfam,$dtype,$dos);
[1067]128$dsuf = pb_distro_get_param($ddir,$dver,$darch,$ossuffix,$dfam,$dtype,$dos);
129$dnover = pb_distro_get_param($ddir,$dver,$darch,$osnover,$dfam,$dtype,$dos);
130$drmdot = pb_distro_get_param($ddir,$dver,$darch,$osremovedotinver,$dfam,$dtype,$dos);
[867]131
132# Some OS have no interesting version
133$dver = "nover" if ($dnover eq "true");
134
135# For some OS remove the . in version name
[1050]136$dver =~ s/\.//g if ($drmdot eq "true");
[867]137
[969]138if ((not defined $dsuf) || ($dsuf eq "")) {
[867]139 # By default suffix is a concatenation of .ddir and dver
140 $dsuf = ".$ddir$dver"
[11]141} else {
[867]142 # concat just the version to what has been found
143 $dsuf = ".$dsuf$dver";
[11]144}
145
[867]146# if ($arch eq "x86_64") {
147# $opt="--exclude=*.i?86";
148# }
[1111]149pb_log(2,"DEBUG: pb_distro_init: $ddir, $dver, $dfam, $dtype, $dsuf, $dupd, $dins, $darch\n");
[867]150
[1111]151return($ddir, $dver, $dfam, $dtype, $dos, $dsuf, $dupd, $dins, $darch);
[11]152}
[23]153
[756]154=item B<pb_distro_get>
[395]155
[756]156This 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]157
[1156]158On my home machine it would currently report ("mandriva","2010.2").
[395]159
160=cut
161
[622]162sub pb_distro_get {
[23]163
[869]164# 1: List of files that unambiguously indicates what distro we have
165# 2: List of files that ambiguously indicates what distro we have
166# 3: Should have the same keys as the previous one. If ambiguity, which other distributions should be checked
167# 4: Matching Rg. Expr to detect distribution and version
168my ($single_rel_files, $ambiguous_rel_files,$distro_similar,$distro_match) = pb_conf_get("osrelfile","osrelambfile","osambiguous","osrelexpr");
[23]169
170my $release;
171my $distro;
172
[391]173# Begin to test presence of non-ambiguous files
[23]174# that way we reduce the choice
[24]175my ($d,$r);
[869]176while (($d,$r) = each %$single_rel_files) {
[1027]177 if (defined $ambiguous_rel_files->{$d}) {
178 print STDERR "The key $d is considered as both unambiguous and ambigous.\n";
[1029]179 print STDERR "Please fix your configuration file.\n"
[1027]180 }
[869]181 if (-f "$r" && ! -l "$r") {
182 my $tmp=pb_get_content("$r");
[23]183 # Found the only possibility.
184 # Try to get version and return
[869]185 if (defined ($distro_match->{$d})) {
186 ($release) = $tmp =~ m/$distro_match->{$d}/m;
[23]187 } else {
[1102]188 print STDERR "Unable to find $d version in $r (non-ambiguous)\n";
[23]189 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
190 $release = "unknown";
191 }
192 return($d,$release);
193 }
194}
195
[423]196# Now look at ambiguous files
[1027]197# Ubuntu before 10.04 includes a /etc/debian_version file that creates an ambiguity with debian
[423]198# So we need to look at distros in reverse alphabetic order to treat ubuntu always first via lsb
[869]199foreach $d (reverse keys %$ambiguous_rel_files) {
200 $r = $ambiguous_rel_files->{$d};
201 if (-f "$r" && !-l "$r") {
[23]202 # Found one possibility.
203 # Get all distros concerned by that file
[869]204 my $tmp=pb_get_content("$r");
[24]205 my $found = 0;
[869]206 my $ptr = $distro_similar->{$d};
[423]207 pb_log(2,"amb: ".Dumper($ptr)."\n");
[24]208 $release = "unknown";
[869]209 foreach my $dd (split(/,/,$ptr)) {
[423]210 pb_log(2,"check $dd\n");
[23]211 # Try to check pattern
[869]212 if (defined $distro_match->{$dd}) {
213 pb_log(2,"cmp: $distro_match->{$dd} - vs - $tmp\n");
214 ($release) = $tmp =~ m/$distro_match->{$dd}/m;
[24]215 if ((defined $release) && ($release ne "unknown")) {
216 $distro = $dd;
217 $found = 1;
218 last;
219 }
[23]220 }
221 }
222 if ($found == 0) {
[1102]223 print STDERR "Unable to find $d version in $r (ambiguous)\n";
[23]224 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
225 $release = "unknown";
226 } else {
227 return($distro,$release);
228 }
229 }
230}
231return("unknown","unknown");
[24]232}
[23]233
[1071]234=item B<pb_distro_getlsb>
[621]235
[1071]236This function returns the 5 lsb values LSB version, distribution ID, Description, release and codename.
237As entry it takes an optional parameter to specify whether the output is short or not.
238
239=cut
240
241sub pb_distro_getlsb {
242
243my $s = shift;
244pb_log(3,"Entering pb_distro_getlsb\n");
245
246my ($ambiguous_rel_files) = pb_conf_get("osrelambfile");
247my $lsbf = $ambiguous_rel_files->{"lsb"};
248
249# LSB has not been configured.
250if (not defined $lsbf) {
251 print STDERR "no lsb entry defined for osrelambfile\n";
252 die "You modified upstream delivery and lost !\n";
253}
254
255if (-r $lsbf) {
256 my $rep = pb_get_content($lsbf);
257 # Create elementary fields
258 my ($c, $r, $d, $i, $l) = ("", "", "", "", "");
259 for my $f (split(/\n/,$rep)) {
260 pb_log(3,"Reading file part ***$f***\n");
261 $c = $f if ($f =~ /^DISTRIB_CODENAME/);
262 $c =~ s/DISTRIB_CODENAME=/Codename:\t/;
263 $r = $f if ($f =~ /^DISTRIB_RELEASE/);
264 $r =~ s/DISTRIB_RELEASE=/Release:\t/;
265 $d = $f if ($f =~ /^DISTRIB_DESCRIPTION/);
266 $d =~ s/DISTRIB_DESCRIPTION=/Description:\t/;
267 $d =~ s/"//g;
268 $i = $f if ($f =~ /^DISTRIB_ID/);
269 $i =~ s/DISTRIB_ID=/Distributor ID:\t/;
270 $l = $f if ($f =~ /^LSB_VERSION/);
271 $l =~ s/LSB_VERSION=/LSB Version:\t/;
272 }
273 $c =~ s/^[A-z ]*:[\t ]*// if (defined $s);
274 $r =~ s/^[A-z ]*:[\t ]*// if (defined $s);
275 $d =~ s/^[A-z ]*:[\t ]*// if (defined $s);
276 $i =~ s/^[A-z ]*:[\t ]*// if (defined $s);
277 $l =~ s/^[A-z ]*:[\t ]*// if (defined $s);
278 return($l, $i, $d, $r, $c);
279} else {
280 print STDERR "Unable to read $lsbf file\n";
281 die "Please report to the maintainer bruno_at_project-builder.org\n";
282}
283}
284
[621]285=item B<pb_distro_installdeps>
286
287This function install the dependencies required to build the package on an RPM based distro
[756]288dependencies can be passed as a parameter in which case they are not computed
[621]289
290=cut
291
292sub pb_distro_installdeps {
293
294# SPEC file
295my $f = shift || undef;
296my $dtype = shift || undef;
297my $dupd = shift || undef;
298my $deps = shift || undef;
299
300# Protection
301return if (not defined $dupd);
302
303# Get dependecies in the build file if not forced
[899]304$deps = pb_distro_getdeps($f, $dtype) if (not defined $deps);
[621]305pb_log(2,"deps: $deps\n");
[623]306return if ((not defined $deps) || ($deps =~ /^\s*$/));
[621]307if ($deps !~ /^[ ]*$/) {
[1137]308 # This may not be // proof. We should test for availability of repo and sleep if not
[621]309 pb_system("$dupd $deps","Installing dependencies ($deps)");
310 }
311}
312
313=item B<pb_distro_getdeps>
314
315This function computes the dependencies indicated in the build file and return them as a string of packages to install
316
317=cut
318
319sub pb_distro_getdeps {
320
321my $f = shift || undef;
322my $dtype = shift || undef;
323
324my $regexp = "";
325my $deps = "";
326my $sep = $/;
327
328# Protection
[622]329return("") if (not defined $dtype);
[899]330return("") if (not defined $f);
331
332pb_log(3,"entering pb_distro_getdeps: $dtype - $f\n");
[621]333if ($dtype eq "rpm") {
334 # In RPM this could include files, but we do not handle them atm.
335 $regexp = '^BuildRequires:(.*)$';
[696]336} elsif ($dtype eq "deb") {
[621]337 $regexp = '^Build-Depends:(.*)$';
338} elsif ($dtype eq "ebuild") {
339 $sep = '"'.$/;
340 $regexp = '^DEPEND="(.*)"\n'
341} else {
342 # No idea
[622]343 return("");
[621]344}
345pb_log(2,"regexp: $regexp\n");
346
347# Preserve separator before using the one we need
348my $oldsep = $/;
349$/ = $sep;
350open(DESC,"$f") || die "Unable to open $f";
351while (<DESC>) {
352 pb_log(4,"read: $_\n");
353 next if (! /$regexp/);
354 chomp();
355 # What we found with the regexp is the list of deps.
356 pb_log(2,"found deps: $_\n");
[681]357 s/$regexp/$1/i;
[698]358 # Remove conditions in the middle and at the end for deb
[931]359 s/\(\s*[><=]+.*\)[^,]*,/,/g;
[698]360 s/\(\s*[><=]+.*$//g;
361 # Same for rpm
[931]362 s/[><=]+[^,]*,/,/g;
[652]363 s/[><=]+.*$//g;
[621]364 # Improve string format (remove , and spaces at start, end and in double
[652]365 s/,/ /g;
[621]366 s/^\s*//;
367 s/\s*$//;
368 s/\s+/ /g;
369 $deps .= " ".$_;
370}
371close(DESC);
372$/ = $oldsep;
373pb_log(2,"now deps: $deps\n");
[622]374my $deps2 = pb_distro_only_deps_needed($dtype,$deps);
375return($deps2);
376}
377
378
379=item B<pb_distro_only_deps_needed>
380
381This function returns only the dependencies not yet installed
382
383=cut
384
385sub pb_distro_only_deps_needed {
386
[623]387my $dtype = shift || undef;
[622]388my $deps = shift || undef;
389
[623]390return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
[621]391my $deps2 = "";
392# Avoid to install what is already there
393foreach my $p (split(/ /,$deps)) {
394 if ($dtype eq "rpm") {
395 my $res = pb_system("rpm -q --whatprovides --quiet $p","","quiet");
396 next if ($res eq 0);
[696]397 } elsif ($dtype eq "deb") {
[621]398 my $res = pb_system("dpkg -L $p","","quiet");
399 next if ($res eq 0);
400 } elsif ($dtype eq "ebuild") {
401 } else {
402 # Not reached
403 }
404 pb_log(2,"found deps2: $p\n");
405 $deps2 .= " $p";
406}
407
408$deps2 =~ s/^\s*//;
409pb_log(2,"now deps2: $deps2\n");
410return($deps2);
411}
412
[1132]413=item B<pb_distro_setuposrepo>
414
415This function sets up potential additional repository for the setup phase
416
417=cut
418
419sub pb_distro_setuposrepo {
420
421my $ddir = shift || undef;
422my $dver = shift;
423my $darch = shift;
424my $dtype = shift || undef;
425my $dfam = shift || undef;
426my $dos = shift || undef;
427
428pb_distro_setuprepo_gen($ddir,$dver,$darch,$dtype,$dfam,$dos,pb_distro_conffile(),"osrepo");
429}
430
[702]431=item B<pb_distro_setuprepo>
432
433This function sets up potential additional repository to the build environment
434
435=cut
436
437sub pb_distro_setuprepo {
438
439my $ddir = shift || undef;
440my $dver = shift;
441my $darch = shift;
442my $dtype = shift || undef;
[1132]443my $dfam = shift || undef;
444my $dos = shift || undef;
[702]445
[1132]446pb_distro_setuprepo_gen($ddir,$dver,$darch,$dtype,$dfam,$dos,"$ENV{'PBDESTDIR'}/pbrc","addrepo");
447}
448
449=item B<pb_distro_setuprepo_gen>
450
451This function sets up in a generic way potential additional repository
452
453=cut
454
455sub pb_distro_setuprepo_gen {
456
457my $ddir = shift || undef;
458my $dver = shift;
459my $darch = shift;
460my $dtype = shift || undef;
461my $dfam = shift || undef;
462my $dos = shift || undef;
463my $pbconf = shift || undef;
464my $pbkey = shift || undef;
465
466return if (not defined $pbconf);
467return if (not defined $pbkey);
468my ($addrepo) = pb_conf_read($pbconf,$pbkey);
[702]469return if (not defined $addrepo);
470
[1132]471my $param = pb_distro_get_param($ddir,$dver,$darch,$addrepo,$dfam,$dtype,$dos);
[702]472return if ($param eq "");
473
474# Loop on the list of additional repo
475foreach my $i (split(/,/,$param)) {
476
477 my ($scheme, $account, $host, $port, $path) = pb_get_uri($i);
478 my $bn = basename($i);
479
480 # The repo file can be local or remote. download or copy at the right place
481 if (($scheme eq "ftp") || ($scheme eq "http")) {
482 pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Donwloading additional repository file $i");
483 } else {
484 copy($i,$ENV{'PBTMP'}/$bn);
485 }
486
487 # The repo file can be a real file or a package
488 if ($dtype eq "rpm") {
489 if ($bn =~ /\.rpm$/) {
[721]490 my $pn = $bn;
491 $pn =~ s/\.rpm//;
492 if (pb_system("rpm -q --quiet $pn","","quiet") != 0) {
493 pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package to setup repository");
494 }
495 } elsif ($bn =~ /\.repo$/) {
[702]496 # Yum repo
[721]497 pb_system("sudo mv $ENV{'PBTMP'}/$bn /etc/yum.repos.d","Adding yum repository") if (not -f "/etc/yum.repos.d/$bn");
[702]498 } elsif ($bn =~ /\.addmedia/) {
499 # URPMI repo
[721]500 # We should test that it's not already a urpmi repo
[702]501 pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository");
502 } else {
503 pb_log(0,"Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
504 }
505 } elsif ($dtype eq "deb") {
[721]506 if (($bn =~ /\.sources.list$/) && (not -f "/etc/apt/sources.list.d/$bn")) {
[711]507 pb_system("sudo mv $ENV{'PBTMP'}/$bn /etc/apt/sources.list.d","Adding apt repository");
508 pb_system("sudo apt-get update","Updating apt repository");
[702]509 } else {
510 pb_log(0,"Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
511 }
512 } else {
513 pb_log(0,"Unable to deal with repository file $i on that distro ! Please report to dev team\n");
514 }
515}
516return;
517}
518
519=item B<pb_distro_get_param>
520
521This function gets the parameter in the conf file from the most precise tuple up to default
522
523=cut
524
525sub pb_distro_get_param {
526
527my $param = "";
528my $ddir = shift;
529my $dver = shift;
530my $darch = shift;
531my $opt = shift;
[867]532my $dfam = shift || "unknown";
533my $dtype = shift || "unknown";
[1064]534my $dos = shift || "unknown";
[702]535
[1132]536pb_log(2,"DEBUG: pb_distro_get_param on $ddir-$dver-$darch for ".Dumper($opt)."\n");
[702]537if (defined $opt->{"$ddir-$dver-$darch"}) {
538 $param = $opt->{"$ddir-$dver-$darch"};
539} elsif (defined $opt->{"$ddir-$dver"}) {
540 $param = $opt->{"$ddir-$dver"};
541} elsif (defined $opt->{"$ddir"}) {
542 $param = $opt->{"$ddir"};
[867]543} elsif (defined $opt->{$dfam}) {
544 $param = $opt->{$dfam};
545} elsif (defined $opt->{$dtype}) {
546 $param = $opt->{$dtype};
[1064]547} elsif (defined $opt->{$dos}) {
548 $param = $opt->{$dos};
[702]549} elsif (defined $opt->{"default"}) {
550 $param = $opt->{"default"};
551} else {
[963]552 $param = "";
[702]553}
[974]554
[982]555# Allow replacement of variables inside the parameter such as ddir, dver, darch for rpmbootstrap
[1029]556# but not shell variable which are backslashed
[982]557if ($param =~ /[^\\]\$/) {
558 pb_log(3,"Expanding variable on $param\n");
559 eval { $param =~ s/(\$\w+)/$1/eeg };
560}
[974]561
[982]562pb_log(2,"DEBUG: pb_distro_get_param on $ddir-$dver-$darch returns ==$param==\n");
[702]563return($param);
564
565}
[749]566
567
[395]568=back
[23]569
[395]570=head1 WEB SITES
[23]571
[395]572The 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/>.
573
574=head1 USER MAILING LIST
575
576None exists for the moment.
577
578=head1 AUTHORS
579
580The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
581
582=head1 COPYRIGHT
583
584Project-Builder.org is distributed under the GPL v2.0 license
585described in the file C<COPYING> included with the distribution.
586
587=cut
588
589
[11]5901;
Note: See TracBrowser for help on using the repository browser.