Codebase list liblinux-distribution-perl / 08fee96
[svn-inject] Installing original source of liblinux-distribution-perl (0.20) Takaki Taniguchi 13 years ago
8 changed file(s) with 407 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 use strict;
1 use Module::Build;
2 #created by eumm-migrate.pl
3
4 unless (lc $^O eq "linux") {
5 die "OS unsupported\n";
6 }
7
8 my $build = Module::Build->new(
9 'auto_configure_requires' => 0,
10 'dist_author' => 'Alexandr Ciornii <alexchorny@gmail.com>',
11 'module_name' => 'Linux::Distribution',
12 'meta_merge' => {
13 'resources' => {
14 'repository' => 'https://github.com/chorny/Linux-Distribution'
15 }
16 },
17 'requires' => {
18 'perl' => '5.006',
19 'Test::More' => 0
20 },
21 'license' => 'perl',
22 'dist_version_from' => 'lib/Linux/Distribution.pm'
23 );
24
25 $build->create_build_script();
0 * 2010-12-05 version 0.20
1
2 Fixed version detection for Gentoo
3 CentOS detection (modified patch from XSAWYERX)
4 Start writing distribution-oriented tests
5 Build.PL added
6
7 * 2008-11-02 version 0.14_01
8
9 Remove bareword filehandles and 2-arg open
10 Will not install if not on Linux
11 Modernize Makefile.PL
12 Fix for 5.10
13 Pardus support (by BURAK, RT#27357)
14 Fix Fedora recognition (reported by sagarshahATsofthome.net, RT#11996)
15
16 * 2005-02-16
17
18 Added support for Redflag.
19
20 * 2006-01-16
21 Thanks to Judith Lebzelter now the module gets advantage of the LSB's /etc/lsb-release file.
22 Added support for Ubuntu and version information retrieval for gentoo, debian, suse, fedora,
23 redhat and slackware.
24
25 * 2005-03-10
26 Added support for yoper, arch-linux and libranet.
27
28 * 2005-03-09
29 Cleaned code.
30
31 * 2005-03-08
32 Version 0.03.
33 Added support for Adamantix and Trustix.
34
35 * 2005-03-07
36 Reduced needed perl version to 5.6.0.
37 Removed copyright line from pod documentation.
38
39 * 2005-03-06
40 Version 0.02.
41
42 * 2005-03-06
43 Added support for immunix, conectiva, tinysofa and va-linux.
44
45 * 2005-03-06
46 Updated changelog
47
48 * 2005-03-06 20:14:28 +0100
49 Version 0.01.
0 Changes
1 Makefile.PL
2 Build.PL
3 MANIFEST
4 README
5 t/Linux-Distribution.t
6 lib/Linux/Distribution.pm
7 META.yml Module meta-data (added by MakeMaker)
0 ---
1 abstract: 'Perl extension to detect on which Linux distribution we are running.'
2 author:
3 - 'Alexandr Ciornii <alexchorny@gmail.com>'
4 generated_by: 'Module::Build version 0.3617'
5 license: perl
6 meta-spec:
7 url: http://module-build.sourceforge.net/META-spec-v1.4.html
8 version: 1.4
9 name: Linux-Distribution
10 provides:
11 Linux::Distribution:
12 file: lib/Linux/Distribution.pm
13 version: 0.20
14 requires:
15 Test::More: 0
16 perl: 5.006
17 resources:
18 license: http://dev.perl.org/licenses/
19 repository: https://github.com/chorny/Linux-Distribution
20 version: 0.20
0 use 5.006000;
1 use ExtUtils::MakeMaker;
2 unless (lc $^O eq "linux") {
3 die "OS unsupported\n";
4 }
5 WriteMakefile(
6 NAME => 'Linux::Distribution',
7 VERSION_FROM => 'lib/Linux/Distribution.pm', # finds $VERSION
8 PREREQ_PM => {
9 'Test::More' => 0,
10 },
11 ABSTRACT_FROM => 'lib/Linux/Distribution.pm', # retrieve abstract from module
12 AUTHOR => 'Alexandr Ciornii <alexchorny@gmail.com>', #Alberto Re <alberto@accidia.net>
13 ($ExtUtils::MakeMaker::VERSION ge '6.48'?
14 ('LICENSE' => 'perl', 'MIN_PERL_VERSION' => 5.006,) : ()),
15 PL_FILES => {},
16 );
0 Linux-Distribution version 0.20
1 ===============================
2
3 This is a simple module that tries to guess on what linux distribution we are running by looking
4 for release's files in /etc. It now looks for 'lsb-release' first as that should be the most cor-
5 rect and adds ubuntu support. Secondly, it will look for the distro specific files.
6
7 It currently recognizes slackware, debian, suse, fedora, redhat, turbolinux, yellowdog, knoppix,
8 mandrake, conectiva, immunix, tinysofa, va-linux, trustix, adamantix, yoper, arch-linux, libranet,
9 gentoo, ubuntu and redflag.
10
11 It has function to get the version for debian, suse, redhat, gentoo, slackware, redflag and
12 ubuntu(lsb). People running unsupported distro's are greatly encouraged to submit patches :-)
13
14 INSTALLATION
15
16 To install this module type the following:
17
18 perl Makefile.PL
19 make
20 make test
21 make install
22
23 DEPENDENCIES
24
25 This module has no dependencies.
26
27 COPYRIGHT AND LICENCE
28
29 Copyright (C) 2006 by Alberto Re <alberto@accidia.net>
30 (c) 2008-2010 Alexandr Ciornii <alexchorny@gmail.com>
31
32
33 This library is free software; you can redistribute it and/or modify
34 it under the same terms as Perl itself, either Perl version 5.8.5 or,
35 at your option, any later version of Perl 5 you may have available.
0 package Linux::Distribution;
1
2 use 5.006000;
3 use strict;
4 use warnings;
5
6 require Exporter;
7
8 our @ISA = qw(Exporter);
9
10 our @EXPORT_OK = qw( distribution_name distribution_version );
11
12 our $VERSION = '0.20';
13
14 our $release_files_directory='/etc';
15 our $standard_release_file = 'lsb-release';
16
17 our %release_files = (
18 'gentoo-release' => 'gentoo',
19 'fedora-release' => 'fedora',
20 'turbolinux-release' => 'turbolinux',
21 'mandrake-release' => 'mandrake',
22 'mandrakelinux-release' => 'mandrakelinux',
23 'debian_version' => 'debian',
24 'debian_release' => 'debian',
25 'SuSE-release' => 'suse',
26 'knoppix-version' => 'knoppix',
27 'yellowdog-release' => 'yellowdog',
28 'slackware-version' => 'slackware',
29 'slackware-release' => 'slackware',
30 'redflag-release' => 'redflag',
31 'redhat-release' => 'redhat',
32 'redhat_version' => 'redhat',
33 'conectiva-release' => 'conectiva',
34 'immunix-release' => 'immunix',
35 'tinysofa-release' => 'tinysofa',
36 'trustix-release' => 'trustix',
37 'adamantix_version' => 'adamantix',
38 'yoper-release' => 'yoper',
39 'arch-release' => 'arch',
40 'libranet_version' => 'libranet',
41 'va-release' => 'va-linux',
42 'pardus-release' => 'pardus',
43 );
44
45 our %version_match = (
46 'gentoo' => 'Gentoo Base System release (.*)',
47 'debian' => '(.+)',
48 'suse' => 'VERSION = (.*)',
49 'fedora' => 'Fedora Core release (\d+) \(',
50 'redflag' => 'Red Flag (?:Desktop|Linux) (?:release |\()(.*?)(?: \(.+)?\)',
51 'redhat' => 'Red Hat Linux release (.*) \(',
52 'slackware' => '^Slackware (.+)$',
53 'pardus' => '^Pardus (.+)$',
54 'centos' => '^CentOS release (.+)(?:\s\(Final\))',
55 );
56
57
58 if ($^O ne 'linux') {
59 # require Carp;
60 # Carp::croak('you are trying to use a linux specific module on a different OS');
61 }
62
63 sub new {
64 my %self = (
65 'DISTRIB_ID' => '',
66 'DISTRIB_RELEASE' => '',
67 'DISTRIB_CODENAME' => '',
68 'DISTRIB_DESCRIPTION' => '',
69 'release_file' => '',
70 'pattern' => ''
71 );
72
73 return bless \%self;
74 }
75
76 sub distribution_name {
77 my $self = shift || new();
78 my $distro;
79 if ($distro = $self->_get_lsb_info()){
80 return $distro if ($distro);
81 }
82
83 foreach ('fedora-release') {
84 if (-f "$release_files_directory/$_" && !-l "$release_files_directory/$_"){
85 if (-f "$release_files_directory/$_" && !-l "$release_files_directory/$_"){
86 $self->{'DISTRIB_ID'} = $release_files{$_};
87 $self->{'release_file'} = $_;
88 return $self->{'DISTRIB_ID'};
89 }
90 }
91 }
92
93 foreach (keys %release_files) {
94 if (-f "$release_files_directory/$_" && !-l "$release_files_directory/$_"){
95 if (-f "$release_files_directory/$_" && !-l "$release_files_directory/$_"){
96 if ( $release_files{$_} eq 'redhat' ) {
97 $self->{'pattern'} = $version_match{'centos'};
98 $self->{'release_file'}='redhat-release';
99 if ( $self->_get_file_info() ) {
100 $self->{'DISTRIB_ID'} = 'centos';
101 $self->{'release_file'} = $_;
102 return $self->{'DISTRIB_ID'};
103 } else {
104 $self->{'pattern'}='';
105 }
106 }
107 $self->{'release_file'} = $_;
108 $self->{'DISTRIB_ID'} = $release_files{$_};
109 return $self->{'DISTRIB_ID'};
110 }
111 }
112 }
113 undef
114 }
115
116 sub distribution_version {
117 my $self = shift || new();
118 my $release;
119 return $release if ($release = $self->_get_lsb_info('DISTRIB_RELEASE'));
120 if (! $self->{'DISTRIB_ID'}){
121 $self->distribution_name() or die 'No version because no distro.';
122 }
123 $self->{'pattern'} = $version_match{$self->{'DISTRIB_ID'}};
124 $release = $self->_get_file_info();
125 $self->{'DISTRIB_RELEASE'} = $release;
126 return $release;
127 }
128
129 sub _get_lsb_info {
130 my $self = shift;
131 my $field = shift || 'DISTRIB_ID';
132 my $tmp = $self->{'release_file'};
133 if ( -r "$release_files_directory/" . $standard_release_file ) {
134 $self->{'release_file'} = $standard_release_file;
135 $self->{'pattern'} = $field . '=(.+)';
136 my $info = $self->_get_file_info();
137 if ($info){
138 $self->{$field} = $info;
139 return $info
140 }
141 }
142 $self->{'release_file'} = $tmp;
143 $self->{'pattern'} = '';
144 undef;
145 }
146
147 sub _get_file_info {
148 my $self = shift;
149 open my $fh, '<', "$release_files_directory/" . $self->{'release_file'} or die 'Cannot open file: '.$release_files_directory.'/' . $self->{'release_file'};
150 my $info = '';
151 local $_;
152 while (<$fh>){
153 chomp $_;
154 ($info) = $_ =~ m/$self->{'pattern'}/;
155 return "\L$info" if $info;
156 }
157 undef;
158 }
159
160 1;
161 __END__
162
163
164 =head1 NAME
165
166 Linux::Distribution - Perl extension to detect on which Linux distribution we are running.
167
168 =head1 SYNOPSIS
169
170 use Linux::Distribution qw(distribution_name distribution_version);
171
172 if(my $distro = distribution_name) {
173 my $version = distribution_version();
174 print "you are running $distro, version $version\n";
175 } else {
176 print "distribution unknown\n";
177 }
178
179 Or else do it OO:
180
181 use Linux::Distribution qw(distribution_name distribution_version);
182
183 my $linux = Linux::Distribution->new;
184 if(my $distro = $linux->distribution_name()) {
185 my $version = $linux->distribution_version();
186 print "you are running $distro, version $version\n";
187 } else {
188 print "distribution unknown\n";
189 }
190
191 =head1 DESCRIPTION
192
193 This is a simple module 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.
194
195 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 and redflag.
196
197 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 :-)
198
199 =head2 EXPORT
200
201 None by default.
202
203 =head1 TODO
204
205 Add the capability of recognize the version of the distribution for all recognized distributions.
206
207 =head1 AUTHORS
208
209 Alexandr Ciornii E<lt>alexchorny@gmail.comE<gt>, L<http://chorny.net>
210 Alberto Re, E<lt>alberto@accidia.netE<gt>
211 Judith Lebzelter, E<lt>judith@osdl.orgE<gt>
212
213 =head1 COPYRIGHT AND LICENSE
214
215 This library is free software; you can redistribute it and/or modify
216 it under the same terms as Perl itself, either Perl version 5.8.5 or,
217 at your option, any later version of Perl 5 you may have available.
218
219 =cut
220
0 # Before `make install' is performed this script should be runnable with
1 # `make test'. After `make install' it should work as `perl Linux-Distribution.t'
2
3 #########################
4
5 # change 'tests => 1' to 'tests => last_test_to_print';
6
7 use Test::More tests => 2;
8 BEGIN { use_ok('Linux::Distribution') };
9
10 if(isnt(Linux::Distribution::distribution_name(), undef, 'Checking your distro...')) {
11 diag('It seems that we are running on '.Linux::Distribution::distribution_name.' version '.(Linux::Distribution::distribution_version || 'unknown'))
12 } else {
13 diag ('Please report your Linux dist to dist maintainer');
14 if (-e '/etc/lsb-release') {
15 diag ('lsb-release exists') ;
16 open my $fh,'<','/etc/lsb-release';
17 my $content=join('',<$fh>);
18 diag($content);
19 }
20 }
21
22
23 #########################
24
25 # Insert your test code below, the Test::More module is use()ed here so read
26 # its man page ( perldoc Test::More ) for help writing this test script.
27