I am making a function to return a two-item tuple of ('os basename', 'os distro')
. For instance, the return for Vista should be ('Windows', 'Vista')
and ('Linux', 'Fedora')
for Fedora.
Does this parse OS names correctly? Is this great, good or ugly functionally wise, and code-wise? Is there a flaw in any given ideal circumstance?
(Tested only on Vista SP2, Python 2.7.)
import sys, platform
PLATFORM_SYSTEM, PLATFORM_NODE, PLATFORM_RELEASE, PLATFORM_VERSION, PLATFORM_MACHINE, PLATFORM_PROCESSOR = platform.uname()
VER_NT_WORKSTATION = 1
VER_PLATFORM_WIN32_NT = 2
WIN_VERSIONS = {
#major, minor, platform, product_type
(5, 1, VER_PLATFORM_WIN32_NT, VER_NT_WORKSTATION): 'XP',
(5, 2, VER_PLATFORM_WIN32_NT, VER_NT_WORKSTATION): '2003',
(6, 0, VER_PLATFORM_WIN32_NT, VER_NT_WORKSTATION): 'Vista',
(6, 1, VER_PLATFORM_WIN32_NT, 0): 'Server 2008',
(6, 1, VER_PLATFORM_WIN32_NT, VER_NT_WORKSTATION): '7',
(6, 2, VER_PLATFORM_WIN32_NT, 0): 'Server 2012',
(6, 2, VER_PLATFORM_WIN32_NT, VER_NT_WORKSTATION): '8',
(6, 3, VER_PLATFORM_WIN32_NT, VER_NT_WORKSTATION): '8.1',
(6, 3, VER_PLATFORM_WIN32_NT, 0): 'Server 2012 R2'
}
def ostuple():
"""return a two string tuple of basename of os and distro name, ie: ('Windows', 'Vista')"""
ret = [PLATFORM_SYSTEM] #platform.uname()[0]
if os.name == 'nt':
ver = sys.getwindowsversion()
major, minor, platform, product_type = (ver.major, ver.minor, ver.platform, ver.product_type)
for t in WIN_VERSIONS:
m, n, p, y = t
if major == m:
if minor == n:
if platform == p:
if y == 0:
if product_type != VER_NT_WORKSTATION:
ret += [WIN_VERSIONS[t]]
else:
ret += [WIN_VERSIONS[t]]
else:
if hasattr(platform, 'linux_distribution'):
y = list(platform.linux_distribution(full_distribution_name=0))
for k in y[:]:
if not k: #to remove empty strings
y.remove(k)
if y:
ret += [' '.join(y)]
if len(ret) < 2:
ret += ['']
return tuple(ret)
for k in y[:]
andif not k:
construct? Why do you do that? – holroy Dec 5 at 2:43ostuple()
is quite close to being awintuple()
... :-) – holroy yesterday