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

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