Question

I'm developing a script that needs the package managers of a System. I've identified Fedora, Gentoo, and Arch Linux using the os.uname() function.

However, the (open)SUSE uname results is the same as other Linux Distros. I found the uname results of many distros on Wikipedia.

Is there any smart way to identify (open)SUSE with Python?

Was it helpful?

Solution

From the comments at the top:

  • I need to know if the OS is (Open)SUSE so as to use the correct package installer (zypper). If it is DEBIAN (For Example), I will use apt-get...

I suggest you directly solve the actual problem. Instead of identifying the OS, identify the package manager available.

import os

if os.path.exists('/usr/bin/zypper'):
    ... # do the SUSE case
elif os.path.exists('/usr/bin/apt-get'):
    ... # do the Debian/Ubuntu case
elif os.path.exists('/usr/bin/yum'):
    ... # do the Red Hat case
else:
    raise OSError, "cannot find a usable package manager"

EDIT: Although the code here shows detecting the package manager program, it might be better to detect the main package registry itself. For example, on Debian/Ubuntu systems that use dpkg, there will be a directory /var/lib/dpkg holding the package database; that is a sure sign that dpkg or apt-get are appropriate. I don't know what the equivalent directories are for SUSE and Red Hat and so on, but if you are supporting those you can find out.

apt-get has been ported to Red Hat systems, and via a program called alien you can get rpm on Debian systems, and so on. Detecting the package database itself is the most foolproof way of figuring out what package system is in use.

If you detect the package manager, then your code will automatically work on all related distros. If you detect the dpkg database, your code will work on Debian, Ubuntu, Linux Mint, and the many other distros based on Debian. If you detect the rpm database, your code will work on Red Hat, Centos, Fedora, Mandriva, and all the many other distros based on RPM.

OTHER TIPS

If the distribution follows the Linux Standard Base, you could read the output of lsb_release -i.

Something like this:

import os

try:
    distro = os.popen('lsb_release -i').read().split(':')[1].strip()
except IndexError:
    distro = None

This little bit of Python boilerplate will print out your platform information:

import platform  

print platform.linux_distribution()  
('openSUSE ', '11.4', 'x86_64')  

should do the job.

Output of os.uname():

('Linux',
 'i7',
 '2.6.32-41-generic',
 '#90-Ubuntu SMP Tue May 22 11:31:25 UTC 2012',
 'i686')

The uname -a command should give you much of the same information.

% uname -a
Linux i7 2.6.32-41-generic #90-Ubuntu SMP Tue May 22 11:31:25 UTC 2012 i686 GNU/Linux

then

distr = os.popen('uname -a').read().split()

gives you:

['Linux',
 'i7',
 '2.6.32-41-generic',
 '#90-Ubuntu',
 'SMP',
 'Tue',
 'May',
 '22',
 '11:31:25',
 'UTC',
 '2012',
 'i686',
 'GNU/Linux']

And you can pick the relevant fields you need.

I am assuming the uname -a command provides somewhat uniform output for the distributions. If not, then this won't work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top