Issue #7264: Fix a possible deadlock when deallocating thread-local objects
[python.git] / Lib / platform.py
blob5fda9fb10a65164f70b0f67f3488b1106698e4ce
1 #!/usr/bin/env python
3 """ This module tries to retrieve as much platform-identifying data as
4 possible. It makes this information available via function APIs.
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.
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 bug tracker (http://bugs.python.org) and assign them to "lemburg".
15 # Note: Please keep this module compatible to Python 1.5.2.
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
23 # Many thanks to all those who helped adding platform-specific
24 # checks (in no particular order):
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),
31 # Colin Kong, Trent Mick, Guido van Rossum, Anthony Baxter
33 # History:
35 # <see CVS and SVN checkin messages for history>
37 # 1.0.7 - added DEV_NULL
38 # 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
41 # 1.0.3 - added normalization of Windows system name
42 # 1.0.2 - added more Windows support
43 # 1.0.1 - reformatted to make doc.py happy
44 # 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
87 # You can always get the latest version of this module at:
89 # http://www.egenix.com/files/python/platform.py
91 # If that URL should fail, try contacting the author.
93 __copyright__ = """
94 Copyright (c) 1999-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
95 Copyright (c) 2000-2009, eGenix.com Software GmbH; mailto:info@egenix.com
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.
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 !
114 __version__ = '1.0.7'
116 import sys,string,os,re
118 ### Globals & Constants
120 # Determine the platform's /dev/null device
121 try:
122 DEV_NULL = os.devnull
123 except 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'
133 ### Platform specific APIs
135 _libc_search = re.compile(r'(__libc_init)'
137 '(GLIBC_([0-9.]+))'
139 '(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)')
141 def libc_ver(executable=sys.executable,lib='',version='',
143 chunksize=2048):
145 """ Tries to determine the libc version that the file executable
146 (which defaults to the Python interpreter) is linked against.
148 Returns a tuple of strings (lib,version) which default to the
149 given parameters in case the lookup fails.
151 Note that the function has intimate knowledge of how different
152 libc versions add symbols to the executable and thus is probably
153 only useable for executables compiled using gcc.
155 The file is read and scanned in chunks of chunksize bytes.
158 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)
163 f = open(executable,'rb')
164 binary = f.read(chunksize)
165 pos = 0
166 while 1:
167 m = _libc_search.search(binary,pos)
168 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'
186 if soversion > version:
187 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
194 def _dist_try_harder(distname,version,id):
196 """ Tries some special tricks to get the distribution
197 information in case the default method fails.
199 Currently supports older SuSE Linux, Caldera OpenLinux and
200 Slackware Linux distributions.
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
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
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')
233 for n in range(len(verfiles)-1, -1, -1):
234 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
242 return distname,version,id
244 _release_filename = re.compile(r'(\w+)[-_](release|version)')
245 _lsb_release_version = re.compile(r'(.+)'
246 ' release '
247 '([\d.]+)'
248 '[^(]*(?:\((.+)\))?')
249 _release_version = re.compile(r'([^0-9]+)'
250 '(?: release )?'
251 '([\d.]+)'
252 '[^(]*(?:\((.+)\))?')
254 # See also http://www.novell.com/coolsolutions/feature/11251.html
255 # and http://linuxmafia.com/faq/Admin/release-files.html
256 # and http://data.linux-ntfs.org/rpm/whichrpm
257 # and http://www.die.net/doc/linux/man/man1/lsb_release.1.html
259 _supported_dists = (
260 'SuSE', 'debian', 'fedora', 'redhat', 'centos',
261 'mandrake', 'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo',
262 'UnitedLinux', 'turbolinux')
264 def _parse_release_file(firstline):
266 # Parse the first line
267 m = _lsb_release_version.match(firstline)
268 if m is not None:
269 # LSB format: "distro release x.x (codename)"
270 return tuple(m.groups())
272 # Pre-LSB format: "distro x.x (codename)"
273 m = _release_version.match(firstline)
274 if m is not None:
275 return tuple(m.groups())
277 # Unkown format... take the first two words
278 l = string.split(string.strip(firstline))
279 if l:
280 version = l[0]
281 if len(l) > 1:
282 id = l[1]
283 else:
284 id = ''
285 return '', version, id
287 def linux_distribution(distname='', version='', id='',
289 supported_dists=_supported_dists,
290 full_distribution_name=1):
292 """ Tries to determine the name of the Linux OS distribution name.
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.
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.
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.
307 Returns a tuple (distname,version,id) which default to the
308 args given as parameters.
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)
327 # 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)
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
341 # To maintain backwards compatibility:
343 def dist(distname='',version='',id='',
345 supported_dists=_supported_dists):
347 """ Tries to determine the name of the Linux OS distribution name.
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.
353 Returns a tuple (distname,version,id) which default to the
354 args given as parameters.
357 return linux_distribution(distname, version, id,
358 supported_dists=supported_dists,
359 full_distribution_name=0)
361 class _popen:
363 """ Fairly portable (alternative) popen implementation.
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.
369 Writing to the pipe is currently not supported.
372 tmpfile = ''
373 pipe = None
374 bufsize = None
375 mode = 'r'
377 def __init__(self,cmd,mode='r',bufsize=None):
379 if mode != 'r':
380 raise ValueError,'popen()-emulation only supports read mode'
381 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
388 def read(self):
390 return self.pipe.read()
392 def readlines(self):
394 if self.bufsize is not None:
395 return self.pipe.readlines()
397 def close(self,
399 remove=os.unlink,error=os.error):
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
412 # Alias
413 __del__ = close
415 def popen(cmd, mode='r', bufsize=None):
417 """ Portable popen() interface.
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)
448 def _norm_version(version, build=''):
450 """ Normalize the version and build strings and return a single
451 version string using the format major.minor.build (or patchlevel).
453 l = string.split(version,'.')
454 if build:
455 l.append(build)
456 try:
457 ints = map(int,l)
458 except ValueError:
459 strings = l
460 else:
461 strings = map(str,ints)
462 version = string.join(strings[:3],'.')
463 return version
465 _ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) '
466 '.*'
467 '\[.* ([\d.]+)\])')
469 # Examples of VER command output:
471 # Windows 2000: Microsoft Windows 2000 [Version 5.00.2195]
472 # Windows XP: Microsoft Windows XP [Version 5.1.2600]
473 # Windows Vista: Microsoft Windows [Version 6.0.6002]
475 # Note that the "Version" string gets localized on different
476 # Windows versions.
478 def _syscmd_ver(system='', release='', version='',
480 supported_platforms=('win32','win16','dos','os2')):
482 """ Tries to figure out the OS version used and returns
483 a tuple (system,release,version).
485 It uses the "ver" shell command for this which is known
486 to exists on Windows, DOS and OS/2. XXX Others too ?
488 In case this fails, the given parameters are used as
489 defaults.
492 if sys.platform not in supported_platforms:
493 return system,release,version
495 # Try some common cmd strings
496 for cmd in ('ver','command /c ver','cmd /c ver'):
497 try:
498 pipe = popen(cmd)
499 info = pipe.read()
500 if pipe.close():
501 raise os.error,'command failed'
502 # XXX How can I supress shell errors from being written
503 # to stderr ?
504 except os.error,why:
505 #print 'Command %s failed: %s' % (cmd,why)
506 continue
507 except IOError,why:
508 #print 'Command %s failed: %s' % (cmd,why)
509 continue
510 else:
511 break
512 else:
513 return system,release,version
515 # Parse the output
516 info = string.strip(info)
517 m = _ver_output.match(info)
518 if m is not None:
519 system,release,version = m.groups()
520 # Strip trailing dots from version and release
521 if release[-1] == '.':
522 release = release[:-1]
523 if version[-1] == '.':
524 version = version[:-1]
525 # Normalize the version and build strings (eliminating additional
526 # zeros)
527 version = _norm_version(version)
528 return system,release,version
530 def _win32_getvalue(key,name,default=''):
532 """ Read a value for name from the registry key.
534 In case this fails, default is returned.
537 try:
538 # Use win32api if available
539 from win32api import RegQueryValueEx
540 except ImportError:
541 # On Python 2.0 and later, emulate using _winreg
542 import _winreg
543 RegQueryValueEx = _winreg.QueryValueEx
544 try:
545 return RegQueryValueEx(key,name)
546 except:
547 return default
549 def win32_ver(release='',version='',csd='',ptype=''):
551 """ Get additional version information from the Windows Registry
552 and return a tuple (version,csd,ptype) referring to version
553 number, CSD level and OS type (multi/single
554 processor).
556 As a hint: ptype returns 'Uniprocessor Free' on single
557 processor NT machines and 'Multiprocessor Free' on multi
558 processor machines. The 'Free' refers to the OS version being
559 free of debugging code. It could also state 'Checked' which
560 means the OS version uses debugging code, i.e. code that
561 checks arguments, ranges, etc. (Thomas Heller).
563 Note: this function works best with Mark Hammond's win32
564 package installed, but also on Python 2.3 and later. It
565 obviously only runs on Win32 compatible platforms.
568 # XXX Is there any way to find out the processor type on WinXX ?
569 # XXX Is win32 available on Windows CE ?
571 # Adapted from code posted by Karl Putland to comp.lang.python.
573 # The mappings between reg. values and release names can be found
574 # here: http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
576 # Import the needed APIs
577 try:
578 import win32api
579 from win32api import RegQueryValueEx, RegOpenKeyEx, \
580 RegCloseKey, GetVersionEx
581 from win32con import HKEY_LOCAL_MACHINE, VER_PLATFORM_WIN32_NT, \
582 VER_PLATFORM_WIN32_WINDOWS, VER_NT_WORKSTATION
583 except ImportError:
584 # Emulate the win32api module using Python APIs
585 try:
586 sys.getwindowsversion
587 except AttributeError:
588 # No emulation possible, so return the defaults...
589 return release,version,csd,ptype
590 else:
591 # Emulation using _winreg (added in Python 2.0) and
592 # sys.getwindowsversion() (added in Python 2.3)
593 import _winreg
594 GetVersionEx = sys.getwindowsversion
595 RegQueryValueEx = _winreg.QueryValueEx
596 RegOpenKeyEx = _winreg.OpenKeyEx
597 RegCloseKey = _winreg.CloseKey
598 HKEY_LOCAL_MACHINE = _winreg.HKEY_LOCAL_MACHINE
599 VER_PLATFORM_WIN32_WINDOWS = 1
600 VER_PLATFORM_WIN32_NT = 2
601 VER_NT_WORKSTATION = 1
603 # Find out the registry key and some general version infos
604 maj,min,buildno,plat,csd = GetVersionEx()
605 version = '%i.%i.%i' % (maj,min,buildno & 0xFFFF)
606 if csd[:13] == 'Service Pack ':
607 csd = 'SP' + csd[13:]
609 if plat == VER_PLATFORM_WIN32_WINDOWS:
610 regkey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
611 # Try to guess the release name
612 if maj == 4:
613 if min == 0:
614 release = '95'
615 elif min == 10:
616 release = '98'
617 elif min == 90:
618 release = 'Me'
619 else:
620 release = 'postMe'
621 elif maj == 5:
622 release = '2000'
624 elif plat == VER_PLATFORM_WIN32_NT:
625 regkey = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion'
626 if maj <= 4:
627 release = 'NT'
628 elif maj == 5:
629 if min == 0:
630 release = '2000'
631 elif min == 1:
632 release = 'XP'
633 elif min == 2:
634 release = '2003Server'
635 else:
636 release = 'post2003'
637 elif maj == 6:
638 if min == 0:
639 # Per http://msdn2.microsoft.com/en-us/library/ms724429.aspx
640 try:
641 productType = GetVersionEx(1)[8]
642 except TypeError:
643 # sys.getwindowsversion() doesn't take any arguments, so
644 # we cannot detect 2008 Server that way.
645 # XXX Add some other means of detecting 2008 Server ?!
646 release = 'Vista'
647 else:
648 if productType == VER_NT_WORKSTATION:
649 release = 'Vista'
650 else:
651 release = '2008Server'
652 #elif min == 1:
653 # # Windows 7 release candidate uses version 6.1.7100
654 # release = '7RC'
655 else:
656 release = 'post2008Server'
658 else:
659 if not release:
660 # E.g. Win3.1 with win32s
661 release = '%i.%i' % (maj,min)
662 return release,version,csd,ptype
664 # Open the registry key
665 try:
666 keyCurVer = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey)
667 # Get a value to make sure the key exists...
668 RegQueryValueEx(keyCurVer, 'SystemRoot')
669 except:
670 return release,version,csd,ptype
672 # Parse values
673 #subversion = _win32_getvalue(keyCurVer,
674 # 'SubVersionNumber',
675 # ('',1))[0]
676 #if subversion:
677 # release = release + subversion # 95a, 95b, etc.
678 build = _win32_getvalue(keyCurVer,
679 'CurrentBuildNumber',
680 ('',1))[0]
681 ptype = _win32_getvalue(keyCurVer,
682 'CurrentType',
683 (ptype,1))[0]
685 # Normalize version
686 version = _norm_version(version,build)
688 # Close key
689 RegCloseKey(keyCurVer)
690 return release,version,csd,ptype
692 def _mac_ver_lookup(selectors,default=None):
694 from gestalt import gestalt
695 import MacOS
696 l = []
697 append = l.append
698 for selector in selectors:
699 try:
700 append(gestalt(selector))
701 except (RuntimeError, MacOS.Error):
702 append(default)
703 return l
705 def _bcd2str(bcd):
707 return hex(bcd)[2:]
709 def mac_ver(release='',versioninfo=('','',''),machine=''):
711 """ Get MacOS version information and return it as tuple (release,
712 versioninfo, machine) with versioninfo being a tuple (version,
713 dev_stage, non_release_version).
715 Entries which cannot be determined are set to the paramter values
716 which default to ''. All tuple entries are strings.
718 Thanks to Mark R. Levinson for mailing documentation links and
719 code examples for this function. Documentation for the
720 gestalt() API is available online at:
722 http://www.rgaros.nl/gestalt/
725 # Check whether the version info module is available
726 try:
727 import gestalt
728 import MacOS
729 except ImportError:
730 return release,versioninfo,machine
731 # Get the infos
732 sysv,sysu,sysa = _mac_ver_lookup(('sysv','sysu','sysa'))
733 # Decode the infos
734 if sysv:
735 major = (sysv & 0xFF00) >> 8
736 minor = (sysv & 0x00F0) >> 4
737 patch = (sysv & 0x000F)
739 if (major, minor) >= (10, 4):
740 # the 'sysv' gestald cannot return patchlevels
741 # higher than 9. Apple introduced 3 new
742 # gestalt codes in 10.4 to deal with this
743 # issue (needed because patch levels can
744 # run higher than 9, such as 10.4.11)
745 major,minor,patch = _mac_ver_lookup(('sys1','sys2','sys3'))
746 release = '%i.%i.%i' %(major, minor, patch)
747 else:
748 release = '%s.%i.%i' % (_bcd2str(major),minor,patch)
750 if sysu:
751 # NOTE: this block is left as documentation of the
752 # intention of this function, the 'sysu' gestalt is no
753 # longer available and there are no alternatives.
754 major = int((sysu & 0xFF000000L) >> 24)
755 minor = (sysu & 0x00F00000) >> 20
756 bugfix = (sysu & 0x000F0000) >> 16
757 stage = (sysu & 0x0000FF00) >> 8
758 nonrel = (sysu & 0x000000FF)
759 version = '%s.%i.%i' % (_bcd2str(major),minor,bugfix)
760 nonrel = _bcd2str(nonrel)
761 stage = {0x20:'development',
762 0x40:'alpha',
763 0x60:'beta',
764 0x80:'final'}.get(stage,'')
765 versioninfo = (version,stage,nonrel)
768 if sysa:
769 machine = {0x1: '68k',
770 0x2: 'PowerPC',
771 0xa: 'i386'}.get(sysa,'')
772 return release,versioninfo,machine
774 def _java_getprop(name,default):
776 from java.lang import System
777 try:
778 value = System.getProperty(name)
779 if value is None:
780 return default
781 return value
782 except AttributeError:
783 return default
785 def java_ver(release='',vendor='',vminfo=('','',''),osinfo=('','','')):
787 """ Version interface for Jython.
789 Returns a tuple (release,vendor,vminfo,osinfo) with vminfo being
790 a tuple (vm_name,vm_release,vm_vendor) and osinfo being a
791 tuple (os_name,os_version,os_arch).
793 Values which cannot be determined are set to the defaults
794 given as parameters (which all default to '').
797 # Import the needed APIs
798 try:
799 import java.lang
800 except ImportError:
801 return release,vendor,vminfo,osinfo
803 vendor = _java_getprop('java.vendor', vendor)
804 release = _java_getprop('java.version', release)
805 vm_name, vm_release, vm_vendor = vminfo
806 vm_name = _java_getprop('java.vm.name', vm_name)
807 vm_vendor = _java_getprop('java.vm.vendor', vm_vendor)
808 vm_release = _java_getprop('java.vm.version', vm_release)
809 vminfo = vm_name, vm_release, vm_vendor
810 os_name, os_version, os_arch = osinfo
811 os_arch = _java_getprop('java.os.arch', os_arch)
812 os_name = _java_getprop('java.os.name', os_name)
813 os_version = _java_getprop('java.os.version', os_version)
814 osinfo = os_name, os_version, os_arch
816 return release, vendor, vminfo, osinfo
818 ### System name aliasing
820 def system_alias(system,release,version):
822 """ Returns (system,release,version) aliased to common
823 marketing names used for some systems.
825 It also does some reordering of the information in some cases
826 where it would otherwise cause confusion.
829 if system == 'Rhapsody':
830 # Apple's BSD derivative
831 # XXX How can we determine the marketing release number ?
832 return 'MacOS X Server',system+release,version
834 elif system == 'SunOS':
835 # Sun's OS
836 if release < '5':
837 # These releases use the old name SunOS
838 return system,release,version
839 # Modify release (marketing release = SunOS release - 3)
840 l = string.split(release,'.')
841 if l:
842 try:
843 major = int(l[0])
844 except ValueError:
845 pass
846 else:
847 major = major - 3
848 l[0] = str(major)
849 release = string.join(l,'.')
850 if release < '6':
851 system = 'Solaris'
852 else:
853 # XXX Whatever the new SunOS marketing name is...
854 system = 'Solaris'
856 elif system == 'IRIX64':
857 # IRIX reports IRIX64 on platforms with 64-bit support; yet it
858 # is really a version and not a different platform, since 32-bit
859 # apps are also supported..
860 system = 'IRIX'
861 if version:
862 version = version + ' (64bit)'
863 else:
864 version = '64bit'
866 elif system in ('win32','win16'):
867 # In case one of the other tricks
868 system = 'Windows'
870 return system,release,version
872 ### Various internal helpers
874 def _platform(*args):
876 """ Helper to format the platform string in a filename
877 compatible format e.g. "system-version-machine".
879 # Format the platform string
880 platform = string.join(
881 map(string.strip,
882 filter(len, args)),
883 '-')
885 # Cleanup some possible filename obstacles...
886 replace = string.replace
887 platform = replace(platform,' ','_')
888 platform = replace(platform,'/','-')
889 platform = replace(platform,'\\','-')
890 platform = replace(platform,':','-')
891 platform = replace(platform,';','-')
892 platform = replace(platform,'"','-')
893 platform = replace(platform,'(','-')
894 platform = replace(platform,')','-')
896 # No need to report 'unknown' information...
897 platform = replace(platform,'unknown','')
899 # Fold '--'s and remove trailing '-'
900 while 1:
901 cleaned = replace(platform,'--','-')
902 if cleaned == platform:
903 break
904 platform = cleaned
905 while platform[-1] == '-':
906 platform = platform[:-1]
908 return platform
910 def _node(default=''):
912 """ Helper to determine the node name of this machine.
914 try:
915 import socket
916 except ImportError:
917 # No sockets...
918 return default
919 try:
920 return socket.gethostname()
921 except socket.error:
922 # Still not working...
923 return default
925 # os.path.abspath is new in Python 1.5.2:
926 if not hasattr(os.path,'abspath'):
928 def _abspath(path,
930 isabs=os.path.isabs,join=os.path.join,getcwd=os.getcwd,
931 normpath=os.path.normpath):
933 if not isabs(path):
934 path = join(getcwd(), path)
935 return normpath(path)
937 else:
939 _abspath = os.path.abspath
941 def _follow_symlinks(filepath):
943 """ In case filepath is a symlink, follow it until a
944 real file is reached.
946 filepath = _abspath(filepath)
947 while os.path.islink(filepath):
948 filepath = os.path.normpath(
949 os.path.join(os.path.dirname(filepath),os.readlink(filepath)))
950 return filepath
952 def _syscmd_uname(option,default=''):
954 """ Interface to the system's uname command.
956 if sys.platform in ('dos','win32','win16','os2'):
957 # XXX Others too ?
958 return default
959 try:
960 f = os.popen('uname %s 2> %s' % (option, DEV_NULL))
961 except (AttributeError,os.error):
962 return default
963 output = string.strip(f.read())
964 rc = f.close()
965 if not output or rc:
966 return default
967 else:
968 return output
970 def _syscmd_file(target,default=''):
972 """ Interface to the system's file command.
974 The function uses the -b option of the file command to have it
975 ommit the filename in its output and if possible the -L option
976 to have the command follow symlinks. It returns default in
977 case the command should fail.
980 if sys.platform in ('dos','win32','win16','os2'):
981 # XXX Others too ?
982 return default
983 target = _follow_symlinks(target)
984 try:
985 f = os.popen('file "%s" 2> %s' % (target, DEV_NULL))
986 except (AttributeError,os.error):
987 return default
988 output = string.strip(f.read())
989 rc = f.close()
990 if not output or rc:
991 return default
992 else:
993 return output
995 ### Information about the used architecture
997 # Default values for architecture; non-empty strings override the
998 # defaults given as parameters
999 _default_architecture = {
1000 'win32': ('','WindowsPE'),
1001 'win16': ('','Windows'),
1002 'dos': ('','MSDOS'),
1005 _architecture_split = re.compile(r'[\s,]').split
1007 def architecture(executable=sys.executable,bits='',linkage=''):
1009 """ Queries the given executable (defaults to the Python interpreter
1010 binary) for various architecture information.
1012 Returns a tuple (bits,linkage) which contains information about
1013 the bit architecture and the linkage format used for the
1014 executable. Both values are returned as strings.
1016 Values that cannot be determined are returned as given by the
1017 parameter presets. If bits is given as '', the sizeof(pointer)
1018 (or sizeof(long) on Python version < 1.5.2) is used as
1019 indicator for the supported pointer size.
1021 The function relies on the system's "file" command to do the
1022 actual work. This is available on most if not all Unix
1023 platforms. On some non-Unix platforms where the "file" command
1024 does not exist and the executable is set to the Python interpreter
1025 binary defaults from _default_architecture are used.
1028 # Use the sizeof(pointer) as default number of bits if nothing
1029 # else is given as default.
1030 if not bits:
1031 import struct
1032 try:
1033 size = struct.calcsize('P')
1034 except struct.error:
1035 # Older installations can only query longs
1036 size = struct.calcsize('l')
1037 bits = str(size*8) + 'bit'
1039 # Get data from the 'file' system command
1040 if executable:
1041 output = _syscmd_file(executable, '')
1042 else:
1043 output = ''
1045 if not output and \
1046 executable == sys.executable:
1047 # "file" command did not return anything; we'll try to provide
1048 # some sensible defaults then...
1049 if _default_architecture.has_key(sys.platform):
1050 b,l = _default_architecture[sys.platform]
1051 if b:
1052 bits = b
1053 if l:
1054 linkage = l
1055 return bits,linkage
1057 # Split the output into a list of strings omitting the filename
1058 fileout = _architecture_split(output)[1:]
1060 if 'executable' not in fileout:
1061 # Format not supported
1062 return bits,linkage
1064 # Bits
1065 if '32-bit' in fileout:
1066 bits = '32bit'
1067 elif 'N32' in fileout:
1068 # On Irix only
1069 bits = 'n32bit'
1070 elif '64-bit' in fileout:
1071 bits = '64bit'
1073 # Linkage
1074 if 'ELF' in fileout:
1075 linkage = 'ELF'
1076 elif 'PE' in fileout:
1077 # E.g. Windows uses this format
1078 if 'Windows' in fileout:
1079 linkage = 'WindowsPE'
1080 else:
1081 linkage = 'PE'
1082 elif 'COFF' in fileout:
1083 linkage = 'COFF'
1084 elif 'MS-DOS' in fileout:
1085 linkage = 'MSDOS'
1086 else:
1087 # XXX the A.OUT format also falls under this class...
1088 pass
1090 return bits,linkage
1092 ### Portable uname() interface
1094 _uname_cache = None
1096 def uname():
1098 """ Fairly portable uname interface. Returns a tuple
1099 of strings (system,node,release,version,machine,processor)
1100 identifying the underlying platform.
1102 Note that unlike the os.uname function this also returns
1103 possible processor information as an additional tuple entry.
1105 Entries which cannot be determined are set to ''.
1108 global _uname_cache
1109 no_os_uname = 0
1111 if _uname_cache is not None:
1112 return _uname_cache
1114 processor = ''
1116 # Get some infos from the builtin os.uname API...
1117 try:
1118 system,node,release,version,machine = os.uname()
1119 except AttributeError:
1120 no_os_uname = 1
1122 if no_os_uname or not filter(None, (system, node, release, version, machine)):
1123 # Hmm, no there is either no uname or uname has returned
1124 #'unknowns'... we'll have to poke around the system then.
1125 if no_os_uname:
1126 system = sys.platform
1127 release = ''
1128 version = ''
1129 node = _node()
1130 machine = ''
1132 use_syscmd_ver = 1
1134 # Try win32_ver() on win32 platforms
1135 if system == 'win32':
1136 release,version,csd,ptype = win32_ver()
1137 if release and version:
1138 use_syscmd_ver = 0
1139 # Try to use the PROCESSOR_* environment variables
1140 # available on Win XP and later; see
1141 # http://support.microsoft.com/kb/888731 and
1142 # http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM
1143 if not machine:
1144 machine = os.environ.get('PROCESSOR_ARCHITECTURE', '')
1145 if not processor:
1146 processor = os.environ.get('PROCESSOR_IDENTIFIER', machine)
1148 # Try the 'ver' system command available on some
1149 # platforms
1150 if use_syscmd_ver:
1151 system,release,version = _syscmd_ver(system)
1152 # Normalize system to what win32_ver() normally returns
1153 # (_syscmd_ver() tends to return the vendor name as well)
1154 if system == 'Microsoft Windows':
1155 system = 'Windows'
1156 elif system == 'Microsoft' and release == 'Windows':
1157 # Under Windows Vista and Windows Server 2008,
1158 # Microsoft changed the output of the ver command. The
1159 # release is no longer printed. This causes the
1160 # system and release to be misidentified.
1161 system = 'Windows'
1162 if '6.0' == version[:3]:
1163 release = 'Vista'
1164 else:
1165 release = ''
1167 # In case we still don't know anything useful, we'll try to
1168 # help ourselves
1169 if system in ('win32','win16'):
1170 if not version:
1171 if system == 'win32':
1172 version = '32bit'
1173 else:
1174 version = '16bit'
1175 system = 'Windows'
1177 elif system[:4] == 'java':
1178 release,vendor,vminfo,osinfo = java_ver()
1179 system = 'Java'
1180 version = string.join(vminfo,', ')
1181 if not version:
1182 version = vendor
1184 elif os.name == 'mac':
1185 release,(version,stage,nonrel),machine = mac_ver()
1186 system = 'MacOS'
1188 # System specific extensions
1189 if system == 'OpenVMS':
1190 # OpenVMS seems to have release and version mixed up
1191 if not release or release == '0':
1192 release = version
1193 version = ''
1194 # Get processor information
1195 try:
1196 import vms_lib
1197 except ImportError:
1198 pass
1199 else:
1200 csid, cpu_number = vms_lib.getsyi('SYI$_CPU',0)
1201 if (cpu_number >= 128):
1202 processor = 'Alpha'
1203 else:
1204 processor = 'VAX'
1205 if not processor:
1206 # Get processor information from the uname system command
1207 processor = _syscmd_uname('-p','')
1209 #If any unknowns still exist, replace them with ''s, which are more portable
1210 if system == 'unknown':
1211 system = ''
1212 if node == 'unknown':
1213 node = ''
1214 if release == 'unknown':
1215 release = ''
1216 if version == 'unknown':
1217 version = ''
1218 if machine == 'unknown':
1219 machine = ''
1220 if processor == 'unknown':
1221 processor = ''
1223 # normalize name
1224 if system == 'Microsoft' and release == 'Windows':
1225 system = 'Windows'
1226 release = 'Vista'
1228 _uname_cache = system,node,release,version,machine,processor
1229 return _uname_cache
1231 ### Direct interfaces to some of the uname() return values
1233 def system():
1235 """ Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.
1237 An empty string is returned if the value cannot be determined.
1240 return uname()[0]
1242 def node():
1244 """ Returns the computer's network name (which may not be fully
1245 qualified)
1247 An empty string is returned if the value cannot be determined.
1250 return uname()[1]
1252 def release():
1254 """ Returns the system's release, e.g. '2.2.0' or 'NT'
1256 An empty string is returned if the value cannot be determined.
1259 return uname()[2]
1261 def version():
1263 """ Returns the system's release version, e.g. '#3 on degas'
1265 An empty string is returned if the value cannot be determined.
1268 return uname()[3]
1270 def machine():
1272 """ Returns the machine type, e.g. 'i386'
1274 An empty string is returned if the value cannot be determined.
1277 return uname()[4]
1279 def processor():
1281 """ Returns the (true) processor name, e.g. 'amdk6'
1283 An empty string is returned if the value cannot be
1284 determined. Note that many platforms do not provide this
1285 information or simply return the same value as for machine(),
1286 e.g. NetBSD does this.
1289 return uname()[5]
1291 ### Various APIs for extracting information from sys.version
1293 _sys_version_parser = re.compile(
1294 r'([\w.+]+)\s*'
1295 '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
1296 '\[([^\]]+)\]?')
1298 _ironpython_sys_version_parser = re.compile(
1299 r'IronPython\s*'
1300 '([\d\.]+)'
1301 '(?: \(([\d\.]+)\))?'
1302 ' on (.NET [\d\.]+)')
1304 _pypy_sys_version_parser = re.compile(
1305 r'([\w.+]+)\s*'
1306 '\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
1307 '\[PyPy [^\]]+\]?')
1309 _sys_version_cache = {}
1311 def _sys_version(sys_version=None):
1313 """ Returns a parsed version of Python's sys.version as tuple
1314 (name, version, branch, revision, buildno, builddate, compiler)
1315 referring to the Python implementation name, version, branch,
1316 revision, build number, build date/time as string and the compiler
1317 identification string.
1319 Note that unlike the Python sys.version, the returned value
1320 for the Python version will always include the patchlevel (it
1321 defaults to '.0').
1323 The function returns empty strings for tuple entries that
1324 cannot be determined.
1326 sys_version may be given to parse an alternative version
1327 string, e.g. if the version was read from a different Python
1328 interpreter.
1331 # Get the Python version
1332 if sys_version is None:
1333 sys_version = sys.version
1335 # Try the cache first
1336 result = _sys_version_cache.get(sys_version, None)
1337 if result is not None:
1338 return result
1340 # Parse it
1341 if sys_version[:10] == 'IronPython':
1342 # IronPython
1343 name = 'IronPython'
1344 match = _ironpython_sys_version_parser.match(sys_version)
1345 if match is None:
1346 raise ValueError(
1347 'failed to parse IronPython sys.version: %s' %
1348 repr(sys_version))
1349 version, alt_version, compiler = match.groups()
1350 buildno = ''
1351 builddate = ''
1353 elif sys.platform[:4] == 'java':
1354 # Jython
1355 name = 'Jython'
1356 match = _sys_version_parser.match(sys_version)
1357 if match is None:
1358 raise ValueError(
1359 'failed to parse Jython sys.version: %s' %
1360 repr(sys_version))
1361 version, buildno, builddate, buildtime, _ = match.groups()
1362 compiler = sys.platform
1364 elif "PyPy" in sys_version:
1365 # PyPy
1366 name = "PyPy"
1367 match = _pypy_sys_version_parser.match(sys_version)
1368 if match is None:
1369 raise ValueError("failed to parse PyPy sys.version: %s" %
1370 repr(sys_version))
1371 version, buildno, builddate, buildtime = match.groups()
1372 compiler = ""
1374 else:
1375 # CPython
1376 match = _sys_version_parser.match(sys_version)
1377 if match is None:
1378 raise ValueError(
1379 'failed to parse CPython sys.version: %s' %
1380 repr(sys_version))
1381 version, buildno, builddate, buildtime, compiler = \
1382 match.groups()
1383 name = 'CPython'
1384 builddate = builddate + ' ' + buildtime
1386 if hasattr(sys, 'subversion'):
1387 # sys.subversion was added in Python 2.5
1388 _, branch, revision = sys.subversion
1389 else:
1390 branch = ''
1391 revision = ''
1393 # Add the patchlevel version if missing
1394 l = string.split(version, '.')
1395 if len(l) == 2:
1396 l.append('0')
1397 version = string.join(l, '.')
1399 # Build and cache the result
1400 result = (name, version, branch, revision, buildno, builddate, compiler)
1401 _sys_version_cache[sys_version] = result
1402 return result
1404 def python_implementation():
1406 """ Returns a string identifying the Python implementation.
1408 Currently, the following implementations are identified:
1409 'CPython' (C implementation of Python),
1410 'IronPython' (.NET implementation of Python),
1411 'Jython' (Java implementation of Python).
1414 return _sys_version()[0]
1416 def python_version():
1418 """ Returns the Python version as string 'major.minor.patchlevel'
1420 Note that unlike the Python sys.version, the returned value
1421 will always include the patchlevel (it defaults to 0).
1424 return _sys_version()[1]
1426 def python_version_tuple():
1428 """ Returns the Python version as tuple (major, minor, patchlevel)
1429 of strings.
1431 Note that unlike the Python sys.version, the returned value
1432 will always include the patchlevel (it defaults to 0).
1435 return tuple(string.split(_sys_version()[1], '.'))
1437 def python_branch():
1439 """ Returns a string identifying the Python implementation
1440 branch.
1442 For CPython this is the Subversion branch from which the
1443 Python binary was built.
1445 If not available, an empty string is returned.
1449 return _sys_version()[2]
1451 def python_revision():
1453 """ Returns a string identifying the Python implementation
1454 revision.
1456 For CPython this is the Subversion revision from which the
1457 Python binary was built.
1459 If not available, an empty string is returned.
1462 return _sys_version()[3]
1464 def python_build():
1466 """ Returns a tuple (buildno, builddate) stating the Python
1467 build number and date as strings.
1470 return _sys_version()[4:6]
1472 def python_compiler():
1474 """ Returns a string identifying the compiler used for compiling
1475 Python.
1478 return _sys_version()[6]
1480 ### The Opus Magnum of platform strings :-)
1482 _platform_cache = {}
1484 def platform(aliased=0, terse=0):
1486 """ Returns a single string identifying the underlying platform
1487 with as much useful information as possible (but no more :).
1489 The output is intended to be human readable rather than
1490 machine parseable. It may look different on different
1491 platforms and this is intended.
1493 If "aliased" is true, the function will use aliases for
1494 various platforms that report system names which differ from
1495 their common names, e.g. SunOS will be reported as
1496 Solaris. The system_alias() function is used to implement
1497 this.
1499 Setting terse to true causes the function to return only the
1500 absolute minimum information needed to identify the platform.
1503 result = _platform_cache.get((aliased, terse), None)
1504 if result is not None:
1505 return result
1507 # Get uname information and then apply platform specific cosmetics
1508 # to it...
1509 system,node,release,version,machine,processor = uname()
1510 if machine == processor:
1511 processor = ''
1512 if aliased:
1513 system,release,version = system_alias(system,release,version)
1515 if system == 'Windows':
1516 # MS platforms
1517 rel,vers,csd,ptype = win32_ver(version)
1518 if terse:
1519 platform = _platform(system,release)
1520 else:
1521 platform = _platform(system,release,version,csd)
1523 elif system in ('Linux',):
1524 # Linux based systems
1525 distname,distversion,distid = dist('')
1526 if distname and not terse:
1527 platform = _platform(system,release,machine,processor,
1528 'with',
1529 distname,distversion,distid)
1530 else:
1531 # If the distribution name is unknown check for libc vs. glibc
1532 libcname,libcversion = libc_ver(sys.executable)
1533 platform = _platform(system,release,machine,processor,
1534 'with',
1535 libcname+libcversion)
1536 elif system == 'Java':
1537 # Java platforms
1538 r,v,vminfo,(os_name,os_version,os_arch) = java_ver()
1539 if terse or not os_name:
1540 platform = _platform(system,release,version)
1541 else:
1542 platform = _platform(system,release,version,
1543 'on',
1544 os_name,os_version,os_arch)
1546 elif system == 'MacOS':
1547 # MacOS platforms
1548 if terse:
1549 platform = _platform(system,release)
1550 else:
1551 platform = _platform(system,release,machine)
1553 else:
1554 # Generic handler
1555 if terse:
1556 platform = _platform(system,release)
1557 else:
1558 bits,linkage = architecture(sys.executable)
1559 platform = _platform(system,release,machine,processor,bits,linkage)
1561 _platform_cache[(aliased, terse)] = platform
1562 return platform
1564 ### Command line interface
1566 if __name__ == '__main__':
1567 # Default is to print the aliased verbose platform string
1568 terse = ('terse' in sys.argv or '--terse' in sys.argv)
1569 aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv)
1570 print platform(aliased,terse)
1571 sys.exit(0)