File Coverage

blib/lib/Linux/Distribution.pm
Criterion Covered Total %
statement 66 68 97.0
branch 22 30 73.3
condition 12 20 60.0
subroutine 8 8 100.0
pod 0 3 0.0
total 108 129 83.7


line stmt bran cond sub pod time code
1             package Linux::Distribution;
2              
3 13     13   223651 use 5.006000;
  13         46  
  13         603  
4 13     13   58 use strict;
  13         18  
  13         335  
5 13     13   52 use warnings;
  13         24  
  13         10376  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10              
11             our @EXPORT_OK = qw( distribution_name distribution_version );
12              
13             our $VERSION = '0.23';
14              
15             our $release_files_directory='/etc';
16             our $standard_release_file = 'lsb-release';
17              
18             our %release_files = (
19             'gentoo-release' => 'gentoo',
20             'fedora-release' => 'fedora',
21             'centos-release' => 'centos',
22             'enterprise-release' => 'oracle enterprise linux',
23             'turbolinux-release' => 'turbolinux',
24             'mandrake-release' => 'mandrake',
25             'mandrakelinux-release' => 'mandrakelinux',
26             'debian_version' => 'debian',
27             'debian_release' => 'debian',
28             'SuSE-release' => 'suse',
29             'knoppix-version' => 'knoppix',
30             'yellowdog-release' => 'yellowdog',
31             'slackware-version' => 'slackware',
32             'slackware-release' => 'slackware',
33             'redflag-release' => 'redflag',
34             'redhat-release' => 'redhat',
35             'redhat_version' => 'redhat',
36             'conectiva-release' => 'conectiva',
37             'immunix-release' => 'immunix',
38             'tinysofa-release' => 'tinysofa',
39             'trustix-release' => 'trustix',
40             'adamantix_version' => 'adamantix',
41             'yoper-release' => 'yoper',
42             'arch-release' => 'arch',
43             'libranet_version' => 'libranet',
44             'va-release' => 'va-linux',
45             'pardus-release' => 'pardus',
46             'system-release' => 'amazon',
47             'CloudLinux-release' => 'CloudLinux',
48             );
49              
50             our %version_match = (
51             'gentoo' => 'Gentoo Base System release (.*)',
52             'debian' => '(.+)',
53             'suse' => 'VERSION = (.*)',
54             'fedora' => 'Fedora(?: Core)? release (\d+) \(',
55             'redflag' => 'Red Flag (?:Desktop|Linux) (?:release |\()(.*?)(?: \(.+)?\)',
56             'redhat' => 'Red Hat(?: Enterprise)? Linux(?: Server)? release (.*) \(',
57             'oracle enterprise linux' => 'Enterprise Linux Server release (.+) \(',
58             'slackware' => '^Slackware (.+)$',
59             'pardus' => '^Pardus (.+)$',
60             'centos' => '^CentOS(?: Linux)? release (.+) \(',
61             'scientific' => '^Scientific Linux release (.+) \(',
62             'amazon' => 'Amazon Linux AMI release (.+)$',
63             'CloudLinux' => 'CloudLinux Server release (\S+)'
64             );
65              
66              
67             if ($^O ne 'linux') {
68             # require Carp;
69             # Carp::croak('you are trying to use a linux specific module on a different OS');
70             }
71              
72             sub new {
73 15     15 0 213 my %self = (
74             'DISTRIB_ID' => '',
75             'DISTRIB_RELEASE' => '',
76             'DISTRIB_CODENAME' => '',
77             'DISTRIB_DESCRIPTION' => '',
78             'release_file' => '',
79             'pattern' => ''
80             );
81            
82 15         54 return bless \%self;
83             }
84              
85             sub distribution_name {
86 14   66 14 0 144 my $self = shift || new();
87 14         23 my $distro;
88 14 100       82 if ($distro = $self->_get_lsb_info()){
89 3 50       25 return $distro if ($distro);
90             }
91              
92 11         31 foreach (qw(enterprise-release fedora-release CloudLinux-release)) {
93 30 100 66     249 if (-f "$release_files_directory/$_" && !-l "$release_files_directory/$_"){
94 3 50 33     50 if (-f "$release_files_directory/$_" && !-l "$release_files_directory/$_"){
95 3         9 $self->{'DISTRIB_ID'} = $release_files{$_};
96 3         5 $self->{'release_file'} = $_;
97 3         10 return $self->{'DISTRIB_ID'};
98             }
99             }
100             }
101              
102 8         76 foreach (keys %release_files) {
103 136 100 66     1138 if (-f "$release_files_directory/$_" && !-l "$release_files_directory/$_"){
104 8 50 33     136 if (-f "$release_files_directory/$_" && !-l "$release_files_directory/$_"){
105 8 100       31 if ( $release_files{$_} eq 'redhat' ) {
106 4         11 foreach my $rhel_deriv ('centos','scientific',) {
107 7         21 $self->{'pattern'} = $version_match{$rhel_deriv};
108 7         11 $self->{'release_file'}='redhat-release';
109 7 100       15 if ( $self->_get_file_info() ) {
110 2         5 $self->{'DISTRIB_ID'} = $rhel_deriv;
111 2         4 $self->{'release_file'} = $_;
112 2         7 return $self->{'DISTRIB_ID'};
113             }
114             }
115 2         5 $self->{'pattern'}='';
116             }
117 6         17 $self->{'release_file'} = $_;
118 6         14 $self->{'DISTRIB_ID'} = $release_files{$_};
119 6         400 return $self->{'DISTRIB_ID'};
120             }
121             }
122             }
123             undef
124 0         0 }
125              
126             sub distribution_version {
127 13   66 13 0 7791 my $self = shift || new();
128 13         22 my $release;
129 13 100       45 return $release if ($release = $self->_get_lsb_info('DISTRIB_RELEASE'));
130 11 50       469 if (! $self->{'DISTRIB_ID'}){
131 0 0       0 $self->distribution_name() or die 'No version because no distro.';
132             }
133 11         60 $self->{'pattern'} = $version_match{$self->{'DISTRIB_ID'}};
134 11         46 $release = $self->_get_file_info();
135 11         42 $self->{'DISTRIB_RELEASE'} = $release;
136 11         606 return $release;
137             }
138              
139             sub _get_lsb_info {
140 27     27   39 my $self = shift;
141 27   100     120 my $field = shift || 'DISTRIB_ID';
142 27         46 my $tmp = $self->{'release_file'};
143 27 100       397 if ( -r "$release_files_directory/" . $standard_release_file ) {
144 5         8 $self->{'release_file'} = $standard_release_file;
145 5         13 $self->{'pattern'} = $field . '=["]?([^"]+)["]?';
146 5         11 my $info = $self->_get_file_info();
147 5 50       12 if ($info){
148 5         9 $self->{$field} = $info;
149 5         20 return $info
150             }
151             }
152 22         41 $self->{'release_file'} = $tmp;
153 22         33 $self->{'pattern'} = '';
154 22         76 undef;
155             }
156              
157             sub _get_file_info {
158 23     23   39 my $self = shift;
159 23 50       658 open my $fh, '<', "$release_files_directory/" . $self->{'release_file'} or die 'Cannot open file: '.$release_files_directory.'/' . $self->{'release_file'};
160 23         42 my $info = '';
161 23         34 local $_;
162 23         332 while (<$fh>){
163 27         524 chomp $_;
164 27         519 ($info) = $_ =~ m/$self->{'pattern'}/;
165 27 100       1159 return "\L$info" if $info;
166             }
167 5         42 undef;
168             }
169              
170             1;
171             __END__