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

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