Detecting OS platform in Python

D

Devraj

Hi everyone,

My Python program needs reliably detect which Operating System its
being run on, infact it even needs to know which distribution of say
Linux its running on. The reason being its a GTK application that
needs to adapt itself to be a Hildon application if run on devices
like the N800.

I have been searching around for an answer to this, and did find some
messages on a lists that suggested the use of sys.platform to detect
platform, with counter posts saying that it didn't work on Windows
etc.

Can anyone please shed some light on this?

Thanks a lot.
 
M

Mike Meyer

Hi everyone,

My Python program needs reliably detect which Operating System its
being run on, infact it even needs to know which distribution of say
Linux its running on. The reason being its a GTK application that
needs to adapt itself to be a Hildon application if run on devices
like the N800.

I don't think it can be done. For most Unix system, os.uname() will give
you the information you want:
('FreeBSD', 'bhuda.mired.org', '6.2-STABLE', 'FreeBSD 6.2-STABLE #6: Sun Jun 3 04:17:59 EDT 2007 (e-mail address removed):/usr/src/sys/amd64/compile/BHUDA', 'amd64')

(let's see - OS name, the system name, the release level, complete
build information, down to the configuration file for the build and
build number for that configuration, and the hardware type it was
built for).

For GNU/Linux systems, it won't, because it's a kernel facility, and
kernels don't know what distribution they're part of:

Linux student.mired.org 2.6.12-9-386 #1 Mon Oct 10 13:14:36 BST 2005 i686 GNU/Linux

(kernel name, system name, kernel version and build information,
platform, and operating system).

GNU/Linux distributions are collections of lots of people software, so
each has it's own way to state what "distribution" it is. I believe
there's some sort of standard - except not everybody follows it. So
you wind up using a series of heuristics to chase this information
down.
I have been searching around for an answer to this, and did find some
messages on a lists that suggested the use of sys.platform to detect
platform, with counter posts saying that it didn't work on Windows
etc.

Oh, you want it to work on Windows? Hmmm. [snotty comment about
Windows deleted].

Does the underlying platform claim to be POSIX.2 compliant? If so,
then os.uname() should work on it, but as indicated, that may well not
be complete.

On the other hand, trying to figure out what features you have
available by guessing based on the platform type is generally the
wrong way to approach this kind of problem - only in part because you
wind up reduced to a series of heuristics to figure out the
platform. And once you've done that, you could wind up being wrong.

Generally, you're better of probing the platform to find out if it has
the facilities you're looking for. For python, that generally means
trying to import the modules you need, and catching failures; or
possibly looking for attributes on modules if they adopt to the
environment around them.

I'm not a GTK programmer, and have never even heard of Hildon. Is
there some associated module you could try and import that doesn't
exist on the N800? I.e.:

try:
import gtk
mygui = 'gtk'
except ImportError:
import Hildon
mygui = 'Hildon'

or maybe something like:

import gtk
mygui = 'gtk' if not hasattr(gtk, 'Hildon') else 'Hildon'

<mike
 
B

Benjamin

Hi everyone,

My Python program needs reliably detect which Operating System its
being run on, infact it even needs to know which distribution of say
Linux its running on. The reason being its a GTK application that
needs to adapt itself to be a Hildon application if run on devices
like the N800.
platform.dist might help you. It's not very complete at all, though.
(This is supposed to improve in 2.6, though)
 
P

Paul Boddie

My Python program needs reliably detect which Operating System its
being run on, infact it even needs to know which distribution of say
Linux its running on. The reason being its a GTK application that
needs to adapt itself to be a Hildon application if run on devices
like the N800.

I don't think it can be done. For most Unix system, os.uname() will give
you the information you want:
[...]

GNU/Linux distributions are collections of lots of people software, so
each has it's own way to state what "distribution" it is. I believe
there's some sort of standard - except not everybody follows it. So
you wind up using a series of heuristics to chase this information
down.

On Red Hat distributions, there appears to be a file called /etc/
redhat-release containing the distribution name. I can't remember the
Debian equivalent, but it may be /etc/debian_version or something like
that. There's LSB-related stuff, too, but I'm not sure if any of that
tells you about the distribution itself.

[...]
I'm not a GTK programmer, and have never even heard of Hildon. Is
there some associated module you could try and import that doesn't
exist on the N800? I.e.:

try:
import gtk
mygui = 'gtk'
except ImportError:
import Hildon
mygui = 'Hildon'

or maybe something like:

import gtk
mygui = 'gtk' if not hasattr(gtk, 'Hildon') else 'Hildon'

There must be Hildon-related modules or features that one can import
or test for, as Mike suggests. In the desktop module [1], I generally
test various environment variables or the output of various programs
in order to deduce which desktop environment is being used, but this
is in software which isn't generally trying to display a graphical
user interface. Just testing for the presence of a file or a program
isn't usually enough because on systems where desktop environments co-
exist, for example, the presence of a GNOME-related file or program
doesn't mean that the user is actually using GNOME at that particular
time. If you might be using Hildon and are actually displaying a GUI,
however, attempting to get the resources you need should be enough to
confirm whether you're running Hildon or not.

Paul

[1] http://www.python.org/pypi/desktop
 
N

Nikita the Spider

Mike Meyer said:
Hi everyone,

My Python program needs reliably detect which Operating System its
being run on, infact it even needs to know which distribution of say
Linux its running on. The reason being its a GTK application that
needs to adapt itself to be a Hildon application if run on devices
like the N800.

I don't think it can be done.
[...]

...trying to figure out what features you have
available by guessing based on the platform type is generally the
wrong way to approach this kind of problem - only in part because you
wind up reduced to a series of heuristics to figure out the
platform. And once you've done that, you could wind up being wrong.

Generally, you're better of probing the platform to find out if it has
the facilities you're looking for. For python, that generally means
trying to import the modules you need, and catching failures; or
possibly looking for attributes on modules if they adopt to the
environment around them.


Much agreed. I just went through this with my SHM module. Compilation
was failing because of a variation in ipc_perm in ipc.h on various
platforms. I didn't feel confident at all that I could compile a list of
all of the variations let alone keep it accurate and updated. The
clincher was when I found that OS X >= 10.4 has two flavors of ipc_perm
and which gets used depends on a compile flag, so identifying the OS
would not have been useful in that case.

OP, I don't know what a Hildon or N800 is, but is it possible that the
same OS fingerprint could show up on different devices? If so then
you're really out of luck. I think you'll be much better off if you
focus less on the OS and more on the features it offers.

Good luck
 
M

MrJean1

platform.dist might help you. It's not very complete at all, though.
(This is supposed to improve in 2.6, though)

Better yet, first use sys.platform. If that is 'linux2', then use
platform.dist().

/Jean Brouwers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top