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