Search code examples
pythonopensusesuseidentificationuname

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?


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.