| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
|
| 4 |
######################################################################## |
| 5 |
|
| 6 |
package Linux::Distribution; |
| 7 |
|
| 8 |
use 5.006000; |
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
|
| 12 |
require Exporter; |
| 13 |
|
| 14 |
our @ISA = qw(Exporter); |
| 15 |
|
| 16 |
our @EXPORT_OK = qw( distribution_name distribution_version ); |
| 17 |
|
| 18 |
our $VERSION = '0.14.1'; |
| 19 |
|
| 20 |
our $standard_release_file = 'lsb-release'; |
| 21 |
|
| 22 |
our %primary_release_files = ( |
| 23 |
'debian_version' => 'debian', |
| 24 |
'debian_release' => 'debian', |
| 25 |
'redhat-release' => 'redhat', |
| 26 |
'redhat_version' => 'redhat', |
| 27 |
); |
| 28 |
|
| 29 |
our %secondary_release_files = ( |
| 30 |
'gentoo-release' => 'gentoo', |
| 31 |
'fedora-release' => 'fedora', |
| 32 |
'turbolinux-release' => 'turbolinux', |
| 33 |
'mandrake-release' => 'mandrake', |
| 34 |
'mandrakelinux-release' => 'mandrakelinux', |
| 35 |
'mandriva-release' => 'mandriva', |
| 36 |
'SuSE-release' => 'suse', |
| 37 |
'knoppix-version' => 'knoppix', |
| 38 |
'yellowdog-release' => 'yellowdog', |
| 39 |
'slackware-version' => 'slackware', |
| 40 |
'slackware-release' => 'slackware', |
| 41 |
'redflag-release' => 'redflag', |
| 42 |
'conectiva-release' => 'conectiva', |
| 43 |
'immunix-release' => 'immunix', |
| 44 |
'tinysofa-release' => 'tinysofa', |
| 45 |
'trustix-release' => 'trustix', |
| 46 |
'adamantix_version' => 'adamantix', |
| 47 |
'yoper-release' => 'yoper', |
| 48 |
'arch-release' => 'arch', |
| 49 |
'libranet_version' => 'libranet', |
| 50 |
'va-release' => 'va-linux', |
| 51 |
'xandros-desktop-version' => 'xandros', |
| 52 |
); |
| 53 |
|
| 54 |
our %version_match = ( |
| 55 |
'gentoo' => 'Gentoo Base System version (.*)', |
| 56 |
'debian' => '(.+)', |
| 57 |
'suse' => 'VERSION = (.*)', |
| 58 |
'fedora' => 'Fedora Core release (\d+) \(', |
| 59 |
'redflag' => 'Red Flag (?:Desktop|Linux) (?:release |\()(.*?)(?: \(.+)?\)', |
| 60 |
'redhat' => 'Red Hat (?:Desktop|Linux) release (.*) \(', |
| 61 |
'slackware' => '^Slackware (.+)$', |
| 62 |
'mandriva' => 'Mandriva Linux release (\d+.\d+) \(', |
| 63 |
'arch' => 'Arch Linux (.*) \(', |
| 64 |
'xandros' => 'Version: Xandros Desktop OS (.+)' |
| 65 |
); |
| 66 |
|
| 67 |
|
| 68 |
if ($^O ne 'linux') { |
| 69 |
require Carp; |
| 70 |
Carp::croak 'you are trying to use a linux specific module on a different OS'; |
| 71 |
} |
| 72 |
|
| 73 |
sub new { |
| 74 |
my %self = ( |
| 75 |
'DISTRIB_ID' => '', |
| 76 |
'DISTRIB_RELEASE' => '', |
| 77 |
'DISTRIB_CODENAME' => '', |
| 78 |
'DISTRIB_DESCRIPTION' => '', |
| 79 |
'release_file' => '', |
| 80 |
'pattern' => '' |
| 81 |
); |
| 82 |
|
| 83 |
return bless \%self; |
| 84 |
} |
| 85 |
|
| 86 |
sub distribution_name { |
| 87 |
my $self = shift || new(); |
| 88 |
my $distro; |
| 89 |
if ($distro = $self->_get_lsb_info()){ |
| 90 |
return $distro if ($distro); |
| 91 |
} |
| 92 |
foreach (keys %secondary_release_files) { |
| 93 |
if (-f "/etc/$_" && !-l "/etc/$_"){ |
| 94 |
if (-f "/etc/$_" && !-l "/etc/$_"){ |
| 95 |
$self->{'DISTRIB_ID'} = $secondary_release_files{$_}; |
| 96 |
$self->{'release_file'} = $_; |
| 97 |
return $self->{'DISTRIB_ID'}; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
foreach (keys %primary_release_files) { |
| 102 |
if (-f "/etc/$_" && !-l "/etc/$_"){ |
| 103 |
if (-f "/etc/$_" && !-l "/etc/$_"){ |
| 104 |
$self->{'DISTRIB_ID'} = $primary_release_files{$_}; |
| 105 |
$self->{'release_file'} = $_; |
| 106 |
return $self->{'DISTRIB_ID'}; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
undef |
| 111 |
} |
| 112 |
|
| 113 |
sub distribution_version { |
| 114 |
my $self = shift || new(); |
| 115 |
my $release; |
| 116 |
return $release if ($release = $self->_get_lsb_info('DISTRIB_RELEASE')); |
| 117 |
if (! $self->{'DISTRIB_ID'}){ |
| 118 |
$self->distribution_name() or die 'No version because no distro.'; |
| 119 |
} |
| 120 |
$self->{'pattern'} = $version_match{$self->{'DISTRIB_ID'}}; |
| 121 |
return "unknown" if (! $self->{'pattern'}); |
| 122 |
return "unknown" if (! ($release = $self->_get_file_info())); |
| 123 |
$self->{'DISTRIB_RELEASE'} = $release; |
| 124 |
return $release; |
| 125 |
} |
| 126 |
|
| 127 |
sub _get_lsb_info { |
| 128 |
my $self = shift; |
| 129 |
my $field = shift || 'DISTRIB_ID'; |
| 130 |
my $tmp = $self->{'release_file'}; |
| 131 |
if ( -r '/etc/' . $standard_release_file ) { |
| 132 |
$self->{'release_file'} = $standard_release_file; |
| 133 |
$self->{'pattern'} = $field . '=(.+)'; |
| 134 |
my $info = $self->_get_file_info(); |
| 135 |
if ($info){ |
| 136 |
$self->{$field} = $info; |
| 137 |
return $info |
| 138 |
} |
| 139 |
} |
| 140 |
$self->{'release_file'} = $tmp; |
| 141 |
$self->{'pattern'} = ''; |
| 142 |
undef; |
| 143 |
} |
| 144 |
|
| 145 |
sub _get_file_info { |
| 146 |
my $self = shift; |
| 147 |
open FH, '/etc/' . $self->{'release_file'} or die 'Cannot open file: /etc/' . $self->{'release_file'}; |
| 148 |
my $info = ''; |
| 149 |
while (<FH>){ |
| 150 |
chomp $_; |
| 151 |
($info) = $_ =~ m/$self->{'pattern'}/; |
| 152 |
return "\L$info" if $info; |
| 153 |
} |
| 154 |
undef; |
| 155 |
} |
| 156 |
|
| 157 |
1; |
| 158 |
|
| 159 |
|
| 160 |
# Simple script to use the above perl module to print the distro name |
| 161 |
if (my $distro = distribution_name) { |
| 162 |
my $version = distribution_version(); |
| 163 |
print "$distro $version\n"; |
| 164 |
} else { |
| 165 |
print "distribution unknown\n"; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
__END__ |
| 171 |
|
| 172 |
|
| 173 |
=head1 NAME |
| 174 |
|
| 175 |
linux_distro - tool for printing the current linux distribution |
| 176 |
|
| 177 |
=head1 SYNOPSIS |
| 178 |
|
| 179 |
linux_distro |
| 180 |
|
| 181 |
=head1 DESCRIPTION |
| 182 |
|
| 183 |
This is a simple tool that tries to guess on what linux distribution we are running by looking for release's files in /etc. It now looks for 'lsb-release' first as that should be the most correct and adds ubuntu support. Secondly, it will look for the distro specific files. |
| 184 |
|
| 185 |
It currently recognizes slackware, debian, suse, fedora, redhat, turbolinux, yellowdog, knoppix, mandrake, conectiva, immunix, tinysofa, va-linux, trustix, adamantix, yoper, arch-linux, libranet, gentoo, ubuntu, redflag and xandros. |
| 186 |
|
| 187 |
It has function to get the version for debian, suse, redhat, gentoo, slackware, redflag and ubuntu(lsb). People running unsupported distro's are greatly encouraged to submit patches :-) |
| 188 |
|
| 189 |
=head2 EXPORT |
| 190 |
|
| 191 |
None by default. |
| 192 |
|
| 193 |
=head1 TODO |
| 194 |
|
| 195 |
Add the capability of recognize the version of the distribution for all recognized distributions. |
| 196 |
|
| 197 |
=head1 AUTHORS |
| 198 |
|
| 199 |
Alberto Re, E<lt>[email protected]<gt> |
| 200 |
Judith Lebzelter, E<lt>[email protected]<gt> |
| 201 |
|
| 202 |
=head1 COPYRIGHT AND LICENSE |
| 203 |
|
| 204 |
This library is free software; you can redistribute it and/or modify |
| 205 |
it under the same terms as Perl itself, either Perl version 5.8.5 or, |
| 206 |
at your option, any later version of Perl 5 you may have available. |
| 207 |
|
| 208 |
=cut |
| 209 |
|