[Portland] [bryce@osdl.org: Re: system info utility?]

Bryce Harrington bryce at osdl.org
Thu Jul 20 20:01:41 PDT 2006


Note that in the desktop_environment script there's a small bug in that
if two users are running X on the same machine, and the other user runs
xfce, it will mis-detect.  If anyone is running xfce, see if there's an
env variable set, like with gnome or kde, so we can use that instead.

Bryce


----- Forwarded message from Bryce Harrington <bryce at osdl.org> -----

Date: Thu, 20 Jul 2006 19:36:04 -0700
From: Bryce Harrington <bryce at osdl.org>
To: "Whipple, Tom" <tom.whipple at intel.com>
Cc: "Bastian, Waldo" <waldo.bastian at intel.com>
Subject: Re: system info utility?

On Thu, Jul 20, 2006 at 02:53:47PM -0700, Whipple, Tom wrote:
> Bryce,
> 
> At the session Waldo & I had at your office last month we discussed
> creating a system info utility that could capture the desktop
> environment version, and distro version.
> 
> If I remember correctly, this is something that you were going to take
> on. Are you still working on it, and if so, is this something we can
> include with the test suite for BETA2 of Portland?

Attached.

Bryce


# desktop_environment
#!/bin/bash

# From Sutoka on FreeNode/#kde
kde_version=`kde-config --version | grep KDE | cut -d' ' -f2 2> /dev/null`
kde_running=$KDE_FULL_SESSION
if [ $kde_running ]; then
    echo "kde $kde_version"
fi

# From kees on #osdl
gnome_version=`pkg-config --modversion libgnome-2.0`
if [ ! -z "$GNOME_DESKTOP_SESSION_ID" ]; then
    gnome_running=true
    echo "gnome $gnome_version"
fi

# From massonnet on FreeNode/#xfce
xfce4_version=`xfce4-session --version | grep xfce4-session | cut -d' ' -f2 2> /dev/null`
if [ `pidof xfce4-session` ]; then
    xfce4_running=true
    echo "xfce4 $xfce_version"
fi




# linux_distro
#!/usr/bin/perl


########################################################################

package Linux::Distribution;

use 5.006000;
use strict;
use warnings;

require Exporter;

our @ISA = qw(Exporter);

our @EXPORT_OK = qw( distribution_name distribution_version );

our $VERSION = '0.14';

our $standard_release_file = 'lsb-release';

our %release_files = (
    'gentoo-release'        => 'gentoo',
    'fedora-release'        => 'fedora',
    'turbolinux-release'    => 'turbolinux',
    'mandrake-release'      => 'mandrake',
    'mandrakelinux-release' => 'mandrakelinux',
    'debian_version'        => 'debian',
    'debian_release'        => 'debian',
    'SuSE-release'          => 'suse',
    'knoppix-version'       => 'knoppix',
    'yellowdog-release'     => 'yellowdog',
    'slackware-version'     => 'slackware',
    'slackware-release'     => 'slackware',
    'redflag-release'       => 'redflag',
    'redhat-release'        => 'redhat',
    'redhat_version'        => 'redhat',
    'conectiva-release'     => 'conectiva',
    'immunix-release'       => 'immunix',
    'tinysofa-release'      => 'tinysofa',
    'trustix-release'       => 'trustix',
    'adamantix_version'     => 'adamantix',
    'yoper-release'         => 'yoper',
    'arch-release'          => 'arch',
    'libranet_version'      => 'libranet',
    'va-release'            => 'va-linux'
);

our %version_match = (
    'gentoo'                => 'Gentoo Base System version (.*)',
    'debian'                => '(.+)',
    'suse'                  => 'VERSION = (.*)',
    'fedora'                => 'Fedora Core release (\d+) \(',
    'redflag'               => 'Red Flag (?:Desktop|Linux) (?:release |\()(.*?)(?: \(.+)?\)',
    'redhat'                => 'Red Hat Linux release (.*) \(',
    'slackware'             => '^Slackware (.+)$'
);


if ($^O ne 'linux') {
	require Carp;
	Carp::croak 'you are trying to use a linux specific module on a different OS';
}

sub new {
    my %self = (
        'DISTRIB_ID'          => '',
        'DISTRIB_RELEASE'     => '',
        'DISTRIB_CODENAME'    => '',
        'DISTRIB_DESCRIPTION' => '',
        'release_file'        => '',
        'pattern'             => ''
    );
    
    return bless \%self;
}

sub distribution_name {
    my $self = shift || new();
    my $distro;
    if ($distro = $self->_get_lsb_info()){
        return $distro if ($distro);
    }
    foreach (keys %release_files) {
        if (-f "/etc/$_" && !-l "/etc/$_"){
            if (-f "/etc/$_" && !-l "/etc/$_"){
                $self->{'DISTRIB_ID'} = $release_files{$_};
                $self->{'release_file'} = $_;
                return $self->{'DISTRIB_ID'};
            }
        }
    }
    undef 
}

sub distribution_version {
    my $self = shift || new();
    my $release;
    return $release if ($release = $self->_get_lsb_info('DISTRIB_RELEASE'));
    if (! $self->{'DISTRIB_ID'}){
         $self->distribution_name() or die 'No version because no distro.';
    }
    $self->{'pattern'} = $version_match{$self->{'DISTRIB_ID'}};
    $release = $self->_get_file_info();
    $self->{'DISTRIB_RELEASE'} = $release;
    return $release;
}

sub _get_lsb_info {
    my $self = shift;
    my $field = shift || 'DISTRIB_ID';
    my $tmp = $self->{'release_file'};
    if ( -r '/etc/' . $standard_release_file ) {
        $self->{'release_file'} = $standard_release_file;
        $self->{'pattern'} = $field . '=(.+)';
        my $info = $self->_get_file_info();
        if ($info){
            $self->{$field} = $info;
            return $info
        }
    } 
    $self->{'release_file'} = $tmp;
    $self->{'pattern'} = '';
    undef;
}

sub _get_file_info {
    my $self = shift;
    open FH, '/etc/' . $self->{'release_file'} or die 'Cannot open file: /etc/' . $self->{'release_file'};
    my $info = '';
    while (<FH>){
        chomp $_;
        ($info) = $_ =~ m/$self->{'pattern'}/;
        return "\L$info" if $info;
    }
    undef;
}

1;


# Simple script to use the above perl module to print the distro name
if (my $distro = distribution_name) {
    my $version = distribution_version();
    print "$distro $version\n";
} else {
    print "distribution unknown\n";
}



__END__


=head1 NAME

linux_distro - tool for printing the current linux distribution

=head1 SYNOPSIS

  linux_distro

=head1 DESCRIPTION

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.

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.

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 :-)

=head2 EXPORT

None by default.

=head1 TODO

Add the capability of recognize the version of the distribution for all recognized distributions.

=head1 AUTHORS

Alberto Re, E<lt>alberto at accidia.netE<gt>
Judith Lebzelter, E<lt>judith at osdl.orgE<gt>

=head1 COPYRIGHT AND LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.

=cut

----- End forwarded message -----


More information about the Portland mailing list