Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying If the OS is (Open)SUSE in Python?

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?

like image 234
ant0nisk Avatar asked Jun 17 '12 19:06

ant0nisk


People also ask

Can Python detect os?

uname() method in python is used to get information about the current operating system. This method returns information like name, release, and version of the current operating system, name of the machine on the network, and hardware identifier in the form of attributes of a tuple-like object.


3 Answers

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.

like image 180
steveha Avatar answered Oct 21 '22 23:10

steveha


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
like image 36
ʇsәɹoɈ Avatar answered Oct 22 '22 00:10

ʇsәɹoɈ


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.

like image 35
karolus Avatar answered Oct 22 '22 01:10

karolus