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

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