blob: ab0cf254bc69cf018df04dd5684c35c4ba45ee0a [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Marc-André Lemburg246d8472003-04-24 11:36:11 +00002
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
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000013# Python bug tracker (http://bugs.python.org) and assign them to "lemburg".
Marc-André Lemburg246d8472003-04-24 11:36:11 +000014#
Marc-André Lemburg246d8472003-04-24 11:36:11 +000015# Still needed:
Marc-André Lemburg246d8472003-04-24 11:36:11 +000016# * support for MS-DOS (PythonDX ?)
17# * support for Amiga and other still unsupported platforms running Python
18# * support for additional Linux distributions
19#
Brett Cannon8ab27df2003-08-05 03:52:04 +000020# Many thanks to all those who helped adding platform-specific
Marc-André Lemburg246d8472003-04-24 11:36:11 +000021# checks (in no particular order):
22#
23# Charles G Waldman, David Arnold, Gordon McMillan, Ben Darnell,
24# Jeff Bauer, Cliff Crawford, Ivan Van Laningham, Josef
25# Betancourt, Randall Hopper, Karl Putland, John Farrell, Greg
26# Andruk, Just van Rossum, Thomas Heller, Mark R. Levinson, Mark
27# Hammond, Bill Tutt, Hans Nowak, Uwe Zessin (OpenVMS support),
Steve Dowerb9f4fea2015-09-22 17:23:39 -070028# Colin Kong, Trent Mick, Guido van Rossum, Anthony Baxter, Steve
29# Dower
Marc-André Lemburg246d8472003-04-24 11:36:11 +000030#
31# History:
Marc-André Lemburg380f4172005-11-07 16:11:02 +000032#
33# <see CVS and SVN checkin messages for history>
34#
Steve Dowerb9f4fea2015-09-22 17:23:39 -070035# 1.0.8 - changed Windows support to read version from kernel32.dll
Alexandre Vassalottie52e3782009-07-17 09:18:18 +000036# 1.0.7 - added DEV_NULL
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000037# 1.0.6 - added linux_distribution()
38# 1.0.5 - fixed Java support to allow running the module on Jython
39# 1.0.4 - added IronPython support
Marc-André Lemburgcdc79232004-06-19 17:17:00 +000040# 1.0.3 - added normalization of Windows system name
Marc-André Lemburg91e83e22004-03-25 18:35:12 +000041# 1.0.2 - added more Windows support
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +000042# 1.0.1 - reformatted to make doc.py happy
Marc-André Lemburg246d8472003-04-24 11:36:11 +000043# 1.0.0 - reformatted a bit and checked into Python CVS
44# 0.8.0 - added sys.version parser and various new access
45# APIs (python_version(), python_compiler(), etc.)
46# 0.7.2 - fixed architecture() to use sizeof(pointer) where available
47# 0.7.1 - added support for Caldera OpenLinux
48# 0.7.0 - some fixes for WinCE; untabified the source file
49# 0.6.2 - support for OpenVMS - requires version 1.5.2-V006 or higher and
50# vms_lib.getsyi() configured
51# 0.6.1 - added code to prevent 'uname -p' on platforms which are
52# known not to support it
53# 0.6.0 - fixed win32_ver() to hopefully work on Win95,98,NT and Win2k;
54# did some cleanup of the interfaces - some APIs have changed
55# 0.5.5 - fixed another type in the MacOS code... should have
56# used more coffee today ;-)
57# 0.5.4 - fixed a few typos in the MacOS code
58# 0.5.3 - added experimental MacOS support; added better popen()
59# workarounds in _syscmd_ver() -- still not 100% elegant
60# though
61# 0.5.2 - fixed uname() to return '' instead of 'unknown' in all
62# return values (the system uname command tends to return
Martin Pantereb995702016-07-28 01:11:04 +000063# 'unknown' instead of just leaving the field empty)
Marc-André Lemburg246d8472003-04-24 11:36:11 +000064# 0.5.1 - included code for slackware dist; added exception handlers
65# to cover up situations where platforms don't have os.popen
66# (e.g. Mac) or fail on socket.gethostname(); fixed libc
67# detection RE
68# 0.5.0 - changed the API names referring to system commands to *syscmd*;
69# added java_ver(); made syscmd_ver() a private
70# API (was system_ver() in previous versions) -- use uname()
71# instead; extended the win32_ver() to also return processor
72# type information
73# 0.4.0 - added win32_ver() and modified the platform() output for WinXX
74# 0.3.4 - fixed a bug in _follow_symlinks()
75# 0.3.3 - fixed popen() and "file" command invokation bugs
76# 0.3.2 - added architecture() API and support for it in platform()
77# 0.3.1 - fixed syscmd_ver() RE to support Windows NT
78# 0.3.0 - added system alias support
79# 0.2.3 - removed 'wince' again... oh well.
80# 0.2.2 - added 'wince' to syscmd_ver() supported platforms
81# 0.2.1 - added cache logic and changed the platform string format
82# 0.2.0 - changed the API to use functions instead of module globals
83# since some action take too long to be run on module import
84# 0.1.0 - first release
85#
86# You can always get the latest version of this module at:
87#
88# http://www.egenix.com/files/python/platform.py
89#
90# If that URL should fail, try contacting the author.
91
92__copyright__ = """
93 Copyright (c) 1999-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Benjamin Peterson46a99002010-01-09 18:45:30 +000094 Copyright (c) 2000-2010, eGenix.com Software GmbH; mailto:info@egenix.com
Marc-André Lemburg246d8472003-04-24 11:36:11 +000095
96 Permission to use, copy, modify, and distribute this software and its
97 documentation for any purpose and without fee or royalty is hereby granted,
98 provided that the above copyright notice appear in all copies and that
99 both that copyright notice and this permission notice appear in
100 supporting documentation or portions thereof, including modifications,
101 that you make.
102
103 EGENIX.COM SOFTWARE GMBH DISCLAIMS ALL WARRANTIES WITH REGARD TO
104 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
105 FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
106 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
107 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
108 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
109 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !
110
111"""
112
Matthias Bussonnier6059ce42017-02-24 02:47:34 -0800113__version__ = '1.0.8'
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000114
Larry Hastings68386bc2012-06-24 14:30:41 -0700115import collections
Victor Stinnerb8e689a2018-12-04 17:18:12 +0100116import os
117import re
118import sys
Berker Peksag1392f712015-05-16 20:24:28 +0300119
Alexandre Vassalottie52e3782009-07-17 09:18:18 +0000120### Globals & Constants
121
Serhiy Storchaka7d81e8f2018-08-27 13:29:51 +0300122# Helper for comparing two version number strings.
123# Based on the description of the PHP's version_compare():
124# http://php.net/manual/en/function.version-compare.php
125
126_ver_stages = {
127 # any string not found in this dict, will get 0 assigned
128 'dev': 10,
129 'alpha': 20, 'a': 20,
130 'beta': 30, 'b': 30,
131 'c': 40,
132 'RC': 50, 'rc': 50,
133 # number, will get 100 assigned
134 'pl': 200, 'p': 200,
135}
136
137_component_re = re.compile(r'([0-9]+|[._+-])')
138
139def _comparable_version(version):
140 result = []
141 for v in _component_re.split(version):
142 if v not in '._+-':
143 try:
144 v = int(v, 10)
145 t = 100
146 except ValueError:
147 t = _ver_stages.get(v, 0)
148 result.extend((t, v))
149 return result
150
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000151### Platform specific APIs
152
Antoine Pitrouba7c2262011-10-07 13:26:59 +0200153_libc_search = re.compile(b'(__libc_init)'
154 b'|'
155 b'(GLIBC_([0-9.]+))'
156 b'|'
157 br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII)
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000158
Victor Stinner476b1132018-12-05 14:04:52 +0100159def libc_ver(executable=None, lib='', version='', chunksize=16384):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000160
Brett Cannon8ab27df2003-08-05 03:52:04 +0000161 """ Tries to determine the libc version that the file executable
162 (which defaults to the Python interpreter) is linked against.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000163
164 Returns a tuple of strings (lib,version) which default to the
165 given parameters in case the lookup fails.
166
167 Note that the function has intimate knowledge of how different
Brett Cannon8ab27df2003-08-05 03:52:04 +0000168 libc versions add symbols to the executable and thus is probably
169 only useable for executables compiled using gcc.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000170
171 The file is read and scanned in chunks of chunksize bytes.
172
173 """
Victor Stinner476b1132018-12-05 14:04:52 +0100174 if executable is None:
175 try:
176 ver = os.confstr('CS_GNU_LIBC_VERSION')
177 # parse 'glibc 2.28' as ('glibc', '2.28')
178 parts = ver.split(maxsplit=1)
179 if len(parts) == 2:
180 return tuple(parts)
181 except (AttributeError, ValueError, OSError):
182 # os.confstr() or CS_GNU_LIBC_VERSION value not available
183 pass
184
185 executable = sys.executable
186
Serhiy Storchaka7d81e8f2018-08-27 13:29:51 +0300187 V = _comparable_version
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000188 if hasattr(os.path, 'realpath'):
189 # Python 2.2 introduced os.path.realpath(); it is used
190 # here to work around problems with Cygwin not being
191 # able to open symlinks for reading
192 executable = os.path.realpath(executable)
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300193 with open(executable, 'rb') as f:
194 binary = f.read(chunksize)
195 pos = 0
Serhiy Storchaka2a9b8ba2018-07-09 11:47:45 +0300196 while pos < len(binary):
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300197 if b'libc' in binary or b'GLIBC' in binary:
198 m = _libc_search.search(binary, pos)
199 else:
200 m = None
Serhiy Storchaka2a9b8ba2018-07-09 11:47:45 +0300201 if not m or m.end() == len(binary):
202 chunk = f.read(chunksize)
203 if chunk:
204 binary = binary[max(pos, len(binary) - 1000):] + chunk
205 pos = 0
206 continue
207 if not m:
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300208 break
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300209 libcinit, glibc, glibcversion, so, threads, soversion = [
210 s.decode('latin1') if s is not None else s
211 for s in m.groups()]
212 if libcinit and not lib:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000213 lib = 'libc'
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300214 elif glibc:
215 if lib != 'glibc':
216 lib = 'glibc'
217 version = glibcversion
Serhiy Storchaka2a9b8ba2018-07-09 11:47:45 +0300218 elif V(glibcversion) > V(version):
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300219 version = glibcversion
220 elif so:
221 if lib != 'glibc':
222 lib = 'libc'
Serhiy Storchaka2a9b8ba2018-07-09 11:47:45 +0300223 if soversion and (not version or V(soversion) > V(version)):
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300224 version = soversion
225 if threads and version[-len(threads):] != threads:
226 version = version + threads
227 pos = m.end()
Victor Stinnerced39362013-12-09 00:14:52 +0100228 return lib, version
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000229
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000230def _norm_version(version, build=''):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000231
Brett Cannon8ab27df2003-08-05 03:52:04 +0000232 """ Normalize the version and build strings and return a single
Walter Dörwalde5a7fad2005-11-21 17:01:27 +0000233 version string using the format major.minor.build (or patchlevel).
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000234 """
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000235 l = version.split('.')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000236 if build:
237 l.append(build)
238 try:
Victor Stinnerced39362013-12-09 00:14:52 +0100239 ints = map(int, l)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000240 except ValueError:
241 strings = l
242 else:
Victor Stinnerced39362013-12-09 00:14:52 +0100243 strings = list(map(str, ints))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000244 version = '.'.join(strings[:3])
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000245 return version
246
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000247_ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) '
R David Murray44b548d2016-09-08 13:59:53 -0400248 r'.*'
249 r'\[.* ([\d.]+)\])')
Alexandre Vassalottie52e3782009-07-17 09:18:18 +0000250
251# Examples of VER command output:
252#
253# Windows 2000: Microsoft Windows 2000 [Version 5.00.2195]
254# Windows XP: Microsoft Windows XP [Version 5.1.2600]
255# Windows Vista: Microsoft Windows [Version 6.0.6002]
256#
257# Note that the "Version" string gets localized on different
258# Windows versions.
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000259
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000260def _syscmd_ver(system='', release='', version='',
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000261
Victor Stinnerced39362013-12-09 00:14:52 +0100262 supported_platforms=('win32', 'win16', 'dos')):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000263
264 """ Tries to figure out the OS version used and returns
Victor Stinnerced39362013-12-09 00:14:52 +0100265 a tuple (system, release, version).
Tim Peters0eadaac2003-04-24 16:02:54 +0000266
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000267 It uses the "ver" shell command for this which is known
Jesus Ceaf1af7052012-10-05 02:48:46 +0200268 to exists on Windows, DOS. XXX Others too ?
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000269
270 In case this fails, the given parameters are used as
271 defaults.
272
273 """
274 if sys.platform not in supported_platforms:
Victor Stinnerced39362013-12-09 00:14:52 +0100275 return system, release, version
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000276
277 # Try some common cmd strings
Victor Stinner3a521f02018-12-07 11:10:33 +0100278 import subprocess
Victor Stinnerced39362013-12-09 00:14:52 +0100279 for cmd in ('ver', 'command /c ver', 'cmd /c ver'):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000280 try:
Victor Stinner3a521f02018-12-07 11:10:33 +0100281 info = subprocess.check_output(cmd,
282 stderr=subprocess.DEVNULL,
283 text=True,
284 shell=True)
285 except (OSError, subprocess.CalledProcessError) as why:
286 #print('Command %s failed: %s' % (cmd, why))
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000287 continue
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000288 else:
289 break
290 else:
Victor Stinnerced39362013-12-09 00:14:52 +0100291 return system, release, version
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000292
293 # Parse the output
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000294 info = info.strip()
Marc-André Lemburg366a0fe2003-04-24 11:46:35 +0000295 m = _ver_output.match(info)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000296 if m is not None:
Victor Stinnerced39362013-12-09 00:14:52 +0100297 system, release, version = m.groups()
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000298 # Strip trailing dots from version and release
299 if release[-1] == '.':
300 release = release[:-1]
301 if version[-1] == '.':
302 version = version[:-1]
303 # Normalize the version and build strings (eliminating additional
304 # zeros)
305 version = _norm_version(version)
Victor Stinnerced39362013-12-09 00:14:52 +0100306 return system, release, version
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000307
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700308_WIN32_CLIENT_RELEASES = {
309 (5, 0): "2000",
310 (5, 1): "XP",
311 # Strictly, 5.2 client is XP 64-bit, but platform.py historically
312 # has always called it 2003 Server
313 (5, 2): "2003Server",
314 (5, None): "post2003",
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000315
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700316 (6, 0): "Vista",
317 (6, 1): "7",
318 (6, 2): "8",
319 (6, 3): "8.1",
320 (6, None): "post8.1",
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000321
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700322 (10, 0): "10",
323 (10, None): "post10",
324}
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000325
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700326# Server release name lookup will default to client names if necessary
327_WIN32_SERVER_RELEASES = {
328 (5, 2): "2003Server",
329
330 (6, 0): "2008Server",
331 (6, 1): "2008ServerR2",
332 (6, 2): "2012Server",
333 (6, 3): "2012ServerR2",
334 (6, None): "post2012ServerR2",
335}
336
Victor Stinnerced39362013-12-09 00:14:52 +0100337def win32_ver(release='', version='', csd='', ptype=''):
Steve Dower8f278f12015-09-22 17:35:24 -0700338 try:
339 from sys import getwindowsversion
340 except ImportError:
341 return release, version, csd, ptype
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000342 try:
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700343 from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
Brett Cannoncd171c82013-07-04 17:43:24 -0400344 except ImportError:
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700345 from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE
346
347 winver = getwindowsversion()
Steve Dower74f4af72016-09-17 17:27:48 -0700348 maj, min, build = winver.platform_version or winver[:3]
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700349 version = '{0}.{1}.{2}'.format(maj, min, build)
350
351 release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or
352 _WIN32_CLIENT_RELEASES.get((maj, None)) or
353 release)
354
355 # getwindowsversion() reflect the compatibility mode Python is
356 # running under, and so the service pack value is only going to be
357 # valid if the versions match.
358 if winver[:2] == (maj, min):
Christian Heimes02781dc2008-03-21 01:11:52 +0000359 try:
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700360 csd = 'SP{}'.format(winver.service_pack_major)
Christian Heimes02781dc2008-03-21 01:11:52 +0000361 except AttributeError:
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700362 if csd[:13] == 'Service Pack ':
363 csd = 'SP' + csd[13:]
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000364
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700365 # VER_NT_SERVER = 3
Steve Dower41519b22016-09-09 09:46:56 -0700366 if getattr(winver, 'product_type', None) == 3:
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700367 release = (_WIN32_SERVER_RELEASES.get((maj, min)) or
368 _WIN32_SERVER_RELEASES.get((maj, None)) or
369 release)
Alexandre Vassalottie52e3782009-07-17 09:18:18 +0000370
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700371 key = None
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000372 try:
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700373 key = OpenKeyEx(HKEY_LOCAL_MACHINE,
374 r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
375 ptype = QueryValueEx(key, 'CurrentType')[0]
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000376 except:
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700377 pass
378 finally:
379 if key:
380 CloseKey(key)
Tim Peters0eadaac2003-04-24 16:02:54 +0000381
Victor Stinnerced39362013-12-09 00:14:52 +0100382 return release, version, csd, ptype
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000383
Steve Dowerb9f4fea2015-09-22 17:23:39 -0700384
Ronald Oussorene186e382010-07-23 11:54:59 +0000385def _mac_ver_xml():
386 fn = '/System/Library/CoreServices/SystemVersion.plist'
387 if not os.path.exists(fn):
388 return None
389
390 try:
391 import plistlib
Brett Cannoncd171c82013-07-04 17:43:24 -0400392 except ImportError:
Ronald Oussorene186e382010-07-23 11:54:59 +0000393 return None
394
Ned Deily936dfae2014-01-13 11:34:19 -0800395 with open(fn, 'rb') as f:
396 pl = plistlib.load(f)
Ronald Oussorene186e382010-07-23 11:54:59 +0000397 release = pl['ProductVersion']
Victor Stinnerced39362013-12-09 00:14:52 +0100398 versioninfo = ('', '', '')
Larry Hastings605a62d2012-06-24 04:33:36 -0700399 machine = os.uname().machine
Ronald Oussorenfcd77012010-08-03 07:42:42 +0000400 if machine in ('ppc', 'Power Macintosh'):
Ezio Melotti9a3777e2013-08-17 15:53:55 +0300401 # Canonical name
Ronald Oussorene186e382010-07-23 11:54:59 +0000402 machine = 'PowerPC'
403
Victor Stinnerced39362013-12-09 00:14:52 +0100404 return release, versioninfo, machine
Ronald Oussorene186e382010-07-23 11:54:59 +0000405
406
Victor Stinnerced39362013-12-09 00:14:52 +0100407def mac_ver(release='', versioninfo=('', '', ''), machine=''):
Ronald Oussorene186e382010-07-23 11:54:59 +0000408
Victor Stinnerb0e08772018-12-12 17:48:08 +0100409 """ Get macOS version information and return it as tuple (release,
Ronald Oussorene186e382010-07-23 11:54:59 +0000410 versioninfo, machine) with versioninfo being a tuple (version,
411 dev_stage, non_release_version).
412
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300413 Entries which cannot be determined are set to the parameter values
Ronald Oussorene186e382010-07-23 11:54:59 +0000414 which default to ''. All tuple entries are strings.
415 """
416
417 # First try reading the information from an XML file which should
418 # always be present
419 info = _mac_ver_xml()
420 if info is not None:
421 return info
422
Ronald Oussorene186e382010-07-23 11:54:59 +0000423 # If that also doesn't work return the default values
Victor Stinnerced39362013-12-09 00:14:52 +0100424 return release, versioninfo, machine
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000425
Victor Stinnerced39362013-12-09 00:14:52 +0100426def _java_getprop(name, default):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000427
428 from java.lang import System
429 try:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000430 value = System.getProperty(name)
431 if value is None:
432 return default
433 return value
434 except AttributeError:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000435 return default
436
Victor Stinnerced39362013-12-09 00:14:52 +0100437def java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', '')):
Tim Peters0eadaac2003-04-24 16:02:54 +0000438
Brett Cannon8ab27df2003-08-05 03:52:04 +0000439 """ Version interface for Jython.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000440
Victor Stinnerced39362013-12-09 00:14:52 +0100441 Returns a tuple (release, vendor, vminfo, osinfo) with vminfo being
442 a tuple (vm_name, vm_release, vm_vendor) and osinfo being a
443 tuple (os_name, os_version, os_arch).
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000444
445 Values which cannot be determined are set to the defaults
446 given as parameters (which all default to '').
447
448 """
449 # Import the needed APIs
450 try:
451 import java.lang
Brett Cannoncd171c82013-07-04 17:43:24 -0400452 except ImportError:
Victor Stinnerced39362013-12-09 00:14:52 +0100453 return release, vendor, vminfo, osinfo
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000454
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000455 vendor = _java_getprop('java.vendor', vendor)
456 release = _java_getprop('java.version', release)
457 vm_name, vm_release, vm_vendor = vminfo
458 vm_name = _java_getprop('java.vm.name', vm_name)
459 vm_vendor = _java_getprop('java.vm.vendor', vm_vendor)
460 vm_release = _java_getprop('java.vm.version', vm_release)
461 vminfo = vm_name, vm_release, vm_vendor
462 os_name, os_version, os_arch = osinfo
463 os_arch = _java_getprop('java.os.arch', os_arch)
464 os_name = _java_getprop('java.os.name', os_name)
465 os_version = _java_getprop('java.os.version', os_version)
466 osinfo = os_name, os_version, os_arch
Tim Peters0eadaac2003-04-24 16:02:54 +0000467
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000468 return release, vendor, vminfo, osinfo
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000469
470### System name aliasing
471
Victor Stinnerced39362013-12-09 00:14:52 +0100472def system_alias(system, release, version):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000473
Victor Stinnerced39362013-12-09 00:14:52 +0100474 """ Returns (system, release, version) aliased to common
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000475 marketing names used for some systems.
476
477 It also does some reordering of the information in some cases
478 where it would otherwise cause confusion.
479
480 """
Victor Stinnerb0e08772018-12-12 17:48:08 +0100481 if system == 'SunOS':
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000482 # Sun's OS
483 if release < '5':
484 # These releases use the old name SunOS
Victor Stinnerced39362013-12-09 00:14:52 +0100485 return system, release, version
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000486 # Modify release (marketing release = SunOS release - 3)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000487 l = release.split('.')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000488 if l:
489 try:
490 major = int(l[0])
491 except ValueError:
492 pass
493 else:
494 major = major - 3
495 l[0] = str(major)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000496 release = '.'.join(l)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000497 if release < '6':
498 system = 'Solaris'
499 else:
500 # XXX Whatever the new SunOS marketing name is...
501 system = 'Solaris'
502
503 elif system == 'IRIX64':
504 # IRIX reports IRIX64 on platforms with 64-bit support; yet it
505 # is really a version and not a different platform, since 32-bit
506 # apps are also supported..
507 system = 'IRIX'
508 if version:
509 version = version + ' (64bit)'
510 else:
511 version = '64bit'
512
Victor Stinnerced39362013-12-09 00:14:52 +0100513 elif system in ('win32', 'win16'):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000514 # In case one of the other tricks
515 system = 'Windows'
516
Victor Stinnerced39362013-12-09 00:14:52 +0100517 return system, release, version
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000518
519### Various internal helpers
520
521def _platform(*args):
522
523 """ Helper to format the platform string in a filename
524 compatible format e.g. "system-version-machine".
525 """
526 # Format the platform string
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000527 platform = '-'.join(x.strip() for x in filter(len, args))
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000528
529 # Cleanup some possible filename obstacles...
Victor Stinnerced39362013-12-09 00:14:52 +0100530 platform = platform.replace(' ', '_')
531 platform = platform.replace('/', '-')
532 platform = platform.replace('\\', '-')
533 platform = platform.replace(':', '-')
534 platform = platform.replace(';', '-')
535 platform = platform.replace('"', '-')
536 platform = platform.replace('(', '-')
537 platform = platform.replace(')', '-')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000538
539 # No need to report 'unknown' information...
Victor Stinnerced39362013-12-09 00:14:52 +0100540 platform = platform.replace('unknown', '')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000541
542 # Fold '--'s and remove trailing '-'
543 while 1:
Victor Stinnerced39362013-12-09 00:14:52 +0100544 cleaned = platform.replace('--', '-')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000545 if cleaned == platform:
546 break
547 platform = cleaned
548 while platform[-1] == '-':
549 platform = platform[:-1]
550
551 return platform
552
553def _node(default=''):
554
555 """ Helper to determine the node name of this machine.
556 """
557 try:
558 import socket
Brett Cannoncd171c82013-07-04 17:43:24 -0400559 except ImportError:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000560 # No sockets...
561 return default
562 try:
563 return socket.gethostname()
Andrew Svetlov0832af62012-12-18 23:10:48 +0200564 except OSError:
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000565 # Still not working...
566 return default
567
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000568def _follow_symlinks(filepath):
569
570 """ In case filepath is a symlink, follow it until a
571 real file is reached.
572 """
Georg Brandl673f7ef2008-01-05 21:20:19 +0000573 filepath = os.path.abspath(filepath)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000574 while os.path.islink(filepath):
575 filepath = os.path.normpath(
Victor Stinnerced39362013-12-09 00:14:52 +0100576 os.path.join(os.path.dirname(filepath), os.readlink(filepath)))
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000577 return filepath
578
Victor Stinnerced39362013-12-09 00:14:52 +0100579def _syscmd_uname(option, default=''):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000580
581 """ Interface to the system's uname command.
582 """
Victor Stinnerced39362013-12-09 00:14:52 +0100583 if sys.platform in ('dos', 'win32', 'win16'):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000584 # XXX Others too ?
585 return default
Victor Stinner3a521f02018-12-07 11:10:33 +0100586
587 import subprocess
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000588 try:
Victor Stinner3a521f02018-12-07 11:10:33 +0100589 output = subprocess.check_output(('uname', option),
590 stderr=subprocess.DEVNULL,
591 text=True)
592 except (OSError, subprocess.CalledProcessError):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000593 return default
Victor Stinner3a521f02018-12-07 11:10:33 +0100594 return (output.strip() or default)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000595
Victor Stinnerced39362013-12-09 00:14:52 +0100596def _syscmd_file(target, default=''):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000597
598 """ Interface to the system's file command.
599
600 The function uses the -b option of the file command to have it
Victor Stinnerddfb2c32010-08-13 16:30:15 +0000601 omit the filename in its output. Follow the symlinks. It returns
602 default in case the command should fail.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000603
604 """
Victor Stinnerced39362013-12-09 00:14:52 +0100605 if sys.platform in ('dos', 'win32', 'win16'):
Hirokazu Yamamotod26782e2008-09-01 14:35:47 +0000606 # XXX Others too ?
607 return default
Victor Stinnerb8e689a2018-12-04 17:18:12 +0100608
609 import subprocess
Jesus Ceafc990e92012-10-04 13:51:43 +0200610 target = _follow_symlinks(target)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000611 try:
Victor Stinner3a521f02018-12-07 11:10:33 +0100612 output = subprocess.check_output(['file', target],
613 stderr=subprocess.DEVNULL,
614 encoding='latin-1')
615 except (OSError, subprocess.CalledProcessError):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000616 return default
Victor Stinner3a521f02018-12-07 11:10:33 +0100617 return (output or default)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000618
619### Information about the used architecture
620
621# Default values for architecture; non-empty strings override the
622# defaults given as parameters
623_default_architecture = {
Victor Stinnerced39362013-12-09 00:14:52 +0100624 'win32': ('', 'WindowsPE'),
625 'win16': ('', 'Windows'),
626 'dos': ('', 'MSDOS'),
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000627}
628
Victor Stinnerced39362013-12-09 00:14:52 +0100629def architecture(executable=sys.executable, bits='', linkage=''):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000630
631 """ Queries the given executable (defaults to the Python interpreter
Brett Cannon8ab27df2003-08-05 03:52:04 +0000632 binary) for various architecture information.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000633
Victor Stinnerced39362013-12-09 00:14:52 +0100634 Returns a tuple (bits, linkage) which contains information about
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000635 the bit architecture and the linkage format used for the
636 executable. Both values are returned as strings.
637
638 Values that cannot be determined are returned as given by the
639 parameter presets. If bits is given as '', the sizeof(pointer)
640 (or sizeof(long) on Python version < 1.5.2) is used as
641 indicator for the supported pointer size.
642
643 The function relies on the system's "file" command to do the
644 actual work. This is available on most if not all Unix
Brett Cannon8ab27df2003-08-05 03:52:04 +0000645 platforms. On some non-Unix platforms where the "file" command
646 does not exist and the executable is set to the Python interpreter
647 binary defaults from _default_architecture are used.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000648
649 """
650 # Use the sizeof(pointer) as default number of bits if nothing
651 # else is given as default.
652 if not bits:
653 import struct
654 try:
655 size = struct.calcsize('P')
656 except struct.error:
657 # Older installations can only query longs
658 size = struct.calcsize('l')
659 bits = str(size*8) + 'bit'
Tim Peters0eadaac2003-04-24 16:02:54 +0000660
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000661 # Get data from the 'file' system command
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000662 if executable:
Victor Stinnerddfb2c32010-08-13 16:30:15 +0000663 fileout = _syscmd_file(executable, '')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000664 else:
Victor Stinnerddfb2c32010-08-13 16:30:15 +0000665 fileout = ''
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000666
Victor Stinnerddfb2c32010-08-13 16:30:15 +0000667 if not fileout and \
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000668 executable == sys.executable:
669 # "file" command did not return anything; we'll try to provide
Tim Peters0eadaac2003-04-24 16:02:54 +0000670 # some sensible defaults then...
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000671 if sys.platform in _default_architecture:
Victor Stinnerced39362013-12-09 00:14:52 +0100672 b, l = _default_architecture[sys.platform]
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000673 if b:
674 bits = b
675 if l:
676 linkage = l
Victor Stinnerced39362013-12-09 00:14:52 +0100677 return bits, linkage
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000678
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000679 if 'executable' not in fileout:
680 # Format not supported
Victor Stinnerced39362013-12-09 00:14:52 +0100681 return bits, linkage
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000682
683 # Bits
684 if '32-bit' in fileout:
685 bits = '32bit'
686 elif 'N32' in fileout:
687 # On Irix only
688 bits = 'n32bit'
689 elif '64-bit' in fileout:
690 bits = '64bit'
691
692 # Linkage
693 if 'ELF' in fileout:
694 linkage = 'ELF'
695 elif 'PE' in fileout:
696 # E.g. Windows uses this format
697 if 'Windows' in fileout:
698 linkage = 'WindowsPE'
699 else:
700 linkage = 'PE'
701 elif 'COFF' in fileout:
702 linkage = 'COFF'
703 elif 'MS-DOS' in fileout:
704 linkage = 'MSDOS'
705 else:
706 # XXX the A.OUT format also falls under this class...
707 pass
708
Victor Stinnerced39362013-12-09 00:14:52 +0100709 return bits, linkage
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000710
711### Portable uname() interface
Tim Peters0eadaac2003-04-24 16:02:54 +0000712
Larry Hastings68386bc2012-06-24 14:30:41 -0700713uname_result = collections.namedtuple("uname_result",
714 "system node release version machine processor")
715
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000716_uname_cache = None
717
718def uname():
719
720 """ Fairly portable uname interface. Returns a tuple
Victor Stinnerced39362013-12-09 00:14:52 +0100721 of strings (system, node, release, version, machine, processor)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000722 identifying the underlying platform.
723
724 Note that unlike the os.uname function this also returns
Brett Cannon8ab27df2003-08-05 03:52:04 +0000725 possible processor information as an additional tuple entry.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000726
727 Entries which cannot be determined are set to ''.
728
729 """
730 global _uname_cache
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000731 no_os_uname = 0
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000732
733 if _uname_cache is not None:
734 return _uname_cache
735
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000736 processor = ''
737
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000738 # Get some infos from the builtin os.uname API...
739 try:
Victor Stinnerced39362013-12-09 00:14:52 +0100740 system, node, release, version, machine = os.uname()
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000741 except AttributeError:
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000742 no_os_uname = 1
743
Georg Brandl62e2ca22010-07-31 21:54:24 +0000744 if no_os_uname or not list(filter(None, (system, node, release, version, machine))):
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000745 # Hmm, no there is either no uname or uname has returned
746 #'unknowns'... we'll have to poke around the system then.
747 if no_os_uname:
748 system = sys.platform
749 release = ''
750 version = ''
751 node = _node()
752 machine = ''
753
Amaury Forgeot d'Arc7a019842008-06-17 21:42:46 +0000754 use_syscmd_ver = 1
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000755
756 # Try win32_ver() on win32 platforms
757 if system == 'win32':
Victor Stinnerced39362013-12-09 00:14:52 +0100758 release, version, csd, ptype = win32_ver()
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000759 if release and version:
760 use_syscmd_ver = 0
Christian Heimes02781dc2008-03-21 01:11:52 +0000761 # Try to use the PROCESSOR_* environment variables
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000762 # available on Win XP and later; see
763 # http://support.microsoft.com/kb/888731 and
764 # http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000765 if not machine:
R. David Murrayca2edce2010-03-22 17:48:48 +0000766 # WOW64 processes mask the native architecture
767 if "PROCESSOR_ARCHITEW6432" in os.environ:
768 machine = os.environ.get("PROCESSOR_ARCHITEW6432", '')
769 else:
770 machine = os.environ.get('PROCESSOR_ARCHITECTURE', '')
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000771 if not processor:
772 processor = os.environ.get('PROCESSOR_IDENTIFIER', machine)
Tim Peters0eadaac2003-04-24 16:02:54 +0000773
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000774 # Try the 'ver' system command available on some
775 # platforms
776 if use_syscmd_ver:
Victor Stinnerced39362013-12-09 00:14:52 +0100777 system, release, version = _syscmd_ver(system)
Marc-André Lemburgcdc79232004-06-19 17:17:00 +0000778 # Normalize system to what win32_ver() normally returns
779 # (_syscmd_ver() tends to return the vendor name as well)
780 if system == 'Microsoft Windows':
781 system = 'Windows'
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000782 elif system == 'Microsoft' and release == 'Windows':
783 # Under Windows Vista and Windows Server 2008,
784 # Microsoft changed the output of the ver command. The
785 # release is no longer printed. This causes the
786 # system and release to be misidentified.
787 system = 'Windows'
788 if '6.0' == version[:3]:
789 release = 'Vista'
790 else:
791 release = ''
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000792
793 # In case we still don't know anything useful, we'll try to
794 # help ourselves
Victor Stinnerced39362013-12-09 00:14:52 +0100795 if system in ('win32', 'win16'):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000796 if not version:
797 if system == 'win32':
798 version = '32bit'
799 else:
800 version = '16bit'
801 system = 'Windows'
802
803 elif system[:4] == 'java':
Victor Stinnerced39362013-12-09 00:14:52 +0100804 release, vendor, vminfo, osinfo = java_ver()
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000805 system = 'Java'
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000806 version = ', '.join(vminfo)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000807 if not version:
808 version = vendor
809
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000810 # System specific extensions
811 if system == 'OpenVMS':
812 # OpenVMS seems to have release and version mixed up
813 if not release or release == '0':
814 release = version
815 version = ''
816 # Get processor information
817 try:
818 import vms_lib
Brett Cannoncd171c82013-07-04 17:43:24 -0400819 except ImportError:
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000820 pass
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000821 else:
Victor Stinnerced39362013-12-09 00:14:52 +0100822 csid, cpu_number = vms_lib.getsyi('SYI$_CPU', 0)
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000823 if (cpu_number >= 128):
824 processor = 'Alpha'
825 else:
826 processor = 'VAX'
827 if not processor:
828 # Get processor information from the uname system command
Victor Stinnerced39362013-12-09 00:14:52 +0100829 processor = _syscmd_uname('-p', '')
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000830
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000831 #If any unknowns still exist, replace them with ''s, which are more portable
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000832 if system == 'unknown':
833 system = ''
834 if node == 'unknown':
835 node = ''
836 if release == 'unknown':
837 release = ''
838 if version == 'unknown':
839 version = ''
840 if machine == 'unknown':
841 machine = ''
842 if processor == 'unknown':
843 processor = ''
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000844
845 # normalize name
846 if system == 'Microsoft' and release == 'Windows':
847 system = 'Windows'
848 release = 'Vista'
849
Victor Stinnerced39362013-12-09 00:14:52 +0100850 _uname_cache = uname_result(system, node, release, version,
851 machine, processor)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000852 return _uname_cache
853
854### Direct interfaces to some of the uname() return values
855
856def system():
857
858 """ Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.
859
860 An empty string is returned if the value cannot be determined.
861
862 """
Larry Hastings68386bc2012-06-24 14:30:41 -0700863 return uname().system
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000864
865def node():
866
Brett Cannon8ab27df2003-08-05 03:52:04 +0000867 """ Returns the computer's network name (which may not be fully
868 qualified)
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000869
870 An empty string is returned if the value cannot be determined.
871
872 """
Larry Hastings68386bc2012-06-24 14:30:41 -0700873 return uname().node
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000874
875def release():
876
877 """ Returns the system's release, e.g. '2.2.0' or 'NT'
878
879 An empty string is returned if the value cannot be determined.
880
881 """
Larry Hastings68386bc2012-06-24 14:30:41 -0700882 return uname().release
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000883
884def version():
885
886 """ Returns the system's release version, e.g. '#3 on degas'
887
888 An empty string is returned if the value cannot be determined.
889
890 """
Larry Hastings68386bc2012-06-24 14:30:41 -0700891 return uname().version
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000892
893def machine():
894
895 """ Returns the machine type, e.g. 'i386'
896
897 An empty string is returned if the value cannot be determined.
898
899 """
Larry Hastings68386bc2012-06-24 14:30:41 -0700900 return uname().machine
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000901
902def processor():
903
904 """ Returns the (true) processor name, e.g. 'amdk6'
905
906 An empty string is returned if the value cannot be
907 determined. Note that many platforms do not provide this
908 information or simply return the same value as for machine(),
909 e.g. NetBSD does this.
910
911 """
Larry Hastings68386bc2012-06-24 14:30:41 -0700912 return uname().processor
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000913
914### Various APIs for extracting information from sys.version
915
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000916_sys_version_parser = re.compile(
Martin Panter4e505532016-06-08 06:12:22 +0000917 r'([\w.+]+)\s*' # "version<space>"
918 r'\(#?([^,]+)' # "(#buildno"
919 r'(?:,\s*([\w ]*)' # ", builddate"
920 r'(?:,\s*([\w :]*))?)?\)\s*' # ", buildtime)<space>"
921 r'\[([^\]]+)\]?', re.ASCII) # "[compiler]"
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000922
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000923_ironpython_sys_version_parser = re.compile(
924 r'IronPython\s*'
R David Murray44b548d2016-09-08 13:59:53 -0400925 r'([\d\.]+)'
926 r'(?: \(([\d\.]+)\))?'
927 r' on (.NET [\d\.]+)', re.ASCII)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000928
Ezio Melottif076f532013-10-21 03:03:32 +0300929# IronPython covering 2.6 and 2.7
930_ironpython26_sys_version_parser = re.compile(
931 r'([\d.]+)\s*'
R David Murray44b548d2016-09-08 13:59:53 -0400932 r'\(IronPython\s*'
933 r'[\d.]+\s*'
934 r'\(([\d.]+)\) on ([\w.]+ [\d.]+(?: \(\d+-bit\))?)\)'
Ezio Melottif076f532013-10-21 03:03:32 +0300935)
936
Benjamin Petersone549ead2009-03-28 21:42:05 +0000937_pypy_sys_version_parser = re.compile(
938 r'([\w.+]+)\s*'
R David Murray44b548d2016-09-08 13:59:53 -0400939 r'\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*'
940 r'\[PyPy [^\]]+\]?')
Benjamin Petersone549ead2009-03-28 21:42:05 +0000941
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000942_sys_version_cache = {}
943
944def _sys_version(sys_version=None):
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000945
946 """ Returns a parsed version of Python's sys.version as tuple
Benjamin Peterson5f28b7b2009-03-26 21:49:58 +0000947 (name, version, branch, revision, buildno, builddate, compiler)
948 referring to the Python implementation name, version, branch,
949 revision, build number, build date/time as string and the compiler
950 identification string.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000951
952 Note that unlike the Python sys.version, the returned value
953 for the Python version will always include the patchlevel (it
954 defaults to '.0').
955
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000956 The function returns empty strings for tuple entries that
957 cannot be determined.
Marc-André Lemburg246d8472003-04-24 11:36:11 +0000958
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000959 sys_version may be given to parse an alternative version
960 string, e.g. if the version was read from a different Python
961 interpreter.
962
963 """
964 # Get the Python version
965 if sys_version is None:
966 sys_version = sys.version
967
968 # Try the cache first
969 result = _sys_version_cache.get(sys_version, None)
970 if result is not None:
971 return result
972
973 # Parse it
Ezio Melottif076f532013-10-21 03:03:32 +0300974 if 'IronPython' in sys_version:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000975 # IronPython
976 name = 'IronPython'
Ezio Melottif076f532013-10-21 03:03:32 +0300977 if sys_version.startswith('IronPython'):
978 match = _ironpython_sys_version_parser.match(sys_version)
979 else:
980 match = _ironpython26_sys_version_parser.match(sys_version)
981
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000982 if match is None:
983 raise ValueError(
984 'failed to parse IronPython sys.version: %s' %
985 repr(sys_version))
Ezio Melottif076f532013-10-21 03:03:32 +0300986
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000987 version, alt_version, compiler = match.groups()
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000988 buildno = ''
989 builddate = ''
990
Ezio Melottif076f532013-10-21 03:03:32 +0300991 elif sys.platform.startswith('java'):
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000992 # Jython
993 name = 'Jython'
Benjamin Petersone549ead2009-03-28 21:42:05 +0000994 match = _sys_version_parser.match(sys_version)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000995 if match is None:
996 raise ValueError(
997 'failed to parse Jython sys.version: %s' %
998 repr(sys_version))
Benjamin Petersone549ead2009-03-28 21:42:05 +0000999 version, buildno, builddate, buildtime, _ = match.groups()
Martin Panter4e505532016-06-08 06:12:22 +00001000 if builddate is None:
1001 builddate = ''
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001002 compiler = sys.platform
Benjamin Petersone549ead2009-03-28 21:42:05 +00001003
1004 elif "PyPy" in sys_version:
1005 # PyPy
1006 name = "PyPy"
1007 match = _pypy_sys_version_parser.match(sys_version)
1008 if match is None:
1009 raise ValueError("failed to parse PyPy sys.version: %s" %
1010 repr(sys_version))
1011 version, buildno, builddate, buildtime = match.groups()
1012 compiler = ""
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001013
1014 else:
1015 # CPython
1016 match = _sys_version_parser.match(sys_version)
1017 if match is None:
1018 raise ValueError(
1019 'failed to parse CPython sys.version: %s' %
1020 repr(sys_version))
1021 version, buildno, builddate, buildtime, compiler = \
1022 match.groups()
Benjamin Petersone549ead2009-03-28 21:42:05 +00001023 name = 'CPython'
Martin Panter4e505532016-06-08 06:12:22 +00001024 if builddate is None:
1025 builddate = ''
1026 elif buildtime:
1027 builddate = builddate + ' ' + buildtime
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001028
Ned Deily5c4b0d02017-03-04 00:19:55 -05001029 if hasattr(sys, '_git'):
1030 _, branch, revision = sys._git
1031 elif hasattr(sys, '_mercurial'):
Georg Brandl82562422011-03-05 21:09:22 +01001032 _, branch, revision = sys._mercurial
Benjamin Petersone549ead2009-03-28 21:42:05 +00001033 else:
1034 branch = ''
1035 revision = ''
1036
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001037 # Add the patchlevel version if missing
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001038 l = version.split('.')
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001039 if len(l) == 2:
1040 l.append('0')
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001041 version = '.'.join(l)
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001042
1043 # Build and cache the result
1044 result = (name, version, branch, revision, buildno, builddate, compiler)
1045 _sys_version_cache[sys_version] = result
1046 return result
1047
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001048def python_implementation():
1049
1050 """ Returns a string identifying the Python implementation.
1051
1052 Currently, the following implementations are identified:
Ezio Melottif16898b2011-05-04 18:37:50 +03001053 'CPython' (C implementation of Python),
1054 'IronPython' (.NET implementation of Python),
1055 'Jython' (Java implementation of Python),
1056 'PyPy' (Python implementation of Python).
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001057
1058 """
1059 return _sys_version()[0]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001060
1061def python_version():
1062
1063 """ Returns the Python version as string 'major.minor.patchlevel'
1064
1065 Note that unlike the Python sys.version, the returned value
1066 will always include the patchlevel (it defaults to 0).
1067
1068 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001069 return _sys_version()[1]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001070
1071def python_version_tuple():
1072
1073 """ Returns the Python version as tuple (major, minor, patchlevel)
1074 of strings.
1075
1076 Note that unlike the Python sys.version, the returned value
1077 will always include the patchlevel (it defaults to 0).
1078
1079 """
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001080 return tuple(_sys_version()[1].split('.'))
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001081
1082def python_branch():
1083
1084 """ Returns a string identifying the Python implementation
1085 branch.
1086
Victor Stinnerfe2d5ba2017-11-28 22:29:32 +01001087 For CPython this is the SCM branch from which the
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001088 Python binary was built.
1089
1090 If not available, an empty string is returned.
1091
1092 """
Thomas Wouters9fe394c2007-02-05 01:24:16 +00001093
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001094 return _sys_version()[2]
1095
1096def python_revision():
1097
1098 """ Returns a string identifying the Python implementation
1099 revision.
1100
Victor Stinnerfe2d5ba2017-11-28 22:29:32 +01001101 For CPython this is the SCM revision from which the
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001102 Python binary was built.
1103
1104 If not available, an empty string is returned.
1105
1106 """
1107 return _sys_version()[3]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001108
1109def python_build():
1110
1111 """ Returns a tuple (buildno, builddate) stating the Python
1112 build number and date as strings.
1113
1114 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001115 return _sys_version()[4:6]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001116
1117def python_compiler():
1118
1119 """ Returns a string identifying the compiler used for compiling
1120 Python.
1121
1122 """
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001123 return _sys_version()[6]
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001124
1125### The Opus Magnum of platform strings :-)
1126
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001127_platform_cache = {}
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001128
1129def platform(aliased=0, terse=0):
1130
1131 """ Returns a single string identifying the underlying platform
1132 with as much useful information as possible (but no more :).
Tim Peters0eadaac2003-04-24 16:02:54 +00001133
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001134 The output is intended to be human readable rather than
1135 machine parseable. It may look different on different
1136 platforms and this is intended.
1137
1138 If "aliased" is true, the function will use aliases for
1139 various platforms that report system names which differ from
1140 their common names, e.g. SunOS will be reported as
1141 Solaris. The system_alias() function is used to implement
1142 this.
1143
1144 Setting terse to true causes the function to return only the
1145 absolute minimum information needed to identify the platform.
1146
1147 """
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001148 result = _platform_cache.get((aliased, terse), None)
1149 if result is not None:
1150 return result
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001151
1152 # Get uname information and then apply platform specific cosmetics
1153 # to it...
Victor Stinnerced39362013-12-09 00:14:52 +01001154 system, node, release, version, machine, processor = uname()
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001155 if machine == processor:
1156 processor = ''
1157 if aliased:
Victor Stinnerced39362013-12-09 00:14:52 +01001158 system, release, version = system_alias(system, release, version)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001159
Victor Stinnerea0ca212018-12-05 22:41:52 +01001160 if system == 'Darwin':
1161 # macOS (darwin kernel)
1162 macos_release = mac_ver()[0]
1163 if macos_release:
Victor Stinnerea0ca212018-12-05 22:41:52 +01001164 system = 'macOS'
1165 release = macos_release
1166
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001167 if system == 'Windows':
1168 # MS platforms
Victor Stinnerced39362013-12-09 00:14:52 +01001169 rel, vers, csd, ptype = win32_ver(version)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001170 if terse:
Victor Stinnerced39362013-12-09 00:14:52 +01001171 platform = _platform(system, release)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001172 else:
Victor Stinnerced39362013-12-09 00:14:52 +01001173 platform = _platform(system, release, version, csd)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001174
1175 elif system in ('Linux',):
Petr Viktorin8b94b412018-05-16 11:51:18 -04001176 # check for libc vs. glibc
1177 libcname, libcversion = libc_ver(sys.executable)
1178 platform = _platform(system, release, machine, processor,
1179 'with',
1180 libcname+libcversion)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001181 elif system == 'Java':
1182 # Java platforms
Victor Stinnerced39362013-12-09 00:14:52 +01001183 r, v, vminfo, (os_name, os_version, os_arch) = java_ver()
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001184 if terse or not os_name:
Victor Stinnerced39362013-12-09 00:14:52 +01001185 platform = _platform(system, release, version)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001186 else:
Victor Stinnerced39362013-12-09 00:14:52 +01001187 platform = _platform(system, release, version,
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001188 'on',
Victor Stinnerced39362013-12-09 00:14:52 +01001189 os_name, os_version, os_arch)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001190
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001191 else:
1192 # Generic handler
1193 if terse:
Victor Stinnerced39362013-12-09 00:14:52 +01001194 platform = _platform(system, release)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001195 else:
Victor Stinnerced39362013-12-09 00:14:52 +01001196 bits, linkage = architecture(sys.executable)
1197 platform = _platform(system, release, machine,
1198 processor, bits, linkage)
Tim Peters0eadaac2003-04-24 16:02:54 +00001199
Marc-André Lemburg91e83e22004-03-25 18:35:12 +00001200 _platform_cache[(aliased, terse)] = platform
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001201 return platform
1202
1203### Command line interface
1204
1205if __name__ == '__main__':
1206 # Default is to print the aliased verbose platform string
Tim Peters0eadaac2003-04-24 16:02:54 +00001207 terse = ('terse' in sys.argv or '--terse' in sys.argv)
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001208 aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv)
Victor Stinnerced39362013-12-09 00:14:52 +01001209 print(platform(aliased, terse))
Marc-André Lemburg246d8472003-04-24 11:36:11 +00001210 sys.exit(0)