Identification If OS (Open) SUSE in Python?

I am developing a script that needs package managers in the System. I have identified Fedora, Gentoo, and Arch Linux using the os.uname() function.

However, the results of (open) SUSE uname consistent with those of other Linux Distros. I found uname results for many Wikipedia distributions.

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

+6
source share
4 answers

From the comments above:

  • I need to know if the OS is (Open) SUSE in order to use the correct package installer (zypper). If it is DEBIAN (for example), I will use apt-get ...

I suggest you directly solve the real problem. Instead of identifying the OS, define an available package manager.

 import os def file_exists(fname): try: os.stat(fname) return True except OSError: return False if file_exists('/usr/bin/zypper'): ... # do the SUSE case elif file_exists('/usr/bin/apt-get'): ... # do the Debian/Ubuntu case elif file_exists('/usr/bin/yum'): ... # do the Red Hat case else: raise OSError, "cannot find a usable package manager" 

EDIT: Although the package manager program detection code is shown here, it is better to find the main package registry. For example, on Debian / Ubuntu systems that use dpkg , there will be a directory /var/lib/dpkg containing the package database; this is a sure sign that dpkg or apt-get are appropriate. I do not know what equivalent directories are for SUSE and Red Hat, etc., but if you support the ones you can find out.

apt-get been ported to Red Hat systems, and through a program called alien you can get rpm on Debian systems and so on. Discovering the package database itself is the most reliable way to find out which package system is being used.

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

+6
source

If the distribution follows the standard Linux base , you can read the output of lsb_release -i .

Something like that:

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

The 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 provide you with most 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 select the appropriate fields that you need.

I assume that the uname -a command provides a somewhat uniform output for distributions. If not, then this will not work.

0
source

This small part of the Python template will output information about your platform:

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

must do the job.

0
source

Source: https://habr.com/ru/post/918297/


All Articles