[Distutils] Rewritten 'get_platform()'

Greg Ward gward@python.net
Sat Sep 9 20:59:07 2000

Hi all --

for months I have resisted the urge to turn the 'util.get_platform()'
function into a miniaturized version of Marc-André's platform.py or the
GNU script that spits out the old familar "os-vendor-hardware" triple.

Well, today I succumbed.  The idea is to generate a string that does a
better job of describing the current platform than sys.platform or
os.name.  This has (so far) two purposes in the distutils: naming the
build directories where platform-specific files (.o, .so, etc.) go, and
naming built distributions.

IMHO the exact information to include depends on the OS: eg. for Linux,
the exact kernel version isn't very relevant, whereas for IRIX the
version is crucial but the hardware isn't.  Typically, though, OS, OS
version, and hardware are all relevant.  This information is, AFAIK,
always available from the 'uname()' system call, exposed in Python as
'os.uname()'.  However, it typically needs a bit of massaging to become
something that we all know and recognize: eg. "SunOS 5.x" is really
"Solaris 2.x".

So, here's my initial crack at a rewritten 'get_platform()'.  Currently
it only handles POSIX platforms; on Windows and Mac OS it'll just return
sys.platform (as it has always done).

def get_platform ():
    """Return a string that identifies the current platform.  This is used
    mainly to distinguish platform-specific build directories and
    platform-specific built distributions.  Typically includes the OS name
    and version and the architecture (as supplied by 'os.uname()'),
    although the exact information included depends on the OS; eg. for IRIX
    the architecture isn't particularly important (IRIX only runs on SGI
    hardware), but for Linux the kernel version isn't particularly

    Examples of returned values:
       linux-alpha (?)
    For non-POSIX platforms, currently just returns 'sys.platform'.
    if os.name != "posix":
        # XXX what about the architecture? NT is Intel or Alpha,
        # Mac OS is M68k or PPC, etc.
        return sys.platform

    # Try to distinguish various flavours of Unix

    (os, host, release, version, machine) = os.uname()
    os = string.lower(os)
    if os[:5] == "linux":
        # At least on Linux/Intel, 'machine' is the processor --
        # i386, etc.
        # XXX what about Alpha, SPARC, etc?
        return  "%s-%s" % (os, machine)
    elif os[:5] == "sunos":
        if release[0] >= "5":           # SunOS 5 == Solaris 2
            os = "solaris"
            release = "%d.%s" % (int(release[0]) - 3, release[2:])
        # fall through to standard os-release-machine representation
    elif os[:4] == "irix":              # could be "irix64"!
        return "%s-%s" % (os, release)
    return "%s-%s-%s" % (os, release, machine)

There are a heck of a lot of POSIX-ish platforms out there, and I'll be
very happy if this function returns something sensible for the
  * Linux on any architecture
  * {Free,Net,Open}BSD on any architecture
  * Solaris on SPARC, Intel
  * IRIX
  * AIX
  * HP-UX

...plus of course Windows and Mac OS.  Like the rest of the world, I'm a 
little unclear on just how many versions of Windows there really are.
;-)  I suspect we can get away with Win32 vs. Win64, but what about NT
on Alpha -- is that relevant?  Are there any major Unices that I'm
missing here?  As long as the os-release-machine triple is sensible,
it's OK to leave them out of the switch on OS name up there, but I
suspect most will need some special-case tweaking like the three that
I'm familar with.

And yes, I'm going to go dig up MAL's platform.py and see what it does.
My informal rule is that, at least for POSIX-ish platforms, we should
rely solely on os.uname().  It remains to be seen how foolishly
optimistic that is.  ;-)

Greg Ward                                      gward@python.net
War is Peace; Freedom is Slavery; Ignorance is Knowledge