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

Last change on this file since 768 was 768, checked in by Bruno Cornec, 15 years ago
  • No snapshot needed if creating the VM
  • Remove the VE befor recreating it if snapshot
  • Force yum update
File size: 16.9 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::Base;
13use ProjectBuilder::Conf;
14use File::Basename;
15use File::Copy;
16
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);
25our @EXPORT = qw(pb_distro_init pb_distro_get pb_distro_installdeps pb_distro_getdeps pb_distro_only_deps_needed pb_distro_setuprepo pb_distro_get_param);
26
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 #
44 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init();
45 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch)."\n";
46 #
47 # Return information on the requested distro
48 #
49 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init("ubuntu","7.10","x86_64");
50 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch)."\n";
51 #
52 # Return information on the running distro
53 #
54 my ($ddir,$dver) = pb_distro_get();
55 my ($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch) = pb_distro_init($ddir,$dver);
56 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf, $pbupd, $arch)."\n";
57
58=head1 USAGE
59
60=over 4
61
62=item B<pb_distro_init>
63
64This 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.
65
66As an example, Ubuntu and Debian are in the same "du" family. As well as RedHat, RHEL, CentOS, fedora are on the same "rh" family.
67Mandriva, Open SuSE and Fedora have all the same "rpm" type of build system. Ubuntu ad Debian have the same "deb" type of build system.
68And "fc" is the extension generated for all Fedora packages (Version will be added by pb).
69
70When 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.
71
72Cf: http://linuxmafia.com/faq/Admin/release-files.html
73Ideas taken from http://search.cpan.org/~kerberus/Linux-Distribution-0.14/lib/Linux/Distribution.pm
74
75=cut
76
77
78sub pb_distro_init {
79
80my $ddir = shift || undef;
81my $dver = shift || undef;
82my $dfam = "unknown";
83my $dtype = "unknown";
84my $dsuf = "unknown";
85my $dupd = "unknown";
86my $arch = shift || undef;
87
88# If we don't know which distribution we're on, then guess it
89($ddir,$dver) = pb_distro_get() if ((not defined $ddir) || (not defined $dver));
90
91# Initialize arch
92$arch=pb_get_arch() if (not defined $arch);
93
94# There should be unicity of names between ddir dfam and dtype
95# In case of duplicate, bad things can happen
96if (($ddir =~ /debian/) ||
97 ($ddir =~ /ubuntu/)) {
98 $dfam="du";
99 $dtype="deb";
100 $dsuf=".$ddir$dver";
101 # Chaining the commands allow to only test for what is able o be installed,
102 # not the update of the repo which may well be unaccessible if too old
103 $dupd="sudo apt-get update ; sudo apt-get -y install ";
104} elsif ($ddir =~ /gentoo/) {
105 $dfam="gen";
106 $dtype="ebuild";
107 $dver="nover";
108 $dsuf=".$ddir";
109 $dupd="sudo emerge ";
110} elsif ($ddir =~ /slackware/) {
111 $dfam="slack";
112 $dtype="tgz";
113 $dsuf=".$dfam$dver";
114} elsif (($ddir =~ /suse/) ||
115 ($ddir =~ /sles/)) {
116 $dfam="novell";
117 $dtype="rpm";
118 $dsuf=".$ddir$dver";
119 $dupd="export TERM=linux ; export PATH=\$PATH:/sbin:/usr/sbin ; sudo yast2 -i ";
120} elsif (($ddir =~ /redhat/) ||
121 ($ddir =~ /rhel/) ||
122 ($ddir =~ /fedora/) ||
123 ($ddir =~ /vmware/) ||
124 ($ddir =~ /asianux/) ||
125 ($ddir =~ /centos/)) {
126 $dfam="rh";
127 $dtype="rpm";
128 my $dver1 = $dver;
129 $dver1 =~ s/\.//;
130
131 # By defaut propose yum
132 my $opt = "";
133 if ($arch eq "x86_64") {
134 $opt="--exclude=*.i?86";
135 }
136 $dupd="sudo yum clean all; sudo yum -y update ; sudo yum -y $opt install ";
137 if ($ddir =~ /fedora/) {
138 $dsuf=".fc$dver1";
139 } elsif ($ddir =~ /redhat/) {
140 $dsuf=".rh$dver1";
141 $dupd="unknown";
142 } elsif ($ddir =~ /asianux/) {
143 $dsuf=".asianux$dver1";
144 } elsif ($ddir =~ /vmware/) {
145 $dsuf=".vwm$dver1";
146 $dupd="unknown";
147 } else {
148 # older versions of rhel ran up2date
149 if ((($dver eq "2.1") || ($dver eq "3") || ($dver eq "4")) && ($ddir eq "rhel")) {
150 $dupd="sudo up2date -y ";
151 }
152 $dsuf=".$ddir$dver1";
153 }
154} elsif (($ddir =~ /mandrake/) ||
155 ($ddir =~ /mandrakelinux/) ||
156 ($ddir =~ /mandriva/)) {
157 $dfam="md";
158 $dtype="rpm";
159 if ($ddir =~ /mandrakelinux/) {
160 $ddir = "mandrake";
161 }
162 if ($ddir =~ /mandrake/) {
163 my $dver1 = $dver;
164 $dver1 =~ s/\.//;
165 $dsuf=".mdk$dver1";
166 } else {
167 $dsuf=".mdv$dver";
168 }
169 # Chaining the commands allow to only test for what is able o be installed,
170 # not the update of the repo which may well be unaccessible if too old
171 $dupd="sudo urpmi.update -a ; sudo urpmi --auto ";
172} elsif ($ddir =~ /freebsd/) {
173 $dfam="bsd";
174 $dtype="port";
175 my $dver1 = $dver;
176 $dver1 =~ s/\.//;
177 $dsuf=".$dfam$dver1";
178} else {
179 $dfam="unknown";
180}
181
182return($ddir, $dver, $dfam, $dtype, $dsuf, $dupd, $arch);
183}
184
185=item B<pb_distro_get>
186
187This 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.
188
189On my home machine it would currently report ("mandriva","2009.0").
190
191=cut
192
193sub pb_distro_get {
194
195my $base="/etc";
196
197# List of files that unambiguously indicates what distro we have
198my %single_rel_files = (
199# Tested
200 'gentoo' => 'gentoo-release', # >= 1.6
201 'slackware' => 'slackware-version', # >= 10.2
202 'mandriva' => 'mandriva-release', # >=2006.0
203 'mandrakelinux' => 'mandrakelinux-release',# = 10.2
204 'fedora' => 'fedora-release', # >= 4
205 'vmware' => 'vmware-release', # >= 3
206 'sles' => 'sles-release', # Doesn't exist as of 10
207 'asianux' => 'asianux-release', # >= 2.2
208# Untested
209 'knoppix' => 'knoppix_version', #
210 'yellowdog' => 'yellowdog-release', #
211 'esmith' => 'e-smith-release', #
212 'turbolinux' => 'turbolinux-release', #
213 'blackcat' => 'blackcat-release', #
214 'aurox' => 'aurox-release', #
215 'annvix' => 'annvix-release', #
216 'cobalt' => 'cobalt-release', #
217 'redflag' => 'redflag-release', #
218 'ark' => 'ark-release', #
219 'pld' => 'pld-release', #
220 'nld' => 'nld-release', #
221 'lfs' => 'lfs-release', #
222 'mk' => 'mk-release', #
223 'conectiva' => 'conectiva-release', #
224 'immunix' => 'immunix-release', #
225 'tinysofa' => 'tinysofa-release', #
226 'trustix' => 'trustix-release', #
227 'adamantix' => 'adamantix_version', #
228 'yoper' => 'yoper-release', #
229 'arch' => 'arch-release', #
230 'libranet' => 'libranet_version', #
231 'valinux' => 'va-release', #
232 'yellowdog' => 'yellowdog-release', #
233 'ultrapenguin' => 'ultrapenguin-release', #
234 );
235
236# List of files that ambiguously indicates what distro we have
237my %ambiguous_rel_files = (
238 'mandrake' => 'mandrake-release', # <= 10.1
239 'debian' => 'debian_version', # >= 3.1
240 'suse' => 'SuSE-release', # >= 10.0
241 'redhat' => 'redhat-release', # >= 7.3
242 'lsb' => 'lsb-release', # ???
243 );
244
245# Should have the same keys as the previous one.
246# If ambiguity, which other distributions should be checked
247my %distro_similar = (
248 'mandrake' => ['mandrake', 'mandrakelinux'],
249 'debian' => ['debian', 'ubuntu'],
250 'suse' => ['suse', 'sles', 'opensuse'],
251 'redhat' => ['redhat', 'rhel', 'centos', 'mandrake', 'vmware'],
252 'lsb' => ['ubuntu', 'lsb'],
253 );
254
255my %distro_match = (
256# Tested
257 'gentoo' => '.* version (.+)',
258 'slackware' => 'S[^ ]* (.+)$',
259# There should be no ambiguity between potential ambiguous distro
260 'mandrakelinux' => 'Mandrakelinux release (.+) \(',
261 'mandrake' => 'Mandr[^ ]* release (.+) \(',
262 'mandriva' => 'Mandr[^ ]* [^ ]* release (.+) \(',
263 'fedora' => 'Fedora .*release (\d+) \(',
264 'vmware' => 'VMware ESX Server (\d+) \(',
265 'rhel' => 'Red Hat (?:Enterprise Linux|Linux Advanced Server) .*release ([0-9.]+).* \(',
266 'centos' => '.*CentOS .*release ([0-9]).* ',
267 'redhat' => 'Red Hat Linux release (.+) \(',
268 'sles' => 'SUSE .* Enterprise Server (\d+) \(',
269 'suse' => 'SUSE LINUX (\d.+) \(',
270 'opensuse' => 'openSUSE (\d.+) \(',
271 'asianux' => 'Asianux (?:Server|release) ([0-9]).* \(',
272 'lsb' => '.*[^Ubunt].*\nDISTRIB_RELEASE=(.+)',
273# Ubuntu includes a /etc/debian_version file that creates an ambiguity with debian
274# So we need to look at distros in reverse alphabetic order to treat ubuntu always first
275 'ubuntu' => '.*Ubuntu.*\nDISTRIB_RELEASE=(.+)',
276 'debian' => '(.+)',
277# Not tested
278 'arch' => '.* ([0-9.]+) .*',
279 'redflag' => 'Red Flag (?:Desktop|Linux) (?:release |\()(.*?)(?: \(.+)?\)',
280);
281
282my $release;
283my $distro;
284
285# Begin to test presence of non-ambiguous files
286# that way we reduce the choice
287my ($d,$r);
288while (($d,$r) = each %single_rel_files) {
289 if (-f "$base/$r" && ! -l "$base/$r") {
290 my $tmp=pb_get_content("$base/$r");
291 # Found the only possibility.
292 # Try to get version and return
293 if (defined ($distro_match{$d})) {
294 ($release) = $tmp =~ m/$distro_match{$d}/m;
295 } else {
296 print STDERR "Unable to find $d version in $r\n";
297 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
298 $release = "unknown";
299 }
300 return($d,$release);
301 }
302}
303
304# Now look at ambiguous files
305# Ubuntu includes a /etc/debian_version file that creates an ambiguity with debian
306# So we need to look at distros in reverse alphabetic order to treat ubuntu always first via lsb
307foreach $d (reverse keys %ambiguous_rel_files) {
308 $r = $ambiguous_rel_files{$d};
309 if (-f "$base/$r" && !-l "$base/$r") {
310 # Found one possibility.
311 # Get all distros concerned by that file
312 my $tmp=pb_get_content("$base/$r");
313 my $found = 0;
314 my $ptr = $distro_similar{$d};
315 pb_log(2,"amb: ".Dumper($ptr)."\n");
316 $release = "unknown";
317 foreach my $dd (@$ptr) {
318 pb_log(2,"check $dd\n");
319 # Try to check pattern
320 if (defined $distro_match{$dd}) {
321 pb_log(2,"cmp: $distro_match{$dd} - vs - $tmp\n");
322 ($release) = $tmp =~ m/$distro_match{$dd}/m;
323 if ((defined $release) && ($release ne "unknown")) {
324 $distro = $dd;
325 $found = 1;
326 last;
327 }
328 }
329 }
330 if ($found == 0) {
331 print STDERR "Unable to find $d version in $r\n";
332 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
333 $release = "unknown";
334 } else {
335 return($distro,$release);
336 }
337 }
338}
339return("unknown","unknown");
340}
341
342
343=over 4
344
345=item B<pb_distro_installdeps>
346
347This function install the dependencies required to build the package on an RPM based distro
348dependencies can be passed as a parameter in which case they are not computed
349
350=cut
351
352sub pb_distro_installdeps {
353
354# SPEC file
355my $f = shift || undef;
356my $dtype = shift || undef;
357my $dupd = shift || undef;
358my $deps = shift || undef;
359
360# Protection
361return if (not defined $dupd);
362
363# Get dependecies in the build file if not forced
364$deps = pb_distro_getdeps("$f", $dtype) if (not defined $deps);
365pb_log(2,"deps: $deps\n");
366return if ((not defined $deps) || ($deps =~ /^\s*$/));
367if ($deps !~ /^[ ]*$/) {
368 pb_system("$dupd $deps","Installing dependencies ($deps)");
369 }
370}
371
372=over 4
373
374=item B<pb_distro_getdeps>
375
376This function computes the dependencies indicated in the build file and return them as a string of packages to install
377
378=cut
379
380sub pb_distro_getdeps {
381
382my $f = shift || undef;
383my $dtype = shift || undef;
384
385my $regexp = "";
386my $deps = "";
387my $sep = $/;
388
389pb_log(3,"entering pb_distro_getdeps: $dtype - $f\n");
390# Protection
391return("") if (not defined $dtype);
392if ($dtype eq "rpm") {
393 # In RPM this could include files, but we do not handle them atm.
394 $regexp = '^BuildRequires:(.*)$';
395} elsif ($dtype eq "deb") {
396 $regexp = '^Build-Depends:(.*)$';
397} elsif ($dtype eq "ebuild") {
398 $sep = '"'.$/;
399 $regexp = '^DEPEND="(.*)"\n'
400} else {
401 # No idea
402 return("");
403}
404pb_log(2,"regexp: $regexp\n");
405
406
407# Protection
408return("") if (not defined $f);
409
410# Preserve separator before using the one we need
411my $oldsep = $/;
412$/ = $sep;
413open(DESC,"$f") || die "Unable to open $f";
414while (<DESC>) {
415 pb_log(4,"read: $_\n");
416 next if (! /$regexp/);
417 chomp();
418 # What we found with the regexp is the list of deps.
419 pb_log(2,"found deps: $_\n");
420 s/$regexp/$1/i;
421 # Remove conditions in the middle and at the end for deb
422 s/\(\s*[><=]+.*\)\s*,/,/g;
423 s/\(\s*[><=]+.*$//g;
424 # Same for rpm
425 s/[><=]+.*,/,/g;
426 s/[><=]+.*$//g;
427 # Improve string format (remove , and spaces at start, end and in double
428 s/,/ /g;
429 s/^\s*//;
430 s/\s*$//;
431 s/\s+/ /g;
432 $deps .= " ".$_;
433}
434close(DESC);
435$/ = $oldsep;
436pb_log(2,"now deps: $deps\n");
437my $deps2 = pb_distro_only_deps_needed($dtype,$deps);
438return($deps2);
439}
440
441
442=over 4
443
444=item B<pb_distro_only_deps_needed>
445
446This function returns only the dependencies not yet installed
447
448=cut
449
450sub pb_distro_only_deps_needed {
451
452my $dtype = shift || undef;
453my $deps = shift || undef;
454
455return("") if ((not defined $deps) || ($deps =~ /^\s*$/));
456my $deps2 = "";
457# Avoid to install what is already there
458foreach my $p (split(/ /,$deps)) {
459 if ($dtype eq "rpm") {
460 my $res = pb_system("rpm -q --whatprovides --quiet $p","","quiet");
461 next if ($res eq 0);
462 } elsif ($dtype eq "deb") {
463 my $res = pb_system("dpkg -L $p","","quiet");
464 next if ($res eq 0);
465 } elsif ($dtype eq "ebuild") {
466 } else {
467 # Not reached
468 }
469 pb_log(2,"found deps2: $p\n");
470 $deps2 .= " $p";
471}
472
473$deps2 =~ s/^\s*//;
474pb_log(2,"now deps2: $deps2\n");
475return($deps2);
476}
477
478=over 4
479
480=item B<pb_distro_setuprepo>
481
482This function sets up potential additional repository to the build environment
483
484=cut
485
486sub pb_distro_setuprepo {
487
488my $ddir = shift || undef;
489my $dver = shift;
490my $darch = shift;
491my $dtype = shift || undef;
492
493my ($addrepo) = pb_conf_read("$ENV{'PBDESTDIR'}/pbrc","addrepo");
494return if (not defined $addrepo);
495
496my $param = pb_distro_get_param($ddir,$dver,$darch,$addrepo);
497return if ($param eq "");
498
499# Loop on the list of additional repo
500foreach my $i (split(/,/,$param)) {
501
502 my ($scheme, $account, $host, $port, $path) = pb_get_uri($i);
503 my $bn = basename($i);
504
505 # The repo file can be local or remote. download or copy at the right place
506 if (($scheme eq "ftp") || ($scheme eq "http")) {
507 pb_system("wget -O $ENV{'PBTMP'}/$bn $i","Donwloading additional repository file $i");
508 } else {
509 copy($i,$ENV{'PBTMP'}/$bn);
510 }
511
512 # The repo file can be a real file or a package
513 if ($dtype eq "rpm") {
514 if ($bn =~ /\.rpm$/) {
515 my $pn = $bn;
516 $pn =~ s/\.rpm//;
517 if (pb_system("rpm -q --quiet $pn","","quiet") != 0) {
518 pb_system("sudo rpm -Uvh $ENV{'PBTMP'}/$bn","Adding package to setup repository");
519 }
520 } elsif ($bn =~ /\.repo$/) {
521 # Yum repo
522 pb_system("sudo mv $ENV{'PBTMP'}/$bn /etc/yum.repos.d","Adding yum repository") if (not -f "/etc/yum.repos.d/$bn");
523 } elsif ($bn =~ /\.addmedia/) {
524 # URPMI repo
525 # We should test that it's not already a urpmi repo
526 pb_system("chmod 755 $ENV{'PBTMP'}/$bn ; sudo $ENV{'PBTMP'}/$bn 2>&1 > /dev/null","Adding urpmi repository");
527 } else {
528 pb_log(0,"Unable to deal with repository file $i on rpm distro ! Please report to dev team\n");
529 }
530 } elsif ($dtype eq "deb") {
531 if (($bn =~ /\.sources.list$/) && (not -f "/etc/apt/sources.list.d/$bn")) {
532 pb_system("sudo mv $ENV{'PBTMP'}/$bn /etc/apt/sources.list.d","Adding apt repository");
533 pb_system("sudo apt-get update","Updating apt repository");
534 } else {
535 pb_log(0,"Unable to deal with repository file $i on deb distro ! Please report to dev team\n");
536 }
537 } else {
538 pb_log(0,"Unable to deal with repository file $i on that distro ! Please report to dev team\n");
539 }
540}
541return;
542}
543
544=over 4
545
546=item B<pb_distro_get_param>
547
548This function gets the parameter in the conf file from the most precise tuple up to default
549
550=cut
551
552sub pb_distro_get_param {
553
554my $param = "";
555my $ddir = shift;
556my $dver = shift;
557my $darch = shift;
558my $opt = shift;
559
560if (defined $opt->{"$ddir-$dver-$darch"}) {
561 $param = $opt->{"$ddir-$dver-$darch"};
562} elsif (defined $opt->{"$ddir-$dver"}) {
563 $param = $opt->{"$ddir-$dver"};
564} elsif (defined $opt->{"$ddir"}) {
565 $param = $opt->{"$ddir"};
566} elsif (defined $opt->{"default"}) {
567 $param = $opt->{"default"};
568} else {
569 $param = "";
570}
571return($param);
572
573}
574
575
576=back
577
578=head1 WEB SITES
579
580The 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/>.
581
582=head1 USER MAILING LIST
583
584None exists for the moment.
585
586=head1 AUTHORS
587
588The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
589
590=head1 COPYRIGHT
591
592Project-Builder.org is distributed under the GPL v2.0 license
593described in the file C<COPYING> included with the distribution.
594
595=cut
596
597
5981;
Note: See TracBrowser for help on using the repository browser.