Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 2 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 3 | """ This module tries to retrieve as much platform-identifying data as |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 4 | 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 Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 13 | # Python bug tracker (http://bugs.python.org) and assign them to "lemburg". |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 14 | # |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 15 | # Still needed: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 16 | # * support for MS-DOS (PythonDX ?) |
| 17 | # * support for Amiga and other still unsupported platforms running Python |
| 18 | # * support for additional Linux distributions |
| 19 | # |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 20 | # Many thanks to all those who helped adding platform-specific |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 21 | # 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 Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 28 | # Colin Kong, Trent Mick, Guido van Rossum, Anthony Baxter, Steve |
| 29 | # Dower |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 30 | # |
| 31 | # History: |
Marc-André Lemburg | 380f417 | 2005-11-07 16:11:02 +0000 | [diff] [blame] | 32 | # |
| 33 | # <see CVS and SVN checkin messages for history> |
| 34 | # |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 35 | # 1.0.8 - changed Windows support to read version from kernel32.dll |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 36 | # 1.0.7 - added DEV_NULL |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 37 | # 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é Lemburg | cdc7923 | 2004-06-19 17:17:00 +0000 | [diff] [blame] | 40 | # 1.0.3 - added normalization of Windows system name |
Marc-André Lemburg | 91e83e2 | 2004-03-25 18:35:12 +0000 | [diff] [blame] | 41 | # 1.0.2 - added more Windows support |
Marc-André Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 42 | # 1.0.1 - reformatted to make doc.py happy |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 43 | # 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 Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 63 | # 'unknown' instead of just leaving the field empty) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 64 | # 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 Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 94 | Copyright (c) 2000-2010, eGenix.com Software GmbH; mailto:info@egenix.com |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 95 | |
| 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 Bussonnier | 6059ce4 | 2017-02-24 02:47:34 -0800 | [diff] [blame] | 113 | __version__ = '1.0.8' |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 114 | |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 115 | import collections |
Victor Stinner | b8e689a | 2018-12-04 17:18:12 +0100 | [diff] [blame] | 116 | import os |
| 117 | import re |
| 118 | import sys |
Berker Peksag | 1392f71 | 2015-05-16 20:24:28 +0300 | [diff] [blame] | 119 | |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 120 | ### Globals & Constants |
| 121 | |
Serhiy Storchaka | 7d81e8f | 2018-08-27 13:29:51 +0300 | [diff] [blame] | 122 | # 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 | |
| 139 | def _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é Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 151 | ### Platform specific APIs |
| 152 | |
Antoine Pitrou | ba7c226 | 2011-10-07 13:26:59 +0200 | [diff] [blame] | 153 | _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é Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 158 | |
Victor Stinner | 476b113 | 2018-12-05 14:04:52 +0100 | [diff] [blame] | 159 | def libc_ver(executable=None, lib='', version='', chunksize=16384): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 160 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 161 | """ Tries to determine the libc version that the file executable |
| 162 | (which defaults to the Python interpreter) is linked against. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 163 | |
| 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 Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 168 | libc versions add symbols to the executable and thus is probably |
| 169 | only useable for executables compiled using gcc. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 170 | |
| 171 | The file is read and scanned in chunks of chunksize bytes. |
| 172 | |
| 173 | """ |
Victor Stinner | 476b113 | 2018-12-05 14:04:52 +0100 | [diff] [blame] | 174 | 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 Storchaka | 7d81e8f | 2018-08-27 13:29:51 +0300 | [diff] [blame] | 187 | V = _comparable_version |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 188 | 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 Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 193 | with open(executable, 'rb') as f: |
| 194 | binary = f.read(chunksize) |
| 195 | pos = 0 |
Serhiy Storchaka | 2a9b8ba | 2018-07-09 11:47:45 +0300 | [diff] [blame] | 196 | while pos < len(binary): |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 197 | if b'libc' in binary or b'GLIBC' in binary: |
| 198 | m = _libc_search.search(binary, pos) |
| 199 | else: |
| 200 | m = None |
Serhiy Storchaka | 2a9b8ba | 2018-07-09 11:47:45 +0300 | [diff] [blame] | 201 | 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 Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 208 | break |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 209 | 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é Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 213 | lib = 'libc' |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 214 | elif glibc: |
| 215 | if lib != 'glibc': |
| 216 | lib = 'glibc' |
| 217 | version = glibcversion |
Serhiy Storchaka | 2a9b8ba | 2018-07-09 11:47:45 +0300 | [diff] [blame] | 218 | elif V(glibcversion) > V(version): |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 219 | version = glibcversion |
| 220 | elif so: |
| 221 | if lib != 'glibc': |
| 222 | lib = 'libc' |
Serhiy Storchaka | 2a9b8ba | 2018-07-09 11:47:45 +0300 | [diff] [blame] | 223 | if soversion and (not version or V(soversion) > V(version)): |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 224 | version = soversion |
| 225 | if threads and version[-len(threads):] != threads: |
| 226 | version = version + threads |
| 227 | pos = m.end() |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 228 | return lib, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 229 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 230 | def _norm_version(version, build=''): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 231 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 232 | """ Normalize the version and build strings and return a single |
Walter Dörwald | e5a7fad | 2005-11-21 17:01:27 +0000 | [diff] [blame] | 233 | version string using the format major.minor.build (or patchlevel). |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 234 | """ |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 235 | l = version.split('.') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 236 | if build: |
| 237 | l.append(build) |
| 238 | try: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 239 | ints = map(int, l) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 240 | except ValueError: |
| 241 | strings = l |
| 242 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 243 | strings = list(map(str, ints)) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 244 | version = '.'.join(strings[:3]) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 245 | return version |
| 246 | |
Marc-André Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 247 | _ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) ' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 248 | r'.*' |
| 249 | r'\[.* ([\d.]+)\])') |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 250 | |
| 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é Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 259 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 260 | def _syscmd_ver(system='', release='', version='', |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 261 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 262 | supported_platforms=('win32', 'win16', 'dos')): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 263 | |
| 264 | """ Tries to figure out the OS version used and returns |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 265 | a tuple (system, release, version). |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 266 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 267 | It uses the "ver" shell command for this which is known |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 268 | to exists on Windows, DOS. XXX Others too ? |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 269 | |
| 270 | In case this fails, the given parameters are used as |
| 271 | defaults. |
| 272 | |
| 273 | """ |
| 274 | if sys.platform not in supported_platforms: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 275 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 276 | |
| 277 | # Try some common cmd strings |
Victor Stinner | 3a521f0 | 2018-12-07 11:10:33 +0100 | [diff] [blame] | 278 | import subprocess |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 279 | for cmd in ('ver', 'command /c ver', 'cmd /c ver'): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 280 | try: |
Victor Stinner | 3a521f0 | 2018-12-07 11:10:33 +0100 | [diff] [blame] | 281 | 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é Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 287 | continue |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 288 | else: |
| 289 | break |
| 290 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 291 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 292 | |
| 293 | # Parse the output |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 294 | info = info.strip() |
Marc-André Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 295 | m = _ver_output.match(info) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 296 | if m is not None: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 297 | system, release, version = m.groups() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 298 | # 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 Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 306 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 307 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 308 | _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é Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 315 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 316 | (6, 0): "Vista", |
| 317 | (6, 1): "7", |
| 318 | (6, 2): "8", |
| 319 | (6, 3): "8.1", |
| 320 | (6, None): "post8.1", |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 321 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 322 | (10, 0): "10", |
| 323 | (10, None): "post10", |
| 324 | } |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 325 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 326 | # 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 Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 337 | def win32_ver(release='', version='', csd='', ptype=''): |
Steve Dower | 8f278f1 | 2015-09-22 17:35:24 -0700 | [diff] [blame] | 338 | try: |
| 339 | from sys import getwindowsversion |
| 340 | except ImportError: |
| 341 | return release, version, csd, ptype |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 342 | try: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 343 | from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 344 | except ImportError: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 345 | from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE |
| 346 | |
| 347 | winver = getwindowsversion() |
Steve Dower | 74f4af7 | 2016-09-17 17:27:48 -0700 | [diff] [blame] | 348 | maj, min, build = winver.platform_version or winver[:3] |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 349 | 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 Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 359 | try: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 360 | csd = 'SP{}'.format(winver.service_pack_major) |
Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 361 | except AttributeError: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 362 | if csd[:13] == 'Service Pack ': |
| 363 | csd = 'SP' + csd[13:] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 364 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 365 | # VER_NT_SERVER = 3 |
Steve Dower | 41519b2 | 2016-09-09 09:46:56 -0700 | [diff] [blame] | 366 | if getattr(winver, 'product_type', None) == 3: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 367 | release = (_WIN32_SERVER_RELEASES.get((maj, min)) or |
| 368 | _WIN32_SERVER_RELEASES.get((maj, None)) or |
| 369 | release) |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 370 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 371 | key = None |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 372 | try: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 373 | key = OpenKeyEx(HKEY_LOCAL_MACHINE, |
| 374 | r'SOFTWARE\Microsoft\Windows NT\CurrentVersion') |
| 375 | ptype = QueryValueEx(key, 'CurrentType')[0] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 376 | except: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 377 | pass |
| 378 | finally: |
| 379 | if key: |
| 380 | CloseKey(key) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 381 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 382 | return release, version, csd, ptype |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 383 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 384 | |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 385 | def _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 Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 392 | except ImportError: |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 393 | return None |
| 394 | |
Ned Deily | 936dfae | 2014-01-13 11:34:19 -0800 | [diff] [blame] | 395 | with open(fn, 'rb') as f: |
| 396 | pl = plistlib.load(f) |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 397 | release = pl['ProductVersion'] |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 398 | versioninfo = ('', '', '') |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 399 | machine = os.uname().machine |
Ronald Oussoren | fcd7701 | 2010-08-03 07:42:42 +0000 | [diff] [blame] | 400 | if machine in ('ppc', 'Power Macintosh'): |
Ezio Melotti | 9a3777e | 2013-08-17 15:53:55 +0300 | [diff] [blame] | 401 | # Canonical name |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 402 | machine = 'PowerPC' |
| 403 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 404 | return release, versioninfo, machine |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 405 | |
| 406 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 407 | def mac_ver(release='', versioninfo=('', '', ''), machine=''): |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 408 | |
Victor Stinner | b0e0877 | 2018-12-12 17:48:08 +0100 | [diff] [blame] | 409 | """ Get macOS version information and return it as tuple (release, |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 410 | versioninfo, machine) with versioninfo being a tuple (version, |
| 411 | dev_stage, non_release_version). |
| 412 | |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 413 | Entries which cannot be determined are set to the parameter values |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 414 | 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 Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 423 | # If that also doesn't work return the default values |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 424 | return release, versioninfo, machine |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 425 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 426 | def _java_getprop(name, default): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 427 | |
| 428 | from java.lang import System |
| 429 | try: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 430 | value = System.getProperty(name) |
| 431 | if value is None: |
| 432 | return default |
| 433 | return value |
| 434 | except AttributeError: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 435 | return default |
| 436 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 437 | def java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', '')): |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 438 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 439 | """ Version interface for Jython. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 440 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 441 | 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é Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 444 | |
| 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 Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 452 | except ImportError: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 453 | return release, vendor, vminfo, osinfo |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 454 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 455 | 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 Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 467 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 468 | return release, vendor, vminfo, osinfo |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 469 | |
| 470 | ### System name aliasing |
| 471 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 472 | def system_alias(system, release, version): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 473 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 474 | """ Returns (system, release, version) aliased to common |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 475 | 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 Stinner | b0e0877 | 2018-12-12 17:48:08 +0100 | [diff] [blame] | 481 | if system == 'SunOS': |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 482 | # Sun's OS |
| 483 | if release < '5': |
| 484 | # These releases use the old name SunOS |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 485 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 486 | # Modify release (marketing release = SunOS release - 3) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 487 | l = release.split('.') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 488 | 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 Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 496 | release = '.'.join(l) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 497 | 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 Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 513 | elif system in ('win32', 'win16'): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 514 | # In case one of the other tricks |
| 515 | system = 'Windows' |
| 516 | |
Victor Stinner | 60875db | 2018-12-18 19:51:35 +0100 | [diff] [blame] | 517 | # bpo-35516: Don't replace Darwin with macOS since input release and |
| 518 | # version arguments can be different than the currently running version. |
| 519 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 520 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 521 | |
| 522 | ### Various internal helpers |
| 523 | |
| 524 | def _platform(*args): |
| 525 | |
| 526 | """ Helper to format the platform string in a filename |
| 527 | compatible format e.g. "system-version-machine". |
| 528 | """ |
| 529 | # Format the platform string |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 530 | platform = '-'.join(x.strip() for x in filter(len, args)) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 531 | |
| 532 | # Cleanup some possible filename obstacles... |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 533 | platform = platform.replace(' ', '_') |
| 534 | platform = platform.replace('/', '-') |
| 535 | platform = platform.replace('\\', '-') |
| 536 | platform = platform.replace(':', '-') |
| 537 | platform = platform.replace(';', '-') |
| 538 | platform = platform.replace('"', '-') |
| 539 | platform = platform.replace('(', '-') |
| 540 | platform = platform.replace(')', '-') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 541 | |
| 542 | # No need to report 'unknown' information... |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 543 | platform = platform.replace('unknown', '') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 544 | |
| 545 | # Fold '--'s and remove trailing '-' |
| 546 | while 1: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 547 | cleaned = platform.replace('--', '-') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 548 | if cleaned == platform: |
| 549 | break |
| 550 | platform = cleaned |
| 551 | while platform[-1] == '-': |
| 552 | platform = platform[:-1] |
| 553 | |
| 554 | return platform |
| 555 | |
| 556 | def _node(default=''): |
| 557 | |
| 558 | """ Helper to determine the node name of this machine. |
| 559 | """ |
| 560 | try: |
| 561 | import socket |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 562 | except ImportError: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 563 | # No sockets... |
| 564 | return default |
| 565 | try: |
| 566 | return socket.gethostname() |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 567 | except OSError: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 568 | # Still not working... |
| 569 | return default |
| 570 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 571 | def _follow_symlinks(filepath): |
| 572 | |
| 573 | """ In case filepath is a symlink, follow it until a |
| 574 | real file is reached. |
| 575 | """ |
Georg Brandl | 673f7ef | 2008-01-05 21:20:19 +0000 | [diff] [blame] | 576 | filepath = os.path.abspath(filepath) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 577 | while os.path.islink(filepath): |
| 578 | filepath = os.path.normpath( |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 579 | os.path.join(os.path.dirname(filepath), os.readlink(filepath))) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 580 | return filepath |
| 581 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 582 | def _syscmd_uname(option, default=''): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 583 | |
| 584 | """ Interface to the system's uname command. |
| 585 | """ |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 586 | if sys.platform in ('dos', 'win32', 'win16'): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 587 | # XXX Others too ? |
| 588 | return default |
Victor Stinner | 3a521f0 | 2018-12-07 11:10:33 +0100 | [diff] [blame] | 589 | |
| 590 | import subprocess |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 591 | try: |
Victor Stinner | 3a521f0 | 2018-12-07 11:10:33 +0100 | [diff] [blame] | 592 | output = subprocess.check_output(('uname', option), |
| 593 | stderr=subprocess.DEVNULL, |
| 594 | text=True) |
| 595 | except (OSError, subprocess.CalledProcessError): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 596 | return default |
Victor Stinner | 3a521f0 | 2018-12-07 11:10:33 +0100 | [diff] [blame] | 597 | return (output.strip() or default) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 598 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 599 | def _syscmd_file(target, default=''): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 600 | |
| 601 | """ Interface to the system's file command. |
| 602 | |
| 603 | The function uses the -b option of the file command to have it |
Victor Stinner | ddfb2c3 | 2010-08-13 16:30:15 +0000 | [diff] [blame] | 604 | omit the filename in its output. Follow the symlinks. It returns |
| 605 | default in case the command should fail. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 606 | |
| 607 | """ |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 608 | if sys.platform in ('dos', 'win32', 'win16'): |
Hirokazu Yamamoto | d26782e | 2008-09-01 14:35:47 +0000 | [diff] [blame] | 609 | # XXX Others too ? |
| 610 | return default |
Victor Stinner | b8e689a | 2018-12-04 17:18:12 +0100 | [diff] [blame] | 611 | |
| 612 | import subprocess |
Jesus Cea | fc990e9 | 2012-10-04 13:51:43 +0200 | [diff] [blame] | 613 | target = _follow_symlinks(target) |
Victor Stinner | 0af9c33 | 2018-12-17 18:47:24 +0100 | [diff] [blame] | 614 | # "file" output is locale dependent: force the usage of the C locale |
| 615 | # to get deterministic behavior. |
| 616 | env = dict(os.environ, LC_ALL='C') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 617 | try: |
Victor Stinner | 0af9c33 | 2018-12-17 18:47:24 +0100 | [diff] [blame] | 618 | # -b: do not prepend filenames to output lines (brief mode) |
| 619 | output = subprocess.check_output(['file', '-b', target], |
Victor Stinner | 3a521f0 | 2018-12-07 11:10:33 +0100 | [diff] [blame] | 620 | stderr=subprocess.DEVNULL, |
Victor Stinner | 0af9c33 | 2018-12-17 18:47:24 +0100 | [diff] [blame] | 621 | env=env) |
Victor Stinner | 3a521f0 | 2018-12-07 11:10:33 +0100 | [diff] [blame] | 622 | except (OSError, subprocess.CalledProcessError): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 623 | return default |
Victor Stinner | 0af9c33 | 2018-12-17 18:47:24 +0100 | [diff] [blame] | 624 | if not output: |
| 625 | return default |
| 626 | # With the C locale, the output should be mostly ASCII-compatible. |
| 627 | # Decode from Latin-1 to prevent Unicode decode error. |
| 628 | return output.decode('latin-1') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 629 | |
| 630 | ### Information about the used architecture |
| 631 | |
| 632 | # Default values for architecture; non-empty strings override the |
| 633 | # defaults given as parameters |
| 634 | _default_architecture = { |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 635 | 'win32': ('', 'WindowsPE'), |
| 636 | 'win16': ('', 'Windows'), |
| 637 | 'dos': ('', 'MSDOS'), |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 640 | def architecture(executable=sys.executable, bits='', linkage=''): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 641 | |
| 642 | """ Queries the given executable (defaults to the Python interpreter |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 643 | binary) for various architecture information. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 644 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 645 | Returns a tuple (bits, linkage) which contains information about |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 646 | the bit architecture and the linkage format used for the |
| 647 | executable. Both values are returned as strings. |
| 648 | |
| 649 | Values that cannot be determined are returned as given by the |
| 650 | parameter presets. If bits is given as '', the sizeof(pointer) |
| 651 | (or sizeof(long) on Python version < 1.5.2) is used as |
| 652 | indicator for the supported pointer size. |
| 653 | |
| 654 | The function relies on the system's "file" command to do the |
| 655 | actual work. This is available on most if not all Unix |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 656 | platforms. On some non-Unix platforms where the "file" command |
| 657 | does not exist and the executable is set to the Python interpreter |
| 658 | binary defaults from _default_architecture are used. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 659 | |
| 660 | """ |
| 661 | # Use the sizeof(pointer) as default number of bits if nothing |
| 662 | # else is given as default. |
| 663 | if not bits: |
| 664 | import struct |
Victor Stinner | 4aa917c | 2018-12-14 13:14:10 +0100 | [diff] [blame] | 665 | size = struct.calcsize('P') |
| 666 | bits = str(size * 8) + 'bit' |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 667 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 668 | # Get data from the 'file' system command |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 669 | if executable: |
Victor Stinner | ddfb2c3 | 2010-08-13 16:30:15 +0000 | [diff] [blame] | 670 | fileout = _syscmd_file(executable, '') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 671 | else: |
Victor Stinner | ddfb2c3 | 2010-08-13 16:30:15 +0000 | [diff] [blame] | 672 | fileout = '' |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 673 | |
Victor Stinner | ddfb2c3 | 2010-08-13 16:30:15 +0000 | [diff] [blame] | 674 | if not fileout and \ |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 675 | executable == sys.executable: |
| 676 | # "file" command did not return anything; we'll try to provide |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 677 | # some sensible defaults then... |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 678 | if sys.platform in _default_architecture: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 679 | b, l = _default_architecture[sys.platform] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 680 | if b: |
| 681 | bits = b |
| 682 | if l: |
| 683 | linkage = l |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 684 | return bits, linkage |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 685 | |
Victor Stinner | 0af9c33 | 2018-12-17 18:47:24 +0100 | [diff] [blame] | 686 | if 'executable' not in fileout and 'shared object' not in fileout: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 687 | # Format not supported |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 688 | return bits, linkage |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 689 | |
| 690 | # Bits |
| 691 | if '32-bit' in fileout: |
| 692 | bits = '32bit' |
| 693 | elif 'N32' in fileout: |
| 694 | # On Irix only |
| 695 | bits = 'n32bit' |
| 696 | elif '64-bit' in fileout: |
| 697 | bits = '64bit' |
| 698 | |
| 699 | # Linkage |
| 700 | if 'ELF' in fileout: |
| 701 | linkage = 'ELF' |
| 702 | elif 'PE' in fileout: |
| 703 | # E.g. Windows uses this format |
| 704 | if 'Windows' in fileout: |
| 705 | linkage = 'WindowsPE' |
| 706 | else: |
| 707 | linkage = 'PE' |
| 708 | elif 'COFF' in fileout: |
| 709 | linkage = 'COFF' |
| 710 | elif 'MS-DOS' in fileout: |
| 711 | linkage = 'MSDOS' |
| 712 | else: |
| 713 | # XXX the A.OUT format also falls under this class... |
| 714 | pass |
| 715 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 716 | return bits, linkage |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 717 | |
| 718 | ### Portable uname() interface |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 719 | |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 720 | uname_result = collections.namedtuple("uname_result", |
| 721 | "system node release version machine processor") |
| 722 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 723 | _uname_cache = None |
| 724 | |
| 725 | def uname(): |
| 726 | |
| 727 | """ Fairly portable uname interface. Returns a tuple |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 728 | of strings (system, node, release, version, machine, processor) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 729 | identifying the underlying platform. |
| 730 | |
| 731 | Note that unlike the os.uname function this also returns |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 732 | possible processor information as an additional tuple entry. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 733 | |
| 734 | Entries which cannot be determined are set to ''. |
| 735 | |
| 736 | """ |
| 737 | global _uname_cache |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 738 | no_os_uname = 0 |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 739 | |
| 740 | if _uname_cache is not None: |
| 741 | return _uname_cache |
| 742 | |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 743 | processor = '' |
| 744 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 745 | # Get some infos from the builtin os.uname API... |
| 746 | try: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 747 | system, node, release, version, machine = os.uname() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 748 | except AttributeError: |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 749 | no_os_uname = 1 |
| 750 | |
Georg Brandl | 62e2ca2 | 2010-07-31 21:54:24 +0000 | [diff] [blame] | 751 | if no_os_uname or not list(filter(None, (system, node, release, version, machine))): |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 752 | # Hmm, no there is either no uname or uname has returned |
| 753 | #'unknowns'... we'll have to poke around the system then. |
| 754 | if no_os_uname: |
| 755 | system = sys.platform |
| 756 | release = '' |
| 757 | version = '' |
| 758 | node = _node() |
| 759 | machine = '' |
| 760 | |
Amaury Forgeot d'Arc | 7a01984 | 2008-06-17 21:42:46 +0000 | [diff] [blame] | 761 | use_syscmd_ver = 1 |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 762 | |
| 763 | # Try win32_ver() on win32 platforms |
| 764 | if system == 'win32': |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 765 | release, version, csd, ptype = win32_ver() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 766 | if release and version: |
| 767 | use_syscmd_ver = 0 |
Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 768 | # Try to use the PROCESSOR_* environment variables |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 769 | # available on Win XP and later; see |
| 770 | # http://support.microsoft.com/kb/888731 and |
| 771 | # http://www.geocities.com/rick_lively/MANUALS/ENV/MSWIN/PROCESSI.HTM |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 772 | if not machine: |
R. David Murray | ca2edce | 2010-03-22 17:48:48 +0000 | [diff] [blame] | 773 | # WOW64 processes mask the native architecture |
| 774 | if "PROCESSOR_ARCHITEW6432" in os.environ: |
| 775 | machine = os.environ.get("PROCESSOR_ARCHITEW6432", '') |
| 776 | else: |
| 777 | machine = os.environ.get('PROCESSOR_ARCHITECTURE', '') |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 778 | if not processor: |
| 779 | processor = os.environ.get('PROCESSOR_IDENTIFIER', machine) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 780 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 781 | # Try the 'ver' system command available on some |
| 782 | # platforms |
| 783 | if use_syscmd_ver: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 784 | system, release, version = _syscmd_ver(system) |
Marc-André Lemburg | cdc7923 | 2004-06-19 17:17:00 +0000 | [diff] [blame] | 785 | # Normalize system to what win32_ver() normally returns |
| 786 | # (_syscmd_ver() tends to return the vendor name as well) |
| 787 | if system == 'Microsoft Windows': |
| 788 | system = 'Windows' |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 789 | elif system == 'Microsoft' and release == 'Windows': |
| 790 | # Under Windows Vista and Windows Server 2008, |
| 791 | # Microsoft changed the output of the ver command. The |
| 792 | # release is no longer printed. This causes the |
| 793 | # system and release to be misidentified. |
| 794 | system = 'Windows' |
| 795 | if '6.0' == version[:3]: |
| 796 | release = 'Vista' |
| 797 | else: |
| 798 | release = '' |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 799 | |
| 800 | # In case we still don't know anything useful, we'll try to |
| 801 | # help ourselves |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 802 | if system in ('win32', 'win16'): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 803 | if not version: |
| 804 | if system == 'win32': |
| 805 | version = '32bit' |
| 806 | else: |
| 807 | version = '16bit' |
| 808 | system = 'Windows' |
| 809 | |
| 810 | elif system[:4] == 'java': |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 811 | release, vendor, vminfo, osinfo = java_ver() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 812 | system = 'Java' |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 813 | version = ', '.join(vminfo) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 814 | if not version: |
| 815 | version = vendor |
| 816 | |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 817 | # System specific extensions |
| 818 | if system == 'OpenVMS': |
| 819 | # OpenVMS seems to have release and version mixed up |
| 820 | if not release or release == '0': |
| 821 | release = version |
| 822 | version = '' |
| 823 | # Get processor information |
| 824 | try: |
| 825 | import vms_lib |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 826 | except ImportError: |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 827 | pass |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 828 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 829 | csid, cpu_number = vms_lib.getsyi('SYI$_CPU', 0) |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 830 | if (cpu_number >= 128): |
| 831 | processor = 'Alpha' |
| 832 | else: |
| 833 | processor = 'VAX' |
| 834 | if not processor: |
| 835 | # Get processor information from the uname system command |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 836 | processor = _syscmd_uname('-p', '') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 837 | |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 838 | #If any unknowns still exist, replace them with ''s, which are more portable |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 839 | if system == 'unknown': |
| 840 | system = '' |
| 841 | if node == 'unknown': |
| 842 | node = '' |
| 843 | if release == 'unknown': |
| 844 | release = '' |
| 845 | if version == 'unknown': |
| 846 | version = '' |
| 847 | if machine == 'unknown': |
| 848 | machine = '' |
| 849 | if processor == 'unknown': |
| 850 | processor = '' |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 851 | |
| 852 | # normalize name |
| 853 | if system == 'Microsoft' and release == 'Windows': |
| 854 | system = 'Windows' |
| 855 | release = 'Vista' |
| 856 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 857 | _uname_cache = uname_result(system, node, release, version, |
| 858 | machine, processor) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 859 | return _uname_cache |
| 860 | |
| 861 | ### Direct interfaces to some of the uname() return values |
| 862 | |
| 863 | def system(): |
| 864 | |
| 865 | """ Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. |
| 866 | |
| 867 | An empty string is returned if the value cannot be determined. |
| 868 | |
| 869 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 870 | return uname().system |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 871 | |
| 872 | def node(): |
| 873 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 874 | """ Returns the computer's network name (which may not be fully |
| 875 | qualified) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 876 | |
| 877 | An empty string is returned if the value cannot be determined. |
| 878 | |
| 879 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 880 | return uname().node |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 881 | |
| 882 | def release(): |
| 883 | |
| 884 | """ Returns the system's release, e.g. '2.2.0' or 'NT' |
| 885 | |
| 886 | An empty string is returned if the value cannot be determined. |
| 887 | |
| 888 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 889 | return uname().release |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 890 | |
| 891 | def version(): |
| 892 | |
| 893 | """ Returns the system's release version, e.g. '#3 on degas' |
| 894 | |
| 895 | An empty string is returned if the value cannot be determined. |
| 896 | |
| 897 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 898 | return uname().version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 899 | |
| 900 | def machine(): |
| 901 | |
| 902 | """ Returns the machine type, e.g. 'i386' |
| 903 | |
| 904 | An empty string is returned if the value cannot be determined. |
| 905 | |
| 906 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 907 | return uname().machine |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 908 | |
| 909 | def processor(): |
| 910 | |
| 911 | """ Returns the (true) processor name, e.g. 'amdk6' |
| 912 | |
| 913 | An empty string is returned if the value cannot be |
| 914 | determined. Note that many platforms do not provide this |
| 915 | information or simply return the same value as for machine(), |
| 916 | e.g. NetBSD does this. |
| 917 | |
| 918 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 919 | return uname().processor |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 920 | |
| 921 | ### Various APIs for extracting information from sys.version |
| 922 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 923 | _sys_version_parser = re.compile( |
Martin Panter | 4e50553 | 2016-06-08 06:12:22 +0000 | [diff] [blame] | 924 | r'([\w.+]+)\s*' # "version<space>" |
| 925 | r'\(#?([^,]+)' # "(#buildno" |
| 926 | r'(?:,\s*([\w ]*)' # ", builddate" |
| 927 | r'(?:,\s*([\w :]*))?)?\)\s*' # ", buildtime)<space>" |
| 928 | r'\[([^\]]+)\]?', re.ASCII) # "[compiler]" |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 929 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 930 | _ironpython_sys_version_parser = re.compile( |
| 931 | r'IronPython\s*' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 932 | r'([\d\.]+)' |
| 933 | r'(?: \(([\d\.]+)\))?' |
| 934 | r' on (.NET [\d\.]+)', re.ASCII) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 935 | |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 936 | # IronPython covering 2.6 and 2.7 |
| 937 | _ironpython26_sys_version_parser = re.compile( |
| 938 | r'([\d.]+)\s*' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 939 | r'\(IronPython\s*' |
| 940 | r'[\d.]+\s*' |
| 941 | r'\(([\d.]+)\) on ([\w.]+ [\d.]+(?: \(\d+-bit\))?)\)' |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 942 | ) |
| 943 | |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 944 | _pypy_sys_version_parser = re.compile( |
| 945 | r'([\w.+]+)\s*' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 946 | r'\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*' |
| 947 | r'\[PyPy [^\]]+\]?') |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 948 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 949 | _sys_version_cache = {} |
| 950 | |
| 951 | def _sys_version(sys_version=None): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 952 | |
| 953 | """ Returns a parsed version of Python's sys.version as tuple |
Benjamin Peterson | 5f28b7b | 2009-03-26 21:49:58 +0000 | [diff] [blame] | 954 | (name, version, branch, revision, buildno, builddate, compiler) |
| 955 | referring to the Python implementation name, version, branch, |
| 956 | revision, build number, build date/time as string and the compiler |
| 957 | identification string. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 958 | |
| 959 | Note that unlike the Python sys.version, the returned value |
| 960 | for the Python version will always include the patchlevel (it |
| 961 | defaults to '.0'). |
| 962 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 963 | The function returns empty strings for tuple entries that |
| 964 | cannot be determined. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 965 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 966 | sys_version may be given to parse an alternative version |
| 967 | string, e.g. if the version was read from a different Python |
| 968 | interpreter. |
| 969 | |
| 970 | """ |
| 971 | # Get the Python version |
| 972 | if sys_version is None: |
| 973 | sys_version = sys.version |
| 974 | |
| 975 | # Try the cache first |
| 976 | result = _sys_version_cache.get(sys_version, None) |
| 977 | if result is not None: |
| 978 | return result |
| 979 | |
| 980 | # Parse it |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 981 | if 'IronPython' in sys_version: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 982 | # IronPython |
| 983 | name = 'IronPython' |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 984 | if sys_version.startswith('IronPython'): |
| 985 | match = _ironpython_sys_version_parser.match(sys_version) |
| 986 | else: |
| 987 | match = _ironpython26_sys_version_parser.match(sys_version) |
| 988 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 989 | if match is None: |
| 990 | raise ValueError( |
| 991 | 'failed to parse IronPython sys.version: %s' % |
| 992 | repr(sys_version)) |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 993 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 994 | version, alt_version, compiler = match.groups() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 995 | buildno = '' |
| 996 | builddate = '' |
| 997 | |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 998 | elif sys.platform.startswith('java'): |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 999 | # Jython |
| 1000 | name = 'Jython' |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1001 | match = _sys_version_parser.match(sys_version) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1002 | if match is None: |
| 1003 | raise ValueError( |
| 1004 | 'failed to parse Jython sys.version: %s' % |
| 1005 | repr(sys_version)) |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1006 | version, buildno, builddate, buildtime, _ = match.groups() |
Martin Panter | 4e50553 | 2016-06-08 06:12:22 +0000 | [diff] [blame] | 1007 | if builddate is None: |
| 1008 | builddate = '' |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1009 | compiler = sys.platform |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1010 | |
| 1011 | elif "PyPy" in sys_version: |
| 1012 | # PyPy |
| 1013 | name = "PyPy" |
| 1014 | match = _pypy_sys_version_parser.match(sys_version) |
| 1015 | if match is None: |
| 1016 | raise ValueError("failed to parse PyPy sys.version: %s" % |
| 1017 | repr(sys_version)) |
| 1018 | version, buildno, builddate, buildtime = match.groups() |
| 1019 | compiler = "" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1020 | |
| 1021 | else: |
| 1022 | # CPython |
| 1023 | match = _sys_version_parser.match(sys_version) |
| 1024 | if match is None: |
| 1025 | raise ValueError( |
| 1026 | 'failed to parse CPython sys.version: %s' % |
| 1027 | repr(sys_version)) |
| 1028 | version, buildno, builddate, buildtime, compiler = \ |
| 1029 | match.groups() |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1030 | name = 'CPython' |
Martin Panter | 4e50553 | 2016-06-08 06:12:22 +0000 | [diff] [blame] | 1031 | if builddate is None: |
| 1032 | builddate = '' |
| 1033 | elif buildtime: |
| 1034 | builddate = builddate + ' ' + buildtime |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1035 | |
Ned Deily | 5c4b0d0 | 2017-03-04 00:19:55 -0500 | [diff] [blame] | 1036 | if hasattr(sys, '_git'): |
| 1037 | _, branch, revision = sys._git |
| 1038 | elif hasattr(sys, '_mercurial'): |
Georg Brandl | 8256242 | 2011-03-05 21:09:22 +0100 | [diff] [blame] | 1039 | _, branch, revision = sys._mercurial |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1040 | else: |
| 1041 | branch = '' |
| 1042 | revision = '' |
| 1043 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1044 | # Add the patchlevel version if missing |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1045 | l = version.split('.') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1046 | if len(l) == 2: |
| 1047 | l.append('0') |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1048 | version = '.'.join(l) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1049 | |
| 1050 | # Build and cache the result |
| 1051 | result = (name, version, branch, revision, buildno, builddate, compiler) |
| 1052 | _sys_version_cache[sys_version] = result |
| 1053 | return result |
| 1054 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1055 | def python_implementation(): |
| 1056 | |
| 1057 | """ Returns a string identifying the Python implementation. |
| 1058 | |
| 1059 | Currently, the following implementations are identified: |
Ezio Melotti | f16898b | 2011-05-04 18:37:50 +0300 | [diff] [blame] | 1060 | 'CPython' (C implementation of Python), |
| 1061 | 'IronPython' (.NET implementation of Python), |
| 1062 | 'Jython' (Java implementation of Python), |
| 1063 | 'PyPy' (Python implementation of Python). |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1064 | |
| 1065 | """ |
| 1066 | return _sys_version()[0] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1067 | |
| 1068 | def python_version(): |
| 1069 | |
| 1070 | """ Returns the Python version as string 'major.minor.patchlevel' |
| 1071 | |
| 1072 | Note that unlike the Python sys.version, the returned value |
| 1073 | will always include the patchlevel (it defaults to 0). |
| 1074 | |
| 1075 | """ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1076 | return _sys_version()[1] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1077 | |
| 1078 | def python_version_tuple(): |
| 1079 | |
| 1080 | """ Returns the Python version as tuple (major, minor, patchlevel) |
| 1081 | of strings. |
| 1082 | |
| 1083 | Note that unlike the Python sys.version, the returned value |
| 1084 | will always include the patchlevel (it defaults to 0). |
| 1085 | |
| 1086 | """ |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1087 | return tuple(_sys_version()[1].split('.')) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1088 | |
| 1089 | def python_branch(): |
| 1090 | |
| 1091 | """ Returns a string identifying the Python implementation |
| 1092 | branch. |
| 1093 | |
Victor Stinner | fe2d5ba | 2017-11-28 22:29:32 +0100 | [diff] [blame] | 1094 | For CPython this is the SCM branch from which the |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1095 | Python binary was built. |
| 1096 | |
| 1097 | If not available, an empty string is returned. |
| 1098 | |
| 1099 | """ |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1100 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1101 | return _sys_version()[2] |
| 1102 | |
| 1103 | def python_revision(): |
| 1104 | |
| 1105 | """ Returns a string identifying the Python implementation |
| 1106 | revision. |
| 1107 | |
Victor Stinner | fe2d5ba | 2017-11-28 22:29:32 +0100 | [diff] [blame] | 1108 | For CPython this is the SCM revision from which the |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1109 | Python binary was built. |
| 1110 | |
| 1111 | If not available, an empty string is returned. |
| 1112 | |
| 1113 | """ |
| 1114 | return _sys_version()[3] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1115 | |
| 1116 | def python_build(): |
| 1117 | |
| 1118 | """ Returns a tuple (buildno, builddate) stating the Python |
| 1119 | build number and date as strings. |
| 1120 | |
| 1121 | """ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1122 | return _sys_version()[4:6] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1123 | |
| 1124 | def python_compiler(): |
| 1125 | |
| 1126 | """ Returns a string identifying the compiler used for compiling |
| 1127 | Python. |
| 1128 | |
| 1129 | """ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1130 | return _sys_version()[6] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1131 | |
| 1132 | ### The Opus Magnum of platform strings :-) |
| 1133 | |
Marc-André Lemburg | 91e83e2 | 2004-03-25 18:35:12 +0000 | [diff] [blame] | 1134 | _platform_cache = {} |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1135 | |
| 1136 | def platform(aliased=0, terse=0): |
| 1137 | |
| 1138 | """ Returns a single string identifying the underlying platform |
| 1139 | with as much useful information as possible (but no more :). |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 1140 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1141 | The output is intended to be human readable rather than |
| 1142 | machine parseable. It may look different on different |
| 1143 | platforms and this is intended. |
| 1144 | |
| 1145 | If "aliased" is true, the function will use aliases for |
| 1146 | various platforms that report system names which differ from |
| 1147 | their common names, e.g. SunOS will be reported as |
| 1148 | Solaris. The system_alias() function is used to implement |
| 1149 | this. |
| 1150 | |
| 1151 | Setting terse to true causes the function to return only the |
| 1152 | absolute minimum information needed to identify the platform. |
| 1153 | |
| 1154 | """ |
Marc-André Lemburg | 91e83e2 | 2004-03-25 18:35:12 +0000 | [diff] [blame] | 1155 | result = _platform_cache.get((aliased, terse), None) |
| 1156 | if result is not None: |
| 1157 | return result |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1158 | |
| 1159 | # Get uname information and then apply platform specific cosmetics |
| 1160 | # to it... |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1161 | system, node, release, version, machine, processor = uname() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1162 | if machine == processor: |
| 1163 | processor = '' |
| 1164 | if aliased: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1165 | system, release, version = system_alias(system, release, version) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1166 | |
Victor Stinner | ea0ca21 | 2018-12-05 22:41:52 +0100 | [diff] [blame] | 1167 | if system == 'Darwin': |
| 1168 | # macOS (darwin kernel) |
| 1169 | macos_release = mac_ver()[0] |
| 1170 | if macos_release: |
Victor Stinner | ea0ca21 | 2018-12-05 22:41:52 +0100 | [diff] [blame] | 1171 | system = 'macOS' |
| 1172 | release = macos_release |
| 1173 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1174 | if system == 'Windows': |
| 1175 | # MS platforms |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1176 | rel, vers, csd, ptype = win32_ver(version) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1177 | if terse: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1178 | platform = _platform(system, release) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1179 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1180 | platform = _platform(system, release, version, csd) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1181 | |
| 1182 | elif system in ('Linux',): |
Petr Viktorin | 8b94b41 | 2018-05-16 11:51:18 -0400 | [diff] [blame] | 1183 | # check for libc vs. glibc |
| 1184 | libcname, libcversion = libc_ver(sys.executable) |
| 1185 | platform = _platform(system, release, machine, processor, |
| 1186 | 'with', |
| 1187 | libcname+libcversion) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1188 | elif system == 'Java': |
| 1189 | # Java platforms |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1190 | r, v, vminfo, (os_name, os_version, os_arch) = java_ver() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1191 | if terse or not os_name: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1192 | platform = _platform(system, release, version) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1193 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1194 | platform = _platform(system, release, version, |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1195 | 'on', |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1196 | os_name, os_version, os_arch) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1197 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1198 | else: |
| 1199 | # Generic handler |
| 1200 | if terse: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1201 | platform = _platform(system, release) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1202 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1203 | bits, linkage = architecture(sys.executable) |
| 1204 | platform = _platform(system, release, machine, |
| 1205 | processor, bits, linkage) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 1206 | |
Marc-André Lemburg | 91e83e2 | 2004-03-25 18:35:12 +0000 | [diff] [blame] | 1207 | _platform_cache[(aliased, terse)] = platform |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1208 | return platform |
| 1209 | |
| 1210 | ### Command line interface |
| 1211 | |
| 1212 | if __name__ == '__main__': |
| 1213 | # Default is to print the aliased verbose platform string |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 1214 | terse = ('terse' in sys.argv or '--terse' in sys.argv) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1215 | aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv) |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1216 | print(platform(aliased, terse)) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1217 | sys.exit(0) |