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 |
Jesus Cea | fc990e9 | 2012-10-04 13:51:43 +0200 | [diff] [blame] | 116 | import sys, os, re, subprocess |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 117 | |
Berker Peksag | 1392f71 | 2015-05-16 20:24:28 +0300 | [diff] [blame] | 118 | import warnings |
| 119 | |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 120 | ### Globals & Constants |
| 121 | |
| 122 | # Determine the platform's /dev/null device |
| 123 | try: |
| 124 | DEV_NULL = os.devnull |
| 125 | except AttributeError: |
| 126 | # os.devnull was added in Python 2.4, so emulate it for earlier |
| 127 | # Python versions |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 128 | if sys.platform in ('dos', 'win32', 'win16'): |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 129 | # Use the old CP/M NUL as device name |
| 130 | DEV_NULL = 'NUL' |
| 131 | else: |
| 132 | # Standard Unix uses /dev/null |
| 133 | DEV_NULL = '/dev/null' |
| 134 | |
Victor Stinner | 620c48b | 2013-12-09 00:01:27 +0100 | [diff] [blame] | 135 | # Directory to search for configuration information on Unix. |
| 136 | # Constant used by test_platform to test linux_distribution(). |
| 137 | _UNIXCONFDIR = '/etc' |
| 138 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 139 | ### Platform specific APIs |
| 140 | |
Antoine Pitrou | ba7c226 | 2011-10-07 13:26:59 +0200 | [diff] [blame] | 141 | _libc_search = re.compile(b'(__libc_init)' |
| 142 | b'|' |
| 143 | b'(GLIBC_([0-9.]+))' |
| 144 | b'|' |
| 145 | br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII) |
Marc-André Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 146 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 147 | def libc_ver(executable=sys.executable, lib='', version='', |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 148 | |
Antoine Pitrou | ba7c226 | 2011-10-07 13:26:59 +0200 | [diff] [blame] | 149 | chunksize=16384): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 150 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 151 | """ Tries to determine the libc version that the file executable |
| 152 | (which defaults to the Python interpreter) is linked against. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 153 | |
| 154 | Returns a tuple of strings (lib,version) which default to the |
| 155 | given parameters in case the lookup fails. |
| 156 | |
| 157 | Note that the function has intimate knowledge of how different |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 158 | libc versions add symbols to the executable and thus is probably |
| 159 | only useable for executables compiled using gcc. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 160 | |
| 161 | The file is read and scanned in chunks of chunksize bytes. |
| 162 | |
| 163 | """ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 164 | if hasattr(os.path, 'realpath'): |
| 165 | # Python 2.2 introduced os.path.realpath(); it is used |
| 166 | # here to work around problems with Cygwin not being |
| 167 | # able to open symlinks for reading |
| 168 | executable = os.path.realpath(executable) |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 169 | with open(executable, 'rb') as f: |
| 170 | binary = f.read(chunksize) |
| 171 | pos = 0 |
| 172 | while 1: |
| 173 | if b'libc' in binary or b'GLIBC' in binary: |
| 174 | m = _libc_search.search(binary, pos) |
| 175 | else: |
| 176 | m = None |
| 177 | if not m: |
| 178 | binary = f.read(chunksize) |
| 179 | if not binary: |
| 180 | break |
| 181 | pos = 0 |
| 182 | continue |
| 183 | libcinit, glibc, glibcversion, so, threads, soversion = [ |
| 184 | s.decode('latin1') if s is not None else s |
| 185 | for s in m.groups()] |
| 186 | if libcinit and not lib: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 187 | lib = 'libc' |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 188 | elif glibc: |
| 189 | if lib != 'glibc': |
| 190 | lib = 'glibc' |
| 191 | version = glibcversion |
| 192 | elif glibcversion > version: |
| 193 | version = glibcversion |
| 194 | elif so: |
| 195 | if lib != 'glibc': |
| 196 | lib = 'libc' |
| 197 | if soversion and soversion > version: |
| 198 | version = soversion |
| 199 | if threads and version[-len(threads):] != threads: |
| 200 | version = version + threads |
| 201 | pos = m.end() |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 202 | return lib, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 203 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 204 | def _dist_try_harder(distname, version, id): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 205 | |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 206 | """ Tries some special tricks to get the distribution |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 207 | information in case the default method fails. |
| 208 | |
| 209 | Currently supports older SuSE Linux, Caldera OpenLinux and |
| 210 | Slackware Linux distributions. |
| 211 | |
| 212 | """ |
| 213 | if os.path.exists('/var/adm/inst-log/info'): |
| 214 | # SuSE Linux stores distribution information in that file |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 215 | distname = 'SuSE' |
Florent Xicluna | 7dde792 | 2010-09-03 19:52:03 +0000 | [diff] [blame] | 216 | for line in open('/var/adm/inst-log/info'): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 217 | tv = line.split() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 218 | if len(tv) == 2: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 219 | tag, value = tv |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 220 | else: |
| 221 | continue |
| 222 | if tag == 'MIN_DIST_VERSION': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 223 | version = value.strip() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 224 | elif tag == 'DIST_IDENT': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 225 | values = value.split('-') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 226 | id = values[2] |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 227 | return distname, version, id |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 228 | |
| 229 | if os.path.exists('/etc/.installed'): |
| 230 | # Caldera OpenLinux has some infos in that file (thanks to Colin Kong) |
Florent Xicluna | 7dde792 | 2010-09-03 19:52:03 +0000 | [diff] [blame] | 231 | for line in open('/etc/.installed'): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 232 | pkg = line.split('-') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 233 | if len(pkg) >= 2 and pkg[0] == 'OpenLinux': |
| 234 | # XXX does Caldera support non Intel platforms ? If yes, |
| 235 | # where can we find the needed id ? |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 236 | return 'OpenLinux', pkg[1], id |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 237 | |
| 238 | if os.path.isdir('/usr/lib/setup'): |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 239 | # Check for slackware version tag file (thanks to Greg Andruk) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 240 | verfiles = os.listdir('/usr/lib/setup') |
Guido van Rossum | 843c734 | 2004-05-04 18:18:59 +0000 | [diff] [blame] | 241 | for n in range(len(verfiles)-1, -1, -1): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 242 | if verfiles[n][:14] != 'slack-version-': |
| 243 | del verfiles[n] |
| 244 | if verfiles: |
| 245 | verfiles.sort() |
| 246 | distname = 'slackware' |
| 247 | version = verfiles[-1][14:] |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 248 | return distname, version, id |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 249 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 250 | return distname, version, id |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 252 | _release_filename = re.compile(r'(\w+)[-_](release|version)', re.ASCII) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 253 | _lsb_release_version = re.compile(r'(.+)' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 254 | r' release ' |
| 255 | r'([\d.]+)' |
| 256 | r'[^(]*(?:\((.+)\))?', re.ASCII) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 257 | _release_version = re.compile(r'([^0-9]+)' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 258 | r'(?: release )?' |
| 259 | r'([\d.]+)' |
| 260 | r'[^(]*(?:\((.+)\))?', re.ASCII) |
Marc-André Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 261 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 262 | # See also http://www.novell.com/coolsolutions/feature/11251.html |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 263 | # and http://linuxmafia.com/faq/Admin/release-files.html |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 264 | # and http://data.linux-ntfs.org/rpm/whichrpm |
| 265 | # and http://www.die.net/doc/linux/man/man1/lsb_release.1.html |
Marc-André Lemburg | 380f417 | 2005-11-07 16:11:02 +0000 | [diff] [blame] | 266 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 267 | _supported_dists = ( |
| 268 | 'SuSE', 'debian', 'fedora', 'redhat', 'centos', |
| 269 | 'mandrake', 'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo', |
Antoine Pitrou | ab88803 | 2012-06-24 22:20:18 +0200 | [diff] [blame] | 270 | 'UnitedLinux', 'turbolinux', 'arch', 'mageia') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 271 | |
| 272 | def _parse_release_file(firstline): |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 273 | |
Benjamin Peterson | 2500147 | 2010-01-25 03:37:42 +0000 | [diff] [blame] | 274 | # Default to empty 'version' and 'id' strings. Both defaults are used |
| 275 | # when 'firstline' is empty. 'id' defaults to empty when an id can not |
| 276 | # be deduced. |
| 277 | version = '' |
| 278 | id = '' |
| 279 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 280 | # Parse the first line |
| 281 | m = _lsb_release_version.match(firstline) |
| 282 | if m is not None: |
| 283 | # LSB format: "distro release x.x (codename)" |
| 284 | return tuple(m.groups()) |
| 285 | |
| 286 | # Pre-LSB format: "distro x.x (codename)" |
| 287 | m = _release_version.match(firstline) |
| 288 | if m is not None: |
| 289 | return tuple(m.groups()) |
| 290 | |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 291 | # Unknown format... take the first two words |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 292 | l = firstline.strip().split() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 293 | if l: |
| 294 | version = l[0] |
| 295 | if len(l) > 1: |
| 296 | id = l[1] |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 297 | return '', version, id |
| 298 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 299 | def linux_distribution(distname='', version='', id='', |
| 300 | |
| 301 | supported_dists=_supported_dists, |
| 302 | full_distribution_name=1): |
Berker Peksag | 2f3742b | 2015-05-13 12:32:20 +0300 | [diff] [blame] | 303 | import warnings |
| 304 | warnings.warn("dist() and linux_distribution() functions are deprecated " |
Berker Peksag | 8d8221f | 2016-04-24 03:32:24 +0300 | [diff] [blame] | 305 | "in Python 3.5", PendingDeprecationWarning, stacklevel=2) |
Berker Peksag | 2f3742b | 2015-05-13 12:32:20 +0300 | [diff] [blame] | 306 | return _linux_distribution(distname, version, id, supported_dists, |
| 307 | full_distribution_name) |
| 308 | |
| 309 | def _linux_distribution(distname, version, id, supported_dists, |
| 310 | full_distribution_name): |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 311 | |
| 312 | """ Tries to determine the name of the Linux OS distribution name. |
| 313 | |
| 314 | The function first looks for a distribution release file in |
| 315 | /etc and then reverts to _dist_try_harder() in case no |
| 316 | suitable files are found. |
| 317 | |
| 318 | supported_dists may be given to define the set of Linux |
| 319 | distributions to look for. It defaults to a list of currently |
| 320 | supported Linux distributions identified by their release file |
| 321 | name. |
| 322 | |
| 323 | If full_distribution_name is true (default), the full |
| 324 | distribution read from the OS is returned. Otherwise the short |
| 325 | name taken from supported_dists is used. |
| 326 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 327 | Returns a tuple (distname, version, id) which default to the |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 328 | args given as parameters. |
| 329 | |
| 330 | """ |
| 331 | try: |
Victor Stinner | 620c48b | 2013-12-09 00:01:27 +0100 | [diff] [blame] | 332 | etc = os.listdir(_UNIXCONFDIR) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 333 | except OSError: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 334 | # Probably not a Unix system |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 335 | return distname, version, id |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 336 | etc.sort() |
| 337 | for file in etc: |
| 338 | m = _release_filename.match(file) |
| 339 | if m is not None: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 340 | _distname, dummy = m.groups() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 341 | if _distname in supported_dists: |
| 342 | distname = _distname |
| 343 | break |
| 344 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 345 | return _dist_try_harder(distname, version, id) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 346 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 347 | # Read the first line |
Victor Stinner | 620c48b | 2013-12-09 00:01:27 +0100 | [diff] [blame] | 348 | with open(os.path.join(_UNIXCONFDIR, file), 'r', |
| 349 | encoding='utf-8', errors='surrogateescape') as f: |
Florent Xicluna | 7dde792 | 2010-09-03 19:52:03 +0000 | [diff] [blame] | 350 | firstline = f.readline() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 351 | _distname, _version, _id = _parse_release_file(firstline) |
| 352 | |
| 353 | if _distname and full_distribution_name: |
| 354 | distname = _distname |
| 355 | if _version: |
| 356 | version = _version |
| 357 | if _id: |
| 358 | id = _id |
| 359 | return distname, version, id |
| 360 | |
| 361 | # To maintain backwards compatibility: |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 362 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 363 | def dist(distname='', version='', id='', |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 364 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 365 | supported_dists=_supported_dists): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 366 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 367 | """ Tries to determine the name of the Linux OS distribution name. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 368 | |
| 369 | The function first looks for a distribution release file in |
| 370 | /etc and then reverts to _dist_try_harder() in case no |
| 371 | suitable files are found. |
| 372 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 373 | Returns a tuple (distname, version, id) which default to the |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 374 | args given as parameters. |
| 375 | |
| 376 | """ |
Berker Peksag | 2f3742b | 2015-05-13 12:32:20 +0300 | [diff] [blame] | 377 | import warnings |
| 378 | warnings.warn("dist() and linux_distribution() functions are deprecated " |
Berker Peksag | 8d8221f | 2016-04-24 03:32:24 +0300 | [diff] [blame] | 379 | "in Python 3.5", PendingDeprecationWarning, stacklevel=2) |
Berker Peksag | 2f3742b | 2015-05-13 12:32:20 +0300 | [diff] [blame] | 380 | return _linux_distribution(distname, version, id, |
| 381 | supported_dists=supported_dists, |
| 382 | full_distribution_name=0) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 383 | |
Antoine Pitrou | 877766d | 2011-03-19 17:00:37 +0100 | [diff] [blame] | 384 | def popen(cmd, mode='r', bufsize=-1): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 385 | |
| 386 | """ Portable popen() interface. |
| 387 | """ |
Victor Stinner | 25000d4 | 2011-05-24 00:16:16 +0200 | [diff] [blame] | 388 | import warnings |
| 389 | warnings.warn('use os.popen instead', DeprecationWarning, stacklevel=2) |
Victor Stinner | 1dfd380 | 2011-03-03 12:54:07 +0000 | [diff] [blame] | 390 | return os.popen(cmd, mode, bufsize) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 391 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 392 | def _norm_version(version, build=''): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 393 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 394 | """ Normalize the version and build strings and return a single |
Walter Dörwald | e5a7fad | 2005-11-21 17:01:27 +0000 | [diff] [blame] | 395 | version string using the format major.minor.build (or patchlevel). |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 396 | """ |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 397 | l = version.split('.') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 398 | if build: |
| 399 | l.append(build) |
| 400 | try: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 401 | ints = map(int, l) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 402 | except ValueError: |
| 403 | strings = l |
| 404 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 405 | strings = list(map(str, ints)) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 406 | version = '.'.join(strings[:3]) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 407 | return version |
| 408 | |
Marc-André Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 409 | _ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) ' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 410 | r'.*' |
| 411 | r'\[.* ([\d.]+)\])') |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 412 | |
| 413 | # Examples of VER command output: |
| 414 | # |
| 415 | # Windows 2000: Microsoft Windows 2000 [Version 5.00.2195] |
| 416 | # Windows XP: Microsoft Windows XP [Version 5.1.2600] |
| 417 | # Windows Vista: Microsoft Windows [Version 6.0.6002] |
| 418 | # |
| 419 | # Note that the "Version" string gets localized on different |
| 420 | # Windows versions. |
Marc-André Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 421 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 422 | def _syscmd_ver(system='', release='', version='', |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 423 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 424 | supported_platforms=('win32', 'win16', 'dos')): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 425 | |
| 426 | """ Tries to figure out the OS version used and returns |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 427 | a tuple (system, release, version). |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 428 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 429 | It uses the "ver" shell command for this which is known |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 430 | to exists on Windows, DOS. XXX Others too ? |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 431 | |
| 432 | In case this fails, the given parameters are used as |
| 433 | defaults. |
| 434 | |
| 435 | """ |
| 436 | if sys.platform not in supported_platforms: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 437 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 438 | |
| 439 | # Try some common cmd strings |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 440 | for cmd in ('ver', 'command /c ver', 'cmd /c ver'): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 441 | try: |
Steve Dower | cae101f | 2015-08-10 20:57:37 -0700 | [diff] [blame] | 442 | pipe = os.popen(cmd) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 443 | info = pipe.read() |
| 444 | if pipe.close(): |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 445 | raise OSError('command failed') |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 446 | # XXX How can I suppress shell errors from being written |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 447 | # to stderr ? |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 448 | except OSError as why: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 449 | #print 'Command %s failed: %s' % (cmd, why) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 450 | continue |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 451 | else: |
| 452 | break |
| 453 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 454 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 455 | |
| 456 | # Parse the output |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 457 | info = info.strip() |
Marc-André Lemburg | 366a0fe | 2003-04-24 11:46:35 +0000 | [diff] [blame] | 458 | m = _ver_output.match(info) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 459 | if m is not None: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 460 | system, release, version = m.groups() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 461 | # Strip trailing dots from version and release |
| 462 | if release[-1] == '.': |
| 463 | release = release[:-1] |
| 464 | if version[-1] == '.': |
| 465 | version = version[:-1] |
| 466 | # Normalize the version and build strings (eliminating additional |
| 467 | # zeros) |
| 468 | version = _norm_version(version) |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 469 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 470 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 471 | _WIN32_CLIENT_RELEASES = { |
| 472 | (5, 0): "2000", |
| 473 | (5, 1): "XP", |
| 474 | # Strictly, 5.2 client is XP 64-bit, but platform.py historically |
| 475 | # has always called it 2003 Server |
| 476 | (5, 2): "2003Server", |
| 477 | (5, None): "post2003", |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 478 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 479 | (6, 0): "Vista", |
| 480 | (6, 1): "7", |
| 481 | (6, 2): "8", |
| 482 | (6, 3): "8.1", |
| 483 | (6, None): "post8.1", |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 484 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 485 | (10, 0): "10", |
| 486 | (10, None): "post10", |
| 487 | } |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 488 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 489 | # Server release name lookup will default to client names if necessary |
| 490 | _WIN32_SERVER_RELEASES = { |
| 491 | (5, 2): "2003Server", |
| 492 | |
| 493 | (6, 0): "2008Server", |
| 494 | (6, 1): "2008ServerR2", |
| 495 | (6, 2): "2012Server", |
| 496 | (6, 3): "2012ServerR2", |
| 497 | (6, None): "post2012ServerR2", |
| 498 | } |
| 499 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 500 | def win32_ver(release='', version='', csd='', ptype=''): |
Steve Dower | 8f278f1 | 2015-09-22 17:35:24 -0700 | [diff] [blame] | 501 | try: |
| 502 | from sys import getwindowsversion |
| 503 | except ImportError: |
| 504 | return release, version, csd, ptype |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 505 | try: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 506 | from winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 507 | except ImportError: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 508 | from _winreg import OpenKeyEx, QueryValueEx, CloseKey, HKEY_LOCAL_MACHINE |
| 509 | |
| 510 | winver = getwindowsversion() |
Steve Dower | 74f4af7 | 2016-09-17 17:27:48 -0700 | [diff] [blame] | 511 | maj, min, build = winver.platform_version or winver[:3] |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 512 | version = '{0}.{1}.{2}'.format(maj, min, build) |
| 513 | |
| 514 | release = (_WIN32_CLIENT_RELEASES.get((maj, min)) or |
| 515 | _WIN32_CLIENT_RELEASES.get((maj, None)) or |
| 516 | release) |
| 517 | |
| 518 | # getwindowsversion() reflect the compatibility mode Python is |
| 519 | # running under, and so the service pack value is only going to be |
| 520 | # valid if the versions match. |
| 521 | if winver[:2] == (maj, min): |
Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 522 | try: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 523 | csd = 'SP{}'.format(winver.service_pack_major) |
Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 524 | except AttributeError: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 525 | if csd[:13] == 'Service Pack ': |
| 526 | csd = 'SP' + csd[13:] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 527 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 528 | # VER_NT_SERVER = 3 |
Steve Dower | 41519b2 | 2016-09-09 09:46:56 -0700 | [diff] [blame] | 529 | if getattr(winver, 'product_type', None) == 3: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 530 | release = (_WIN32_SERVER_RELEASES.get((maj, min)) or |
| 531 | _WIN32_SERVER_RELEASES.get((maj, None)) or |
| 532 | release) |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 533 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 534 | key = None |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 535 | try: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 536 | key = OpenKeyEx(HKEY_LOCAL_MACHINE, |
| 537 | r'SOFTWARE\Microsoft\Windows NT\CurrentVersion') |
| 538 | ptype = QueryValueEx(key, 'CurrentType')[0] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 539 | except: |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 540 | pass |
| 541 | finally: |
| 542 | if key: |
| 543 | CloseKey(key) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 544 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 545 | return release, version, csd, ptype |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 546 | |
Steve Dower | b9f4fea | 2015-09-22 17:23:39 -0700 | [diff] [blame] | 547 | |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 548 | def _mac_ver_xml(): |
| 549 | fn = '/System/Library/CoreServices/SystemVersion.plist' |
| 550 | if not os.path.exists(fn): |
| 551 | return None |
| 552 | |
| 553 | try: |
| 554 | import plistlib |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 555 | except ImportError: |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 556 | return None |
| 557 | |
Ned Deily | 936dfae | 2014-01-13 11:34:19 -0800 | [diff] [blame] | 558 | with open(fn, 'rb') as f: |
| 559 | pl = plistlib.load(f) |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 560 | release = pl['ProductVersion'] |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 561 | versioninfo = ('', '', '') |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 562 | machine = os.uname().machine |
Ronald Oussoren | fcd7701 | 2010-08-03 07:42:42 +0000 | [diff] [blame] | 563 | if machine in ('ppc', 'Power Macintosh'): |
Ezio Melotti | 9a3777e | 2013-08-17 15:53:55 +0300 | [diff] [blame] | 564 | # Canonical name |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 565 | machine = 'PowerPC' |
| 566 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 567 | return release, versioninfo, machine |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 568 | |
| 569 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 570 | def mac_ver(release='', versioninfo=('', '', ''), machine=''): |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 571 | |
| 572 | """ Get MacOS version information and return it as tuple (release, |
| 573 | versioninfo, machine) with versioninfo being a tuple (version, |
| 574 | dev_stage, non_release_version). |
| 575 | |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 576 | Entries which cannot be determined are set to the parameter values |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 577 | which default to ''. All tuple entries are strings. |
| 578 | """ |
| 579 | |
| 580 | # First try reading the information from an XML file which should |
| 581 | # always be present |
| 582 | info = _mac_ver_xml() |
| 583 | if info is not None: |
| 584 | return info |
| 585 | |
Ronald Oussoren | e186e38 | 2010-07-23 11:54:59 +0000 | [diff] [blame] | 586 | # If that also doesn't work return the default values |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 587 | return release, versioninfo, machine |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 588 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 589 | def _java_getprop(name, default): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 590 | |
| 591 | from java.lang import System |
| 592 | try: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 593 | value = System.getProperty(name) |
| 594 | if value is None: |
| 595 | return default |
| 596 | return value |
| 597 | except AttributeError: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 598 | return default |
| 599 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 600 | def java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', '')): |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 601 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 602 | """ Version interface for Jython. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 603 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 604 | Returns a tuple (release, vendor, vminfo, osinfo) with vminfo being |
| 605 | a tuple (vm_name, vm_release, vm_vendor) and osinfo being a |
| 606 | tuple (os_name, os_version, os_arch). |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 607 | |
| 608 | Values which cannot be determined are set to the defaults |
| 609 | given as parameters (which all default to ''). |
| 610 | |
| 611 | """ |
| 612 | # Import the needed APIs |
| 613 | try: |
| 614 | import java.lang |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 615 | except ImportError: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 616 | return release, vendor, vminfo, osinfo |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 617 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 618 | vendor = _java_getprop('java.vendor', vendor) |
| 619 | release = _java_getprop('java.version', release) |
| 620 | vm_name, vm_release, vm_vendor = vminfo |
| 621 | vm_name = _java_getprop('java.vm.name', vm_name) |
| 622 | vm_vendor = _java_getprop('java.vm.vendor', vm_vendor) |
| 623 | vm_release = _java_getprop('java.vm.version', vm_release) |
| 624 | vminfo = vm_name, vm_release, vm_vendor |
| 625 | os_name, os_version, os_arch = osinfo |
| 626 | os_arch = _java_getprop('java.os.arch', os_arch) |
| 627 | os_name = _java_getprop('java.os.name', os_name) |
| 628 | os_version = _java_getprop('java.os.version', os_version) |
| 629 | osinfo = os_name, os_version, os_arch |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 630 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 631 | return release, vendor, vminfo, osinfo |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 632 | |
| 633 | ### System name aliasing |
| 634 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 635 | def system_alias(system, release, version): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 636 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 637 | """ Returns (system, release, version) aliased to common |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 638 | marketing names used for some systems. |
| 639 | |
| 640 | It also does some reordering of the information in some cases |
| 641 | where it would otherwise cause confusion. |
| 642 | |
| 643 | """ |
| 644 | if system == 'Rhapsody': |
| 645 | # Apple's BSD derivative |
| 646 | # XXX How can we determine the marketing release number ? |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 647 | return 'MacOS X Server', system+release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 648 | |
| 649 | elif system == 'SunOS': |
| 650 | # Sun's OS |
| 651 | if release < '5': |
| 652 | # These releases use the old name SunOS |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 653 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 654 | # Modify release (marketing release = SunOS release - 3) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 655 | l = release.split('.') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 656 | if l: |
| 657 | try: |
| 658 | major = int(l[0]) |
| 659 | except ValueError: |
| 660 | pass |
| 661 | else: |
| 662 | major = major - 3 |
| 663 | l[0] = str(major) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 664 | release = '.'.join(l) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 665 | if release < '6': |
| 666 | system = 'Solaris' |
| 667 | else: |
| 668 | # XXX Whatever the new SunOS marketing name is... |
| 669 | system = 'Solaris' |
| 670 | |
| 671 | elif system == 'IRIX64': |
| 672 | # IRIX reports IRIX64 on platforms with 64-bit support; yet it |
| 673 | # is really a version and not a different platform, since 32-bit |
| 674 | # apps are also supported.. |
| 675 | system = 'IRIX' |
| 676 | if version: |
| 677 | version = version + ' (64bit)' |
| 678 | else: |
| 679 | version = '64bit' |
| 680 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 681 | elif system in ('win32', 'win16'): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 682 | # In case one of the other tricks |
| 683 | system = 'Windows' |
| 684 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 685 | return system, release, version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 686 | |
| 687 | ### Various internal helpers |
| 688 | |
| 689 | def _platform(*args): |
| 690 | |
| 691 | """ Helper to format the platform string in a filename |
| 692 | compatible format e.g. "system-version-machine". |
| 693 | """ |
| 694 | # Format the platform string |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 695 | platform = '-'.join(x.strip() for x in filter(len, args)) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 696 | |
| 697 | # Cleanup some possible filename obstacles... |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 698 | platform = platform.replace(' ', '_') |
| 699 | platform = platform.replace('/', '-') |
| 700 | platform = platform.replace('\\', '-') |
| 701 | platform = platform.replace(':', '-') |
| 702 | platform = platform.replace(';', '-') |
| 703 | platform = platform.replace('"', '-') |
| 704 | platform = platform.replace('(', '-') |
| 705 | platform = platform.replace(')', '-') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 706 | |
| 707 | # No need to report 'unknown' information... |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 708 | platform = platform.replace('unknown', '') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 709 | |
| 710 | # Fold '--'s and remove trailing '-' |
| 711 | while 1: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 712 | cleaned = platform.replace('--', '-') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 713 | if cleaned == platform: |
| 714 | break |
| 715 | platform = cleaned |
| 716 | while platform[-1] == '-': |
| 717 | platform = platform[:-1] |
| 718 | |
| 719 | return platform |
| 720 | |
| 721 | def _node(default=''): |
| 722 | |
| 723 | """ Helper to determine the node name of this machine. |
| 724 | """ |
| 725 | try: |
| 726 | import socket |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 727 | except ImportError: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 728 | # No sockets... |
| 729 | return default |
| 730 | try: |
| 731 | return socket.gethostname() |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 732 | except OSError: |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 733 | # Still not working... |
| 734 | return default |
| 735 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 736 | def _follow_symlinks(filepath): |
| 737 | |
| 738 | """ In case filepath is a symlink, follow it until a |
| 739 | real file is reached. |
| 740 | """ |
Georg Brandl | 673f7ef | 2008-01-05 21:20:19 +0000 | [diff] [blame] | 741 | filepath = os.path.abspath(filepath) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 742 | while os.path.islink(filepath): |
| 743 | filepath = os.path.normpath( |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 744 | os.path.join(os.path.dirname(filepath), os.readlink(filepath))) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 745 | return filepath |
| 746 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 747 | def _syscmd_uname(option, default=''): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 748 | |
| 749 | """ Interface to the system's uname command. |
| 750 | """ |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 751 | if sys.platform in ('dos', 'win32', 'win16'): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 752 | # XXX Others too ? |
| 753 | return default |
| 754 | try: |
Alexandre Vassalotti | e52e378 | 2009-07-17 09:18:18 +0000 | [diff] [blame] | 755 | f = os.popen('uname %s 2> %s' % (option, DEV_NULL)) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 756 | except (AttributeError, OSError): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 757 | return default |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 758 | output = f.read().strip() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 759 | rc = f.close() |
| 760 | if not output or rc: |
| 761 | return default |
| 762 | else: |
| 763 | return output |
| 764 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 765 | def _syscmd_file(target, default=''): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 766 | |
| 767 | """ Interface to the system's file command. |
| 768 | |
| 769 | 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] | 770 | omit the filename in its output. Follow the symlinks. It returns |
| 771 | default in case the command should fail. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 772 | |
| 773 | """ |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 774 | if sys.platform in ('dos', 'win32', 'win16'): |
Hirokazu Yamamoto | d26782e | 2008-09-01 14:35:47 +0000 | [diff] [blame] | 775 | # XXX Others too ? |
| 776 | return default |
Jesus Cea | fc990e9 | 2012-10-04 13:51:43 +0200 | [diff] [blame] | 777 | target = _follow_symlinks(target) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 778 | try: |
Jesus Cea | 685fffa | 2012-10-05 05:21:42 +0200 | [diff] [blame] | 779 | proc = subprocess.Popen(['file', target], |
| 780 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
Jesus Cea | e8801e2 | 2012-10-04 13:56:23 +0200 | [diff] [blame] | 781 | |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 782 | except (AttributeError, OSError): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 783 | return default |
Jesus Cea | cb95996 | 2012-10-05 05:31:31 +0200 | [diff] [blame] | 784 | output = proc.communicate()[0].decode('latin-1') |
Jesus Cea | fc990e9 | 2012-10-04 13:51:43 +0200 | [diff] [blame] | 785 | rc = proc.wait() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 786 | if not output or rc: |
| 787 | return default |
| 788 | else: |
| 789 | return output |
| 790 | |
| 791 | ### Information about the used architecture |
| 792 | |
| 793 | # Default values for architecture; non-empty strings override the |
| 794 | # defaults given as parameters |
| 795 | _default_architecture = { |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 796 | 'win32': ('', 'WindowsPE'), |
| 797 | 'win16': ('', 'Windows'), |
| 798 | 'dos': ('', 'MSDOS'), |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 801 | def architecture(executable=sys.executable, bits='', linkage=''): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 802 | |
| 803 | """ Queries the given executable (defaults to the Python interpreter |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 804 | binary) for various architecture information. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 805 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 806 | Returns a tuple (bits, linkage) which contains information about |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 807 | the bit architecture and the linkage format used for the |
| 808 | executable. Both values are returned as strings. |
| 809 | |
| 810 | Values that cannot be determined are returned as given by the |
| 811 | parameter presets. If bits is given as '', the sizeof(pointer) |
| 812 | (or sizeof(long) on Python version < 1.5.2) is used as |
| 813 | indicator for the supported pointer size. |
| 814 | |
| 815 | The function relies on the system's "file" command to do the |
| 816 | actual work. This is available on most if not all Unix |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 817 | platforms. On some non-Unix platforms where the "file" command |
| 818 | does not exist and the executable is set to the Python interpreter |
| 819 | binary defaults from _default_architecture are used. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 820 | |
| 821 | """ |
| 822 | # Use the sizeof(pointer) as default number of bits if nothing |
| 823 | # else is given as default. |
| 824 | if not bits: |
| 825 | import struct |
| 826 | try: |
| 827 | size = struct.calcsize('P') |
| 828 | except struct.error: |
| 829 | # Older installations can only query longs |
| 830 | size = struct.calcsize('l') |
| 831 | bits = str(size*8) + 'bit' |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 832 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 833 | # Get data from the 'file' system command |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 834 | if executable: |
Victor Stinner | ddfb2c3 | 2010-08-13 16:30:15 +0000 | [diff] [blame] | 835 | fileout = _syscmd_file(executable, '') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 836 | else: |
Victor Stinner | ddfb2c3 | 2010-08-13 16:30:15 +0000 | [diff] [blame] | 837 | fileout = '' |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 838 | |
Victor Stinner | ddfb2c3 | 2010-08-13 16:30:15 +0000 | [diff] [blame] | 839 | if not fileout and \ |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 840 | executable == sys.executable: |
| 841 | # "file" command did not return anything; we'll try to provide |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 842 | # some sensible defaults then... |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 843 | if sys.platform in _default_architecture: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 844 | b, l = _default_architecture[sys.platform] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 845 | if b: |
| 846 | bits = b |
| 847 | if l: |
| 848 | linkage = l |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 849 | return bits, linkage |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 850 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 851 | if 'executable' not in fileout: |
| 852 | # Format not supported |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 853 | return bits, linkage |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 854 | |
| 855 | # Bits |
| 856 | if '32-bit' in fileout: |
| 857 | bits = '32bit' |
| 858 | elif 'N32' in fileout: |
| 859 | # On Irix only |
| 860 | bits = 'n32bit' |
| 861 | elif '64-bit' in fileout: |
| 862 | bits = '64bit' |
| 863 | |
| 864 | # Linkage |
| 865 | if 'ELF' in fileout: |
| 866 | linkage = 'ELF' |
| 867 | elif 'PE' in fileout: |
| 868 | # E.g. Windows uses this format |
| 869 | if 'Windows' in fileout: |
| 870 | linkage = 'WindowsPE' |
| 871 | else: |
| 872 | linkage = 'PE' |
| 873 | elif 'COFF' in fileout: |
| 874 | linkage = 'COFF' |
| 875 | elif 'MS-DOS' in fileout: |
| 876 | linkage = 'MSDOS' |
| 877 | else: |
| 878 | # XXX the A.OUT format also falls under this class... |
| 879 | pass |
| 880 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 881 | return bits, linkage |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 882 | |
| 883 | ### Portable uname() interface |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 884 | |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 885 | uname_result = collections.namedtuple("uname_result", |
| 886 | "system node release version machine processor") |
| 887 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 888 | _uname_cache = None |
| 889 | |
| 890 | def uname(): |
| 891 | |
| 892 | """ Fairly portable uname interface. Returns a tuple |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 893 | of strings (system, node, release, version, machine, processor) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 894 | identifying the underlying platform. |
| 895 | |
| 896 | Note that unlike the os.uname function this also returns |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 897 | possible processor information as an additional tuple entry. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 898 | |
| 899 | Entries which cannot be determined are set to ''. |
| 900 | |
| 901 | """ |
| 902 | global _uname_cache |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 903 | no_os_uname = 0 |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 904 | |
| 905 | if _uname_cache is not None: |
| 906 | return _uname_cache |
| 907 | |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 908 | processor = '' |
| 909 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 910 | # Get some infos from the builtin os.uname API... |
| 911 | try: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 912 | system, node, release, version, machine = os.uname() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 913 | except AttributeError: |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 914 | no_os_uname = 1 |
| 915 | |
Georg Brandl | 62e2ca2 | 2010-07-31 21:54:24 +0000 | [diff] [blame] | 916 | 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] | 917 | # Hmm, no there is either no uname or uname has returned |
| 918 | #'unknowns'... we'll have to poke around the system then. |
| 919 | if no_os_uname: |
| 920 | system = sys.platform |
| 921 | release = '' |
| 922 | version = '' |
| 923 | node = _node() |
| 924 | machine = '' |
| 925 | |
Amaury Forgeot d'Arc | 7a01984 | 2008-06-17 21:42:46 +0000 | [diff] [blame] | 926 | use_syscmd_ver = 1 |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 927 | |
| 928 | # Try win32_ver() on win32 platforms |
| 929 | if system == 'win32': |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 930 | release, version, csd, ptype = win32_ver() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 931 | if release and version: |
| 932 | use_syscmd_ver = 0 |
Christian Heimes | 02781dc | 2008-03-21 01:11:52 +0000 | [diff] [blame] | 933 | # Try to use the PROCESSOR_* environment variables |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 934 | # available on Win XP and later; see |
| 935 | # http://support.microsoft.com/kb/888731 and |
| 936 | # 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] | 937 | if not machine: |
R. David Murray | ca2edce | 2010-03-22 17:48:48 +0000 | [diff] [blame] | 938 | # WOW64 processes mask the native architecture |
| 939 | if "PROCESSOR_ARCHITEW6432" in os.environ: |
| 940 | machine = os.environ.get("PROCESSOR_ARCHITEW6432", '') |
| 941 | else: |
| 942 | machine = os.environ.get('PROCESSOR_ARCHITECTURE', '') |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 943 | if not processor: |
| 944 | processor = os.environ.get('PROCESSOR_IDENTIFIER', machine) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 945 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 946 | # Try the 'ver' system command available on some |
| 947 | # platforms |
| 948 | if use_syscmd_ver: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 949 | system, release, version = _syscmd_ver(system) |
Marc-André Lemburg | cdc7923 | 2004-06-19 17:17:00 +0000 | [diff] [blame] | 950 | # Normalize system to what win32_ver() normally returns |
| 951 | # (_syscmd_ver() tends to return the vendor name as well) |
| 952 | if system == 'Microsoft Windows': |
| 953 | system = 'Windows' |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 954 | elif system == 'Microsoft' and release == 'Windows': |
| 955 | # Under Windows Vista and Windows Server 2008, |
| 956 | # Microsoft changed the output of the ver command. The |
| 957 | # release is no longer printed. This causes the |
| 958 | # system and release to be misidentified. |
| 959 | system = 'Windows' |
| 960 | if '6.0' == version[:3]: |
| 961 | release = 'Vista' |
| 962 | else: |
| 963 | release = '' |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 964 | |
| 965 | # In case we still don't know anything useful, we'll try to |
| 966 | # help ourselves |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 967 | if system in ('win32', 'win16'): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 968 | if not version: |
| 969 | if system == 'win32': |
| 970 | version = '32bit' |
| 971 | else: |
| 972 | version = '16bit' |
| 973 | system = 'Windows' |
| 974 | |
| 975 | elif system[:4] == 'java': |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 976 | release, vendor, vminfo, osinfo = java_ver() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 977 | system = 'Java' |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 978 | version = ', '.join(vminfo) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 979 | if not version: |
| 980 | version = vendor |
| 981 | |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 982 | # System specific extensions |
| 983 | if system == 'OpenVMS': |
| 984 | # OpenVMS seems to have release and version mixed up |
| 985 | if not release or release == '0': |
| 986 | release = version |
| 987 | version = '' |
| 988 | # Get processor information |
| 989 | try: |
| 990 | import vms_lib |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 991 | except ImportError: |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 992 | pass |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 993 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 994 | csid, cpu_number = vms_lib.getsyi('SYI$_CPU', 0) |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 995 | if (cpu_number >= 128): |
| 996 | processor = 'Alpha' |
| 997 | else: |
| 998 | processor = 'VAX' |
| 999 | if not processor: |
| 1000 | # Get processor information from the uname system command |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1001 | processor = _syscmd_uname('-p', '') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1002 | |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 1003 | #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] | 1004 | if system == 'unknown': |
| 1005 | system = '' |
| 1006 | if node == 'unknown': |
| 1007 | node = '' |
| 1008 | if release == 'unknown': |
| 1009 | release = '' |
| 1010 | if version == 'unknown': |
| 1011 | version = '' |
| 1012 | if machine == 'unknown': |
| 1013 | machine = '' |
| 1014 | if processor == 'unknown': |
| 1015 | processor = '' |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 1016 | |
| 1017 | # normalize name |
| 1018 | if system == 'Microsoft' and release == 'Windows': |
| 1019 | system = 'Windows' |
| 1020 | release = 'Vista' |
| 1021 | |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1022 | _uname_cache = uname_result(system, node, release, version, |
| 1023 | machine, processor) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1024 | return _uname_cache |
| 1025 | |
| 1026 | ### Direct interfaces to some of the uname() return values |
| 1027 | |
| 1028 | def system(): |
| 1029 | |
| 1030 | """ Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'. |
| 1031 | |
| 1032 | An empty string is returned if the value cannot be determined. |
| 1033 | |
| 1034 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 1035 | return uname().system |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1036 | |
| 1037 | def node(): |
| 1038 | |
Brett Cannon | 8ab27df | 2003-08-05 03:52:04 +0000 | [diff] [blame] | 1039 | """ Returns the computer's network name (which may not be fully |
| 1040 | qualified) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1041 | |
| 1042 | An empty string is returned if the value cannot be determined. |
| 1043 | |
| 1044 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 1045 | return uname().node |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1046 | |
| 1047 | def release(): |
| 1048 | |
| 1049 | """ Returns the system's release, e.g. '2.2.0' or 'NT' |
| 1050 | |
| 1051 | An empty string is returned if the value cannot be determined. |
| 1052 | |
| 1053 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 1054 | return uname().release |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1055 | |
| 1056 | def version(): |
| 1057 | |
| 1058 | """ Returns the system's release version, e.g. '#3 on degas' |
| 1059 | |
| 1060 | An empty string is returned if the value cannot be determined. |
| 1061 | |
| 1062 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 1063 | return uname().version |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1064 | |
| 1065 | def machine(): |
| 1066 | |
| 1067 | """ Returns the machine type, e.g. 'i386' |
| 1068 | |
| 1069 | An empty string is returned if the value cannot be determined. |
| 1070 | |
| 1071 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 1072 | return uname().machine |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1073 | |
| 1074 | def processor(): |
| 1075 | |
| 1076 | """ Returns the (true) processor name, e.g. 'amdk6' |
| 1077 | |
| 1078 | An empty string is returned if the value cannot be |
| 1079 | determined. Note that many platforms do not provide this |
| 1080 | information or simply return the same value as for machine(), |
| 1081 | e.g. NetBSD does this. |
| 1082 | |
| 1083 | """ |
Larry Hastings | 68386bc | 2012-06-24 14:30:41 -0700 | [diff] [blame] | 1084 | return uname().processor |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1085 | |
| 1086 | ### Various APIs for extracting information from sys.version |
| 1087 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1088 | _sys_version_parser = re.compile( |
Martin Panter | 4e50553 | 2016-06-08 06:12:22 +0000 | [diff] [blame] | 1089 | r'([\w.+]+)\s*' # "version<space>" |
| 1090 | r'\(#?([^,]+)' # "(#buildno" |
| 1091 | r'(?:,\s*([\w ]*)' # ", builddate" |
| 1092 | r'(?:,\s*([\w :]*))?)?\)\s*' # ", buildtime)<space>" |
| 1093 | r'\[([^\]]+)\]?', re.ASCII) # "[compiler]" |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1094 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1095 | _ironpython_sys_version_parser = re.compile( |
| 1096 | r'IronPython\s*' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1097 | r'([\d\.]+)' |
| 1098 | r'(?: \(([\d\.]+)\))?' |
| 1099 | r' on (.NET [\d\.]+)', re.ASCII) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1100 | |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 1101 | # IronPython covering 2.6 and 2.7 |
| 1102 | _ironpython26_sys_version_parser = re.compile( |
| 1103 | r'([\d.]+)\s*' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1104 | r'\(IronPython\s*' |
| 1105 | r'[\d.]+\s*' |
| 1106 | r'\(([\d.]+)\) on ([\w.]+ [\d.]+(?: \(\d+-bit\))?)\)' |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 1107 | ) |
| 1108 | |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1109 | _pypy_sys_version_parser = re.compile( |
| 1110 | r'([\w.+]+)\s*' |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1111 | r'\(#?([^,]+),\s*([\w ]+),\s*([\w :]+)\)\s*' |
| 1112 | r'\[PyPy [^\]]+\]?') |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1113 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1114 | _sys_version_cache = {} |
| 1115 | |
| 1116 | def _sys_version(sys_version=None): |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1117 | |
| 1118 | """ Returns a parsed version of Python's sys.version as tuple |
Benjamin Peterson | 5f28b7b | 2009-03-26 21:49:58 +0000 | [diff] [blame] | 1119 | (name, version, branch, revision, buildno, builddate, compiler) |
| 1120 | referring to the Python implementation name, version, branch, |
| 1121 | revision, build number, build date/time as string and the compiler |
| 1122 | identification string. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1123 | |
| 1124 | Note that unlike the Python sys.version, the returned value |
| 1125 | for the Python version will always include the patchlevel (it |
| 1126 | defaults to '.0'). |
| 1127 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1128 | The function returns empty strings for tuple entries that |
| 1129 | cannot be determined. |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1130 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1131 | sys_version may be given to parse an alternative version |
| 1132 | string, e.g. if the version was read from a different Python |
| 1133 | interpreter. |
| 1134 | |
| 1135 | """ |
| 1136 | # Get the Python version |
| 1137 | if sys_version is None: |
| 1138 | sys_version = sys.version |
| 1139 | |
| 1140 | # Try the cache first |
| 1141 | result = _sys_version_cache.get(sys_version, None) |
| 1142 | if result is not None: |
| 1143 | return result |
| 1144 | |
| 1145 | # Parse it |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 1146 | if 'IronPython' in sys_version: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1147 | # IronPython |
| 1148 | name = 'IronPython' |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 1149 | if sys_version.startswith('IronPython'): |
| 1150 | match = _ironpython_sys_version_parser.match(sys_version) |
| 1151 | else: |
| 1152 | match = _ironpython26_sys_version_parser.match(sys_version) |
| 1153 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1154 | if match is None: |
| 1155 | raise ValueError( |
| 1156 | 'failed to parse IronPython sys.version: %s' % |
| 1157 | repr(sys_version)) |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 1158 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1159 | version, alt_version, compiler = match.groups() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1160 | buildno = '' |
| 1161 | builddate = '' |
| 1162 | |
Ezio Melotti | f076f53 | 2013-10-21 03:03:32 +0300 | [diff] [blame] | 1163 | elif sys.platform.startswith('java'): |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1164 | # Jython |
| 1165 | name = 'Jython' |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1166 | match = _sys_version_parser.match(sys_version) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1167 | if match is None: |
| 1168 | raise ValueError( |
| 1169 | 'failed to parse Jython sys.version: %s' % |
| 1170 | repr(sys_version)) |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1171 | version, buildno, builddate, buildtime, _ = match.groups() |
Martin Panter | 4e50553 | 2016-06-08 06:12:22 +0000 | [diff] [blame] | 1172 | if builddate is None: |
| 1173 | builddate = '' |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1174 | compiler = sys.platform |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1175 | |
| 1176 | elif "PyPy" in sys_version: |
| 1177 | # PyPy |
| 1178 | name = "PyPy" |
| 1179 | match = _pypy_sys_version_parser.match(sys_version) |
| 1180 | if match is None: |
| 1181 | raise ValueError("failed to parse PyPy sys.version: %s" % |
| 1182 | repr(sys_version)) |
| 1183 | version, buildno, builddate, buildtime = match.groups() |
| 1184 | compiler = "" |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1185 | |
| 1186 | else: |
| 1187 | # CPython |
| 1188 | match = _sys_version_parser.match(sys_version) |
| 1189 | if match is None: |
| 1190 | raise ValueError( |
| 1191 | 'failed to parse CPython sys.version: %s' % |
| 1192 | repr(sys_version)) |
| 1193 | version, buildno, builddate, buildtime, compiler = \ |
| 1194 | match.groups() |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1195 | name = 'CPython' |
Martin Panter | 4e50553 | 2016-06-08 06:12:22 +0000 | [diff] [blame] | 1196 | if builddate is None: |
| 1197 | builddate = '' |
| 1198 | elif buildtime: |
| 1199 | builddate = builddate + ' ' + buildtime |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1200 | |
Georg Brandl | 8256242 | 2011-03-05 21:09:22 +0100 | [diff] [blame] | 1201 | if hasattr(sys, '_mercurial'): |
| 1202 | _, branch, revision = sys._mercurial |
| 1203 | elif hasattr(sys, 'subversion'): |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 1204 | # sys.subversion was added in Python 2.5 |
| 1205 | _, branch, revision = sys.subversion |
| 1206 | else: |
| 1207 | branch = '' |
| 1208 | revision = '' |
| 1209 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1210 | # Add the patchlevel version if missing |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1211 | l = version.split('.') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1212 | if len(l) == 2: |
| 1213 | l.append('0') |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1214 | version = '.'.join(l) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1215 | |
| 1216 | # Build and cache the result |
| 1217 | result = (name, version, branch, revision, buildno, builddate, compiler) |
| 1218 | _sys_version_cache[sys_version] = result |
| 1219 | return result |
| 1220 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1221 | def python_implementation(): |
| 1222 | |
| 1223 | """ Returns a string identifying the Python implementation. |
| 1224 | |
| 1225 | Currently, the following implementations are identified: |
Ezio Melotti | f16898b | 2011-05-04 18:37:50 +0300 | [diff] [blame] | 1226 | 'CPython' (C implementation of Python), |
| 1227 | 'IronPython' (.NET implementation of Python), |
| 1228 | 'Jython' (Java implementation of Python), |
| 1229 | 'PyPy' (Python implementation of Python). |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1230 | |
| 1231 | """ |
| 1232 | return _sys_version()[0] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1233 | |
| 1234 | def python_version(): |
| 1235 | |
| 1236 | """ Returns the Python version as string 'major.minor.patchlevel' |
| 1237 | |
| 1238 | Note that unlike the Python sys.version, the returned value |
| 1239 | will always include the patchlevel (it defaults to 0). |
| 1240 | |
| 1241 | """ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1242 | return _sys_version()[1] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1243 | |
| 1244 | def python_version_tuple(): |
| 1245 | |
| 1246 | """ Returns the Python version as tuple (major, minor, patchlevel) |
| 1247 | of strings. |
| 1248 | |
| 1249 | Note that unlike the Python sys.version, the returned value |
| 1250 | will always include the patchlevel (it defaults to 0). |
| 1251 | |
| 1252 | """ |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1253 | return tuple(_sys_version()[1].split('.')) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1254 | |
| 1255 | def python_branch(): |
| 1256 | |
| 1257 | """ Returns a string identifying the Python implementation |
| 1258 | branch. |
| 1259 | |
| 1260 | For CPython this is the Subversion branch from which the |
| 1261 | Python binary was built. |
| 1262 | |
| 1263 | If not available, an empty string is returned. |
| 1264 | |
| 1265 | """ |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1266 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1267 | return _sys_version()[2] |
| 1268 | |
| 1269 | def python_revision(): |
| 1270 | |
| 1271 | """ Returns a string identifying the Python implementation |
| 1272 | revision. |
| 1273 | |
| 1274 | For CPython this is the Subversion revision from which the |
| 1275 | Python binary was built. |
| 1276 | |
| 1277 | If not available, an empty string is returned. |
| 1278 | |
| 1279 | """ |
| 1280 | return _sys_version()[3] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1281 | |
| 1282 | def python_build(): |
| 1283 | |
| 1284 | """ Returns a tuple (buildno, builddate) stating the Python |
| 1285 | build number and date as strings. |
| 1286 | |
| 1287 | """ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1288 | return _sys_version()[4:6] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1289 | |
| 1290 | def python_compiler(): |
| 1291 | |
| 1292 | """ Returns a string identifying the compiler used for compiling |
| 1293 | Python. |
| 1294 | |
| 1295 | """ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1296 | return _sys_version()[6] |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1297 | |
| 1298 | ### The Opus Magnum of platform strings :-) |
| 1299 | |
Marc-André Lemburg | 91e83e2 | 2004-03-25 18:35:12 +0000 | [diff] [blame] | 1300 | _platform_cache = {} |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1301 | |
| 1302 | def platform(aliased=0, terse=0): |
| 1303 | |
| 1304 | """ Returns a single string identifying the underlying platform |
| 1305 | with as much useful information as possible (but no more :). |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 1306 | |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1307 | The output is intended to be human readable rather than |
| 1308 | machine parseable. It may look different on different |
| 1309 | platforms and this is intended. |
| 1310 | |
| 1311 | If "aliased" is true, the function will use aliases for |
| 1312 | various platforms that report system names which differ from |
| 1313 | their common names, e.g. SunOS will be reported as |
| 1314 | Solaris. The system_alias() function is used to implement |
| 1315 | this. |
| 1316 | |
| 1317 | Setting terse to true causes the function to return only the |
| 1318 | absolute minimum information needed to identify the platform. |
| 1319 | |
| 1320 | """ |
Marc-André Lemburg | 91e83e2 | 2004-03-25 18:35:12 +0000 | [diff] [blame] | 1321 | result = _platform_cache.get((aliased, terse), None) |
| 1322 | if result is not None: |
| 1323 | return result |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1324 | |
| 1325 | # Get uname information and then apply platform specific cosmetics |
| 1326 | # to it... |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1327 | system, node, release, version, machine, processor = uname() |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1328 | if machine == processor: |
| 1329 | processor = '' |
| 1330 | if aliased: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1331 | system, release, version = system_alias(system, release, version) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1332 | |
| 1333 | if system == 'Windows': |
| 1334 | # MS platforms |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1335 | rel, vers, csd, ptype = win32_ver(version) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1336 | if terse: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1337 | platform = _platform(system, release) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1338 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1339 | platform = _platform(system, release, version, csd) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1340 | |
| 1341 | elif system in ('Linux',): |
| 1342 | # Linux based systems |
Berker Peksag | 1392f71 | 2015-05-16 20:24:28 +0300 | [diff] [blame] | 1343 | with warnings.catch_warnings(): |
| 1344 | # see issue #1322 for more information |
| 1345 | warnings.filterwarnings( |
| 1346 | 'ignore', |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 1347 | r'dist\(\) and linux_distribution\(\) ' |
Berker Peksag | 1392f71 | 2015-05-16 20:24:28 +0300 | [diff] [blame] | 1348 | 'functions are deprecated .*', |
| 1349 | PendingDeprecationWarning, |
| 1350 | ) |
| 1351 | distname, distversion, distid = dist('') |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1352 | if distname and not terse: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1353 | platform = _platform(system, release, machine, processor, |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1354 | 'with', |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1355 | distname, distversion, distid) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1356 | else: |
| 1357 | # If the distribution name is unknown check for libc vs. glibc |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1358 | libcname, libcversion = libc_ver(sys.executable) |
| 1359 | platform = _platform(system, release, machine, processor, |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1360 | 'with', |
| 1361 | libcname+libcversion) |
| 1362 | elif system == 'Java': |
| 1363 | # Java platforms |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1364 | r, v, vminfo, (os_name, os_version, os_arch) = java_ver() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 1365 | if terse or not os_name: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1366 | platform = _platform(system, release, version) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1367 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1368 | platform = _platform(system, release, version, |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1369 | 'on', |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1370 | os_name, os_version, os_arch) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1371 | |
| 1372 | elif system == 'MacOS': |
| 1373 | # MacOS platforms |
| 1374 | if terse: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1375 | platform = _platform(system, release) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1376 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1377 | platform = _platform(system, release, machine) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1378 | |
| 1379 | else: |
| 1380 | # Generic handler |
| 1381 | if terse: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1382 | platform = _platform(system, release) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1383 | else: |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1384 | bits, linkage = architecture(sys.executable) |
| 1385 | platform = _platform(system, release, machine, |
| 1386 | processor, bits, linkage) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 1387 | |
Marc-André Lemburg | 91e83e2 | 2004-03-25 18:35:12 +0000 | [diff] [blame] | 1388 | _platform_cache[(aliased, terse)] = platform |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1389 | return platform |
| 1390 | |
| 1391 | ### Command line interface |
| 1392 | |
| 1393 | if __name__ == '__main__': |
| 1394 | # Default is to print the aliased verbose platform string |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 1395 | terse = ('terse' in sys.argv or '--terse' in sys.argv) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1396 | aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv) |
Victor Stinner | ced3936 | 2013-12-09 00:14:52 +0100 | [diff] [blame] | 1397 | print(platform(aliased, terse)) |
Marc-André Lemburg | 246d847 | 2003-04-24 11:36:11 +0000 | [diff] [blame] | 1398 | sys.exit(0) |