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

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