Inversus
Inversus

Reputation: 3175

What is the linux equivalent of sys.getwindowsversion()

There is no sys.getlinuxversion(), and I get the feeling I may have to piece it together from a few places (which is fine); but, maybe not?

import sys, os
from collections import namedtuple

## This defines an interface similar to the object that getwindowsversion() returns
## (I say 'similar' so I don't have to explain the descriptor magic behind it)
WindowsVersion = namedtuple('WindowsVersion', 'major minor build platform product_type service_pack service_pack_major service_pack_minor suite_mask')

## Looking for something that quacks like this:
LinuxVersion = namedtuple('LinuxVersion', 'major minor build whatever attrs are needed')

## To create something that quacks similarly to this (in case you're wondering)
PlatformVersion = namedtuple('PlatformVersion', 'major minor micro build info')

## EDIT: A newer version of Version, perhaps?
class Version:
    _fields = None
    def __init__(self, *subversions, **info)
        self.version = '.'.join([str(sub) for sub in subversions])
        self.info = info
        instance_fields = self.__class__._fields
        if instance_fields is not None:
            if isinstance(instance_fields, str):
                instance_fields = instance_fields.split()
            for field in instance_fields:
                if field in self.info:
                    setattr(self, field, self.info.pop(field))

## This makes the PlatformVersion (or any other Version) definition a mere:
class PlatformVersion(Version):
    _fields = 'build'  ## :)

## Now PlatformInfo looks something like:
class PlatformInfo:
    def __init__(self, *version_number, platform=None, version_info=None, **info):
        self.platform = sys.platform if platform is None else platform
        self.info = info
        if self.platform in ('win32', 'win64'):
            works_great = sys.getwindowsversion()
            self.version = PlatformVersion(works_great.major, works_great.minor, works_great.service_pack_major, build=works_great.build, **dict(version_info))
        else:
            self.version = your_answer(PlatformVersion(os.uname().release, build=os.uname().version, **dict(version_info))), 'hopefully'
            self.version = self.version[0]  ## lol

Thanks!

Upvotes: 4

Views: 242

Answers (1)

myaut
myaut

Reputation: 11514

Linux (and all Unix-like systems) have uname syscall that provide such information:

>>> import os
>>> os.uname().release
'3.11.10-17-desktop'

It shows version of kernel.

Note that on Python 2 it will return tuple instead of namedtuple:

>>> import os
>>> os.uname()[2]
'3.11.10-17-desktop'

Upvotes: 6

Related Questions