blob: d5cf62305bb81db589c4d086212ebc85c682dfa3 [file] [log] [blame]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001#!/usr/bin/env python
2
Brett Cannon8ab27df2003-08-05 03:52:04 +00003""" This module tries to retrieve as much platform-identifying data as
Marc-André Lemburg246d8472003-04-24 11:36:11 +00004 possible. It makes this information available via function APIs.
5
6 If called from the command line, it prints the platform
7 information concatenated as single string to stdout. The output
8 format is useable as part of a filename.
9
10"""
11# This module is maintained by Marc-Andre Lemburg <mal@egenix.com>.
12# If you find problems, please submit bug reports/patches via the
13# Python SourceForge Project Page and assign them to "lemburg".
14#
Marc-André Lemburg246d8472003-04-24 11:36:11 +000015# Still needed:
16# * more support for WinCE
17# * support for MS-DOS (PythonDX ?)
18# * support for Amiga and other still unsupported platforms running Python
19# * support for additional Linux distributions
20#
Brett Cannon8ab27df2003-08-05 03:52:04 +000021# Many thanks to all those who helped adding platform-specific
Marc-André Lemburg246d8472003-04-24 11:36:11 +000022# checks (in no particular order):
23#
24# Charles G Waldman, David Arnold, Gordon McMillan, Ben Darnell,
25# Jeff Bauer, Cliff Crawford, Ivan Van Laningham, Josef
26# Betancourt, Randall Hopper, Karl Putland, John Farrell, Greg
27# Andruk, Just van Rossum, Thomas Heller, Mark R. Levinson, Mark
28# Hammond, Bill Tutt, Hans Nowak, Uwe Zessin (OpenVMS support),
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000029# Colin Kong, Trent Mick, Guido van Rossum, Anthony Baxter
Marc-André Lemburg246d8472003-04-24 11:36:11 +000030#
31# History:
Marc-André Lemburg380f4172005-11-07 16:11:02 +000032#
33# <see CVS and SVN checkin messages for history>
34#
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000035# 1.0.6 - added linux_distribution()
36# 1.0.5 - fixed Java support to allow running the module on Jython
37# 1.0.4 - added IronPython support
Marc-André Lemburgcdc79232004-06-19 17:17:00 +000038# 1.0.3 - added normalization of Windows system name
Marc-André Lemburg91e83e22004-03-25 18:35:12 +000039# 1.0.2 - added more Windows support
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +000040# 1.0.1 - reformatted to make doc.py happy
Marc-André Lemburg246d8472003-04-24 11:36:11 +000041# 1.0.0 - reformatted a bit and checked into Python CVS
42# 0.8.0 - added sys.version parser and various new access
43# APIs (python_version(), python_compiler(), etc.)
44# 0.7.2 - fixed architecture() to use sizeof(pointer) where available
45# 0.7.1 - added support for Caldera OpenLinux
46# 0.7.0 - some fixes for WinCE; untabified the source file
47# 0.6.2 - support for OpenVMS - requires version 1.5.2-V006 or higher and
48# vms_lib.getsyi() configured
49# 0.6.1 - added code to prevent 'uname -p' on platforms which are
50# known not to support it
51# 0.6.0 - fixed win32_ver() to hopefully work on Win95,98,NT and Win2k;
52# did some cleanup of the interfaces - some APIs have changed
53# 0.5.5 - fixed another type in the MacOS code... should have
54# used more coffee today ;-)
55# 0.5.4 - fixed a few typos in the MacOS code
56# 0.5.3 - added experimental MacOS support; added better popen()
57# workarounds in _syscmd_ver() -- still not 100% elegant
58# though
59# 0.5.2 - fixed uname() to return '' instead of 'unknown' in all
60# return values (the system uname command tends to return
61# 'unknown' instead of just leaving the field emtpy)
62# 0.5.1 - included code for slackware dist; added exception handlers
63# to cover up situations where platforms don't have os.popen
64# (e.g. Mac) or fail on socket.gethostname(); fixed libc
65# detection RE
66# 0.5.0 - changed the API names referring to system commands to *syscmd*;
67# added java_ver(); made syscmd_ver() a private
68# API (was system_ver() in previous versions) -- use uname()
69# instead; extended the win32_ver() to also return processor
70# type information
71# 0.4.0 - added win32_ver() and modified the platform() output for WinXX
72# 0.3.4 - fixed a bug in _follow_symlinks()
73# 0.3.3 - fixed popen() and "file" command invokation bugs
74# 0.3.2 - added architecture() API and support for it in platform()
75# 0.3.1 - fixed syscmd_ver() RE to support Windows NT
76# 0.3.0 - added system alias support
77# 0.2.3 - removed 'wince' again... oh well.
78# 0.2.2 - added 'wince' to syscmd_ver() supported platforms
79# 0.2.1 - added cache logic and changed the platform string format
80# 0.2.0 - changed the API to use functions instead of module globals
81# since some action take too long to be run on module import
82# 0.1.0 - first release
83#
84# You can always get the latest version of this module at:
85#
86# http://www.egenix.com/files/python/platform.py
87#
88# If that URL should fail, try contacting the author.
89
90__copyright__ = """
91 Copyright (c) 1999-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Christian Heimes02781dc2008-03-21 01:11:52 +000092 Copyright (c) 2000-2008, eGenix.com Software GmbH; mailto:info@egenix.com
Marc-André Lemburg246d8472003-04-24 11:36:11 +000093
94 Permission to use, copy, modify, and distribute this software and its
95 documentation for any purpose and without fee or royalty is hereby granted,
96 provided that the above copyright notice appear in all copies and that
97 both that copyright notice and this permission notice appear in
98 supporting documentation or portions thereof, including modifications,
99 that you make.
100
101 EGENIX.COM SOFTWARE GMBH DISCLAIMS ALL WARRANTIES WITH REGARD TO
102 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
103 FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
104 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
105 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
106 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
107 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !
108
109"""
110
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000111__version__ = '1.0.6'
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000112
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000113import sys, os, re
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000114
115### Platform specific APIs
116
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000117_libc_search = re.compile(r'(__libc_init)'
118 '|'
Tim Peters0eadaac2003-04-24 16:02:54 +0000119 '(GLIBC_([0-9.]+))'
120 '|'
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000121 '(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)')
122
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000123def libc_ver(executable=sys.executable,lib='',version='',
124
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000125 chunksize=2048):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000126
Brett Cannon8ab27df2003-08-05 03:52:04 +0000127 """ Tries to determine the libc version that the file executable
128 (which defaults to the Python interpreter) is linked against.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000129
130 Returns a tuple of strings (lib,version) which default to the
131 given parameters in case the lookup fails.
132
133 Note that the function has intimate knowledge of how different
Brett Cannon8ab27df2003-08-05 03:52:04 +0000134 libc versions add symbols to the executable and thus is probably
135 only useable for executables compiled using gcc.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000136
137 The file is read and scanned in chunks of chunksize bytes.
138
139 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000140 if hasattr(os.path, 'realpath'):
141 # Python 2.2 introduced os.path.realpath(); it is used
142 # here to work around problems with Cygwin not being
143 # able to open symlinks for reading
144 executable = os.path.realpath(executable)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000145 f = open(executable,'rb')
Walter Dörwaldc3b6ac72007-06-07 19:26:24 +0000146 binary = f.read(chunksize).decode('latin-1')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000147 pos = 0
148 while 1:
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000149 m = _libc_search.search(binary,pos)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000150 if not m:
Walter Dörwaldc3b6ac72007-06-07 19:26:24 +0000151 binary = f.read(chunksize).decode('latin-1')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000152 if not binary:
153 break
154 pos = 0
155 continue
156 libcinit,glibc,glibcversion,so,threads,soversion = m.groups()
157 if libcinit and not lib:
158 lib = 'libc'
159 elif glibc:
160 if lib != 'glibc':
161 lib = 'glibc'
162 version = glibcversion
163 elif glibcversion > version:
164 version = glibcversion
165 elif so:
166 if lib != 'glibc':
167 lib = 'libc'
168 if soversion > version:
169 version = soversion
170 if threads and version[-len(threads):] != threads:
171 version = version + threads
172 pos = m.end()
173 f.close()
174 return lib,version
175
176def _dist_try_harder(distname,version,id):
177
Tim Peters0eadaac2003-04-24 16:02:54 +0000178 """ Tries some special tricks to get the distribution
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000179 information in case the default method fails.
180
181 Currently supports older SuSE Linux, Caldera OpenLinux and
182 Slackware Linux distributions.
183
184 """
185 if os.path.exists('/var/adm/inst-log/info'):
186 # SuSE Linux stores distribution information in that file
187 info = open('/var/adm/inst-log/info').readlines()
188 distname = 'SuSE'
189 for line in info:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000190 tv = line.split()
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000191 if len(tv) == 2:
192 tag,value = tv
193 else:
194 continue
195 if tag == 'MIN_DIST_VERSION':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000196 version = value.strip()
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000197 elif tag == 'DIST_IDENT':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000198 values = value.split('-')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000199 id = values[2]
200 return distname,version,id
201
202 if os.path.exists('/etc/.installed'):
203 # Caldera OpenLinux has some infos in that file (thanks to Colin Kong)
204 info = open('/etc/.installed').readlines()
205 for line in info:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000206 pkg = line.split('-')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000207 if len(pkg) >= 2 and pkg[0] == 'OpenLinux':
208 # XXX does Caldera support non Intel platforms ? If yes,
209 # where can we find the needed id ?
210 return 'OpenLinux',pkg[1],id
211
212 if os.path.isdir('/usr/lib/setup'):
213 # Check for slackware verson tag file (thanks to Greg Andruk)
214 verfiles = os.listdir('/usr/lib/setup')
Guido van Rossum843c7342004-05-04 18:18:59 +0000215 for n in range(len(verfiles)-1, -1, -1):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000216 if verfiles[n][:14] != 'slack-version-':
217 del verfiles[n]
218 if verfiles:
219 verfiles.sort()
220 distname = 'slackware'
221 version = verfiles[-1][14:]
222 return distname,version,id
223
224 return distname,version,id
225
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000226_release_filename = re.compile(r'(\w+)[-_](release|version)')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000227_lsb_release_version = re.compile(r'(.+)'
228 ' release '
229 '([\d.]+)'
230 '[^(]*(?:\((.+)\))?')
231_release_version = re.compile(r'([^0-9]+)'
232 '(?: release )?'
233 '([\d.]+)'
234 '[^(]*(?:\((.+)\))?')
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000235
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000236# See also http://www.novell.com/coolsolutions/feature/11251.html
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000237# and http://linuxmafia.com/faq/Admin/release-files.html
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000238# and http://data.linux-ntfs.org/rpm/whichrpm
239# and http://www.die.net/doc/linux/man/man1/lsb_release.1.html
Marc-André Lemburg380f4172005-11-07 16:11:02 +0000240
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000241_supported_dists = (
242 'SuSE', 'debian', 'fedora', 'redhat', 'centos',
243 'mandrake', 'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo',
244 'UnitedLinux', 'turbolinux')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000245
246def _parse_release_file(firstline):
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000247
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000248 # Parse the first line
249 m = _lsb_release_version.match(firstline)
250 if m is not None:
251 # LSB format: "distro release x.x (codename)"
252 return tuple(m.groups())
253
254 # Pre-LSB format: "distro x.x (codename)"
255 m = _release_version.match(firstline)
256 if m is not None:
257 return tuple(m.groups())
258
259 # Unkown format... take the first two words
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000260 l = firstline.strip().split()
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000261 if l:
262 version = l[0]
263 if len(l) > 1:
264 id = l[1]
265 else:
266 id = ''
267 return '', version, id
268
269def _test_parse_release_file():
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000270
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000271 for input, output in (
272 # Examples of release file contents:
273 ('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64'))
274 ('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64'))
275 ('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586'))
276 ('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux'))
277 ('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche'))
278 ('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike'))
279 ('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant'))
280 ('CentOS release 4', ('CentOS', '4', None))
281 ('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia'))
282 ):
283 parsed = _parse_release_file(input)
284 if parsed != output:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000285 print((input, parsed))
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000286
287def linux_distribution(distname='', version='', id='',
288
289 supported_dists=_supported_dists,
290 full_distribution_name=1):
291
292 """ Tries to determine the name of the Linux OS distribution name.
293
294 The function first looks for a distribution release file in
295 /etc and then reverts to _dist_try_harder() in case no
296 suitable files are found.
297
298 supported_dists may be given to define the set of Linux
299 distributions to look for. It defaults to a list of currently
300 supported Linux distributions identified by their release file
301 name.
302
303 If full_distribution_name is true (default), the full
304 distribution read from the OS is returned. Otherwise the short
305 name taken from supported_dists is used.
306
307 Returns a tuple (distname,version,id) which default to the
308 args given as parameters.
309
310 """
311 try:
312 etc = os.listdir('/etc')
313 except os.error:
314 # Probably not a Unix system
315 return distname,version,id
316 etc.sort()
317 for file in etc:
318 m = _release_filename.match(file)
319 if m is not None:
320 _distname,dummy = m.groups()
321 if _distname in supported_dists:
322 distname = _distname
323 break
324 else:
325 return _dist_try_harder(distname,version,id)
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000326
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000327 # Read the first line
328 f = open('/etc/'+file, 'r')
329 firstline = f.readline()
330 f.close()
331 _distname, _version, _id = _parse_release_file(firstline)
332
333 if _distname and full_distribution_name:
334 distname = _distname
335 if _version:
336 version = _version
337 if _id:
338 id = _id
339 return distname, version, id
340
341# To maintain backwards compatibility:
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000342
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000343def dist(distname='',version='',id='',
344
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000345 supported_dists=_supported_dists):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000346
Brett Cannon8ab27df2003-08-05 03:52:04 +0000347 """ Tries to determine the name of the Linux OS distribution name.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000348
349 The function first looks for a distribution release file in
350 /etc and then reverts to _dist_try_harder() in case no
351 suitable files are found.
352
Brett Cannon8ab27df2003-08-05 03:52:04 +0000353 Returns a tuple (distname,version,id) which default to the
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000354 args given as parameters.
355
356 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000357 return linux_distribution(distname, version, id,
358 supported_dists=supported_dists,
359 full_distribution_name=0)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000360
361class _popen:
362
363 """ Fairly portable (alternative) popen implementation.
364
365 This is mostly needed in case os.popen() is not available, or
366 doesn't work as advertised, e.g. in Win9X GUI programs like
367 PythonWin or IDLE.
368
369 Writing to the pipe is currently not supported.
370
371 """
372 tmpfile = ''
373 pipe = None
374 bufsize = None
375 mode = 'r'
376
377 def __init__(self,cmd,mode='r',bufsize=None):
378
379 if mode != 'r':
Collin Winterce36ad82007-08-30 01:19:48 +0000380 raise ValueError('popen()-emulation only supports read mode')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000381 import tempfile
382 self.tmpfile = tmpfile = tempfile.mktemp()
383 os.system(cmd + ' > %s' % tmpfile)
384 self.pipe = open(tmpfile,'rb')
385 self.bufsize = bufsize
386 self.mode = mode
387
388 def read(self):
389
390 return self.pipe.read()
391
392 def readlines(self):
393
394 if self.bufsize is not None:
395 return self.pipe.readlines()
396
397 def close(self,
398
399 remove=os.unlink,error=os.error):
400
401 if self.pipe:
402 rc = self.pipe.close()
403 else:
404 rc = 255
405 if self.tmpfile:
406 try:
407 remove(self.tmpfile)
408 except error:
409 pass
410 return rc
411
412 # Alias
413 __del__ = close
414
415def popen(cmd, mode='r', bufsize=None):
416
417 """ Portable popen() interface.
418 """
419 # Find a working popen implementation preferring win32pipe.popen
420 # over os.popen over _popen
421 popen = None
422 if os.environ.get('OS','') == 'Windows_NT':
423 # On NT win32pipe should work; on Win9x it hangs due to bugs
424 # in the MS C lib (see MS KnowledgeBase article Q150956)
425 try:
426 import win32pipe
427 except ImportError:
428 pass
429 else:
430 popen = win32pipe.popen
431 if popen is None:
432 if hasattr(os,'popen'):
433 popen = os.popen
434 # Check whether it works... it doesn't in GUI programs
435 # on Windows platforms
436 if sys.platform == 'win32': # XXX Others too ?
437 try:
438 popen('')
439 except os.error:
440 popen = _popen
441 else:
442 popen = _popen
443 if bufsize is None:
444 return popen(cmd,mode)
445 else:
446 return popen(cmd,mode,bufsize)
447
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000448def _norm_version(version, build=''):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000449
Brett Cannon8ab27df2003-08-05 03:52:04 +0000450 """ Normalize the version and build strings and return a single
Walter Dörwalde5a7fad2005-11-21 17:01:27 +0000451 version string using the format major.minor.build (or patchlevel).
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000452 """
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000453 l = version.split('.')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000454 if build:
455 l.append(build)
456 try:
457 ints = map(int,l)
458 except ValueError:
459 strings = l
460 else:
Neal Norwitz78a70bd2007-08-30 06:16:26 +0000461 strings = list(map(str,ints))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000462 version = '.'.join(strings[:3])
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000463 return version
464
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000465_ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) '
466 '.*'
467 'Version ([\d.]+))')
468
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000469def _syscmd_ver(system='', release='', version='',
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000470
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000471 supported_platforms=('win32','win16','dos','os2')):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000472
473 """ Tries to figure out the OS version used and returns
474 a tuple (system,release,version).
Tim Peters0eadaac2003-04-24 16:02:54 +0000475
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000476 It uses the "ver" shell command for this which is known
477 to exists on Windows, DOS and OS/2. XXX Others too ?
478
479 In case this fails, the given parameters are used as
480 defaults.
481
482 """
483 if sys.platform not in supported_platforms:
484 return system,release,version
485
486 # Try some common cmd strings
487 for cmd in ('ver','command /c ver','cmd /c ver'):
488 try:
489 pipe = popen(cmd)
490 info = pipe.read()
491 if pipe.close():
Collin Winterce36ad82007-08-30 01:19:48 +0000492 raise os.error('command failed')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000493 # XXX How can I supress shell errors from being written
494 # to stderr ?
Guido van Rossumb940e112007-01-10 16:19:56 +0000495 except os.error as why:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000496 #print 'Command %s failed: %s' % (cmd,why)
497 continue
Guido van Rossumb940e112007-01-10 16:19:56 +0000498 except IOError as why:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000499 #print 'Command %s failed: %s' % (cmd,why)
500 continue
501 else:
502 break
503 else:
504 return system,release,version
505
506 # Parse the output
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000507 info = info.strip()
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000508 m = _ver_output.match(info)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000509 if m is not None:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000510 system,release,version = m.groups()
511 # Strip trailing dots from version and release
512 if release[-1] == '.':
513 release = release[:-1]
514 if version[-1] == '.':
515 version = version[:-1]
516 # Normalize the version and build strings (eliminating additional
517 # zeros)
518 version = _norm_version(version)
519 return system,release,version
520
521def _win32_getvalue(key,name,default=''):
522
523 """ Read a value for name from the registry key.
524
525 In case this fails, default is returned.
526
527 """
Christian Heimes02781dc2008-03-21 01:11:52 +0000528 try:
529 # Use win32api if available
530 from win32api import RegQueryValueEx
531 except ImportError:
532 # On Python 2.0 and later, emulate using _winreg
533 import _winreg
534 RegQueryValueEx = _winreg.QueryValueEx
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000535 try:
536 return RegQueryValueEx(key,name)
537 except:
538 return default
539
540def win32_ver(release='',version='',csd='',ptype=''):
541
542 """ Get additional version information from the Windows Registry
543 and return a tuple (version,csd,ptype) referring to version
544 number, CSD level and OS type (multi/single
545 processor).
546
547 As a hint: ptype returns 'Uniprocessor Free' on single
548 processor NT machines and 'Multiprocessor Free' on multi
549 processor machines. The 'Free' refers to the OS version being
550 free of debugging code. It could also state 'Checked' which
551 means the OS version uses debugging code, i.e. code that
552 checks arguments, ranges, etc. (Thomas Heller).
553
Christian Heimes02781dc2008-03-21 01:11:52 +0000554 Note: this function works best with Mark Hammond's win32
555 package installed, but also on Python 2.3 and later. It
556 obviously only runs on Win32 compatible platforms.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000557
558 """
559 # XXX Is there any way to find out the processor type on WinXX ?
560 # XXX Is win32 available on Windows CE ?
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000561 #
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000562 # Adapted from code posted by Karl Putland to comp.lang.python.
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000563 #
564 # The mappings between reg. values and release names can be found
565 # here: http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000566
567 # Import the needed APIs
568 try:
569 import win32api
Christian Heimes02781dc2008-03-21 01:11:52 +0000570 from win32api import RegQueryValueEx, RegOpenKeyEx, \
571 RegCloseKey, GetVersionEx
572 from win32con import HKEY_LOCAL_MACHINE, VER_PLATFORM_WIN32_NT, \
573 VER_PLATFORM_WIN32_WINDOWS, VER_NT_WORKSTATION
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000574 except ImportError:
Christian Heimes02781dc2008-03-21 01:11:52 +0000575 # Emulate the win32api module using Python APIs
576 try:
577 sys.getwindowsversion
578 except AttributeError:
579 # No emulation possible, so return the defaults...
580 return release,version,csd,ptype
581 else:
582 # Emulation using _winreg (added in Python 2.0) and
583 # sys.getwindowsversion() (added in Python 2.3)
584 import _winreg
585 GetVersionEx = sys.getwindowsversion
586 RegQueryValueEx = _winreg.QueryValueEx
587 RegOpenKeyEx = _winreg.OpenKeyEx
588 RegCloseKey = _winreg.CloseKey
589 HKEY_LOCAL_MACHINE = _winreg.HKEY_LOCAL_MACHINE
590 VER_PLATFORM_WIN32_WINDOWS = 1
591 VER_PLATFORM_WIN32_NT = 2
592 VER_NT_WORKSTATION = 1
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000593
594 # Find out the registry key and some general version infos
595 maj,min,buildno,plat,csd = GetVersionEx()
596 version = '%i.%i.%i' % (maj,min,buildno & 0xFFFF)
597 if csd[:13] == 'Service Pack ':
598 csd = 'SP' + csd[13:]
599 if plat == VER_PLATFORM_WIN32_WINDOWS:
600 regkey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
601 # Try to guess the release name
602 if maj == 4:
603 if min == 0:
604 release = '95'
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000605 elif min == 10:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000606 release = '98'
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000607 elif min == 90:
608 release = 'Me'
609 else:
610 release = 'postMe'
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000611 elif maj == 5:
612 release = '2000'
613 elif plat == VER_PLATFORM_WIN32_NT:
614 regkey = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion'
615 if maj <= 4:
616 release = 'NT'
617 elif maj == 5:
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000618 if min == 0:
619 release = '2000'
620 elif min == 1:
621 release = 'XP'
622 elif min == 2:
623 release = '2003Server'
624 else:
625 release = 'post2003'
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000626 elif maj == 6:
627 if min == 0:
628 # Per http://msdn2.microsoft.com/en-us/library/ms724429.aspx
Christian Heimes02781dc2008-03-21 01:11:52 +0000629 try:
630 productType = GetVersionEx(1)[8]
631 except TypeError:
632 # sys.getwindowsversion() doesn't take any arguments, so
633 # we cannot detect 2008 Server that way.
634 # XXX Add some other means of detecting 2008 Server ?!
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000635 release = 'Vista'
636 else:
Christian Heimes02781dc2008-03-21 01:11:52 +0000637 if productType == VER_NT_WORKSTATION:
638 release = 'Vista'
639 else:
640 release = '2008Server'
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000641 else:
642 release = 'post2008Server'
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000643 else:
644 if not release:
645 # E.g. Win3.1 with win32s
646 release = '%i.%i' % (maj,min)
647 return release,version,csd,ptype
648
649 # Open the registry key
650 try:
Christian Heimes02781dc2008-03-21 01:11:52 +0000651 keyCurVer = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000652 # Get a value to make sure the key exists...
Christian Heimes02781dc2008-03-21 01:11:52 +0000653 RegQueryValueEx(keyCurVer, 'SystemRoot')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000654 except:
655 return release,version,csd,ptype
Tim Peters0eadaac2003-04-24 16:02:54 +0000656
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000657 # Parse values
658 #subversion = _win32_getvalue(keyCurVer,
659 # 'SubVersionNumber',
660 # ('',1))[0]
661 #if subversion:
662 # release = release + subversion # 95a, 95b, etc.
663 build = _win32_getvalue(keyCurVer,
664 'CurrentBuildNumber',
665 ('',1))[0]
666 ptype = _win32_getvalue(keyCurVer,
667 'CurrentType',
668 (ptype,1))[0]
669
670 # Normalize version
671 version = _norm_version(version,build)
672
673 # Close key
674 RegCloseKey(keyCurVer)
675 return release,version,csd,ptype
676
677def _mac_ver_lookup(selectors,default=None):
678
679 from gestalt import gestalt
Jack Jansena290e3d2003-08-11 11:08:49 +0000680 import MacOS
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000681 l = []
682 append = l.append
683 for selector in selectors:
684 try:
685 append(gestalt(selector))
Jack Jansena290e3d2003-08-11 11:08:49 +0000686 except (RuntimeError, MacOS.Error):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000687 append(default)
688 return l
689
690def _bcd2str(bcd):
691
692 return hex(bcd)[2:]
693
694def mac_ver(release='',versioninfo=('','',''),machine=''):
695
696 """ Get MacOS version information and return it as tuple (release,
697 versioninfo, machine) with versioninfo being a tuple (version,
698 dev_stage, non_release_version).
699
Brett Cannon8ab27df2003-08-05 03:52:04 +0000700 Entries which cannot be determined are set to the paramter values
701 which default to ''. All tuple entries are strings.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000702
703 Thanks to Mark R. Levinson for mailing documentation links and
704 code examples for this function. Documentation for the
705 gestalt() API is available online at:
706
707 http://www.rgaros.nl/gestalt/
708
709 """
710 # Check whether the version info module is available
711 try:
712 import gestalt
Jack Jansena290e3d2003-08-11 11:08:49 +0000713 import MacOS
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000714 except ImportError:
715 return release,versioninfo,machine
716 # Get the infos
717 sysv,sysu,sysa = _mac_ver_lookup(('sysv','sysu','sysa'))
718 # Decode the infos
719 if sysv:
720 major = (sysv & 0xFF00) >> 8
721 minor = (sysv & 0x00F0) >> 4
722 patch = (sysv & 0x000F)
Christian Heimese4ca8152008-05-08 17:18:53 +0000723
724 if (major, minor) >= (10, 4):
725 # the 'sysv' gestald cannot return patchlevels
726 # higher than 9. Apple introduced 3 new
727 # gestalt codes in 10.4 to deal with this
728 # issue (needed because patch levels can
729 # run higher than 9, such as 10.4.11)
730 major,minor,patch = _mac_ver_lookup(('sys1','sys2','sys3'))
731 release = '%i.%i.%i' %(major, minor, patch)
732 else:
733 release = '%s.%i.%i' % (_bcd2str(major),minor,patch)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000734 if sysu:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000735 major = int((sysu & 0xFF000000) >> 24)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000736 minor = (sysu & 0x00F00000) >> 20
737 bugfix = (sysu & 0x000F0000) >> 16
738 stage = (sysu & 0x0000FF00) >> 8
739 nonrel = (sysu & 0x000000FF)
740 version = '%s.%i.%i' % (_bcd2str(major),minor,bugfix)
741 nonrel = _bcd2str(nonrel)
742 stage = {0x20:'development',
743 0x40:'alpha',
744 0x60:'beta',
745 0x80:'final'}.get(stage,'')
746 versioninfo = (version,stage,nonrel)
747 if sysa:
Tim Peters0eadaac2003-04-24 16:02:54 +0000748 machine = {0x1: '68k',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000749 0x2: 'PowerPC',
750 0xa: 'i386'}.get(sysa,'')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000751 return release,versioninfo,machine
752
Neal Norwitz9b924c62003-06-29 04:17:45 +0000753def _java_getprop(name,default):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000754
755 from java.lang import System
756 try:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000757 value = System.getProperty(name)
758 if value is None:
759 return default
760 return value
761 except AttributeError:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000762 return default
763
764def java_ver(release='',vendor='',vminfo=('','',''),osinfo=('','','')):
Tim Peters0eadaac2003-04-24 16:02:54 +0000765
Brett Cannon8ab27df2003-08-05 03:52:04 +0000766 """ Version interface for Jython.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000767
768 Returns a tuple (release,vendor,vminfo,osinfo) with vminfo being
769 a tuple (vm_name,vm_release,vm_vendor) and osinfo being a
770 tuple (os_name,os_version,os_arch).
771
772 Values which cannot be determined are set to the defaults
773 given as parameters (which all default to '').
774
775 """
776 # Import the needed APIs
777 try:
778 import java.lang
779 except ImportError:
780 return release,vendor,vminfo,osinfo
781
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000782 vendor = _java_getprop('java.vendor', vendor)
783 release = _java_getprop('java.version', release)
784 vm_name, vm_release, vm_vendor = vminfo
785 vm_name = _java_getprop('java.vm.name', vm_name)
786 vm_vendor = _java_getprop('java.vm.vendor', vm_vendor)
787 vm_release = _java_getprop('java.vm.version', vm_release)
788 vminfo = vm_name, vm_release, vm_vendor
789 os_name, os_version, os_arch = osinfo
790 os_arch = _java_getprop('java.os.arch', os_arch)
791 os_name = _java_getprop('java.os.name', os_name)
792 os_version = _java_getprop('java.os.version', os_version)
793 osinfo = os_name, os_version, os_arch
Tim Peters0eadaac2003-04-24 16:02:54 +0000794
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000795 return release, vendor, vminfo, osinfo
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000796
797### System name aliasing
798
799def system_alias(system,release,version):
800
801 """ Returns (system,release,version) aliased to common
802 marketing names used for some systems.
803
804 It also does some reordering of the information in some cases
805 where it would otherwise cause confusion.
806
807 """
808 if system == 'Rhapsody':
809 # Apple's BSD derivative
810 # XXX How can we determine the marketing release number ?
811 return 'MacOS X Server',system+release,version
812
813 elif system == 'SunOS':
814 # Sun's OS
815 if release < '5':
816 # These releases use the old name SunOS
817 return system,release,version
818 # Modify release (marketing release = SunOS release - 3)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000819 l = release.split('.')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000820 if l:
821 try:
822 major = int(l[0])
823 except ValueError:
824 pass
825 else:
826 major = major - 3
827 l[0] = str(major)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000828 release = '.'.join(l)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000829 if release < '6':
830 system = 'Solaris'
831 else:
832 # XXX Whatever the new SunOS marketing name is...
833 system = 'Solaris'
834
835 elif system == 'IRIX64':
836 # IRIX reports IRIX64 on platforms with 64-bit support; yet it
837 # is really a version and not a different platform, since 32-bit
838 # apps are also supported..
839 system = 'IRIX'
840 if version:
841 version = version + ' (64bit)'
842 else:
843 version = '64bit'
844
845 elif system in ('win32','win16'):
846 # In case one of the other tricks
847 system = 'Windows'
848
849 return system,release,version
850
851### Various internal helpers
852
853def _platform(*args):
854
855 """ Helper to format the platform string in a filename
856 compatible format e.g. "system-version-machine".
857 """
858 # Format the platform string
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000859 platform = '-'.join(x.strip() for x in filter(len, args))
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000860
861 # Cleanup some possible filename obstacles...
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000862 platform = platform.replace(' ','_')
863 platform = platform.replace('/','-')
864 platform = platform.replace('\\','-')
865 platform = platform.replace(':','-')
866 platform = platform.replace(';','-')
867 platform = platform.replace('"','-')
868 platform = platform.replace('(','-')
869 platform = platform.replace(')','-')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000870
871 # No need to report 'unknown' information...
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000872 platform = platform.replace('unknown','')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000873
874 # Fold '--'s and remove trailing '-'
875 while 1:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000876 cleaned = platform.replace('--','-')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000877 if cleaned == platform:
878 break
879 platform = cleaned
880 while platform[-1] == '-':
881 platform = platform[:-1]
882
883 return platform
884
885def _node(default=''):
886
887 """ Helper to determine the node name of this machine.
888 """
889 try:
890 import socket
891 except ImportError:
892 # No sockets...
893 return default
894 try:
895 return socket.gethostname()
896 except socket.error:
897 # Still not working...
898 return default
899
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000900def _follow_symlinks(filepath):
901
902 """ In case filepath is a symlink, follow it until a
903 real file is reached.
904 """
Georg Brandl673f7ef2008-01-05 21:20:19 +0000905 filepath = os.path.abspath(filepath)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000906 while os.path.islink(filepath):
907 filepath = os.path.normpath(
908 os.path.join(filepath,os.readlink(filepath)))
909 return filepath
910
911def _syscmd_uname(option,default=''):
912
913 """ Interface to the system's uname command.
914 """
915 if sys.platform in ('dos','win32','win16','os2'):
916 # XXX Others too ?
917 return default
918 try:
919 f = os.popen('uname %s 2> /dev/null' % option)
920 except (AttributeError,os.error):
921 return default
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000922 output = f.read().strip()
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000923 rc = f.close()
924 if not output or rc:
925 return default
926 else:
927 return output
928
929def _syscmd_file(target,default=''):
930
931 """ Interface to the system's file command.
932
933 The function uses the -b option of the file command to have it
934 ommit the filename in its output and if possible the -L option
935 to have the command follow symlinks. It returns default in
936 case the command should fail.
937
938 """
939 target = _follow_symlinks(target)
940 try:
941 f = os.popen('file %s 2> /dev/null' % target)
942 except (AttributeError,os.error):
943 return default
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000944 output = f.read().strip()
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000945 rc = f.close()
946 if not output or rc:
947 return default
948 else:
949 return output
950
951### Information about the used architecture
952
953# Default values for architecture; non-empty strings override the
954# defaults given as parameters
955_default_architecture = {
956 'win32': ('','WindowsPE'),
957 'win16': ('','Windows'),
958 'dos': ('','MSDOS'),
959}
960
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000961_architecture_split = re.compile(r'[\s,]').split
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000962
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000963def architecture(executable=sys.executable,bits='',linkage=''):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000964
965 """ Queries the given executable (defaults to the Python interpreter
Brett Cannon8ab27df2003-08-05 03:52:04 +0000966 binary) for various architecture information.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000967
Brett Cannon8ab27df2003-08-05 03:52:04 +0000968 Returns a tuple (bits,linkage) which contains information about
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000969 the bit architecture and the linkage format used for the
970 executable. Both values are returned as strings.
971
972 Values that cannot be determined are returned as given by the
973 parameter presets. If bits is given as '', the sizeof(pointer)
974 (or sizeof(long) on Python version < 1.5.2) is used as
975 indicator for the supported pointer size.
976
977 The function relies on the system's "file" command to do the
978 actual work. This is available on most if not all Unix
Brett Cannon8ab27df2003-08-05 03:52:04 +0000979 platforms. On some non-Unix platforms where the "file" command
980 does not exist and the executable is set to the Python interpreter
981 binary defaults from _default_architecture are used.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000982
983 """
984 # Use the sizeof(pointer) as default number of bits if nothing
985 # else is given as default.
986 if not bits:
987 import struct
988 try:
989 size = struct.calcsize('P')
990 except struct.error:
991 # Older installations can only query longs
992 size = struct.calcsize('l')
993 bits = str(size*8) + 'bit'
Tim Peters0eadaac2003-04-24 16:02:54 +0000994
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000995 # Get data from the 'file' system command
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000996 if executable:
997 output = _syscmd_file(executable, '')
998 else:
999 output = ''
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001000
1001 if not output and \
1002 executable == sys.executable:
1003 # "file" command did not return anything; we'll try to provide
Tim Peters0eadaac2003-04-24 16:02:54 +00001004 # some sensible defaults then...
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001005 if sys.platform in _default_architecture:
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001006 b,l = _default_architecture[sys.platform]
1007 if b:
1008 bits = b
1009 if l:
1010 linkage = l
1011 return bits,linkage
1012
1013 # Split the output into a list of strings omitting the filename
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +00001014 fileout = _architecture_split(output)[1:]
Tim Peters0eadaac2003-04-24 16:02:54 +00001015
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001016 if 'executable' not in fileout:
1017 # Format not supported
1018 return bits,linkage
1019
1020 # Bits
1021 if '32-bit' in fileout:
1022 bits = '32bit'
1023 elif 'N32' in fileout:
1024 # On Irix only
1025 bits = 'n32bit'
1026 elif '64-bit' in fileout:
1027 bits = '64bit'
1028
1029 # Linkage
1030 if 'ELF' in fileout:
1031 linkage = 'ELF'
1032 elif 'PE' in fileout:
1033 # E.g. Windows uses this format
1034 if 'Windows' in fileout:
1035 linkage = 'WindowsPE'
1036 else:
1037 linkage = 'PE'
1038 elif 'COFF' in fileout:
1039 linkage = 'COFF'
1040 elif 'MS-DOS' in fileout:
1041 linkage = 'MSDOS'
1042 else:
1043 # XXX the A.OUT format also falls under this class...
1044 pass
1045
1046 return bits,linkage
1047
1048### Portable uname() interface
Tim Peters0eadaac2003-04-24 16:02:54 +00001049
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001050_uname_cache = None
1051
1052def uname():
1053
1054 """ Fairly portable uname interface. Returns a tuple
1055 of strings (system,node,release,version,machine,processor)
1056 identifying the underlying platform.
1057
1058 Note that unlike the os.uname function this also returns
Brett Cannon8ab27df2003-08-05 03:52:04 +00001059 possible processor information as an additional tuple entry.
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001060
1061 Entries which cannot be determined are set to ''.
1062
1063 """
1064 global _uname_cache
1065
1066 if _uname_cache is not None:
1067 return _uname_cache
1068
1069 # Get some infos from the builtin os.uname API...
1070 try:
1071 system,node,release,version,machine = os.uname()
1072
1073 except AttributeError:
1074 # Hmm, no uname... we'll have to poke around the system then.
1075 system = sys.platform
1076 release = ''
1077 version = ''
1078 node = _node()
1079 machine = ''
1080 processor = ''
1081 use_syscmd_ver = 1
1082
1083 # Try win32_ver() on win32 platforms
1084 if system == 'win32':
1085 release,version,csd,ptype = win32_ver()
1086 if release and version:
1087 use_syscmd_ver = 0
Christian Heimes02781dc2008-03-21 01:11:52 +00001088 # Try to use the PROCESSOR_* environment variables
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001089 # available on Win XP and later; see
1090 # http://support.microsoft.com/kb/888731 and
1091 # http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM
Christian Heimes02781dc2008-03-21 01:11:52 +00001092 machine = os.environ.get('PROCESSOR_ARCHITECTURE', '')
1093 processor = os.environ.get('PROCESSOR_IDENTIFIER', machine)
Tim Peters0eadaac2003-04-24 16:02:54 +00001094
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001095 # Try the 'ver' system command available on some
1096 # platforms
1097 if use_syscmd_ver:
1098 system,release,version = _syscmd_ver(system)
Marc-André Lemburgcdc79232004-06-19 17:17:00 +00001099 # Normalize system to what win32_ver() normally returns
1100 # (_syscmd_ver() tends to return the vendor name as well)
1101 if system == 'Microsoft Windows':
1102 system = 'Windows'
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001103 elif system == 'Microsoft' and release == 'Windows':
1104 # Under Windows Vista and Windows Server 2008,
1105 # Microsoft changed the output of the ver command. The
1106 # release is no longer printed. This causes the
1107 # system and release to be misidentified.
1108 system = 'Windows'
1109 if '6.0' == version[:3]:
1110 release = 'Vista'
1111 else:
1112 release = ''
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001113
1114 # In case we still don't know anything useful, we'll try to
1115 # help ourselves
1116 if system in ('win32','win16'):
1117 if not version:
1118 if system == 'win32':
1119 version = '32bit'
1120 else:
1121 version = '16bit'
1122 system = 'Windows'
1123
1124 elif system[:4] == 'java':
1125 release,vendor,vminfo,osinfo = java_ver()
1126 system = 'Java'
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001127 version = ', '.join(vminfo)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001128 if not version:
1129 version = vendor
1130
1131 elif os.name == 'mac':
1132 release,(version,stage,nonrel),machine = mac_ver()
1133 system = 'MacOS'
1134
1135 else:
1136 # System specific extensions
1137 if system == 'OpenVMS':
1138 # OpenVMS seems to have release and version mixed up
1139 if not release or release == '0':
1140 release = version
1141 version = ''
1142 # Get processor information
1143 try:
1144 import vms_lib
1145 except ImportError:
1146 pass
1147 else:
1148 csid, cpu_number = vms_lib.getsyi('SYI$_CPU',0)
1149 if (cpu_number >= 128):
1150 processor = 'Alpha'
1151 else:
1152 processor = 'VAX'
1153 else:
1154 # Get processor information from the uname system command
1155 processor = _syscmd_uname('-p','')
1156
1157 # 'unknown' is not really any useful as information; we'll convert
1158 # it to '' which is more portable
1159 if system == 'unknown':
1160 system = ''
1161 if node == 'unknown':
1162 node = ''
1163 if release == 'unknown':
1164 release = ''
1165 if version == 'unknown':
1166 version = ''
1167 if machine == 'unknown':
1168 machine = ''
1169 if processor == 'unknown':
1170 processor = ''
Thomas Wouters1b7f8912007-09-19 03:06:30 +00001171
1172 # normalize name
1173 if system == 'Microsoft' and release == 'Windows':
1174 system = 'Windows'
1175 release = 'Vista'
1176
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001177 _uname_cache = system,node,release,version,machine,processor
1178 return _uname_cache
1179
1180### Direct interfaces to some of the uname() return values
1181
1182def system():
1183
1184 """ Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.
1185
1186 An empty string is returned if the value cannot be determined.
1187
1188 """
1189 return uname()[0]
1190
1191def node():
1192
Brett Cannon8ab27df2003-08-05 03:52:04 +00001193 """ Returns the computer's network name (which may not be fully
1194 qualified)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001195
1196 An empty string is returned if the value cannot be determined.
1197
1198 """
1199 return uname()[1]
1200
1201def release():
1202
1203 """ Returns the system's release, e.g. '2.2.0' or 'NT'
1204
1205 An empty string is returned if the value cannot be determined.
1206
1207 """
1208 return uname()[2]
1209
1210def version():
1211
1212 """ Returns the system's release version, e.g. '#3 on degas'
1213
1214 An empty string is returned if the value cannot be determined.
1215
1216 """
1217 return uname()[3]
1218
1219def machine():
1220
1221 """ Returns the machine type, e.g. 'i386'
1222
1223 An empty string is returned if the value cannot be determined.
1224
1225 """
1226 return uname()[4]
1227
1228def processor():
1229
1230 """ Returns the (true) processor name, e.g. 'amdk6'
1231
1232 An empty string is returned if the value cannot be
1233 determined. Note that many platforms do not provide this
1234 information or simply return the same value as for machine(),
1235 e.g. NetBSD does this.
1236
1237 """
1238 return uname()[5]
1239
1240### Various APIs for extracting information from sys.version
1241
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001242_sys_version_parser = re.compile(
1243 r'([\w.+]+)\s*'
1244 '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
1245 '\[([^\]]+)\]?')
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001246
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001247_jython_sys_version_parser = re.compile(
1248 r'([\d\.]+)')
1249
1250_ironpython_sys_version_parser = re.compile(
1251 r'IronPython\s*'
1252 '([\d\.]+)'
1253 '(?: \(([\d\.]+)\))?'
1254 ' on (.NET [\d\.]+)')
1255
1256_sys_version_cache = {}
1257
1258def _sys_version(sys_version=None):
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001259
1260 """ Returns a parsed version of Python's sys.version as tuple
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001261 (name, version, branch, revision, buildno, builddate, compiler)
1262 referring to the Python implementation name, version, branch,
1263 revision, build number, build date/time as string and the compiler
1264 identification string.
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001265
1266 Note that unlike the Python sys.version, the returned value
1267 for the Python version will always include the patchlevel (it
1268 defaults to '.0').
1269
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001270 The function returns empty strings for tuple entries that
1271 cannot be determined.
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001272
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001273 sys_version may be given to parse an alternative version
1274 string, e.g. if the version was read from a different Python
1275 interpreter.
1276
1277 """
1278 # Get the Python version
1279 if sys_version is None:
1280 sys_version = sys.version
1281
1282 # Try the cache first
1283 result = _sys_version_cache.get(sys_version, None)
1284 if result is not None:
1285 return result
1286
1287 # Parse it
1288 if sys_version[:10] == 'IronPython':
1289 # IronPython
1290 name = 'IronPython'
1291 match = _ironpython_sys_version_parser.match(sys_version)
1292 if match is None:
1293 raise ValueError(
1294 'failed to parse IronPython sys.version: %s' %
1295 repr(sys_version))
1296 version, alt_version, compiler = match.groups()
1297 branch = ''
1298 revision = ''
1299 buildno = ''
1300 builddate = ''
1301
1302 elif sys.platform[:4] == 'java':
1303 # Jython
1304 name = 'Jython'
1305 match = _jython_sys_version_parser.match(sys_version)
1306 if match is None:
1307 raise ValueError(
1308 'failed to parse Jython sys.version: %s' %
1309 repr(sys_version))
1310 version, = match.groups()
1311 branch = ''
1312 revision = ''
1313 compiler = sys.platform
1314 buildno = ''
1315 builddate = ''
1316
1317 else:
1318 # CPython
1319 match = _sys_version_parser.match(sys_version)
1320 if match is None:
1321 raise ValueError(
1322 'failed to parse CPython sys.version: %s' %
1323 repr(sys_version))
1324 version, buildno, builddate, buildtime, compiler = \
1325 match.groups()
1326 if hasattr(sys, 'subversion'):
1327 # sys.subversion was added in Python 2.5
1328 name, branch, revision = sys.subversion
1329 else:
1330 name = 'CPython'
1331 branch = ''
1332 revision = ''
1333 builddate = builddate + ' ' + buildtime
1334
1335 # Add the patchlevel version if missing
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001336 l = version.split('.')
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001337 if len(l) == 2:
1338 l.append('0')
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001339 version = '.'.join(l)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001340
1341 # Build and cache the result
1342 result = (name, version, branch, revision, buildno, builddate, compiler)
1343 _sys_version_cache[sys_version] = result
1344 return result
1345
1346def _test_sys_version():
1347
1348 _sys_version_cache.clear()
1349 for input, output in (
1350 ('2.4.3 (#1, Jun 21 2006, 13:54:21) \n[GCC 3.3.4 (pre 3.3.5 20040809)]',
1351 ('CPython', '2.4.3', '', '', '1', 'Jun 21 2006 13:54:21', 'GCC 3.3.4 (pre 3.3.5 20040809)')),
1352 ('IronPython 1.0.60816 on .NET 2.0.50727.42',
1353 ('IronPython', '1.0.60816', '', '', '', '', '.NET 2.0.50727.42')),
1354 ('IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42',
1355 ('IronPython', '1.0.0', '', '', '', '', '.NET 2.0.50727.42')),
1356 ):
1357 parsed = _sys_version(input)
1358 if parsed != output:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001359 print((input, parsed))
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001360
1361def python_implementation():
1362
1363 """ Returns a string identifying the Python implementation.
1364
1365 Currently, the following implementations are identified:
1366 'CPython' (C implementation of Python),
1367 'IronPython' (.NET implementation of Python),
1368 'Jython' (Java implementation of Python).
1369
1370 """
1371 return _sys_version()[0]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001372
1373def python_version():
1374
1375 """ Returns the Python version as string 'major.minor.patchlevel'
1376
1377 Note that unlike the Python sys.version, the returned value
1378 will always include the patchlevel (it defaults to 0).
1379
1380 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001381 if hasattr(sys, 'version_info'):
1382 return '%i.%i.%i' % sys.version_info[:3]
1383 return _sys_version()[1]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001384
1385def python_version_tuple():
1386
1387 """ Returns the Python version as tuple (major, minor, patchlevel)
1388 of strings.
1389
1390 Note that unlike the Python sys.version, the returned value
1391 will always include the patchlevel (it defaults to 0).
1392
1393 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001394 if hasattr(sys, 'version_info'):
1395 return sys.version_info[:3]
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001396 return tuple(_sys_version()[1].split('.'))
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001397
1398def python_branch():
1399
1400 """ Returns a string identifying the Python implementation
1401 branch.
1402
1403 For CPython this is the Subversion branch from which the
1404 Python binary was built.
1405
1406 If not available, an empty string is returned.
1407
1408 """
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001409
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001410 return _sys_version()[2]
1411
1412def python_revision():
1413
1414 """ Returns a string identifying the Python implementation
1415 revision.
1416
1417 For CPython this is the Subversion revision from which the
1418 Python binary was built.
1419
1420 If not available, an empty string is returned.
1421
1422 """
1423 return _sys_version()[3]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001424
1425def python_build():
1426
1427 """ Returns a tuple (buildno, builddate) stating the Python
1428 build number and date as strings.
1429
1430 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001431 return _sys_version()[4:6]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001432
1433def python_compiler():
1434
1435 """ Returns a string identifying the compiler used for compiling
1436 Python.
1437
1438 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001439 return _sys_version()[6]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001440
1441### The Opus Magnum of platform strings :-)
1442
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001443_platform_cache = {}
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001444
1445def platform(aliased=0, terse=0):
1446
1447 """ Returns a single string identifying the underlying platform
1448 with as much useful information as possible (but no more :).
Tim Peters0eadaac2003-04-24 16:02:54 +00001449
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001450 The output is intended to be human readable rather than
1451 machine parseable. It may look different on different
1452 platforms and this is intended.
1453
1454 If "aliased" is true, the function will use aliases for
1455 various platforms that report system names which differ from
1456 their common names, e.g. SunOS will be reported as
1457 Solaris. The system_alias() function is used to implement
1458 this.
1459
1460 Setting terse to true causes the function to return only the
1461 absolute minimum information needed to identify the platform.
1462
1463 """
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001464 result = _platform_cache.get((aliased, terse), None)
1465 if result is not None:
1466 return result
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001467
1468 # Get uname information and then apply platform specific cosmetics
1469 # to it...
1470 system,node,release,version,machine,processor = uname()
1471 if machine == processor:
1472 processor = ''
1473 if aliased:
1474 system,release,version = system_alias(system,release,version)
1475
1476 if system == 'Windows':
1477 # MS platforms
1478 rel,vers,csd,ptype = win32_ver(version)
1479 if terse:
1480 platform = _platform(system,release)
1481 else:
1482 platform = _platform(system,release,version,csd)
1483
1484 elif system in ('Linux',):
1485 # Linux based systems
1486 distname,distversion,distid = dist('')
1487 if distname and not terse:
1488 platform = _platform(system,release,machine,processor,
1489 'with',
1490 distname,distversion,distid)
1491 else:
1492 # If the distribution name is unknown check for libc vs. glibc
1493 libcname,libcversion = libc_ver(sys.executable)
1494 platform = _platform(system,release,machine,processor,
1495 'with',
1496 libcname+libcversion)
1497 elif system == 'Java':
1498 # Java platforms
1499 r,v,vminfo,(os_name,os_version,os_arch) = java_ver()
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001500 if terse or not os_name:
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001501 platform = _platform(system,release,version)
1502 else:
1503 platform = _platform(system,release,version,
1504 'on',
1505 os_name,os_version,os_arch)
1506
1507 elif system == 'MacOS':
1508 # MacOS platforms
1509 if terse:
1510 platform = _platform(system,release)
1511 else:
1512 platform = _platform(system,release,machine)
1513
1514 else:
1515 # Generic handler
1516 if terse:
1517 platform = _platform(system,release)
1518 else:
1519 bits,linkage = architecture(sys.executable)
1520 platform = _platform(system,release,machine,processor,bits,linkage)
Tim Peters0eadaac2003-04-24 16:02:54 +00001521
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001522 _platform_cache[(aliased, terse)] = platform
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001523 return platform
1524
1525### Command line interface
1526
1527if __name__ == '__main__':
1528 # Default is to print the aliased verbose platform string
Tim Peters0eadaac2003-04-24 16:02:54 +00001529 terse = ('terse' in sys.argv or '--terse' in sys.argv)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001530 aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv)
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001531 print(platform(aliased,terse))
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001532 sys.exit(0)