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