blob: d1774995738f6ddfcf9334a3c099adf1d097db35 [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
Georg Brandl52c17942009-09-19 08:43:16 +000013# Python bug tracker (http://bugs.python.org) and assign them to "lemburg".
Marc-André Lemburg246d8472003-04-24 11:36:11 +000014#
15# Note: Please keep this module compatible to Python 1.5.2.
16#
17# Still needed:
18# * more support for WinCE
19# * support for MS-DOS (PythonDX ?)
20# * support for Amiga and other still unsupported platforms running Python
21# * support for additional Linux distributions
22#
Brett Cannon8ab27df2003-08-05 03:52:04 +000023# Many thanks to all those who helped adding platform-specific
Marc-André Lemburg246d8472003-04-24 11:36:11 +000024# checks (in no particular order):
25#
26# Charles G Waldman, David Arnold, Gordon McMillan, Ben Darnell,
27# Jeff Bauer, Cliff Crawford, Ivan Van Laningham, Josef
28# Betancourt, Randall Hopper, Karl Putland, John Farrell, Greg
29# Andruk, Just van Rossum, Thomas Heller, Mark R. Levinson, Mark
30# Hammond, Bill Tutt, Hans Nowak, Uwe Zessin (OpenVMS support),
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +000031# Colin Kong, Trent Mick, Guido van Rossum, Anthony Baxter
Marc-André Lemburg246d8472003-04-24 11:36:11 +000032#
33# History:
Marc-André Lemburg380f4172005-11-07 16:11:02 +000034#
35# <see CVS and SVN checkin messages for history>
36#
Marc-André Lemburg19e5b3f2009-07-13 20:23:49 +000037# 1.0.7 - added DEV_NULL
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +000038# 1.0.6 - added linux_distribution()
39# 1.0.5 - fixed Java support to allow running the module on Jython
40# 1.0.4 - added IronPython support
Marc-André Lemburgcdc79232004-06-19 17:17:00 +000041# 1.0.3 - added normalization of Windows system name
Marc-André Lemburg91e83e22004-03-25 18:35:12 +000042# 1.0.2 - added more Windows support
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +000043# 1.0.1 - reformatted to make doc.py happy
Marc-André Lemburg246d8472003-04-24 11:36:11 +000044# 1.0.0 - reformatted a bit and checked into Python CVS
45# 0.8.0 - added sys.version parser and various new access
46# APIs (python_version(), python_compiler(), etc.)
47# 0.7.2 - fixed architecture() to use sizeof(pointer) where available
48# 0.7.1 - added support for Caldera OpenLinux
49# 0.7.0 - some fixes for WinCE; untabified the source file
50# 0.6.2 - support for OpenVMS - requires version 1.5.2-V006 or higher and
51# vms_lib.getsyi() configured
52# 0.6.1 - added code to prevent 'uname -p' on platforms which are
53# known not to support it
54# 0.6.0 - fixed win32_ver() to hopefully work on Win95,98,NT and Win2k;
55# did some cleanup of the interfaces - some APIs have changed
56# 0.5.5 - fixed another type in the MacOS code... should have
57# used more coffee today ;-)
58# 0.5.4 - fixed a few typos in the MacOS code
59# 0.5.3 - added experimental MacOS support; added better popen()
60# workarounds in _syscmd_ver() -- still not 100% elegant
61# though
62# 0.5.2 - fixed uname() to return '' instead of 'unknown' in all
63# return values (the system uname command tends to return
64# 'unknown' instead of just leaving the field emtpy)
65# 0.5.1 - included code for slackware dist; added exception handlers
66# to cover up situations where platforms don't have os.popen
67# (e.g. Mac) or fail on socket.gethostname(); fixed libc
68# detection RE
69# 0.5.0 - changed the API names referring to system commands to *syscmd*;
70# added java_ver(); made syscmd_ver() a private
71# API (was system_ver() in previous versions) -- use uname()
72# instead; extended the win32_ver() to also return processor
73# type information
74# 0.4.0 - added win32_ver() and modified the platform() output for WinXX
75# 0.3.4 - fixed a bug in _follow_symlinks()
76# 0.3.3 - fixed popen() and "file" command invokation bugs
77# 0.3.2 - added architecture() API and support for it in platform()
78# 0.3.1 - fixed syscmd_ver() RE to support Windows NT
79# 0.3.0 - added system alias support
80# 0.2.3 - removed 'wince' again... oh well.
81# 0.2.2 - added 'wince' to syscmd_ver() supported platforms
82# 0.2.1 - added cache logic and changed the platform string format
83# 0.2.0 - changed the API to use functions instead of module globals
84# since some action take too long to be run on module import
85# 0.1.0 - first release
86#
87# You can always get the latest version of this module at:
88#
89# http://www.egenix.com/files/python/platform.py
90#
91# If that URL should fail, try contacting the author.
92
93__copyright__ = """
94 Copyright (c) 1999-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Georg Brandl8cdc9bc2010-01-01 13:07:05 +000095 Copyright (c) 2000-2010, eGenix.com Software GmbH; mailto:info@egenix.com
Marc-André Lemburg246d8472003-04-24 11:36:11 +000096
97 Permission to use, copy, modify, and distribute this software and its
98 documentation for any purpose and without fee or royalty is hereby granted,
99 provided that the above copyright notice appear in all copies and that
100 both that copyright notice and this permission notice appear in
101 supporting documentation or portions thereof, including modifications,
102 that you make.
103
104 EGENIX.COM SOFTWARE GMBH DISCLAIMS ALL WARRANTIES WITH REGARD TO
105 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
106 FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
107 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
108 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
109 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
110 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !
111
112"""
113
Marc-André Lemburg19e5b3f2009-07-13 20:23:49 +0000114__version__ = '1.0.7'
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000115
Jesus Ceadebda5d2012-10-04 15:14:56 +0200116import sys,string,os,re
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000117
Marc-André Lemburg19e5b3f2009-07-13 20:23:49 +0000118### Globals & Constants
119
120# Determine the platform's /dev/null device
121try:
122 DEV_NULL = os.devnull
123except AttributeError:
124 # os.devnull was added in Python 2.4, so emulate it for earlier
125 # Python versions
126 if sys.platform in ('dos','win32','win16','os2'):
127 # Use the old CP/M NUL as device name
128 DEV_NULL = 'NUL'
129 else:
130 # Standard Unix uses /dev/null
131 DEV_NULL = '/dev/null'
132
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000133### Platform specific APIs
134
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000135_libc_search = re.compile(r'(__libc_init)'
136 '|'
Tim Peters0eadaac2003-04-24 16:02:54 +0000137 '(GLIBC_([0-9.]+))'
138 '|'
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000139 '(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)')
140
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000141def libc_ver(executable=sys.executable,lib='',version='',
142
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000143 chunksize=2048):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000144
Brett Cannon8ab27df2003-08-05 03:52:04 +0000145 """ Tries to determine the libc version that the file executable
146 (which defaults to the Python interpreter) is linked against.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000147
148 Returns a tuple of strings (lib,version) which default to the
149 given parameters in case the lookup fails.
150
151 Note that the function has intimate knowledge of how different
Brett Cannon8ab27df2003-08-05 03:52:04 +0000152 libc versions add symbols to the executable and thus is probably
153 only useable for executables compiled using gcc.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000154
155 The file is read and scanned in chunks of chunksize bytes.
156
157 """
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000158 if hasattr(os.path, 'realpath'):
159 # Python 2.2 introduced os.path.realpath(); it is used
160 # here to work around problems with Cygwin not being
161 # able to open symlinks for reading
162 executable = os.path.realpath(executable)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000163 f = open(executable,'rb')
164 binary = f.read(chunksize)
165 pos = 0
166 while 1:
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000167 m = _libc_search.search(binary,pos)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000168 if not m:
169 binary = f.read(chunksize)
170 if not binary:
171 break
172 pos = 0
173 continue
174 libcinit,glibc,glibcversion,so,threads,soversion = m.groups()
175 if libcinit and not lib:
176 lib = 'libc'
177 elif glibc:
178 if lib != 'glibc':
179 lib = 'glibc'
180 version = glibcversion
181 elif glibcversion > version:
182 version = glibcversion
183 elif so:
184 if lib != 'glibc':
185 lib = 'libc'
Victor Stinner429a12b2011-12-15 21:42:03 +0100186 if soversion and soversion > version:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000187 version = soversion
188 if threads and version[-len(threads):] != threads:
189 version = version + threads
190 pos = m.end()
191 f.close()
192 return lib,version
193
194def _dist_try_harder(distname,version,id):
195
Tim Peters0eadaac2003-04-24 16:02:54 +0000196 """ Tries some special tricks to get the distribution
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000197 information in case the default method fails.
198
199 Currently supports older SuSE Linux, Caldera OpenLinux and
200 Slackware Linux distributions.
201
202 """
203 if os.path.exists('/var/adm/inst-log/info'):
204 # SuSE Linux stores distribution information in that file
205 info = open('/var/adm/inst-log/info').readlines()
206 distname = 'SuSE'
207 for line in info:
208 tv = string.split(line)
209 if len(tv) == 2:
210 tag,value = tv
211 else:
212 continue
213 if tag == 'MIN_DIST_VERSION':
214 version = string.strip(value)
215 elif tag == 'DIST_IDENT':
216 values = string.split(value,'-')
217 id = values[2]
218 return distname,version,id
219
220 if os.path.exists('/etc/.installed'):
221 # Caldera OpenLinux has some infos in that file (thanks to Colin Kong)
222 info = open('/etc/.installed').readlines()
223 for line in info:
224 pkg = string.split(line,'-')
225 if len(pkg) >= 2 and pkg[0] == 'OpenLinux':
226 # XXX does Caldera support non Intel platforms ? If yes,
227 # where can we find the needed id ?
228 return 'OpenLinux',pkg[1],id
229
230 if os.path.isdir('/usr/lib/setup'):
231 # Check for slackware verson tag file (thanks to Greg Andruk)
232 verfiles = os.listdir('/usr/lib/setup')
Guido van Rossum843c7342004-05-04 18:18:59 +0000233 for n in range(len(verfiles)-1, -1, -1):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000234 if verfiles[n][:14] != 'slack-version-':
235 del verfiles[n]
236 if verfiles:
237 verfiles.sort()
238 distname = 'slackware'
239 version = verfiles[-1][14:]
240 return distname,version,id
241
242 return distname,version,id
243
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000244_release_filename = re.compile(r'(\w+)[-_](release|version)')
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000245_lsb_release_version = re.compile(r'(.+)'
246 ' release '
247 '([\d.]+)'
248 '[^(]*(?:\((.+)\))?')
249_release_version = re.compile(r'([^0-9]+)'
250 '(?: release )?'
251 '([\d.]+)'
252 '[^(]*(?:\((.+)\))?')
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000253
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000254# See also http://www.novell.com/coolsolutions/feature/11251.html
Tim Petersf733abb2007-01-30 03:03:46 +0000255# and http://linuxmafia.com/faq/Admin/release-files.html
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000256# and http://data.linux-ntfs.org/rpm/whichrpm
257# and http://www.die.net/doc/linux/man/man1/lsb_release.1.html
Marc-André Lemburg380f4172005-11-07 16:11:02 +0000258
Marc-André Lemburg50967bd2008-03-08 10:01:43 +0000259_supported_dists = (
260 'SuSE', 'debian', 'fedora', 'redhat', 'centos',
261 'mandrake', 'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo',
262 'UnitedLinux', 'turbolinux')
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000263
264def _parse_release_file(firstline):
Tim Petersf733abb2007-01-30 03:03:46 +0000265
Benjamin Petersona43f34c2010-01-25 03:31:13 +0000266 # Default to empty 'version' and 'id' strings. Both defaults are used
267 # when 'firstline' is empty. 'id' defaults to empty when an id can not
268 # be deduced.
269 version = ''
270 id = ''
271
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000272 # Parse the first line
273 m = _lsb_release_version.match(firstline)
274 if m is not None:
275 # LSB format: "distro release x.x (codename)"
276 return tuple(m.groups())
277
278 # Pre-LSB format: "distro x.x (codename)"
279 m = _release_version.match(firstline)
280 if m is not None:
281 return tuple(m.groups())
282
283 # Unkown format... take the first two words
284 l = string.split(string.strip(firstline))
285 if l:
286 version = l[0]
287 if len(l) > 1:
288 id = l[1]
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000289 return '', version, id
290
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000291def linux_distribution(distname='', version='', id='',
292
293 supported_dists=_supported_dists,
294 full_distribution_name=1):
295
296 """ Tries to determine the name of the Linux OS distribution name.
297
298 The function first looks for a distribution release file in
299 /etc and then reverts to _dist_try_harder() in case no
300 suitable files are found.
301
302 supported_dists may be given to define the set of Linux
303 distributions to look for. It defaults to a list of currently
304 supported Linux distributions identified by their release file
305 name.
306
307 If full_distribution_name is true (default), the full
308 distribution read from the OS is returned. Otherwise the short
309 name taken from supported_dists is used.
310
311 Returns a tuple (distname,version,id) which default to the
312 args given as parameters.
313
314 """
315 try:
316 etc = os.listdir('/etc')
317 except os.error:
318 # Probably not a Unix system
319 return distname,version,id
320 etc.sort()
321 for file in etc:
322 m = _release_filename.match(file)
323 if m is not None:
324 _distname,dummy = m.groups()
325 if _distname in supported_dists:
326 distname = _distname
327 break
328 else:
329 return _dist_try_harder(distname,version,id)
Tim Petersf733abb2007-01-30 03:03:46 +0000330
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000331 # Read the first line
332 f = open('/etc/'+file, 'r')
333 firstline = f.readline()
334 f.close()
335 _distname, _version, _id = _parse_release_file(firstline)
336
337 if _distname and full_distribution_name:
338 distname = _distname
339 if _version:
340 version = _version
341 if _id:
342 id = _id
343 return distname, version, id
344
345# To maintain backwards compatibility:
Tim Petersf733abb2007-01-30 03:03:46 +0000346
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000347def dist(distname='',version='',id='',
348
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000349 supported_dists=_supported_dists):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000350
Brett Cannon8ab27df2003-08-05 03:52:04 +0000351 """ Tries to determine the name of the Linux OS distribution name.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000352
353 The function first looks for a distribution release file in
354 /etc and then reverts to _dist_try_harder() in case no
355 suitable files are found.
356
Brett Cannon8ab27df2003-08-05 03:52:04 +0000357 Returns a tuple (distname,version,id) which default to the
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000358 args given as parameters.
359
360 """
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000361 return linux_distribution(distname, version, id,
362 supported_dists=supported_dists,
363 full_distribution_name=0)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000364
365class _popen:
366
367 """ Fairly portable (alternative) popen implementation.
368
369 This is mostly needed in case os.popen() is not available, or
370 doesn't work as advertised, e.g. in Win9X GUI programs like
371 PythonWin or IDLE.
372
373 Writing to the pipe is currently not supported.
374
375 """
376 tmpfile = ''
377 pipe = None
378 bufsize = None
379 mode = 'r'
380
381 def __init__(self,cmd,mode='r',bufsize=None):
382
383 if mode != 'r':
384 raise ValueError,'popen()-emulation only supports read mode'
385 import tempfile
386 self.tmpfile = tmpfile = tempfile.mktemp()
387 os.system(cmd + ' > %s' % tmpfile)
388 self.pipe = open(tmpfile,'rb')
389 self.bufsize = bufsize
390 self.mode = mode
391
392 def read(self):
393
394 return self.pipe.read()
395
396 def readlines(self):
397
398 if self.bufsize is not None:
399 return self.pipe.readlines()
400
401 def close(self,
402
403 remove=os.unlink,error=os.error):
404
405 if self.pipe:
406 rc = self.pipe.close()
407 else:
408 rc = 255
409 if self.tmpfile:
410 try:
411 remove(self.tmpfile)
412 except error:
413 pass
414 return rc
415
416 # Alias
417 __del__ = close
418
419def popen(cmd, mode='r', bufsize=None):
420
421 """ Portable popen() interface.
422 """
423 # Find a working popen implementation preferring win32pipe.popen
424 # over os.popen over _popen
425 popen = None
426 if os.environ.get('OS','') == 'Windows_NT':
427 # On NT win32pipe should work; on Win9x it hangs due to bugs
428 # in the MS C lib (see MS KnowledgeBase article Q150956)
429 try:
430 import win32pipe
431 except ImportError:
432 pass
433 else:
434 popen = win32pipe.popen
435 if popen is None:
436 if hasattr(os,'popen'):
437 popen = os.popen
438 # Check whether it works... it doesn't in GUI programs
439 # on Windows platforms
440 if sys.platform == 'win32': # XXX Others too ?
441 try:
442 popen('')
443 except os.error:
444 popen = _popen
445 else:
446 popen = _popen
447 if bufsize is None:
448 return popen(cmd,mode)
449 else:
450 return popen(cmd,mode,bufsize)
451
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000452def _norm_version(version, build=''):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000453
Brett Cannon8ab27df2003-08-05 03:52:04 +0000454 """ Normalize the version and build strings and return a single
Walter Dörwalde5a7fad2005-11-21 17:01:27 +0000455 version string using the format major.minor.build (or patchlevel).
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000456 """
457 l = string.split(version,'.')
458 if build:
459 l.append(build)
460 try:
461 ints = map(int,l)
462 except ValueError:
463 strings = l
464 else:
465 strings = map(str,ints)
466 version = string.join(strings[:3],'.')
467 return version
468
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000469_ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) '
470 '.*'
Marc-André Lemburgcecaa652009-07-13 21:28:33 +0000471 '\[.* ([\d.]+)\])')
472
473# Examples of VER command output:
474#
475# Windows 2000: Microsoft Windows 2000 [Version 5.00.2195]
476# Windows XP: Microsoft Windows XP [Version 5.1.2600]
477# Windows Vista: Microsoft Windows [Version 6.0.6002]
478#
479# Note that the "Version" string gets localized on different
480# Windows versions.
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000481
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000482def _syscmd_ver(system='', release='', version='',
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000483
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000484 supported_platforms=('win32','win16','dos','os2')):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000485
486 """ Tries to figure out the OS version used and returns
487 a tuple (system,release,version).
Tim Peters0eadaac2003-04-24 16:02:54 +0000488
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000489 It uses the "ver" shell command for this which is known
490 to exists on Windows, DOS and OS/2. XXX Others too ?
491
492 In case this fails, the given parameters are used as
493 defaults.
494
495 """
496 if sys.platform not in supported_platforms:
497 return system,release,version
498
499 # Try some common cmd strings
500 for cmd in ('ver','command /c ver','cmd /c ver'):
501 try:
502 pipe = popen(cmd)
503 info = pipe.read()
504 if pipe.close():
505 raise os.error,'command failed'
Ezio Melottic2077b02011-03-16 12:34:31 +0200506 # XXX How can I suppress shell errors from being written
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000507 # to stderr ?
508 except os.error,why:
509 #print 'Command %s failed: %s' % (cmd,why)
510 continue
511 except IOError,why:
512 #print 'Command %s failed: %s' % (cmd,why)
513 continue
514 else:
515 break
516 else:
517 return system,release,version
518
519 # Parse the output
520 info = string.strip(info)
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000521 m = _ver_output.match(info)
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000522 if m is not None:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000523 system,release,version = m.groups()
524 # Strip trailing dots from version and release
525 if release[-1] == '.':
526 release = release[:-1]
527 if version[-1] == '.':
528 version = version[:-1]
529 # Normalize the version and build strings (eliminating additional
530 # zeros)
531 version = _norm_version(version)
532 return system,release,version
533
534def _win32_getvalue(key,name,default=''):
535
536 """ Read a value for name from the registry key.
537
538 In case this fails, default is returned.
539
540 """
Marc-André Lemburgdbd61a22008-03-20 17:31:36 +0000541 try:
542 # Use win32api if available
543 from win32api import RegQueryValueEx
544 except ImportError:
545 # On Python 2.0 and later, emulate using _winreg
546 import _winreg
547 RegQueryValueEx = _winreg.QueryValueEx
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000548 try:
549 return RegQueryValueEx(key,name)
550 except:
551 return default
552
553def win32_ver(release='',version='',csd='',ptype=''):
554
555 """ Get additional version information from the Windows Registry
556 and return a tuple (version,csd,ptype) referring to version
Brian Curtinb4bef302012-02-01 15:17:39 -0600557 number, CSD level (service pack), and OS type (multi/single
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000558 processor).
559
560 As a hint: ptype returns 'Uniprocessor Free' on single
561 processor NT machines and 'Multiprocessor Free' on multi
562 processor machines. The 'Free' refers to the OS version being
563 free of debugging code. It could also state 'Checked' which
564 means the OS version uses debugging code, i.e. code that
565 checks arguments, ranges, etc. (Thomas Heller).
566
Marc-André Lemburgdbd61a22008-03-20 17:31:36 +0000567 Note: this function works best with Mark Hammond's win32
568 package installed, but also on Python 2.3 and later. It
569 obviously only runs on Win32 compatible platforms.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000570
571 """
572 # XXX Is there any way to find out the processor type on WinXX ?
573 # XXX Is win32 available on Windows CE ?
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000574 #
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000575 # Adapted from code posted by Karl Putland to comp.lang.python.
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000576 #
577 # The mappings between reg. values and release names can be found
578 # here: http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000579
580 # Import the needed APIs
581 try:
582 import win32api
Marc-André Lemburgdbd61a22008-03-20 17:31:36 +0000583 from win32api import RegQueryValueEx, RegOpenKeyEx, \
584 RegCloseKey, GetVersionEx
585 from win32con import HKEY_LOCAL_MACHINE, VER_PLATFORM_WIN32_NT, \
586 VER_PLATFORM_WIN32_WINDOWS, VER_NT_WORKSTATION
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000587 except ImportError:
Marc-André Lemburgdbd61a22008-03-20 17:31:36 +0000588 # Emulate the win32api module using Python APIs
589 try:
590 sys.getwindowsversion
591 except AttributeError:
592 # No emulation possible, so return the defaults...
593 return release,version,csd,ptype
594 else:
595 # Emulation using _winreg (added in Python 2.0) and
596 # sys.getwindowsversion() (added in Python 2.3)
597 import _winreg
598 GetVersionEx = sys.getwindowsversion
599 RegQueryValueEx = _winreg.QueryValueEx
600 RegOpenKeyEx = _winreg.OpenKeyEx
601 RegCloseKey = _winreg.CloseKey
602 HKEY_LOCAL_MACHINE = _winreg.HKEY_LOCAL_MACHINE
603 VER_PLATFORM_WIN32_WINDOWS = 1
604 VER_PLATFORM_WIN32_NT = 2
605 VER_NT_WORKSTATION = 1
Brian Curtin11f8b9d2010-05-06 02:54:44 +0000606 VER_NT_SERVER = 3
607 REG_SZ = 1
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000608
609 # Find out the registry key and some general version infos
Brian Curtin11f8b9d2010-05-06 02:54:44 +0000610 winver = GetVersionEx()
611 maj,min,buildno,plat,csd = winver
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000612 version = '%i.%i.%i' % (maj,min,buildno & 0xFFFF)
Brian Curtin11f8b9d2010-05-06 02:54:44 +0000613 if hasattr(winver, "service_pack"):
614 if winver.service_pack != "":
615 csd = 'SP%s' % winver.service_pack_major
616 else:
617 if csd[:13] == 'Service Pack ':
618 csd = 'SP' + csd[13:]
Marc-André Lemburgcecaa652009-07-13 21:28:33 +0000619
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000620 if plat == VER_PLATFORM_WIN32_WINDOWS:
621 regkey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
622 # Try to guess the release name
623 if maj == 4:
624 if min == 0:
625 release = '95'
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000626 elif min == 10:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000627 release = '98'
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000628 elif min == 90:
629 release = 'Me'
630 else:
631 release = 'postMe'
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000632 elif maj == 5:
633 release = '2000'
Marc-André Lemburgcecaa652009-07-13 21:28:33 +0000634
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000635 elif plat == VER_PLATFORM_WIN32_NT:
636 regkey = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion'
637 if maj <= 4:
638 release = 'NT'
639 elif maj == 5:
Marc-André Lemburg91e83e22004-03-25 18:35:12 +0000640 if min == 0:
641 release = '2000'
642 elif min == 1:
643 release = 'XP'
644 elif min == 2:
645 release = '2003Server'
646 else:
647 release = 'post2003'
Marc-André Lemburgb24cd0f2007-06-12 09:26:49 +0000648 elif maj == 6:
Brian Curtin11f8b9d2010-05-06 02:54:44 +0000649 if hasattr(winver, "product_type"):
650 product_type = winver.product_type
651 else:
652 product_type = VER_NT_WORKSTATION
653 # Without an OSVERSIONINFOEX capable sys.getwindowsversion(),
654 # or help from the registry, we cannot properly identify
655 # non-workstation versions.
Marc-André Lemburgdbd61a22008-03-20 17:31:36 +0000656 try:
Brian Curtin11f8b9d2010-05-06 02:54:44 +0000657 key = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey)
658 name, type = RegQueryValueEx(key, "ProductName")
659 # Discard any type that isn't REG_SZ
660 if type == REG_SZ and name.find("Server") != -1:
661 product_type = VER_NT_SERVER
662 except WindowsError:
663 # Use default of VER_NT_WORKSTATION
664 pass
665
666 if min == 0:
667 if product_type == VER_NT_WORKSTATION:
Marc-André Lemburgb24cd0f2007-06-12 09:26:49 +0000668 release = 'Vista'
669 else:
Brian Curtin11f8b9d2010-05-06 02:54:44 +0000670 release = '2008Server'
671 elif min == 1:
672 if product_type == VER_NT_WORKSTATION:
673 release = '7'
674 else:
675 release = '2008ServerR2'
Marc-André Lemburgb24cd0f2007-06-12 09:26:49 +0000676 else:
677 release = 'post2008Server'
Marc-André Lemburgcecaa652009-07-13 21:28:33 +0000678
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000679 else:
680 if not release:
681 # E.g. Win3.1 with win32s
682 release = '%i.%i' % (maj,min)
683 return release,version,csd,ptype
684
685 # Open the registry key
686 try:
Marc-André Lemburgdbd61a22008-03-20 17:31:36 +0000687 keyCurVer = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000688 # Get a value to make sure the key exists...
Marc-André Lemburgdbd61a22008-03-20 17:31:36 +0000689 RegQueryValueEx(keyCurVer, 'SystemRoot')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000690 except:
691 return release,version,csd,ptype
Tim Peters0eadaac2003-04-24 16:02:54 +0000692
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000693 # Parse values
694 #subversion = _win32_getvalue(keyCurVer,
695 # 'SubVersionNumber',
696 # ('',1))[0]
697 #if subversion:
698 # release = release + subversion # 95a, 95b, etc.
699 build = _win32_getvalue(keyCurVer,
700 'CurrentBuildNumber',
701 ('',1))[0]
702 ptype = _win32_getvalue(keyCurVer,
703 'CurrentType',
704 (ptype,1))[0]
705
706 # Normalize version
707 version = _norm_version(version,build)
708
709 # Close key
710 RegCloseKey(keyCurVer)
711 return release,version,csd,ptype
712
713def _mac_ver_lookup(selectors,default=None):
714
715 from gestalt import gestalt
Jack Jansena290e3d2003-08-11 11:08:49 +0000716 import MacOS
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000717 l = []
718 append = l.append
719 for selector in selectors:
720 try:
721 append(gestalt(selector))
Jack Jansena290e3d2003-08-11 11:08:49 +0000722 except (RuntimeError, MacOS.Error):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000723 append(default)
724 return l
725
726def _bcd2str(bcd):
727
728 return hex(bcd)[2:]
729
Ronald Oussoren3a04a252010-07-23 12:41:00 +0000730def _mac_ver_gestalt():
731 """
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000732 Thanks to Mark R. Levinson for mailing documentation links and
733 code examples for this function. Documentation for the
734 gestalt() API is available online at:
735
736 http://www.rgaros.nl/gestalt/
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000737 """
738 # Check whether the version info module is available
739 try:
740 import gestalt
Jack Jansena290e3d2003-08-11 11:08:49 +0000741 import MacOS
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000742 except ImportError:
Ronald Oussoren3a04a252010-07-23 12:41:00 +0000743 return None
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000744 # Get the infos
Ronald Oussoren9341ad22010-02-07 11:29:31 +0000745 sysv,sysa = _mac_ver_lookup(('sysv','sysa'))
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000746 # Decode the infos
747 if sysv:
748 major = (sysv & 0xFF00) >> 8
749 minor = (sysv & 0x00F0) >> 4
750 patch = (sysv & 0x000F)
Ronald Oussorenc27b8b82008-05-08 10:34:39 +0000751
752 if (major, minor) >= (10, 4):
753 # the 'sysv' gestald cannot return patchlevels
754 # higher than 9. Apple introduced 3 new
755 # gestalt codes in 10.4 to deal with this
756 # issue (needed because patch levels can
757 # run higher than 9, such as 10.4.11)
758 major,minor,patch = _mac_ver_lookup(('sys1','sys2','sys3'))
759 release = '%i.%i.%i' %(major, minor, patch)
760 else:
761 release = '%s.%i.%i' % (_bcd2str(major),minor,patch)
Ronald Oussoren7a0f4c72008-05-18 20:54:47 +0000762
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000763 if sysa:
Tim Peters0eadaac2003-04-24 16:02:54 +0000764 machine = {0x1: '68k',
Ronald Oussoren749d0702006-04-17 13:37:15 +0000765 0x2: 'PowerPC',
766 0xa: 'i386'}.get(sysa,'')
Ronald Oussoren3a04a252010-07-23 12:41:00 +0000767
Ned Deily0ab67ee2011-07-13 15:05:31 -0700768 versioninfo=('', '', '')
Ronald Oussoren3a04a252010-07-23 12:41:00 +0000769 return release,versioninfo,machine
770
771def _mac_ver_xml():
772 fn = '/System/Library/CoreServices/SystemVersion.plist'
773 if not os.path.exists(fn):
774 return None
775
776 try:
777 import plistlib
778 except ImportError:
779 return None
780
781 pl = plistlib.readPlist(fn)
782 release = pl['ProductVersion']
783 versioninfo=('', '', '')
784 machine = os.uname()[4]
Ronald Oussoren22e3e692010-08-03 07:44:35 +0000785 if machine in ('ppc', 'Power Macintosh'):
Ronald Oussoren3a04a252010-07-23 12:41:00 +0000786 # for compatibility with the gestalt based code
787 machine = 'PowerPC'
788
789 return release,versioninfo,machine
790
791
792def mac_ver(release='',versioninfo=('','',''),machine=''):
793
794 """ Get MacOS version information and return it as tuple (release,
795 versioninfo, machine) with versioninfo being a tuple (version,
796 dev_stage, non_release_version).
797
798 Entries which cannot be determined are set to the paramter values
799 which default to ''. All tuple entries are strings.
800 """
801
802 # First try reading the information from an XML file which should
803 # always be present
804 info = _mac_ver_xml()
805 if info is not None:
806 return info
807
808 # If that doesn't work for some reason fall back to reading the
809 # information using gestalt calls.
810 info = _mac_ver_gestalt()
811 if info is not None:
812 return info
813
814 # If that also doesn't work return the default values
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000815 return release,versioninfo,machine
816
Neal Norwitz9b924c62003-06-29 04:17:45 +0000817def _java_getprop(name,default):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000818
819 from java.lang import System
820 try:
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000821 value = System.getProperty(name)
822 if value is None:
823 return default
824 return value
825 except AttributeError:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000826 return default
827
828def java_ver(release='',vendor='',vminfo=('','',''),osinfo=('','','')):
Tim Peters0eadaac2003-04-24 16:02:54 +0000829
Brett Cannon8ab27df2003-08-05 03:52:04 +0000830 """ Version interface for Jython.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000831
832 Returns a tuple (release,vendor,vminfo,osinfo) with vminfo being
833 a tuple (vm_name,vm_release,vm_vendor) and osinfo being a
834 tuple (os_name,os_version,os_arch).
835
836 Values which cannot be determined are set to the defaults
837 given as parameters (which all default to '').
838
839 """
840 # Import the needed APIs
841 try:
842 import java.lang
843 except ImportError:
844 return release,vendor,vminfo,osinfo
845
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000846 vendor = _java_getprop('java.vendor', vendor)
847 release = _java_getprop('java.version', release)
848 vm_name, vm_release, vm_vendor = vminfo
849 vm_name = _java_getprop('java.vm.name', vm_name)
850 vm_vendor = _java_getprop('java.vm.vendor', vm_vendor)
851 vm_release = _java_getprop('java.vm.version', vm_release)
852 vminfo = vm_name, vm_release, vm_vendor
853 os_name, os_version, os_arch = osinfo
854 os_arch = _java_getprop('java.os.arch', os_arch)
855 os_name = _java_getprop('java.os.name', os_name)
856 os_version = _java_getprop('java.os.version', os_version)
857 osinfo = os_name, os_version, os_arch
Tim Peters0eadaac2003-04-24 16:02:54 +0000858
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000859 return release, vendor, vminfo, osinfo
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000860
861### System name aliasing
862
863def system_alias(system,release,version):
864
865 """ Returns (system,release,version) aliased to common
866 marketing names used for some systems.
867
868 It also does some reordering of the information in some cases
869 where it would otherwise cause confusion.
870
871 """
872 if system == 'Rhapsody':
873 # Apple's BSD derivative
874 # XXX How can we determine the marketing release number ?
875 return 'MacOS X Server',system+release,version
876
877 elif system == 'SunOS':
878 # Sun's OS
879 if release < '5':
880 # These releases use the old name SunOS
881 return system,release,version
882 # Modify release (marketing release = SunOS release - 3)
883 l = string.split(release,'.')
884 if l:
885 try:
886 major = int(l[0])
887 except ValueError:
888 pass
889 else:
890 major = major - 3
891 l[0] = str(major)
892 release = string.join(l,'.')
893 if release < '6':
894 system = 'Solaris'
895 else:
896 # XXX Whatever the new SunOS marketing name is...
897 system = 'Solaris'
898
899 elif system == 'IRIX64':
900 # IRIX reports IRIX64 on platforms with 64-bit support; yet it
901 # is really a version and not a different platform, since 32-bit
902 # apps are also supported..
903 system = 'IRIX'
904 if version:
905 version = version + ' (64bit)'
906 else:
907 version = '64bit'
908
909 elif system in ('win32','win16'):
910 # In case one of the other tricks
911 system = 'Windows'
912
913 return system,release,version
914
915### Various internal helpers
916
917def _platform(*args):
918
919 """ Helper to format the platform string in a filename
920 compatible format e.g. "system-version-machine".
921 """
922 # Format the platform string
923 platform = string.join(
924 map(string.strip,
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +0000925 filter(len, args)),
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000926 '-')
927
928 # Cleanup some possible filename obstacles...
929 replace = string.replace
930 platform = replace(platform,' ','_')
931 platform = replace(platform,'/','-')
932 platform = replace(platform,'\\','-')
933 platform = replace(platform,':','-')
934 platform = replace(platform,';','-')
935 platform = replace(platform,'"','-')
936 platform = replace(platform,'(','-')
937 platform = replace(platform,')','-')
938
939 # No need to report 'unknown' information...
940 platform = replace(platform,'unknown','')
941
942 # Fold '--'s and remove trailing '-'
943 while 1:
944 cleaned = replace(platform,'--','-')
945 if cleaned == platform:
946 break
947 platform = cleaned
948 while platform[-1] == '-':
949 platform = platform[:-1]
950
951 return platform
952
953def _node(default=''):
954
955 """ Helper to determine the node name of this machine.
956 """
957 try:
958 import socket
959 except ImportError:
960 # No sockets...
961 return default
962 try:
963 return socket.gethostname()
964 except socket.error:
965 # Still not working...
966 return default
967
968# os.path.abspath is new in Python 1.5.2:
969if not hasattr(os.path,'abspath'):
970
971 def _abspath(path,
972
973 isabs=os.path.isabs,join=os.path.join,getcwd=os.getcwd,
974 normpath=os.path.normpath):
975
976 if not isabs(path):
977 path = join(getcwd(), path)
978 return normpath(path)
979
980else:
981
982 _abspath = os.path.abspath
983
984def _follow_symlinks(filepath):
985
986 """ In case filepath is a symlink, follow it until a
987 real file is reached.
988 """
989 filepath = _abspath(filepath)
990 while os.path.islink(filepath):
991 filepath = os.path.normpath(
Hirokazu Yamamoto171c4aa2008-09-04 11:15:14 +0000992 os.path.join(os.path.dirname(filepath),os.readlink(filepath)))
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000993 return filepath
994
995def _syscmd_uname(option,default=''):
996
997 """ Interface to the system's uname command.
998 """
999 if sys.platform in ('dos','win32','win16','os2'):
1000 # XXX Others too ?
1001 return default
1002 try:
Marc-André Lemburg19e5b3f2009-07-13 20:23:49 +00001003 f = os.popen('uname %s 2> %s' % (option, DEV_NULL))
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001004 except (AttributeError,os.error):
1005 return default
1006 output = string.strip(f.read())
1007 rc = f.close()
1008 if not output or rc:
1009 return default
1010 else:
1011 return output
1012
1013def _syscmd_file(target,default=''):
1014
1015 """ Interface to the system's file command.
1016
1017 The function uses the -b option of the file command to have it
1018 ommit the filename in its output and if possible the -L option
1019 to have the command follow symlinks. It returns default in
1020 case the command should fail.
1021
1022 """
Jesus Ceadebda5d2012-10-04 15:14:56 +02001023
1024 # We do the import here to avoid a bootstrap issue.
1025 # See c73b90b6dadd changeset.
1026 #
1027 # [..]
1028 # ranlib libpython2.7.a
1029 # gcc -o python \
1030 # Modules/python.o \
1031 # libpython2.7.a -lsocket -lnsl -ldl -lm
1032 # Traceback (most recent call last):
1033 # File "./setup.py", line 8, in <module>
1034 # from platform import machine as platform_machine
1035 # File "[..]/build/Lib/platform.py", line 116, in <module>
1036 # import sys,string,os,re,subprocess
1037 # File "[..]/build/Lib/subprocess.py", line 429, in <module>
1038 # import select
1039 # ImportError: No module named select
1040
1041 import subprocess
1042
Hirokazu Yamamotoed8c6442008-09-01 14:32:58 +00001043 if sys.platform in ('dos','win32','win16','os2'):
1044 # XXX Others too ?
1045 return default
Jesus Cea3e94e142012-10-04 15:06:57 +02001046 target = _follow_symlinks(target)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001047 try:
Jesus Cea3e94e142012-10-04 15:06:57 +02001048 with open(DEV_NULL) as dev_null:
1049 proc = subprocess.Popen(['file', '-b', '--', target],
1050 stdout=subprocess.PIPE, stderr=dev_null)
1051
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001052 except (AttributeError,os.error):
1053 return default
Jesus Cea3e94e142012-10-04 15:06:57 +02001054 output = proc.stdout.read()
1055 rc = proc.wait()
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001056 if not output or rc:
1057 return default
1058 else:
1059 return output
1060
1061### Information about the used architecture
1062
1063# Default values for architecture; non-empty strings override the
1064# defaults given as parameters
1065_default_architecture = {
1066 'win32': ('','WindowsPE'),
1067 'win16': ('','Windows'),
1068 'dos': ('','MSDOS'),
1069}
1070
Victor Stinner814b6c22010-04-18 18:22:25 +00001071_architecture_split = re.compile(r'[\s,]').split
1072
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +00001073def architecture(executable=sys.executable,bits='',linkage=''):
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001074
1075 """ Queries the given executable (defaults to the Python interpreter
Brett Cannon8ab27df2003-08-05 03:52:04 +00001076 binary) for various architecture information.
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001077
Brett Cannon8ab27df2003-08-05 03:52:04 +00001078 Returns a tuple (bits,linkage) which contains information about
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001079 the bit architecture and the linkage format used for the
1080 executable. Both values are returned as strings.
1081
1082 Values that cannot be determined are returned as given by the
1083 parameter presets. If bits is given as '', the sizeof(pointer)
1084 (or sizeof(long) on Python version < 1.5.2) is used as
1085 indicator for the supported pointer size.
1086
1087 The function relies on the system's "file" command to do the
1088 actual work. This is available on most if not all Unix
Brett Cannon8ab27df2003-08-05 03:52:04 +00001089 platforms. On some non-Unix platforms where the "file" command
1090 does not exist and the executable is set to the Python interpreter
1091 binary defaults from _default_architecture are used.
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001092
1093 """
1094 # Use the sizeof(pointer) as default number of bits if nothing
1095 # else is given as default.
1096 if not bits:
1097 import struct
1098 try:
1099 size = struct.calcsize('P')
1100 except struct.error:
1101 # Older installations can only query longs
1102 size = struct.calcsize('l')
1103 bits = str(size*8) + 'bit'
Tim Peters0eadaac2003-04-24 16:02:54 +00001104
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001105 # Get data from the 'file' system command
Marc-André Lemburg3b8f60b2007-01-13 23:13:54 +00001106 if executable:
Victor Stinner814b6c22010-04-18 18:22:25 +00001107 output = _syscmd_file(executable, '')
Marc-André Lemburg3b8f60b2007-01-13 23:13:54 +00001108 else:
Victor Stinner814b6c22010-04-18 18:22:25 +00001109 output = ''
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001110
Victor Stinner814b6c22010-04-18 18:22:25 +00001111 if not output and \
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001112 executable == sys.executable:
1113 # "file" command did not return anything; we'll try to provide
Tim Peters0eadaac2003-04-24 16:02:54 +00001114 # some sensible defaults then...
Florent Xicluna8d1da0f2010-04-01 18:17:09 +00001115 if sys.platform in _default_architecture:
1116 b, l = _default_architecture[sys.platform]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001117 if b:
1118 bits = b
1119 if l:
1120 linkage = l
Florent Xicluna8d1da0f2010-04-01 18:17:09 +00001121 return bits, linkage
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001122
Victor Stinner814b6c22010-04-18 18:22:25 +00001123 # Split the output into a list of strings omitting the filename
1124 fileout = _architecture_split(output)[1:]
1125
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001126 if 'executable' not in fileout:
1127 # Format not supported
1128 return bits,linkage
1129
1130 # Bits
1131 if '32-bit' in fileout:
1132 bits = '32bit'
1133 elif 'N32' in fileout:
1134 # On Irix only
1135 bits = 'n32bit'
1136 elif '64-bit' in fileout:
1137 bits = '64bit'
1138
1139 # Linkage
1140 if 'ELF' in fileout:
1141 linkage = 'ELF'
1142 elif 'PE' in fileout:
1143 # E.g. Windows uses this format
1144 if 'Windows' in fileout:
1145 linkage = 'WindowsPE'
1146 else:
1147 linkage = 'PE'
1148 elif 'COFF' in fileout:
1149 linkage = 'COFF'
1150 elif 'MS-DOS' in fileout:
1151 linkage = 'MSDOS'
1152 else:
1153 # XXX the A.OUT format also falls under this class...
1154 pass
1155
1156 return bits,linkage
1157
1158### Portable uname() interface
Tim Peters0eadaac2003-04-24 16:02:54 +00001159
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001160_uname_cache = None
1161
1162def uname():
1163
1164 """ Fairly portable uname interface. Returns a tuple
1165 of strings (system,node,release,version,machine,processor)
1166 identifying the underlying platform.
1167
1168 Note that unlike the os.uname function this also returns
Brett Cannon8ab27df2003-08-05 03:52:04 +00001169 possible processor information as an additional tuple entry.
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001170
1171 Entries which cannot be determined are set to ''.
1172
1173 """
1174 global _uname_cache
Benjamin Peterson5c0be322008-06-13 15:11:50 +00001175 no_os_uname = 0
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001176
1177 if _uname_cache is not None:
1178 return _uname_cache
1179
Benjamin Peterson5c0be322008-06-13 15:11:50 +00001180 processor = ''
1181
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001182 # Get some infos from the builtin os.uname API...
1183 try:
1184 system,node,release,version,machine = os.uname()
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001185 except AttributeError:
Benjamin Peterson5c0be322008-06-13 15:11:50 +00001186 no_os_uname = 1
1187
1188 if no_os_uname or not filter(None, (system, node, release, version, machine)):
1189 # Hmm, no there is either no uname or uname has returned
1190 #'unknowns'... we'll have to poke around the system then.
1191 if no_os_uname:
1192 system = sys.platform
1193 release = ''
1194 version = ''
1195 node = _node()
1196 machine = ''
1197
Marc-André Lemburgcecaa652009-07-13 21:28:33 +00001198 use_syscmd_ver = 1
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001199
1200 # Try win32_ver() on win32 platforms
1201 if system == 'win32':
1202 release,version,csd,ptype = win32_ver()
1203 if release and version:
1204 use_syscmd_ver = 0
Marc-André Lemburgdbd61a22008-03-20 17:31:36 +00001205 # Try to use the PROCESSOR_* environment variables
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001206 # available on Win XP and later; see
1207 # http://support.microsoft.com/kb/888731 and
1208 # http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM
Benjamin Peterson5c0be322008-06-13 15:11:50 +00001209 if not machine:
R. David Murrayc9d1a782010-03-22 15:55:09 +00001210 # WOW64 processes mask the native architecture
1211 if "PROCESSOR_ARCHITEW6432" in os.environ:
1212 machine = os.environ.get("PROCESSOR_ARCHITEW6432", '')
1213 else:
1214 machine = os.environ.get('PROCESSOR_ARCHITECTURE', '')
Benjamin Peterson5c0be322008-06-13 15:11:50 +00001215 if not processor:
1216 processor = os.environ.get('PROCESSOR_IDENTIFIER', machine)
Tim Peters0eadaac2003-04-24 16:02:54 +00001217
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001218 # Try the 'ver' system command available on some
1219 # platforms
1220 if use_syscmd_ver:
1221 system,release,version = _syscmd_ver(system)
Marc-André Lemburgcdc79232004-06-19 17:17:00 +00001222 # Normalize system to what win32_ver() normally returns
1223 # (_syscmd_ver() tends to return the vendor name as well)
1224 if system == 'Microsoft Windows':
1225 system = 'Windows'
Marc-André Lemburgb24cd0f2007-06-12 09:26:49 +00001226 elif system == 'Microsoft' and release == 'Windows':
1227 # Under Windows Vista and Windows Server 2008,
1228 # Microsoft changed the output of the ver command. The
1229 # release is no longer printed. This causes the
1230 # system and release to be misidentified.
1231 system = 'Windows'
1232 if '6.0' == version[:3]:
1233 release = 'Vista'
1234 else:
1235 release = ''
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001236
1237 # In case we still don't know anything useful, we'll try to
1238 # help ourselves
1239 if system in ('win32','win16'):
1240 if not version:
1241 if system == 'win32':
1242 version = '32bit'
1243 else:
1244 version = '16bit'
1245 system = 'Windows'
1246
1247 elif system[:4] == 'java':
1248 release,vendor,vminfo,osinfo = java_ver()
1249 system = 'Java'
1250 version = string.join(vminfo,', ')
1251 if not version:
1252 version = vendor
1253
Benjamin Peterson5c0be322008-06-13 15:11:50 +00001254 # System specific extensions
1255 if system == 'OpenVMS':
1256 # OpenVMS seems to have release and version mixed up
1257 if not release or release == '0':
1258 release = version
1259 version = ''
1260 # Get processor information
1261 try:
1262 import vms_lib
1263 except ImportError:
1264 pass
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001265 else:
Benjamin Peterson5c0be322008-06-13 15:11:50 +00001266 csid, cpu_number = vms_lib.getsyi('SYI$_CPU',0)
1267 if (cpu_number >= 128):
1268 processor = 'Alpha'
1269 else:
1270 processor = 'VAX'
1271 if not processor:
1272 # Get processor information from the uname system command
1273 processor = _syscmd_uname('-p','')
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001274
Benjamin Peterson5c0be322008-06-13 15:11:50 +00001275 #If any unknowns still exist, replace them with ''s, which are more portable
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001276 if system == 'unknown':
1277 system = ''
1278 if node == 'unknown':
1279 node = ''
1280 if release == 'unknown':
1281 release = ''
1282 if version == 'unknown':
1283 version = ''
1284 if machine == 'unknown':
1285 machine = ''
1286 if processor == 'unknown':
1287 processor = ''
Sean Reifscheiderf09597c2007-09-17 20:53:21 +00001288
1289 # normalize name
1290 if system == 'Microsoft' and release == 'Windows':
1291 system = 'Windows'
1292 release = 'Vista'
1293
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001294 _uname_cache = system,node,release,version,machine,processor
1295 return _uname_cache
1296
1297### Direct interfaces to some of the uname() return values
1298
1299def system():
1300
1301 """ Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.
1302
1303 An empty string is returned if the value cannot be determined.
1304
1305 """
1306 return uname()[0]
1307
1308def node():
1309
Brett Cannon8ab27df2003-08-05 03:52:04 +00001310 """ Returns the computer's network name (which may not be fully
1311 qualified)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001312
1313 An empty string is returned if the value cannot be determined.
1314
1315 """
1316 return uname()[1]
1317
1318def release():
1319
1320 """ Returns the system's release, e.g. '2.2.0' or 'NT'
1321
1322 An empty string is returned if the value cannot be determined.
1323
1324 """
1325 return uname()[2]
1326
1327def version():
1328
1329 """ Returns the system's release version, e.g. '#3 on degas'
1330
1331 An empty string is returned if the value cannot be determined.
1332
1333 """
1334 return uname()[3]
1335
1336def machine():
1337
1338 """ Returns the machine type, e.g. 'i386'
1339
1340 An empty string is returned if the value cannot be determined.
1341
1342 """
1343 return uname()[4]
1344
1345def processor():
1346
1347 """ Returns the (true) processor name, e.g. 'amdk6'
1348
1349 An empty string is returned if the value cannot be
1350 determined. Note that many platforms do not provide this
1351 information or simply return the same value as for machine(),
1352 e.g. NetBSD does this.
1353
1354 """
1355 return uname()[5]
1356
1357### Various APIs for extracting information from sys.version
1358
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001359_sys_version_parser = re.compile(
1360 r'([\w.+]+)\s*'
1361 '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
1362 '\[([^\]]+)\]?')
1363
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001364_ironpython_sys_version_parser = re.compile(
1365 r'IronPython\s*'
1366 '([\d\.]+)'
1367 '(?: \(([\d\.]+)\))?'
1368 ' on (.NET [\d\.]+)')
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001369
Benjamin Peterson0ad9b772009-03-26 19:09:21 +00001370_pypy_sys_version_parser = re.compile(
1371 r'([\w.+]+)\s*'
1372 '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
1373 '\[PyPy [^\]]+\]?')
1374
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001375_sys_version_cache = {}
1376
1377def _sys_version(sys_version=None):
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001378
1379 """ Returns a parsed version of Python's sys.version as tuple
Marc-André Lemburga519cfc2009-03-25 19:44:58 +00001380 (name, version, branch, revision, buildno, builddate, compiler)
1381 referring to the Python implementation name, version, branch,
1382 revision, build number, build date/time as string and the compiler
1383 identification string.
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001384
1385 Note that unlike the Python sys.version, the returned value
1386 for the Python version will always include the patchlevel (it
1387 defaults to '.0').
1388
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001389 The function returns empty strings for tuple entries that
1390 cannot be determined.
1391
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001392 sys_version may be given to parse an alternative version
1393 string, e.g. if the version was read from a different Python
1394 interpreter.
1395
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001396 """
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001397 # Get the Python version
1398 if sys_version is None:
1399 sys_version = sys.version
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001400
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001401 # Try the cache first
1402 result = _sys_version_cache.get(sys_version, None)
1403 if result is not None:
1404 return result
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001405
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001406 # Parse it
1407 if sys_version[:10] == 'IronPython':
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001408 # IronPython
1409 name = 'IronPython'
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001410 match = _ironpython_sys_version_parser.match(sys_version)
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001411 if match is None:
1412 raise ValueError(
1413 'failed to parse IronPython sys.version: %s' %
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001414 repr(sys_version))
1415 version, alt_version, compiler = match.groups()
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001416 buildno = ''
1417 builddate = ''
1418
1419 elif sys.platform[:4] == 'java':
1420 # Jython
1421 name = 'Jython'
Benjamin Petersonf521b8c2009-03-26 18:35:37 +00001422 match = _sys_version_parser.match(sys_version)
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001423 if match is None:
1424 raise ValueError(
1425 'failed to parse Jython sys.version: %s' %
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001426 repr(sys_version))
Benjamin Petersonf521b8c2009-03-26 18:35:37 +00001427 version, buildno, builddate, buildtime, _ = match.groups()
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001428 compiler = sys.platform
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001429
Benjamin Peterson0ad9b772009-03-26 19:09:21 +00001430 elif "PyPy" in sys_version:
1431 # PyPy
1432 name = "PyPy"
1433 match = _pypy_sys_version_parser.match(sys_version)
1434 if match is None:
1435 raise ValueError("failed to parse PyPy sys.version: %s" %
1436 repr(sys_version))
1437 version, buildno, builddate, buildtime = match.groups()
1438 compiler = ""
1439
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001440 else:
1441 # CPython
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001442 match = _sys_version_parser.match(sys_version)
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001443 if match is None:
1444 raise ValueError(
1445 'failed to parse CPython sys.version: %s' %
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001446 repr(sys_version))
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001447 version, buildno, builddate, buildtime, compiler = \
1448 match.groups()
Benjamin Petersonb457b892009-03-26 18:55:48 +00001449 name = 'CPython'
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001450 builddate = builddate + ' ' + buildtime
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001451
Benjamin Petersonb457b892009-03-26 18:55:48 +00001452 if hasattr(sys, 'subversion'):
1453 # sys.subversion was added in Python 2.5
1454 _, branch, revision = sys.subversion
1455 else:
1456 branch = ''
1457 revision = ''
1458
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001459 # Add the patchlevel version if missing
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001460 l = string.split(version, '.')
1461 if len(l) == 2:
1462 l.append('0')
1463 version = string.join(l, '.')
Marc-André Lemburg2be9d432007-01-13 22:32:21 +00001464
1465 # Build and cache the result
1466 result = (name, version, branch, revision, buildno, builddate, compiler)
1467 _sys_version_cache[sys_version] = result
1468 return result
1469
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001470def python_implementation():
1471
1472 """ Returns a string identifying the Python implementation.
1473
1474 Currently, the following implementations are identified:
Ezio Melotti6fa09472011-05-04 18:37:50 +03001475 'CPython' (C implementation of Python),
1476 'IronPython' (.NET implementation of Python),
1477 'Jython' (Java implementation of Python),
1478 'PyPy' (Python implementation of Python).
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001479
1480 """
1481 return _sys_version()[0]
1482
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001483def python_version():
1484
1485 """ Returns the Python version as string 'major.minor.patchlevel'
1486
1487 Note that unlike the Python sys.version, the returned value
1488 will always include the patchlevel (it defaults to 0).
1489
1490 """
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001491 return _sys_version()[1]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001492
1493def python_version_tuple():
1494
1495 """ Returns the Python version as tuple (major, minor, patchlevel)
1496 of strings.
1497
1498 Note that unlike the Python sys.version, the returned value
1499 will always include the patchlevel (it defaults to 0).
1500
1501 """
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001502 return tuple(string.split(_sys_version()[1], '.'))
1503
1504def python_branch():
1505
Neal Norwitz4b86f8b2007-01-13 21:22:37 +00001506 """ Returns a string identifying the Python implementation
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001507 branch.
1508
1509 For CPython this is the Subversion branch from which the
1510 Python binary was built.
1511
1512 If not available, an empty string is returned.
1513
1514 """
Tim Petersf733abb2007-01-30 03:03:46 +00001515
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001516 return _sys_version()[2]
1517
1518def python_revision():
1519
Neal Norwitz4b86f8b2007-01-13 21:22:37 +00001520 """ Returns a string identifying the Python implementation
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001521 revision.
1522
1523 For CPython this is the Subversion revision from which the
1524 Python binary was built.
1525
1526 If not available, an empty string is returned.
1527
1528 """
1529 return _sys_version()[3]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001530
1531def python_build():
1532
1533 """ Returns a tuple (buildno, builddate) stating the Python
1534 build number and date as strings.
1535
1536 """
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001537 return _sys_version()[4:6]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001538
1539def python_compiler():
1540
1541 """ Returns a string identifying the compiler used for compiling
1542 Python.
1543
1544 """
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001545 return _sys_version()[6]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001546
1547### The Opus Magnum of platform strings :-)
1548
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001549_platform_cache = {}
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001550
1551def platform(aliased=0, terse=0):
1552
1553 """ Returns a single string identifying the underlying platform
1554 with as much useful information as possible (but no more :).
Tim Peters0eadaac2003-04-24 16:02:54 +00001555
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001556 The output is intended to be human readable rather than
1557 machine parseable. It may look different on different
1558 platforms and this is intended.
1559
1560 If "aliased" is true, the function will use aliases for
1561 various platforms that report system names which differ from
1562 their common names, e.g. SunOS will be reported as
1563 Solaris. The system_alias() function is used to implement
1564 this.
1565
1566 Setting terse to true causes the function to return only the
1567 absolute minimum information needed to identify the platform.
1568
1569 """
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001570 result = _platform_cache.get((aliased, terse), None)
1571 if result is not None:
1572 return result
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001573
1574 # Get uname information and then apply platform specific cosmetics
1575 # to it...
1576 system,node,release,version,machine,processor = uname()
1577 if machine == processor:
1578 processor = ''
1579 if aliased:
1580 system,release,version = system_alias(system,release,version)
1581
1582 if system == 'Windows':
1583 # MS platforms
1584 rel,vers,csd,ptype = win32_ver(version)
1585 if terse:
1586 platform = _platform(system,release)
1587 else:
1588 platform = _platform(system,release,version,csd)
1589
1590 elif system in ('Linux',):
1591 # Linux based systems
1592 distname,distversion,distid = dist('')
1593 if distname and not terse:
1594 platform = _platform(system,release,machine,processor,
1595 'with',
1596 distname,distversion,distid)
1597 else:
1598 # If the distribution name is unknown check for libc vs. glibc
1599 libcname,libcversion = libc_ver(sys.executable)
1600 platform = _platform(system,release,machine,processor,
1601 'with',
1602 libcname+libcversion)
1603 elif system == 'Java':
1604 # Java platforms
1605 r,v,vminfo,(os_name,os_version,os_arch) = java_ver()
Marc-André Lemburg9e0dc962007-01-13 21:00:08 +00001606 if terse or not os_name:
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001607 platform = _platform(system,release,version)
1608 else:
1609 platform = _platform(system,release,version,
1610 'on',
1611 os_name,os_version,os_arch)
1612
1613 elif system == 'MacOS':
1614 # MacOS platforms
1615 if terse:
1616 platform = _platform(system,release)
1617 else:
1618 platform = _platform(system,release,machine)
1619
1620 else:
1621 # Generic handler
1622 if terse:
1623 platform = _platform(system,release)
1624 else:
1625 bits,linkage = architecture(sys.executable)
1626 platform = _platform(system,release,machine,processor,bits,linkage)
Tim Peters0eadaac2003-04-24 16:02:54 +00001627
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001628 _platform_cache[(aliased, terse)] = platform
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001629 return platform
1630
1631### Command line interface
1632
1633if __name__ == '__main__':
1634 # Default is to print the aliased verbose platform string
Tim Peters0eadaac2003-04-24 16:02:54 +00001635 terse = ('terse' in sys.argv or '--terse' in sys.argv)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001636 aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv)
1637 print platform(aliased,terse)
1638 sys.exit(0)