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
Line 
1#!/usr/bin/perl -w
2#
3# Creates common environment for distributions
4#
5# $Id$
6#
7
8package ProjectBuilder::Distribution;
9
10use strict;
11use Data::Dumper;
12use ProjectBuilder::Version;
13use ProjectBuilder::Base;
14use ProjectBuilder::Conf;
15use File::Basename;
16use File::Copy;
17
18# Global vars
19# Inherit from the "Exporter" module which handles exporting functions.
20
21use vars qw($VERSION $REVISION @ISA @EXPORT);
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);
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);
29($VERSION,$REVISION) = pb_version_init();
30
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 #
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";
50 #
51 # Return information on the requested distro
52 #
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";
55 #
56 # Return information on the running distro
57 #
58 my ($ddir,$dver) = pb_distro_get();
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";
61
62=head1 USAGE
63
64=over 4
65
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
78=item B<pb_distro_init>
79
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.
81
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).
85All these information are stored in an external configuration file typically at /etc/pb/pb.conf
86
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.
88
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
92=cut
93
94
95sub pb_distro_init {
96
97my $ddir = shift || undef;
98my $dver = shift || undef;
99my $dfam = "unknown";
100my $dtype = "unknown";
101my $dos = "unknown";
102my $dsuf = "unknown";
103my $dupd = "unknown";
104my $dins = "unknown";
105my $darch = shift || undef;
106my $dnover = "false";
107my $drmdot = "false";
108
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
112pb_conf_add(pb_distro_conffile());
113
114# If we don't know which distribution we're on, then guess it
115($ddir,$dver) = pb_distro_get() if ((not defined $ddir) || (not defined $dver));
116
117# Initialize arch
118$darch=pb_get_arch() if (not defined $darch);
119
120my ($osfamily,$ostype,$osupd,$osins,$ossuffix,$osnover,$osremovedotinver,$os) = pb_conf_get("osfamily","ostype","osupd","osins","ossuffix","osnover","osremovedotinver","os");
121
122# Dig into the tuple to find the best answer
123$dfam = pb_distro_get_param($ddir,$dver,$darch,$osfamily);
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);
127$dins = pb_distro_get_param($ddir,$dver,$darch,$osins,$dfam,$dtype,$dos);
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);
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
136$dver =~ s/\.//g if ($drmdot eq "true");
137
138if ((not defined $dsuf) || ($dsuf eq "")) {
139 # By default suffix is a concatenation of .ddir and dver
140 $dsuf = ".$ddir$dver"
141} else {
142 # concat just the version to what has been found
143 $dsuf = ".$dsuf$dver";
144}
145
146# if ($arch eq "x86_64") {
147# $opt="--exclude=*.i?86";
148# }
149pb_log(2,"DEBUG: pb_distro_init: $ddir, $dver, $dfam, $dtype, $dsuf, $dupd, $dins, $darch\n");
150
151return($ddir, $dver, $dfam, $dtype, $dos, $dsuf, $dupd, $dins, $darch);
152}
153
154=item B<pb_distro_get>
155
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.
157
158On my home machine it would currently report ("mandriva","2010.2").
159
160=cut
161
162sub pb_distro_get {
163
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");
169
170my $release;
171my $distro;
172
173# Begin to test presence of non-ambiguous files
174# that way we reduce the choice
175my ($d,$r);
176while (($d,$r) = each %$single_rel_files) {
177 if (defined $ambiguous_rel_files->{$d}) {
178 print STDERR "The key $d is considered as both unambiguous and ambigous.\n";
179 print STDERR "Please fix your configuration file.\n"
180 }
181 if (-f "$r" && ! -l "$r") {
182 my $tmp=pb_get_content("$r");
183 # Found the only possibility.
184 # Try to get version and return
185 if (defined ($distro_match->{$d})) {
186 ($release) = $tmp =~ m/$distro_match->{$d}/m;
187 } else {
188 print STDERR "Unable to find $d version in $r (non-ambiguous)\n";
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
196# Now look at ambiguous files
197# Ubuntu before 10.04 includes a /etc/debian_version file that creates an ambiguity with debian
198# So we need to look at distros in reverse alphabetic order to treat ubuntu always first via lsb
199foreach $d (reverse keys %$ambiguous_rel_files) {
200 $r = $ambiguous_rel_files->{$d};
201 if (-f "$r" && !-l "$r") {
202 # Found one possibility.
203 # Get all distros concerned by that file
204 my $tmp=pb_get_content("$r");
205 my $found = 0;
206 my $ptr = $distro_similar->{$d};
207 pb_log(2,"amb: ".Dumper($ptr)."\n");
208 $release = "unknown";
209 foreach my $dd (split(/,/,$ptr)) {
210 pb_log(2,"check $dd\n");
211 # Try to check pattern
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;
215 if ((defined $release) && ($release ne "unknown")) {
216 $distro = $dd;
217 $found = 1;
218 last;
219 }
220 }
221 }
222 if ($found == 0) {
223 print STDERR "Unable to find $d version in $r (ambiguous)\n";
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");
232}
233
234=item B<pb_distro_getlsb>
235
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
285=item B<pb_distro_installdeps>
286
287This function install the dependencies required to build the package on an RPM based distro
288dependencies can be passed as a parameter in which case they are not computed
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
304$deps = pb_distro_getdeps($f, $dtype) if (not defined $deps);
305pb_log(2,"deps: $deps\n");
306return if ((not defined $deps) || ($deps =~ /^\s*$/));
307if ($deps !~ /^[ ]*$/) {
308 # This may not be // proof. We should test for availability of repo and sleep if not
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
329return("") if (not defined $dtype);
330return("") if (not defined $f);
331
332pb_log(3,"entering pb_distro_getdeps: $dtype - $f\n");
333if ($dtype eq "rpm") {
334 # In RPM this could include files, but we do not handle them atm.
335 $regexp = '^BuildRequires:(.*)$';
336} elsif ($dtype eq "deb") {
337 $regexp = '^Build-Depends:(.*)$';
338} elsif ($dtype eq "ebuild") {
339 $sep = '"'.$/;
340 $regexp = '^DEPEND="(.*)"\n'
341} else {
342 # No idea
343 return("");
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");
357 s/$regexp/$1/i;
358 # Remove conditions in the middle and at the end for deb
359 s/\(\s*[><=]+.*\)[^,]*,/,/g;
360 s/\(\s*[><=]+.*$//g;
361 # Same for rpm
362 s/[><=]+[^,]*,/,/g;
363 s/[><=]+.*$//g;
364 # Improve string format (remove , and spaces at start, end and in double
365 s/,/ /g;
366 s/^\s*//;
367 s/\s*$//;
368 s/\s+/ /g;
369 $deps .= " ".$_;
370}
371close(DESC);
372$/ = $oldsep;
373pb_log(2,"now deps: $deps\n");
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
387my $dtype = shift || undef;
388my $deps = shift || undef;
389
390return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
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);
397 } elsif ($dtype eq "deb") {
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
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
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;
443my $dfam = shift || undef;
444my $dos = shift || undef;
445
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);
469return if (not defined $addrepo);
470
471my $param = pb_distro_get_param($ddir,$dver,$darch,$addrepo,$dfam,$dtype,$dos);
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$/) {
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$/) {
496 # Yum repo
497 pb_system("sudo mv $ENV{'PBTMP'}/$bn /etc/yum.repos.d","Adding yum repository") if (not -f "/etc/yum.repos.d/$bn");
498 } elsif ($bn =~ /\.addmedia/) {
499 # URPMI repo
500 # We should test that it's not already a urpmi repo
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") {
506 if (($bn =~ /\.sources.list$/) && (not -f "/etc/apt/sources.list.d/$bn")) {
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");
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;
532my $dfam = shift || "unknown";
533my $dtype = shift || "unknown";
534my $dos = shift || "unknown";
535
536pb_log(2,"DEBUG: pb_distro_get_param on $ddir-$dver-$darch for ".Dumper($opt)."\n");
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"};
543} elsif (defined $opt->{$dfam}) {
544 $param = $opt->{$dfam};
545} elsif (defined $opt->{$dtype}) {
546 $param = $opt->{$dtype};
547} elsif (defined $opt->{$dos}) {
548 $param = $opt->{$dos};
549} elsif (defined $opt->{"default"}) {
550 $param = $opt->{"default"};
551} else {
552 $param = "";
553}
554
555# Allow replacement of variables inside the parameter such as ddir, dver, darch for rpmbootstrap
556# but not shell variable which are backslashed
557if ($param =~ /[^\\]\$/) {
558 pb_log(3,"Expanding variable on $param\n");
559 eval { $param =~ s/(\$\w+)/$1/eeg };
560}
561
562pb_log(2,"DEBUG: pb_distro_get_param on $ddir-$dver-$darch returns ==$param==\n");
563return($param);
564
565}
566
567
568=back
569
570=head1 WEB SITES
571
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
5901;
Note: See TracBrowser for help on using the repository browser.