source: ProjectBuilder/devel/pb/lib/ProjectBuilder/Distribution.pm@ 396

Last change on this file since 396 was 395, checked in by Bruno Cornec, 16 years ago
  • Move all reusable functions into Base
  • Move all pb only functions into pb
  • pod doc for Base begining
File size: 9.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 ProjectBuilder::Base;
12
13# Inherit from the "Exporter" module which handles exporting functions.
14
15use Exporter;
16
17# Export, by default, all the functions into the namespace of
18# any code which uses this module.
19
20our @ISA = qw(Exporter);
21our @EXPORT = qw(pb_distro_init pb_get_distro);
22
23=pod
24
25=head1 NAME
26
27ProjectBuilder::Distribution, part of the project-builder.org - module dealing with distribution detection
28
29=head1 DESCRIPTION
30
31This modules provides functions to allow detection of Linux distributions, and giving back some attributes concerning them.
32
33=head1 SYNOPSIS
34
35 use ProjectBuilder::Distribution;
36
37 #
38 # Return information on the running distro
39 #
40 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init();
41 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n";
42 #
43 # Return information on the requested distro
44 #
45 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init("ubuntu","7.10");
46 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n";
47 #
48 # Return information on the running distro
49 #
50 my ($ddir,$dver) = pb_get_distro();
51 my ($ddir, $dver, $dfam, $dtype, $pbsuf) = pb_distro_init($ddir,$dver);
52 print "distro tuple: ".Dumper($ddir, $dver, $dfam, $dtype, $pbsuf)."\n";
53
54=head1 USAGE
55
56=over 4
57
58
59=item B<pb_get_distro>
60
61This 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.
62
63On my home machine it would currently report ("mandriva","2008.0").
64
65=cut
66
67sub pb_distro_init {
68
69my $ddir = shift || undef;
70my $dver = shift || undef;
71my $dfam = "unknown";
72my $dtype = "unknown";
73my $dsuf = "unknown";
74
75# If we don't know which distribution we're on, then guess it
76($ddir,$dver) = pb_get_distro() if ((not defined $ddir) || (not defined $dver));
77
78# There should be unicity of names between ddir dfam and dtype
79# In case of duplicate, bad things can happen
80if (($ddir =~ /debian/) ||
81 ($ddir =~ /ubuntu/)) {
82 $dfam="du";
83 $dtype="deb";
84 $dsuf=".$ddir$dver";
85} elsif ($ddir =~ /gentoo/) {
86 $dfam="gen";
87 $dtype="ebuild";
88 $dver="nover";
89 $dsuf=".$ddir";
90} elsif ($ddir =~ /slackware/) {
91 $dfam="slack";
92 $dtype="tgz";
93 $dsuf=".$dfam$dver";
94} elsif (($ddir =~ /suse/) ||
95 ($ddir =~ /sles/)) {
96 if ($ddir =~ /opensuse/) {
97 $ddir = "suse";
98 }
99 $dfam="novell";
100 $dtype="rpm";
101 $dsuf=".$ddir$dver";
102} elsif (($ddir =~ /redhat/) ||
103 ($ddir =~ /rhel/) ||
104 ($ddir =~ /fedora/) ||
105 ($ddir =~ /vmware/) ||
106 ($ddir =~ /centos/)) {
107 $dfam="rh";
108 $dtype="rpm";
109 my $dver1 = $dver;
110 $dver1 =~ s/\.//;
111 if ($ddir =~ /fedora/) {
112 $dsuf=".fc$dver1";
113 } elsif ($ddir =~ /redhat/) {
114 $dsuf=".rh$dver1";
115 } elsif ($ddir =~ /vmware/) {
116 $dsuf=".vwm$dver1";
117 } else {
118 $dsuf=".$ddir$dver1";
119 }
120} elsif (($ddir =~ /mandrake/) ||
121 ($ddir =~ /mandrakelinux/) ||
122 ($ddir =~ /mandriva/)) {
123 $dfam="md";
124 $dtype="rpm";
125 if ($ddir =~ /mandrakelinux/) {
126 $ddir = "mandrake";
127 }
128 if ($ddir =~ /mandrake/) {
129 my $dver1 = $dver;
130 $dver1 =~ s/\.//;
131 $dsuf=".mdk$dver1";
132 } else {
133 $dsuf=".mdv$dver";
134 }
135} elsif ($ddir =~ /freebsd/) {
136 $dfam="bsd";
137 $dtype="port";
138 my $dver1 = $dver;
139 $dver1 =~ s/\.//;
140 $dsuf=".$dfam$dver1";
141} else {
142 $dfam="unknown";
143 $dtype="unknown";
144 $dsuf="unknown";
145}
146
147return($ddir, $dver, $dfam, $dtype, $dsuf);
148}
149
150=item B<pb_distro_init>
151
152This function returns a list of 5 parameters indicating the distribution name, version, family, type of build system and suffix of packages of the underlying Linux distribution. The value of the 5 fields may be "unknown" in case the function was unable to recognize on which distribution it is running.
153
154As an example, Ubuntu and Debian are in the same "du" family. As well as RedHat, RHEL, CentOS, fedora are on the same "rh" family.
155Mandriva, Open SuSE and Fedora have all the same "rpm" type of build system. Ubuntu ad Debian have the same "deb" type of build system.
156And "fc" is the extension generated for all Fedora packages (Version will be added by pb).
157
158When 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.
159
160Cf: http://linuxmafia.com/faq/Admin/release-files.html
161Ideas taken from http://search.cpan.org/~kerberus/Linux-Distribution-0.14/lib/Linux/Distribution.pm
162
163=cut
164
165sub pb_get_distro {
166
167my $base="/etc";
168
169# List of files that unambiguously indicates what distro we have
170my %single_rel_files = (
171# Tested
172 'gentoo' => 'gentoo-release', # >= 1.6
173 'slackware' => 'slackware-version', # >= 10.2
174 'mandriva' => 'mandriva-release', # >=2006.0
175 'mandrakelinux' => 'mandrakelinux-release',# = 10.2
176 'fedora' => 'fedora-release', # >= 4
177 'vmware' => 'vmware-release', # >= 3
178 'sles' => 'sles-release', # Doesn't exist as of 10
179# Untested
180 'knoppix' => 'knoppix_version', #
181 'yellowdog' => 'yellowdog-release', #
182 'esmith' => 'e-smith-release', #
183 'turbolinux' => 'turbolinux-release', #
184 'blackcat' => 'blackcat-release', #
185 'aurox' => 'aurox-release', #
186 'annvix' => 'annvix-release', #
187 'cobalt' => 'cobalt-release', #
188 'redflag' => 'redflag-release', #
189 'ark' => 'ark-release', #
190 'pld' => 'pld-release', #
191 'nld' => 'nld-release', #
192 'lfs' => 'lfs-release', #
193 'mk' => 'mk-release', #
194 'conectiva' => 'conectiva-release', #
195 'immunix' => 'immunix-release', #
196 'tinysofa' => 'tinysofa-release', #
197 'trustix' => 'trustix-release', #
198 'adamantix' => 'adamantix_version', #
199 'yoper' => 'yoper-release', #
200 'arch' => 'arch-release', #
201 'libranet' => 'libranet_version', #
202 'valinux' => 'va-release', #
203 'yellowdog' => 'yellowdog-release', #
204 'ultrapenguin' => 'ultrapenguin-release', #
205 );
206
207# List of files that ambiguously indicates what distro we have
208my %ambiguous_rel_files = (
209 'mandrake' => 'mandrake-release', # <= 10.1
210 'debian' => 'debian_version', # >= 3.1
211 'suse' => 'SuSE-release', # >= 10.0
212 'redhat' => 'redhat-release', # >= 7.3
213 'lsb' => 'lsb-release', # ???
214 );
215
216# Should have the same keys as the previous one.
217# If ambiguity, which other distributions should be checked
218my %distro_similar = (
219 'mandrake' => ['mandrake', 'mandrakelinux'],
220 'debian' => ['debian', 'ubuntu'],
221 'suse' => ['suse', 'sles', 'opensuse'],
222 'redhat' => ['redhat', 'rhel', 'centos', 'mandrake', 'vmware'],
223 'lsb' => ['ubuntu', 'debian', 'lsb'],
224 );
225
226my %distro_match = (
227# Tested
228 'gentoo' => '.* version (.+)',
229 'slackware' => 'S[^ ]* (.+)$',
230# There should be no ambiguity between potential ambiguous distro
231 'mandrakelinux' => 'Mandrakelinux release (.+) \(',
232 'mandrake' => 'Mandr[^ ]* release (.+) \(',
233 'mandriva' => 'Mandr[^ ]* [^ ]* release (.+) \(',
234 'fedora' => 'Fedora .*release (\d+) \(',
235 'vmware' => 'VMware ESX Server (\d+) \(',
236 'rhel' => 'Red Hat Enterprise Linux .*release (.+) \(',
237 'centos' => '.*CentOS .*release (.+) ',
238 'redhat' => 'Red Hat Linux release (.+) \(',
239 'sles' => 'SUSE .* Enterprise Server (\d+) \(',
240 'suse' => 'SUSE LINUX (\d.+) \(',
241 'opensuse' => 'openSUSE (\d.+) \(',
242 'lsb' => '.*[^Ubunt].*\nDISTRIB_RELEASE=(.+)',
243# Ubuntu 6.06 includes a /etc/debian_version file that needs to be
244# renamed to /etc/debian_version.sav as there is no way to distinguish
245# Ubuntu 6.06 from debian testing otherwise
246# Same is true for Ubuntu 7.10 :-(
247 'ubuntu' => '.*Ubuntu.*\nDISTRIB_RELEASE=(.+)',
248 'debian' => '(.+)',
249# Not tested
250 'arch' => '.* ([0-9.]+) .*',
251 'redflag' => 'Red Flag (?:Desktop|Linux) (?:release |\()(.*?)(?: \(.+)?\)',
252);
253
254my $release;
255my $distro;
256
257# Begin to test presence of non-ambiguous files
258# that way we reduce the choice
259my ($d,$r);
260while (($d,$r) = each %single_rel_files) {
261 if (-f "$base/$r" && ! -l "$base/$r") {
262 my $tmp=pb_get_content("$base/$r");
263 # Found the only possibility.
264 # Try to get version and return
265 if (defined ($distro_match{$d})) {
266 ($release) = $tmp =~ m/$distro_match{$d}/m;
267 } else {
268 print STDERR "Unable to find $d version in $r\n";
269 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
270 $release = "unknown";
271 }
272 return($d,$release);
273 }
274}
275
276while (($d,$r) = each %ambiguous_rel_files) {
277 if (-f "$base/$r" && !-l "$base/$r"){
278 # Found one possibility.
279 # Get all distros concerned by that file
280 my $tmp=pb_get_content("$base/$r");
281 my $found = 0;
282 my $ptr = $distro_similar{$d};
283 #pb_log(2,$LOG "amb: ".Dumper($ptr)."\n" if ($debug >= 1);
284 $release = "unknown";
285 foreach my $dd (@$ptr) {
286 #pb_log(2,$LOG "check $dd\n" if ($debug >= 1);
287 # Try to check pattern
288 if (defined $distro_match{$dd}) {
289 #pb_log(2,$LOG "cmp: $distro_match{$dd} - vs - $tmp\n" if ($debug >= 1);
290 ($release) = $tmp =~ m/$distro_match{$dd}/m;
291 if ((defined $release) && ($release ne "unknown")) {
292 $distro = $dd;
293 $found = 1;
294 last;
295 }
296 }
297 }
298 if ($found == 0) {
299 print STDERR "Unable to find $d version in $r\n";
300 print STDERR "Please report to the maintainer bruno_at_project-builder.org\n";
301 $release = "unknown";
302 } else {
303 return($distro,$release);
304 }
305 }
306}
307return("unknown","unknown");
308}
309
310=back
311
312=head1 WEB SITES
313
314The 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/>.
315
316=head1 USER MAILING LIST
317
318None exists for the moment.
319
320=head1 AUTHORS
321
322The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
323
324=head1 COPYRIGHT
325
326Project-Builder.org is distributed under the GPL v2.0 license
327described in the file C<COPYING> included with the distribution.
328
329=cut
330
331
3321;
Note: See TracBrowser for help on using the repository browser.