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

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