[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;
|
---|
[395] | 11 | use ProjectBuilder::Base;
|
---|
[2] | 12 |
|
---|
[329] | 13 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
| 14 |
|
---|
| 15 | use Exporter;
|
---|
| 16 |
|
---|
| 17 | # Export, by default, all the functions into the namespace of
|
---|
| 18 | # any code which uses this module.
|
---|
| 19 |
|
---|
| 20 | our @ISA = qw(Exporter);
|
---|
| 21 | our @EXPORT = qw(pb_distro_init pb_get_distro);
|
---|
| 22 |
|
---|
[391] | 23 | =pod
|
---|
| 24 |
|
---|
| 25 | =head1 NAME
|
---|
| 26 |
|
---|
| 27 | ProjectBuilder::Distribution, part of the project-builder.org - module dealing with distribution detection
|
---|
| 28 |
|
---|
| 29 | =head1 DESCRIPTION
|
---|
| 30 |
|
---|
| 31 | This 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 |
|
---|
[395] | 58 |
|
---|
[391] | 59 | =item B<pb_get_distro>
|
---|
| 60 |
|
---|
| 61 | 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.
|
---|
| 62 |
|
---|
| 63 | On my home machine it would currently report ("mandriva","2008.0").
|
---|
| 64 |
|
---|
| 65 | =cut
|
---|
| 66 |
|
---|
[74] | 67 | sub pb_distro_init {
|
---|
[2] | 68 |
|
---|
[23] | 69 | my $ddir = shift || undef;
|
---|
| 70 | my $dver = shift || undef;
|
---|
[11] | 71 | my $dfam = "unknown";
|
---|
| 72 | my $dtype = "unknown";
|
---|
| 73 | my $dsuf = "unknown";
|
---|
[2] | 74 |
|
---|
[11] | 75 | # If we don't know which distribution we're on, then guess it
|
---|
[74] | 76 | ($ddir,$dver) = pb_get_distro() if ((not defined $ddir) || (not defined $dver));
|
---|
[2] | 77 |
|
---|
[171] | 78 | # There should be unicity of names between ddir dfam and dtype
|
---|
[13] | 79 | # In case of duplicate, bad things can happen
|
---|
[11] | 80 | if (($ddir =~ /debian/) ||
|
---|
| 81 | ($ddir =~ /ubuntu/)) {
|
---|
[13] | 82 | $dfam="du";
|
---|
[11] | 83 | $dtype="deb";
|
---|
| 84 | $dsuf=".$ddir$dver";
|
---|
| 85 | } elsif ($ddir =~ /gentoo/) {
|
---|
[13] | 86 | $dfam="gen";
|
---|
[11] | 87 | $dtype="ebuild";
|
---|
[226] | 88 | $dver="nover";
|
---|
| 89 | $dsuf=".$ddir";
|
---|
[11] | 90 | } elsif ($ddir =~ /slackware/) {
|
---|
[13] | 91 | $dfam="slack";
|
---|
[11] | 92 | $dtype="tgz";
|
---|
| 93 | $dsuf=".$dfam$dver";
|
---|
| 94 | } elsif (($ddir =~ /suse/) ||
|
---|
| 95 | ($ddir =~ /sles/)) {
|
---|
[185] | 96 | if ($ddir =~ /opensuse/) {
|
---|
| 97 | $ddir = "suse";
|
---|
| 98 | }
|
---|
[13] | 99 | $dfam="novell";
|
---|
[11] | 100 | $dtype="rpm";
|
---|
| 101 | $dsuf=".$ddir$dver";
|
---|
| 102 | } elsif (($ddir =~ /redhat/) ||
|
---|
| 103 | ($ddir =~ /rhel/) ||
|
---|
| 104 | ($ddir =~ /fedora/) ||
|
---|
[146] | 105 | ($ddir =~ /vmware/) ||
|
---|
[11] | 106 | ($ddir =~ /centos/)) {
|
---|
[13] | 107 | $dfam="rh";
|
---|
[11] | 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";
|
---|
[146] | 115 | } elsif ($ddir =~ /vmware/) {
|
---|
| 116 | $dsuf=".vwm$dver1";
|
---|
[11] | 117 | } else {
|
---|
| 118 | $dsuf=".$ddir$dver1";
|
---|
| 119 | }
|
---|
| 120 | } elsif (($ddir =~ /mandrake/) ||
|
---|
[171] | 121 | ($ddir =~ /mandrakelinux/) ||
|
---|
[11] | 122 | ($ddir =~ /mandriva/)) {
|
---|
[13] | 123 | $dfam="md";
|
---|
[11] | 124 | $dtype="rpm";
|
---|
[171] | 125 | if ($ddir =~ /mandrakelinux/) {
|
---|
| 126 | $ddir = "mandrake";
|
---|
| 127 | }
|
---|
[11] | 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/) {
|
---|
[13] | 136 | $dfam="bsd";
|
---|
[11] | 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 |
|
---|
| 147 | return($ddir, $dver, $dfam, $dtype, $dsuf);
|
---|
| 148 | }
|
---|
[23] | 149 |
|
---|
[395] | 150 | =item B<pb_distro_init>
|
---|
| 151 |
|
---|
| 152 | 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.
|
---|
| 153 |
|
---|
| 154 | 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.
|
---|
| 155 | 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.
|
---|
| 156 | And "fc" is the extension generated for all Fedora packages (Version will be added by pb).
|
---|
| 157 |
|
---|
| 158 | 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.
|
---|
| 159 |
|
---|
| 160 | Cf: http://linuxmafia.com/faq/Admin/release-files.html
|
---|
| 161 | Ideas taken from http://search.cpan.org/~kerberus/Linux-Distribution-0.14/lib/Linux/Distribution.pm
|
---|
| 162 |
|
---|
| 163 | =cut
|
---|
| 164 |
|
---|
[74] | 165 | sub pb_get_distro {
|
---|
[23] | 166 |
|
---|
| 167 | my $base="/etc";
|
---|
| 168 |
|
---|
| 169 | # List of files that unambiguously indicates what distro we have
|
---|
| 170 | my %single_rel_files = (
|
---|
| 171 | # Tested
|
---|
| 172 | 'gentoo' => 'gentoo-release', # >= 1.6
|
---|
| 173 | 'slackware' => 'slackware-version', # >= 10.2
|
---|
| 174 | 'mandriva' => 'mandriva-release', # >=2006.0
|
---|
[171] | 175 | 'mandrakelinux' => 'mandrakelinux-release',# = 10.2
|
---|
[23] | 176 | 'fedora' => 'fedora-release', # >= 4
|
---|
[146] | 177 | 'vmware' => 'vmware-release', # >= 3
|
---|
[181] | 178 | 'sles' => 'sles-release', # Doesn't exist as of 10
|
---|
[23] | 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
|
---|
| 208 | my %ambiguous_rel_files = (
|
---|
[171] | 209 | 'mandrake' => 'mandrake-release', # <= 10.1
|
---|
[391] | 210 | 'debian' => 'debian_version', # >= 3.1
|
---|
[23] | 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
|
---|
| 218 | my %distro_similar = (
|
---|
[171] | 219 | 'mandrake' => ['mandrake', 'mandrakelinux'],
|
---|
[391] | 220 | 'debian' => ['debian', 'ubuntu'],
|
---|
[185] | 221 | 'suse' => ['suse', 'sles', 'opensuse'],
|
---|
[146] | 222 | 'redhat' => ['redhat', 'rhel', 'centos', 'mandrake', 'vmware'],
|
---|
[24] | 223 | 'lsb' => ['ubuntu', 'debian', 'lsb'],
|
---|
| 224 | );
|
---|
[23] | 225 |
|
---|
| 226 | my %distro_match = (
|
---|
| 227 | # Tested
|
---|
| 228 | 'gentoo' => '.* version (.+)',
|
---|
[24] | 229 | 'slackware' => 'S[^ ]* (.+)$',
|
---|
[23] | 230 | # There should be no ambiguity between potential ambiguous distro
|
---|
[171] | 231 | 'mandrakelinux' => 'Mandrakelinux release (.+) \(',
|
---|
[24] | 232 | 'mandrake' => 'Mandr[^ ]* release (.+) \(',
|
---|
| 233 | 'mandriva' => 'Mandr[^ ]* [^ ]* release (.+) \(',
|
---|
| 234 | 'fedora' => 'Fedora .*release (\d+) \(',
|
---|
[146] | 235 | 'vmware' => 'VMware ESX Server (\d+) \(',
|
---|
[24] | 236 | 'rhel' => 'Red Hat Enterprise Linux .*release (.+) \(',
|
---|
| 237 | 'centos' => '.*CentOS .*release (.+) ',
|
---|
| 238 | 'redhat' => 'Red Hat Linux release (.+) \(',
|
---|
[185] | 239 | 'sles' => 'SUSE .* Enterprise Server (\d+) \(',
|
---|
[188] | 240 | 'suse' => 'SUSE LINUX (\d.+) \(',
|
---|
[185] | 241 | 'opensuse' => 'openSUSE (\d.+) \(',
|
---|
[23] | 242 | 'lsb' => '.*[^Ubunt].*\nDISTRIB_RELEASE=(.+)',
|
---|
[203] | 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
|
---|
[265] | 246 | # Same is true for Ubuntu 7.10 :-(
|
---|
[23] | 247 | 'ubuntu' => '.*Ubuntu.*\nDISTRIB_RELEASE=(.+)',
|
---|
[391] | 248 | 'debian' => '(.+)',
|
---|
[23] | 249 | # Not tested
|
---|
| 250 | 'arch' => '.* ([0-9.]+) .*',
|
---|
| 251 | 'redflag' => 'Red Flag (?:Desktop|Linux) (?:release |\()(.*?)(?: \(.+)?\)',
|
---|
| 252 | );
|
---|
| 253 |
|
---|
| 254 | my $release;
|
---|
| 255 | my $distro;
|
---|
| 256 |
|
---|
[391] | 257 | # Begin to test presence of non-ambiguous files
|
---|
[23] | 258 | # that way we reduce the choice
|
---|
[24] | 259 | my ($d,$r);
|
---|
| 260 | while (($d,$r) = each %single_rel_files) {
|
---|
[171] | 261 | if (-f "$base/$r" && ! -l "$base/$r") {
|
---|
[74] | 262 | my $tmp=pb_get_content("$base/$r");
|
---|
[23] | 263 | # Found the only possibility.
|
---|
| 264 | # Try to get version and return
|
---|
[24] | 265 | if (defined ($distro_match{$d})) {
|
---|
| 266 | ($release) = $tmp =~ m/$distro_match{$d}/m;
|
---|
[23] | 267 | } else {
|
---|
[24] | 268 | print STDERR "Unable to find $d version in $r\n";
|
---|
[23] | 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 |
|
---|
[24] | 276 | while (($d,$r) = each %ambiguous_rel_files) {
|
---|
[23] | 277 | if (-f "$base/$r" && !-l "$base/$r"){
|
---|
| 278 | # Found one possibility.
|
---|
| 279 | # Get all distros concerned by that file
|
---|
[74] | 280 | my $tmp=pb_get_content("$base/$r");
|
---|
[24] | 281 | my $found = 0;
|
---|
| 282 | my $ptr = $distro_similar{$d};
|
---|
[315] | 283 | #pb_log(2,$LOG "amb: ".Dumper($ptr)."\n" if ($debug >= 1);
|
---|
[24] | 284 | $release = "unknown";
|
---|
| 285 | foreach my $dd (@$ptr) {
|
---|
[315] | 286 | #pb_log(2,$LOG "check $dd\n" if ($debug >= 1);
|
---|
[23] | 287 | # Try to check pattern
|
---|
[24] | 288 | if (defined $distro_match{$dd}) {
|
---|
[315] | 289 | #pb_log(2,$LOG "cmp: $distro_match{$dd} - vs - $tmp\n" if ($debug >= 1);
|
---|
[24] | 290 | ($release) = $tmp =~ m/$distro_match{$dd}/m;
|
---|
| 291 | if ((defined $release) && ($release ne "unknown")) {
|
---|
| 292 | $distro = $dd;
|
---|
| 293 | $found = 1;
|
---|
| 294 | last;
|
---|
| 295 | }
|
---|
[23] | 296 | }
|
---|
| 297 | }
|
---|
| 298 | if ($found == 0) {
|
---|
[24] | 299 | print STDERR "Unable to find $d version in $r\n";
|
---|
[23] | 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 | }
|
---|
| 307 | return("unknown","unknown");
|
---|
[24] | 308 | }
|
---|
[23] | 309 |
|
---|
[395] | 310 | =back
|
---|
[23] | 311 |
|
---|
[395] | 312 | =head1 WEB SITES
|
---|
[23] | 313 |
|
---|
[395] | 314 | 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/>.
|
---|
| 315 |
|
---|
| 316 | =head1 USER MAILING LIST
|
---|
| 317 |
|
---|
| 318 | None exists for the moment.
|
---|
| 319 |
|
---|
| 320 | =head1 AUTHORS
|
---|
| 321 |
|
---|
| 322 | The 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 |
|
---|
| 326 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
| 327 | described in the file C<COPYING> included with the distribution.
|
---|
| 328 |
|
---|
| 329 | =cut
|
---|
| 330 |
|
---|
| 331 |
|
---|
[11] | 332 | 1;
|
---|