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

Last change on this file since 1071 was 1071, checked in by bruno, 9 years ago

r3947@sge91-1-82-234-15-218: bruno | 2010-07-10 18:40:35 +0200

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