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

Last change on this file since 964 was 964, checked in by Bruno Cornec, 14 years ago

r3659@localhost: bruno | 2010-02-07 02:02:22 +0100

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