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

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