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

Last change on this file since 1167 was 1167, checked in by Bruno Cornec, 13 years ago

removedot only applies to the extension generated not to the rest of the distro ver (so filters have the right name, ...)

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