Τρίτη, 29 Μαρτίου 2005

python script to detect GNU/Linux distros and Windows

import os

distro_info = {
'Arch Linux': '/etc/arch-release',
'Aurox Linux': '/etc/aurox-release',
'Conectiva Linux': '/etc/conectiva-release',
'Debian GNU/Linux': '/etc/debian_release',
'Debian GNU/Linux': '/etc/debian_version',
'Fedora Linux': '/etc/fedora-release',
'Gentoo Linux': '/etc/gentoo-release',
'Linux from Scratch': '/etc/lfs-release',
'Mandrake Linux': '/etc/mandrake-release',
'Slackware Linux': '/etc/slackware-release',
'Slackware Linux': '/etc/slackware-version',
'Solaris/Sparc': '/etc/release',
'Sun JDS': '/etc/sun-release',
'Novell SUSE Linux': '/etc/SuSE-release',
'PLD Linux': '/etc/pld-release',
'SUSE Linux': '/etc/SuSE-release',
'Yellow Dog Linux': '/etc/yellowdog-release',
# many distros use the /etc/redhat-release for compatibility
# so Redhat is the last
'Redhat Linux': '/etc/redhat-release'
}

def get_os_info():
if os.name == 'nt':
win_version = {
(1, 4, 0): '95',
(1, 4, 10): '98',
(1, 4, 90): 'ME',
(2, 4, 0): 'NT',
(2, 5, 0): '2000',
(2, 5, 1): 'XP'
}[ os.sys.getwindowsversion()[3],
os.sys.getwindowsversion()[0],
os.sys.getwindowsversion()[1] ]
return 'Windows' + ' ' + win_version
elif os.name =='posix':
executable = 'lsb_release'
params = ' --id --codename --release --short'
for path in os.environ['PATH'].split(':'):
full_path_to_executable = os.path.join(path, executable)
if os.path.exists(full_path_to_executable):
command = executable + params
child_stdin, child_stdout = os.popen2(command)
output = child_stdout.readline().strip()
child_stdout.close()
child_stdin.close()
return output
# lsb_release executable not available, so parse files
for distro_name in distro_info:
path_to_file = distro_info[distro_name]
if os.path.exists(path_to_file):
fd = open(path_to_file)
text = fd.read().strip()
fd.close()
if path_to_file.endswith('version'):
text = distro_name + ' ' + text
elif path_to_file.endswith('aurox-release'): # file doesn't have version
text = distro_name
elif path_to_file.endswith('lfs-release'): # file just has version
text = distro_name + ' ' + text
return text

print get_os_info()


it's GPL. Colors thanks to
€ source-highlight -s python -i detectOS.py -f xhtml -o detectOS.py.html

PS.
I later found out that in python there's a builtin platform module but that only detects MDK, Redhat and Debian. Now I admire Guido, but if he's going to add modules that are so limited then why bother at all? (that module also scans for /etc files only, and doesn't care about lsb_release as described here.

platform.release() (which is supposed to return the Name of Windows) also does not work as it should in some versions of windows I tried (xp pro sp1). Luckily to print more than Windows (eg. print Windows XP or Windows 2000 etc) you can have a look at this blog entry of Simon Brunning

So Guido plz expand your module soon with some ideas described here and there


PS2. I may not update this entry again. If you want to make sure you have the latest and working version of this code checkout gajim's source code on gajim.org and grep for 'get_os_info' or even if that has changed (future is uncertain) search for 'Arch Linux'

2 comments:

  1. in the second part (when lsb_release doesn't exists) you never close path_to_file file.

    ΑπάντησηΔιαγραφή
  2. ok you're right. python will clean up on return anyways. but it's better to manually show this

    ΑπάντησηΔιαγραφή