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