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

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