diff --git a/Lib/platform.py b/Lib/platform.py index ddddff4eb92..48b81ac7e50 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -122,7 +122,7 @@ try: except AttributeError: # os.devnull was added in Python 2.4, so emulate it for earlier # Python versions - if sys.platform in ('dos','win32','win16'): + if sys.platform in ('dos', 'win32', 'win16'): # Use the old CP/M NUL as device name DEV_NULL = 'NUL' else: @@ -141,7 +141,7 @@ _libc_search = re.compile(b'(__libc_init)' b'|' br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII) -def libc_ver(executable=sys.executable,lib='',version='', +def libc_ver(executable=sys.executable, lib='', version='', chunksize=16384): @@ -163,12 +163,12 @@ def libc_ver(executable=sys.executable,lib='',version='', # here to work around problems with Cygwin not being # able to open symlinks for reading executable = os.path.realpath(executable) - f = open(executable,'rb') + f = open(executable, 'rb') binary = f.read(chunksize) pos = 0 while 1: if b'libc' in binary or b'GLIBC' in binary: - m = _libc_search.search(binary,pos) + m = _libc_search.search(binary, pos) else: m = None if not m: @@ -177,7 +177,7 @@ def libc_ver(executable=sys.executable,lib='',version='', break pos = 0 continue - libcinit,glibc,glibcversion,so,threads,soversion = [ + libcinit, glibc, glibcversion, so, threads, soversion = [ s.decode('latin1') if s is not None else s for s in m.groups()] if libcinit and not lib: @@ -197,9 +197,9 @@ def libc_ver(executable=sys.executable,lib='',version='', version = version + threads pos = m.end() f.close() - return lib,version + return lib, version -def _dist_try_harder(distname,version,id): +def _dist_try_harder(distname, version, id): """ Tries some special tricks to get the distribution information in case the default method fails. @@ -214,7 +214,7 @@ def _dist_try_harder(distname,version,id): for line in open('/var/adm/inst-log/info'): tv = line.split() if len(tv) == 2: - tag,value = tv + tag, value = tv else: continue if tag == 'MIN_DIST_VERSION': @@ -222,7 +222,7 @@ def _dist_try_harder(distname,version,id): elif tag == 'DIST_IDENT': values = value.split('-') id = values[2] - return distname,version,id + return distname, version, id if os.path.exists('/etc/.installed'): # Caldera OpenLinux has some infos in that file (thanks to Colin Kong) @@ -231,7 +231,7 @@ def _dist_try_harder(distname,version,id): if len(pkg) >= 2 and pkg[0] == 'OpenLinux': # XXX does Caldera support non Intel platforms ? If yes, # where can we find the needed id ? - return 'OpenLinux',pkg[1],id + return 'OpenLinux', pkg[1], id if os.path.isdir('/usr/lib/setup'): # Check for slackware version tag file (thanks to Greg Andruk) @@ -243,9 +243,9 @@ def _dist_try_harder(distname,version,id): verfiles.sort() distname = 'slackware' version = verfiles[-1][14:] - return distname,version,id + return distname, version, id - return distname,version,id + return distname, version, id _release_filename = re.compile(r'(\w+)[-_](release|version)', re.ASCII) _lsb_release_version = re.compile(r'(.+)' @@ -314,7 +314,7 @@ def linux_distribution(distname='', version='', id='', distribution read from the OS is returned. Otherwise the short name taken from supported_dists is used. - Returns a tuple (distname,version,id) which default to the + Returns a tuple (distname, version, id) which default to the args given as parameters. """ @@ -322,17 +322,17 @@ def linux_distribution(distname='', version='', id='', etc = os.listdir(_UNIXCONFDIR) except OSError: # Probably not a Unix system - return distname,version,id + return distname, version, id etc.sort() for file in etc: m = _release_filename.match(file) if m is not None: - _distname,dummy = m.groups() + _distname, dummy = m.groups() if _distname in supported_dists: distname = _distname break else: - return _dist_try_harder(distname,version,id) + return _dist_try_harder(distname, version, id) # Read the first line with open(os.path.join(_UNIXCONFDIR, file), 'r', @@ -350,7 +350,7 @@ def linux_distribution(distname='', version='', id='', # To maintain backwards compatibility: -def dist(distname='',version='',id='', +def dist(distname='', version='', id='', supported_dists=_supported_dists): @@ -360,7 +360,7 @@ def dist(distname='',version='',id='', /etc and then reverts to _dist_try_harder() in case no suitable files are found. - Returns a tuple (distname,version,id) which default to the + Returns a tuple (distname, version, id) which default to the args given as parameters. """ @@ -385,11 +385,11 @@ def _norm_version(version, build=''): if build: l.append(build) try: - ints = map(int,l) + ints = map(int, l) except ValueError: strings = l else: - strings = list(map(str,ints)) + strings = list(map(str, ints)) version = '.'.join(strings[:3]) return version @@ -408,10 +408,10 @@ _ver_output = re.compile(r'(?:([\w ]+) ([\w.]+) ' def _syscmd_ver(system='', release='', version='', - supported_platforms=('win32','win16','dos')): + supported_platforms=('win32', 'win16', 'dos')): """ Tries to figure out the OS version used and returns - a tuple (system,release,version). + a tuple (system, release, version). It uses the "ver" shell command for this which is known to exists on Windows, DOS. XXX Others too ? @@ -421,10 +421,10 @@ def _syscmd_ver(system='', release='', version='', """ if sys.platform not in supported_platforms: - return system,release,version + return system, release, version # Try some common cmd strings - for cmd in ('ver','command /c ver','cmd /c ver'): + for cmd in ('ver', 'command /c ver', 'cmd /c ver'): try: pipe = popen(cmd) info = pipe.read() @@ -433,18 +433,18 @@ def _syscmd_ver(system='', release='', version='', # XXX How can I suppress shell errors from being written # to stderr ? except OSError as why: - #print 'Command %s failed: %s' % (cmd,why) + #print 'Command %s failed: %s' % (cmd, why) continue else: break else: - return system,release,version + return system, release, version # Parse the output info = info.strip() m = _ver_output.match(info) if m is not None: - system,release,version = m.groups() + system, release, version = m.groups() # Strip trailing dots from version and release if release[-1] == '.': release = release[:-1] @@ -453,9 +453,9 @@ def _syscmd_ver(system='', release='', version='', # Normalize the version and build strings (eliminating additional # zeros) version = _norm_version(version) - return system,release,version + return system, release, version -def _win32_getvalue(key,name,default=''): +def _win32_getvalue(key, name, default=''): """ Read a value for name from the registry key. @@ -470,14 +470,14 @@ def _win32_getvalue(key,name,default=''): import winreg RegQueryValueEx = winreg.QueryValueEx try: - return RegQueryValueEx(key,name) + return RegQueryValueEx(key, name) except: return default -def win32_ver(release='',version='',csd='',ptype=''): +def win32_ver(release='', version='', csd='', ptype=''): """ Get additional version information from the Windows Registry - and return a tuple (version,csd,ptype) referring to version + and return a tuple (version, csd, ptype) referring to version number, CSD level (service pack), and OS type (multi/single processor). @@ -514,7 +514,7 @@ def win32_ver(release='',version='',csd='',ptype=''): sys.getwindowsversion except AttributeError: # No emulation possible, so return the defaults... - return release,version,csd,ptype + return release, version, csd, ptype else: # Emulation using winreg (added in Python 2.0) and # sys.getwindowsversion() (added in Python 2.3) @@ -532,8 +532,8 @@ def win32_ver(release='',version='',csd='',ptype=''): # Find out the registry key and some general version infos winver = GetVersionEx() - maj,min,buildno,plat,csd = winver - version = '%i.%i.%i' % (maj,min,buildno & 0xFFFF) + maj, min, buildno, plat, csd = winver + version = '%i.%i.%i' % (maj, min, buildno & 0xFFFF) if hasattr(winver, "service_pack"): if winver.service_pack != "": csd = 'SP%s' % winver.service_pack_major @@ -608,8 +608,8 @@ def win32_ver(release='',version='',csd='',ptype=''): else: if not release: # E.g. Win3.1 with win32s - release = '%i.%i' % (maj,min) - return release,version,csd,ptype + release = '%i.%i' % (maj, min) + return release, version, csd, ptype # Open the registry key try: @@ -617,7 +617,7 @@ def win32_ver(release='',version='',csd='',ptype=''): # Get a value to make sure the key exists... RegQueryValueEx(keyCurVer, 'SystemRoot') except: - return release,version,csd,ptype + return release, version, csd, ptype # Parse values #subversion = _win32_getvalue(keyCurVer, @@ -627,17 +627,17 @@ def win32_ver(release='',version='',csd='',ptype=''): # release = release + subversion # 95a, 95b, etc. build = _win32_getvalue(keyCurVer, 'CurrentBuildNumber', - ('',1))[0] + ('', 1))[0] ptype = _win32_getvalue(keyCurVer, 'CurrentType', - (ptype,1))[0] + (ptype, 1))[0] # Normalize version - version = _norm_version(version,build) + version = _norm_version(version, build) # Close key RegCloseKey(keyCurVer) - return release,version,csd,ptype + return release, version, csd, ptype def _mac_ver_xml(): fn = '/System/Library/CoreServices/SystemVersion.plist' @@ -651,16 +651,16 @@ def _mac_ver_xml(): pl = plistlib.readPlist(fn) release = pl['ProductVersion'] - versioninfo=('', '', '') + versioninfo = ('', '', '') machine = os.uname().machine if machine in ('ppc', 'Power Macintosh'): # Canonical name machine = 'PowerPC' - return release,versioninfo,machine + return release, versioninfo, machine -def mac_ver(release='',versioninfo=('','',''),machine=''): +def mac_ver(release='', versioninfo=('', '', ''), machine=''): """ Get MacOS version information and return it as tuple (release, versioninfo, machine) with versioninfo being a tuple (version, @@ -677,9 +677,9 @@ def mac_ver(release='',versioninfo=('','',''),machine=''): return info # If that also doesn't work return the default values - return release,versioninfo,machine + return release, versioninfo, machine -def _java_getprop(name,default): +def _java_getprop(name, default): from java.lang import System try: @@ -690,13 +690,13 @@ def _java_getprop(name,default): except AttributeError: return default -def java_ver(release='',vendor='',vminfo=('','',''),osinfo=('','','')): +def java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', '')): """ Version interface for Jython. - Returns a tuple (release,vendor,vminfo,osinfo) with vminfo being - a tuple (vm_name,vm_release,vm_vendor) and osinfo being a - tuple (os_name,os_version,os_arch). + Returns a tuple (release, vendor, vminfo, osinfo) with vminfo being + a tuple (vm_name, vm_release, vm_vendor) and osinfo being a + tuple (os_name, os_version, os_arch). Values which cannot be determined are set to the defaults given as parameters (which all default to ''). @@ -706,7 +706,7 @@ def java_ver(release='',vendor='',vminfo=('','',''),osinfo=('','','')): try: import java.lang except ImportError: - return release,vendor,vminfo,osinfo + return release, vendor, vminfo, osinfo vendor = _java_getprop('java.vendor', vendor) release = _java_getprop('java.version', release) @@ -725,9 +725,9 @@ def java_ver(release='',vendor='',vminfo=('','',''),osinfo=('','','')): ### System name aliasing -def system_alias(system,release,version): +def system_alias(system, release, version): - """ Returns (system,release,version) aliased to common + """ Returns (system, release, version) aliased to common marketing names used for some systems. It also does some reordering of the information in some cases @@ -737,13 +737,13 @@ def system_alias(system,release,version): if system == 'Rhapsody': # Apple's BSD derivative # XXX How can we determine the marketing release number ? - return 'MacOS X Server',system+release,version + return 'MacOS X Server', system+release, version elif system == 'SunOS': # Sun's OS if release < '5': # These releases use the old name SunOS - return system,release,version + return system, release, version # Modify release (marketing release = SunOS release - 3) l = release.split('.') if l: @@ -771,11 +771,11 @@ def system_alias(system,release,version): else: version = '64bit' - elif system in ('win32','win16'): + elif system in ('win32', 'win16'): # In case one of the other tricks system = 'Windows' - return system,release,version + return system, release, version ### Various internal helpers @@ -788,21 +788,21 @@ def _platform(*args): platform = '-'.join(x.strip() for x in filter(len, args)) # Cleanup some possible filename obstacles... - platform = platform.replace(' ','_') - platform = platform.replace('/','-') - platform = platform.replace('\\','-') - platform = platform.replace(':','-') - platform = platform.replace(';','-') - platform = platform.replace('"','-') - platform = platform.replace('(','-') - platform = platform.replace(')','-') + platform = platform.replace(' ', '_') + platform = platform.replace('/', '-') + platform = platform.replace('\\', '-') + platform = platform.replace(':', '-') + platform = platform.replace(';', '-') + platform = platform.replace('"', '-') + platform = platform.replace('(', '-') + platform = platform.replace(')', '-') # No need to report 'unknown' information... - platform = platform.replace('unknown','') + platform = platform.replace('unknown', '') # Fold '--'s and remove trailing '-' while 1: - cleaned = platform.replace('--','-') + cleaned = platform.replace('--', '-') if cleaned == platform: break platform = cleaned @@ -834,14 +834,14 @@ def _follow_symlinks(filepath): filepath = os.path.abspath(filepath) while os.path.islink(filepath): filepath = os.path.normpath( - os.path.join(os.path.dirname(filepath),os.readlink(filepath))) + os.path.join(os.path.dirname(filepath), os.readlink(filepath))) return filepath -def _syscmd_uname(option,default=''): +def _syscmd_uname(option, default=''): """ Interface to the system's uname command. """ - if sys.platform in ('dos','win32','win16'): + if sys.platform in ('dos', 'win32', 'win16'): # XXX Others too ? return default try: @@ -855,7 +855,7 @@ def _syscmd_uname(option,default=''): else: return output -def _syscmd_file(target,default=''): +def _syscmd_file(target, default=''): """ Interface to the system's file command. @@ -864,7 +864,7 @@ def _syscmd_file(target,default=''): default in case the command should fail. """ - if sys.platform in ('dos','win32','win16'): + if sys.platform in ('dos', 'win32', 'win16'): # XXX Others too ? return default target = _follow_symlinks(target) @@ -886,17 +886,17 @@ def _syscmd_file(target,default=''): # Default values for architecture; non-empty strings override the # defaults given as parameters _default_architecture = { - 'win32': ('','WindowsPE'), - 'win16': ('','Windows'), - 'dos': ('','MSDOS'), + 'win32': ('', 'WindowsPE'), + 'win16': ('', 'Windows'), + 'dos': ('', 'MSDOS'), } -def architecture(executable=sys.executable,bits='',linkage=''): +def architecture(executable=sys.executable, bits='', linkage=''): """ Queries the given executable (defaults to the Python interpreter binary) for various architecture information. - Returns a tuple (bits,linkage) which contains information about + Returns a tuple (bits, linkage) which contains information about the bit architecture and the linkage format used for the executable. Both values are returned as strings. @@ -934,16 +934,16 @@ def architecture(executable=sys.executable,bits='',linkage=''): # "file" command did not return anything; we'll try to provide # some sensible defaults then... if sys.platform in _default_architecture: - b,l = _default_architecture[sys.platform] + b, l = _default_architecture[sys.platform] if b: bits = b if l: linkage = l - return bits,linkage + return bits, linkage if 'executable' not in fileout: # Format not supported - return bits,linkage + return bits, linkage # Bits if '32-bit' in fileout: @@ -971,7 +971,7 @@ def architecture(executable=sys.executable,bits='',linkage=''): # XXX the A.OUT format also falls under this class... pass - return bits,linkage + return bits, linkage ### Portable uname() interface @@ -983,7 +983,7 @@ _uname_cache = None def uname(): """ Fairly portable uname interface. Returns a tuple - of strings (system,node,release,version,machine,processor) + of strings (system, node, release, version, machine, processor) identifying the underlying platform. Note that unlike the os.uname function this also returns @@ -1002,7 +1002,7 @@ def uname(): # Get some infos from the builtin os.uname API... try: - system,node,release,version,machine = os.uname() + system, node, release, version, machine = os.uname() except AttributeError: no_os_uname = 1 @@ -1020,7 +1020,7 @@ def uname(): # Try win32_ver() on win32 platforms if system == 'win32': - release,version,csd,ptype = win32_ver() + release, version, csd, ptype = win32_ver() if release and version: use_syscmd_ver = 0 # Try to use the PROCESSOR_* environment variables @@ -1039,7 +1039,7 @@ def uname(): # Try the 'ver' system command available on some # platforms if use_syscmd_ver: - system,release,version = _syscmd_ver(system) + system, release, version = _syscmd_ver(system) # Normalize system to what win32_ver() normally returns # (_syscmd_ver() tends to return the vendor name as well) if system == 'Microsoft Windows': @@ -1057,7 +1057,7 @@ def uname(): # In case we still don't know anything useful, we'll try to # help ourselves - if system in ('win32','win16'): + if system in ('win32', 'win16'): if not version: if system == 'win32': version = '32bit' @@ -1066,7 +1066,7 @@ def uname(): system = 'Windows' elif system[:4] == 'java': - release,vendor,vminfo,osinfo = java_ver() + release, vendor, vminfo, osinfo = java_ver() system = 'Java' version = ', '.join(vminfo) if not version: @@ -1084,14 +1084,14 @@ def uname(): except ImportError: pass else: - csid, cpu_number = vms_lib.getsyi('SYI$_CPU',0) + csid, cpu_number = vms_lib.getsyi('SYI$_CPU', 0) if (cpu_number >= 128): processor = 'Alpha' else: processor = 'VAX' if not processor: # Get processor information from the uname system command - processor = _syscmd_uname('-p','') + processor = _syscmd_uname('-p', '') #If any unknowns still exist, replace them with ''s, which are more portable if system == 'unknown': @@ -1112,7 +1112,8 @@ def uname(): system = 'Windows' release = 'Vista' - _uname_cache = uname_result(system,node,release,version,machine,processor) + _uname_cache = uname_result(system, node, release, version, + machine, processor) return _uname_cache ### Direct interfaces to some of the uname() return values @@ -1409,57 +1410,58 @@ def platform(aliased=0, terse=0): # Get uname information and then apply platform specific cosmetics # to it... - system,node,release,version,machine,processor = uname() + system, node, release, version, machine, processor = uname() if machine == processor: processor = '' if aliased: - system,release,version = system_alias(system,release,version) + system, release, version = system_alias(system, release, version) if system == 'Windows': # MS platforms - rel,vers,csd,ptype = win32_ver(version) + rel, vers, csd, ptype = win32_ver(version) if terse: - platform = _platform(system,release) + platform = _platform(system, release) else: - platform = _platform(system,release,version,csd) + platform = _platform(system, release, version, csd) elif system in ('Linux',): # Linux based systems - distname,distversion,distid = dist('') + distname, distversion, distid = dist('') if distname and not terse: - platform = _platform(system,release,machine,processor, + platform = _platform(system, release, machine, processor, 'with', - distname,distversion,distid) + distname, distversion, distid) else: # If the distribution name is unknown check for libc vs. glibc - libcname,libcversion = libc_ver(sys.executable) - platform = _platform(system,release,machine,processor, + libcname, libcversion = libc_ver(sys.executable) + platform = _platform(system, release, machine, processor, 'with', libcname+libcversion) elif system == 'Java': # Java platforms - r,v,vminfo,(os_name,os_version,os_arch) = java_ver() + r, v, vminfo, (os_name, os_version, os_arch) = java_ver() if terse or not os_name: - platform = _platform(system,release,version) + platform = _platform(system, release, version) else: - platform = _platform(system,release,version, + platform = _platform(system, release, version, 'on', - os_name,os_version,os_arch) + os_name, os_version, os_arch) elif system == 'MacOS': # MacOS platforms if terse: - platform = _platform(system,release) + platform = _platform(system, release) else: - platform = _platform(system,release,machine) + platform = _platform(system, release, machine) else: # Generic handler if terse: - platform = _platform(system,release) + platform = _platform(system, release) else: - bits,linkage = architecture(sys.executable) - platform = _platform(system,release,machine,processor,bits,linkage) + bits, linkage = architecture(sys.executable) + platform = _platform(system, release, machine, + processor, bits, linkage) _platform_cache[(aliased, terse)] = platform return platform @@ -1470,5 +1472,5 @@ if __name__ == '__main__': # Default is to print the aliased verbose platform string terse = ('terse' in sys.argv or '--terse' in sys.argv) aliased = (not 'nonaliased' in sys.argv and not '--nonaliased' in sys.argv) - print(platform(aliased,terse)) + print(platform(aliased, terse)) sys.exit(0)