home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_669 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  25.7 KB  |  959 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __copyright__ = '\n    Copyright (c) 1999-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com\n    Copyright (c) 2000-2008, eGenix.com Software GmbH; mailto:info@egenix.com\n\n    Permission to use, copy, modify, and distribute this software and its\n    documentation for any purpose and without fee or royalty is hereby granted,\n    provided that the above copyright notice appear in all copies and that\n    both that copyright notice and this permission notice appear in\n    supporting documentation or portions thereof, including modifications,\n    that you make.\n\n    EGENIX.COM SOFTWARE GMBH DISCLAIMS ALL WARRANTIES WITH REGARD TO\n    THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND\n    FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,\n    INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING\n    FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,\n    NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION\n    WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !\n\n'
  5. __version__ = '1.0.6'
  6. import sys
  7. import string
  8. import os
  9. import re
  10. _libc_search = re.compile('(__libc_init)|(GLIBC_([0-9.]+))|(libc(_\\w+)?\\.so(?:\\.(\\d[0-9.]*))?)')
  11.  
  12. def libc_ver(executable = sys.executable, lib = '', version = '', chunksize = 2048):
  13.     if hasattr(os.path, 'realpath'):
  14.         executable = os.path.realpath(executable)
  15.     
  16.     f = open(executable, 'rb')
  17.     binary = f.read(chunksize)
  18.     pos = 0
  19.     while None:
  20.         m = _libc_search.search(binary, pos)
  21.         if not m:
  22.             binary = f.read(chunksize)
  23.             if not binary:
  24.                 break
  25.             
  26.             pos = 0
  27.             continue
  28.         
  29.         (libcinit, glibc, glibcversion, so, threads, soversion) = m.groups()
  30.         if libcinit and not lib:
  31.             lib = 'libc'
  32.         elif glibc:
  33.             if lib != 'glibc':
  34.                 lib = 'glibc'
  35.                 version = glibcversion
  36.             elif glibcversion > version:
  37.                 version = glibcversion
  38.             
  39.         elif so:
  40.             if lib != 'glibc':
  41.                 lib = 'libc'
  42.                 if soversion > version:
  43.                     version = soversion
  44.                 
  45.                 if threads and version[-len(threads):] != threads:
  46.                     version = version + threads
  47.                 
  48.             
  49.         
  50.         pos = m.end()
  51.         continue
  52.         return (lib, version)
  53.  
  54.  
  55. def _dist_try_harder(distname, version, id):
  56.     if os.path.exists('/var/adm/inst-log/info'):
  57.         info = open('/var/adm/inst-log/info').readlines()
  58.         distname = 'SuSE'
  59.         for line in info:
  60.             tv = string.split(line)
  61.             if len(tv) == 2:
  62.                 (tag, value) = tv
  63.             
  64.             if tag == 'MIN_DIST_VERSION':
  65.                 version = string.strip(value)
  66.                 continue
  67.             len(tv) == 2
  68.             if tag == 'DIST_IDENT':
  69.                 values = string.split(value, '-')
  70.                 id = values[2]
  71.                 continue
  72.         
  73.         return (distname, version, id)
  74.     if os.path.isdir('/usr/lib/setup'):
  75.         verfiles = os.listdir('/usr/lib/setup')
  76.         for n in range(len(verfiles) - 1, -1, -1):
  77.             if verfiles[n][:14] != 'slack-version-':
  78.                 del verfiles[n]
  79.                 continue
  80.             None if os.path.exists('/etc/.installed') else os.path.exists('/var/adm/inst-log/info')
  81.         
  82.         if verfiles:
  83.             verfiles.sort()
  84.             distname = 'slackware'
  85.             version = verfiles[-1][14:]
  86.             return (distname, version, id)
  87.     
  88.     return (distname, version, id)
  89.  
  90. _release_filename = re.compile('(\\w+)[-_](release|version)')
  91. _lsb_release_version = re.compile('(.+) release ([\\d.]+)[^(]*(?:\\((.+)\\))?')
  92. _release_version = re.compile('([^0-9]+)(?: release )?([\\d.]+)[^(]*(?:\\((.+)\\))?')
  93. _supported_dists = ('SuSE', 'debian', 'fedora', 'redhat', 'centos', 'mandrake', 'mandriva', 'rocks', 'slackware', 'yellowdog', 'gentoo', 'UnitedLinux', 'turbolinux')
  94.  
  95. def _parse_release_file(firstline):
  96.     m = _lsb_release_version.match(firstline)
  97.     if m is not None:
  98.         return tuple(m.groups())
  99.     m = _release_version.match(firstline)
  100.     if m is not None:
  101.         return tuple(m.groups())
  102.     l = string.split(string.strip(firstline))
  103.     return ('', version, id)
  104.  
  105.  
  106. def _test_parse_release_file():
  107.     for input, output in ('SuSE Linux 9.3 (x86-64)', ('SuSE Linux ', '9.3', 'x86-64'))('SUSE LINUX 10.1 (X86-64)', ('SUSE LINUX ', '10.1', 'X86-64'))('SUSE LINUX 10.1 (i586)', ('SUSE LINUX ', '10.1', 'i586'))('Fedora Core release 5 (Bordeaux)', ('Fedora Core', '5', 'Bordeaux'))('Red Hat Linux release 8.0 (Psyche)', ('Red Hat Linux', '8.0', 'Psyche'))('Red Hat Linux release 9 (Shrike)', ('Red Hat Linux', '9', 'Shrike'))('Red Hat Enterprise Linux release 4 (Nahant)', ('Red Hat Enterprise Linux', '4', 'Nahant'))('CentOS release 4', ('CentOS', '4', None))('Rocks release 4.2.1 (Cydonia)', ('Rocks', '4.2.1', 'Cydonia')):
  108.         parsed = _parse_release_file(input)
  109.         if parsed != output:
  110.             print (input, parsed)
  111.             continue
  112.     
  113.  
  114.  
  115. def linux_distribution(distname = '', version = '', id = '', supported_dists = _supported_dists, full_distribution_name = 1):
  116.     
  117.     try:
  118.         etc = os.listdir('/etc')
  119.     except os.error:
  120.         return (distname, version, id)
  121.  
  122.     etc.sort()
  123.     for file in etc:
  124.         m = _release_filename.match(file)
  125.         if m is not None:
  126.             (_distname, dummy) = m.groups()
  127.             if _distname in supported_dists:
  128.                 distname = _distname
  129.                 break
  130.             
  131.         _distname in supported_dists
  132.     else:
  133.         return _dist_try_harder(distname, version, id)
  134.     f = None('/etc/' + file, 'r')
  135.     firstline = f.readline()
  136.     f.close()
  137.     (_distname, _version, _id) = _parse_release_file(firstline)
  138.     if _distname and full_distribution_name:
  139.         distname = _distname
  140.     
  141.     if _version:
  142.         version = _version
  143.     
  144.     if _id:
  145.         id = _id
  146.     
  147.     return (distname, version, id)
  148.  
  149.  
  150. def dist(distname = '', version = '', id = '', supported_dists = _supported_dists):
  151.     return linux_distribution(distname, version, id, supported_dists = supported_dists, full_distribution_name = 0)
  152.  
  153.  
  154. class _popen:
  155.     tmpfile = ''
  156.     pipe = None
  157.     bufsize = None
  158.     mode = 'r'
  159.     
  160.     def __init__(self, cmd, mode = 'r', bufsize = None):
  161.         if mode != 'r':
  162.             raise ValueError, 'popen()-emulation only supports read mode'
  163.         mode != 'r'
  164.         import tempfile
  165.         self.tmpfile = tmpfile = tempfile.mktemp()
  166.         os.system(cmd + ' > %s' % tmpfile)
  167.         self.pipe = open(tmpfile, 'rb')
  168.         self.bufsize = bufsize
  169.         self.mode = mode
  170.  
  171.     
  172.     def read(self):
  173.         return self.pipe.read()
  174.  
  175.     
  176.     def readlines(self):
  177.         if self.bufsize is not None:
  178.             return self.pipe.readlines()
  179.  
  180.     
  181.     def close(self, remove = os.unlink, error = os.error):
  182.         if self.pipe:
  183.             rc = self.pipe.close()
  184.         else:
  185.             rc = 255
  186.         if self.tmpfile:
  187.             
  188.             try:
  189.                 remove(self.tmpfile)
  190.             except error:
  191.                 pass
  192.             except:
  193.                 None<EXCEPTION MATCH>error
  194.             
  195.  
  196.         None<EXCEPTION MATCH>error
  197.         return rc
  198.  
  199.     __del__ = close
  200.  
  201.  
  202. def popen(cmd, mode = 'r', bufsize = None):
  203.     popen = None
  204.     if os.environ.get('OS', '') == 'Windows_NT':
  205.         
  206.         try:
  207.             import win32pipe
  208.         except ImportError:
  209.             pass
  210.  
  211.         popen = win32pipe.popen
  212.     
  213.     if popen is None:
  214.         if hasattr(os, 'popen'):
  215.             popen = os.popen
  216.             if sys.platform == 'win32':
  217.                 
  218.                 try:
  219.                     popen('')
  220.                 except os.error:
  221.                     popen = _popen
  222.                 except:
  223.                     None<EXCEPTION MATCH>os.error
  224.                 
  225.  
  226.             None<EXCEPTION MATCH>os.error
  227.         else:
  228.             popen = _popen
  229.     
  230.     if bufsize is None:
  231.         return popen(cmd, mode)
  232.     return popen(cmd, mode, bufsize)
  233.  
  234.  
  235. def _norm_version(version, build = ''):
  236.     l = string.split(version, '.')
  237.     if build:
  238.         l.append(build)
  239.     
  240.     
  241.     try:
  242.         ints = map(int, l)
  243.     except ValueError:
  244.         strings = l
  245.  
  246.     strings = map(str, ints)
  247.     version = string.join(strings[:3], '.')
  248.     return version
  249.  
  250. _ver_output = re.compile('(?:([\\w ]+) ([\\w.]+) .*Version ([\\d.]+))')
  251.  
  252. def _syscmd_ver(system = '', release = '', version = '', supported_platforms = ('win32', 'win16', 'dos', 'os2')):
  253.     if sys.platform not in supported_platforms:
  254.         return (system, release, version)
  255.     for cmd in ('ver', 'command /c ver', 'cmd /c ver'):
  256.         
  257.         try:
  258.             pipe = popen(cmd)
  259.             info = pipe.read()
  260.             if pipe.close():
  261.                 raise os.error, 'command failed'
  262.             pipe.close()
  263.         except os.error:
  264.             sys.platform not in supported_platforms
  265.             why = sys.platform not in supported_platforms
  266.             continue
  267.             continue
  268.             except IOError:
  269.                 why = None
  270.                 continue
  271.                 continue
  272.             else:
  273.                 break
  274.         return (system, release, version)
  275.         info = string.strip(info)
  276.         m = _ver_output.match(info)
  277.  
  278.     return (system, release, version)
  279.  
  280.  
  281. def _win32_getvalue(key, name, default = ''):
  282.     
  283.     try:
  284.         RegQueryValueEx = RegQueryValueEx
  285.         import win32api
  286.     except ImportError:
  287.         import _winreg
  288.         RegQueryValueEx = _winreg.QueryValueEx
  289.  
  290.     
  291.     try:
  292.         return RegQueryValueEx(key, name)
  293.     except:
  294.         return default
  295.  
  296.  
  297.  
  298. def win32_ver(release = '', version = '', csd = '', ptype = ''):
  299.     
  300.     try:
  301.         import win32api
  302.         RegQueryValueEx = RegQueryValueEx
  303.         RegOpenKeyEx = RegOpenKeyEx
  304.         RegCloseKey = RegCloseKey
  305.         GetVersionEx = GetVersionEx
  306.         import win32api
  307.         HKEY_LOCAL_MACHINE = HKEY_LOCAL_MACHINE
  308.         VER_PLATFORM_WIN32_NT = VER_PLATFORM_WIN32_NT
  309.         VER_PLATFORM_WIN32_WINDOWS = VER_PLATFORM_WIN32_WINDOWS
  310.         VER_NT_WORKSTATION = VER_NT_WORKSTATION
  311.         import win32con
  312.     except ImportError:
  313.         
  314.         try:
  315.             sys.getwindowsversion
  316.         except AttributeError:
  317.             return (release, version, csd, ptype)
  318.  
  319.         import _winreg
  320.         GetVersionEx = sys.getwindowsversion
  321.         RegQueryValueEx = _winreg.QueryValueEx
  322.         RegOpenKeyEx = _winreg.OpenKeyEx
  323.         RegCloseKey = _winreg.CloseKey
  324.         HKEY_LOCAL_MACHINE = _winreg.HKEY_LOCAL_MACHINE
  325.         VER_PLATFORM_WIN32_WINDOWS = 1
  326.         VER_PLATFORM_WIN32_NT = 2
  327.         VER_NT_WORKSTATION = 1
  328.  
  329.     (maj, min, buildno, plat, csd) = GetVersionEx()
  330.     version = '%i.%i.%i' % (maj, min, buildno & 65535)
  331.     if csd[:13] == 'Service Pack ':
  332.         csd = 'SP' + csd[13:]
  333.     
  334.     if plat == VER_PLATFORM_WIN32_WINDOWS:
  335.         regkey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion'
  336.         if maj == 4:
  337.             if min == 0:
  338.                 release = '95'
  339.             elif min == 10:
  340.                 release = '98'
  341.             elif min == 90:
  342.                 release = 'Me'
  343.             else:
  344.                 release = 'postMe'
  345.         elif maj == 5:
  346.             release = '2000'
  347.         
  348.     elif plat == VER_PLATFORM_WIN32_NT:
  349.         regkey = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion'
  350.         if maj <= 4:
  351.             release = 'NT'
  352.         elif maj == 5:
  353.             if min == 0:
  354.                 release = '2000'
  355.             elif min == 1:
  356.                 release = 'XP'
  357.             elif min == 2:
  358.                 release = '2003Server'
  359.             else:
  360.                 release = 'post2003'
  361.         elif maj == 6:
  362.             if min == 0:
  363.                 
  364.                 try:
  365.                     productType = GetVersionEx(1)[8]
  366.                 except TypeError:
  367.                     release = 'Vista'
  368.  
  369.                 if productType == VER_NT_WORKSTATION:
  370.                     release = 'Vista'
  371.                 else:
  372.                     release = '2008Server'
  373.             else:
  374.                 release = 'post2008Server'
  375.         
  376.     elif not release:
  377.         release = '%i.%i' % (maj, min)
  378.     
  379.     return (release, version, csd, ptype)
  380.     
  381.     try:
  382.         keyCurVer = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regkey)
  383.         RegQueryValueEx(keyCurVer, 'SystemRoot')
  384.     except:
  385.         return (release, version, csd, ptype)
  386.  
  387.     build = _win32_getvalue(keyCurVer, 'CurrentBuildNumber', ('', 1))[0]
  388.     ptype = _win32_getvalue(keyCurVer, 'CurrentType', (ptype, 1))[0]
  389.     version = _norm_version(version, build)
  390.     RegCloseKey(keyCurVer)
  391.     return (release, version, csd, ptype)
  392.  
  393.  
  394. def _mac_ver_lookup(selectors, default = None):
  395.     gestalt = gestalt
  396.     import gestalt
  397.     import MacOS
  398.     l = []
  399.     append = l.append
  400.     for selector in selectors:
  401.         
  402.         try:
  403.             append(gestalt(selector))
  404.         continue
  405.         except (RuntimeError, MacOS.Error):
  406.             append(default)
  407.             continue
  408.         
  409.  
  410.     
  411.     return l
  412.  
  413.  
  414. def _bcd2str(bcd):
  415.     return hex(bcd)[2:]
  416.  
  417.  
  418. def mac_ver(release = '', versioninfo = ('', '', ''), machine = ''):
  419.     
  420.     try:
  421.         import gestalt
  422.         import MacOS
  423.     except ImportError:
  424.         return (release, versioninfo, machine)
  425.  
  426.     (sysv, sysu, sysa) = _mac_ver_lookup(('sysv', 'sysu', 'sysa'))
  427.     if sysv:
  428.         major = (sysv & 65280) >> 8
  429.         minor = (sysv & 240) >> 4
  430.         patch = sysv & 15
  431.         if (major, minor) >= (10, 4):
  432.             (major, minor, patch) = _mac_ver_lookup(('sys1', 'sys2', 'sys3'))
  433.             release = '%i.%i.%i' % (major, minor, patch)
  434.         else:
  435.             release = '%s.%i.%i' % (_bcd2str(major), minor, patch)
  436.     
  437.     if sysu:
  438.         major = int((sysu & 0xFF000000L) >> 24)
  439.         minor = (sysu & 15728640) >> 20
  440.         bugfix = (sysu & 983040) >> 16
  441.         stage = (sysu & 65280) >> 8
  442.         nonrel = sysu & 255
  443.         version = '%s.%i.%i' % (_bcd2str(major), minor, bugfix)
  444.         nonrel = _bcd2str(nonrel)
  445.         stage = {
  446.             32: 'development',
  447.             64: 'alpha',
  448.             96: 'beta',
  449.             128: 'final' }.get(stage, '')
  450.         versioninfo = (version, stage, nonrel)
  451.     
  452.     if sysa:
  453.         machine = {
  454.             1: '68k',
  455.             2: 'PowerPC',
  456.             10: 'i386' }.get(sysa, '')
  457.     
  458.     return (release, versioninfo, machine)
  459.  
  460.  
  461. def _java_getprop(name, default):
  462.     System = System
  463.     import java.lang
  464.     
  465.     try:
  466.         value = System.getProperty(name)
  467.         if value is None:
  468.             return default
  469.         return value
  470.     except AttributeError:
  471.         return default
  472.  
  473.  
  474.  
  475. def java_ver(release = '', vendor = '', vminfo = ('', '', ''), osinfo = ('', '', '')):
  476.     
  477.     try:
  478.         import java.lang as java
  479.     except ImportError:
  480.         return (release, vendor, vminfo, osinfo)
  481.  
  482.     vendor = _java_getprop('java.vendor', vendor)
  483.     release = _java_getprop('java.version', release)
  484.     (vm_name, vm_release, vm_vendor) = vminfo
  485.     vm_name = _java_getprop('java.vm.name', vm_name)
  486.     vm_vendor = _java_getprop('java.vm.vendor', vm_vendor)
  487.     vm_release = _java_getprop('java.vm.version', vm_release)
  488.     vminfo = (vm_name, vm_release, vm_vendor)
  489.     (os_name, os_version, os_arch) = osinfo
  490.     os_arch = _java_getprop('java.os.arch', os_arch)
  491.     os_name = _java_getprop('java.os.name', os_name)
  492.     os_version = _java_getprop('java.os.version', os_version)
  493.     osinfo = (os_name, os_version, os_arch)
  494.     return (release, vendor, vminfo, osinfo)
  495.  
  496.  
  497. def system_alias(system, release, version):
  498.     if system == 'Rhapsody':
  499.         return ('MacOS X Server', system + release, version)
  500.     if system == 'SunOS':
  501.         if release < '5':
  502.             return (system, release, version)
  503.         l = string.split(release, '.')
  504.         if release < '6':
  505.             system = 'Solaris'
  506.         else:
  507.             system = 'Solaris'
  508.     elif system == 'IRIX64':
  509.         system = 'IRIX'
  510.         if version:
  511.             version = version + ' (64bit)'
  512.         else:
  513.             version = '64bit'
  514.     elif system in ('win32', 'win16'):
  515.         system = 'Windows'
  516.     
  517.     return (system, release, version)
  518.  
  519.  
  520. def _platform(*args):
  521.     platform = string.join(map(string.strip, filter(len, args)), '-')
  522.     replace = string.replace
  523.     platform = replace(platform, ' ', '_')
  524.     platform = replace(platform, '/', '-')
  525.     platform = replace(platform, '\\', '-')
  526.     platform = replace(platform, ':', '-')
  527.     platform = replace(platform, ';', '-')
  528.     platform = replace(platform, '"', '-')
  529.     platform = replace(platform, '(', '-')
  530.     platform = replace(platform, ')', '-')
  531.     platform = replace(platform, 'unknown', '')
  532.     while None:
  533.         cleaned = replace(platform, '--', '-')
  534.         if cleaned == platform:
  535.             break
  536.         
  537.         platform = cleaned
  538.         continue
  539.         while platform[-1] == '-':
  540.             platform = platform[:-1]
  541.         return platform
  542.  
  543.  
  544. def _node(default = ''):
  545.     
  546.     try:
  547.         import socket
  548.     except ImportError:
  549.         return default
  550.  
  551.     
  552.     try:
  553.         return socket.gethostname()
  554.     except socket.error:
  555.         return default
  556.  
  557.  
  558. if not hasattr(os.path, 'abspath'):
  559.     
  560.     def _abspath(path, isabs = os.path.isabs, join = os.path.join, getcwd = os.getcwd, normpath = os.path.normpath):
  561.         if not isabs(path):
  562.             path = join(getcwd(), path)
  563.         
  564.         return normpath(path)
  565.  
  566. else:
  567.     _abspath = os.path.abspath
  568.  
  569. def _follow_symlinks(filepath):
  570.     filepath = _abspath(filepath)
  571.     while os.path.islink(filepath):
  572.         filepath = os.path.normpath(os.path.join(os.path.dirname(filepath), os.readlink(filepath)))
  573.     return filepath
  574.  
  575.  
  576. def _syscmd_uname(option, default = ''):
  577.     if sys.platform in ('dos', 'win32', 'win16', 'os2'):
  578.         return default
  579.     
  580.     try:
  581.         f = os.popen('uname %s 2> /dev/null' % option)
  582.     except (AttributeError, os.error):
  583.         sys.platform in ('dos', 'win32', 'win16', 'os2')
  584.         sys.platform in ('dos', 'win32', 'win16', 'os2')
  585.         return default
  586.  
  587.     output = string.strip(f.read())
  588.     rc = f.close()
  589.     if not output or rc:
  590.         return default
  591.     return output
  592.  
  593.  
  594. def _syscmd_file(target, default = ''):
  595.     if sys.platform in ('dos', 'win32', 'win16', 'os2'):
  596.         return default
  597.     target = _follow_symlinks(target)
  598.     
  599.     try:
  600.         f = os.popen('file "%s" 2> /dev/null' % target)
  601.     except (AttributeError, os.error):
  602.         sys.platform in ('dos', 'win32', 'win16', 'os2')
  603.         sys.platform in ('dos', 'win32', 'win16', 'os2')
  604.         return default
  605.  
  606.     output = string.strip(f.read())
  607.     rc = f.close()
  608.     if not output or rc:
  609.         return default
  610.     return output
  611.  
  612. _default_architecture = {
  613.     'win32': ('', 'WindowsPE'),
  614.     'win16': ('', 'Windows'),
  615.     'dos': ('', 'MSDOS') }
  616. _architecture_split = re.compile('[\\s,]').split
  617.  
  618. def architecture(executable = sys.executable, bits = '', linkage = ''):
  619.     if not bits:
  620.         import struct
  621.         
  622.         try:
  623.             size = struct.calcsize('P')
  624.         except struct.error:
  625.             size = struct.calcsize('l')
  626.  
  627.         bits = str(size * 8) + 'bit'
  628.     
  629.     if executable:
  630.         output = _syscmd_file(executable, '')
  631.     else:
  632.         output = ''
  633.     if not output and executable == sys.executable:
  634.         if _default_architecture.has_key(sys.platform):
  635.             (b, l) = _default_architecture[sys.platform]
  636.             if b:
  637.                 bits = b
  638.             
  639.             if l:
  640.                 linkage = l
  641.             
  642.         
  643.         return (bits, linkage)
  644.     fileout = _architecture_split(output)[1:]
  645.     if 'executable' not in fileout:
  646.         return (bits, linkage)
  647.     if '32-bit' in fileout:
  648.         bits = '32bit'
  649.     elif 'N32' in fileout:
  650.         bits = 'n32bit'
  651.     elif '64-bit' in fileout:
  652.         bits = '64bit'
  653.     
  654.     if 'ELF' in fileout:
  655.         linkage = 'ELF'
  656.     elif 'PE' in fileout:
  657.         if 'Windows' in fileout:
  658.             linkage = 'WindowsPE'
  659.         else:
  660.             linkage = 'PE'
  661.     elif 'COFF' in fileout:
  662.         linkage = 'COFF'
  663.     elif 'MS-DOS' in fileout:
  664.         linkage = 'MSDOS'
  665.     
  666.     return (bits, linkage)
  667.  
  668. _uname_cache = None
  669.  
  670. def uname():
  671.     global _uname_cache
  672.     no_os_uname = 0
  673.     if _uname_cache is not None:
  674.         return _uname_cache
  675.     processor = ''
  676.     
  677.     try:
  678.         (system, node, release, version, machine) = os.uname()
  679.     except AttributeError:
  680.         _uname_cache is not None
  681.         _uname_cache is not None
  682.         no_os_uname = 1
  683.     except:
  684.         _uname_cache is not None
  685.  
  686.     if no_os_uname or not filter(None, (system, node, release, version, machine)):
  687.         if no_os_uname:
  688.             system = sys.platform
  689.             release = ''
  690.             version = ''
  691.             node = _node()
  692.             machine = ''
  693.         
  694.         use_syscmd_ver = 1
  695.         if system == 'win32':
  696.             (release, version, csd, ptype) = win32_ver()
  697.             if release and version:
  698.                 use_syscmd_ver = 0
  699.             
  700.             if not machine:
  701.                 machine = os.environ.get('PROCESSOR_ARCHITECTURE', '')
  702.             
  703.             if not processor:
  704.                 processor = os.environ.get('PROCESSOR_IDENTIFIER', machine)
  705.             
  706.         
  707.         if use_syscmd_ver:
  708.             (system, release, version) = _syscmd_ver(system)
  709.             if system == 'Microsoft Windows':
  710.                 system = 'Windows'
  711.             elif system == 'Microsoft' and release == 'Windows':
  712.                 system = 'Windows'
  713.                 if '6.0' == version[:3]:
  714.                     release = 'Vista'
  715.                 else:
  716.                     release = ''
  717.             
  718.         
  719.         if system in ('win32', 'win16'):
  720.             if not version:
  721.                 if system == 'win32':
  722.                     version = '32bit'
  723.                 else:
  724.                     version = '16bit'
  725.             
  726.             system = 'Windows'
  727.         elif system[:4] == 'java':
  728.             (release, vendor, vminfo, osinfo) = java_ver()
  729.             system = 'Java'
  730.             version = string.join(vminfo, ', ')
  731.             if not version:
  732.                 version = vendor
  733.             
  734.         elif os.name == 'mac':
  735.             (version, stage, nonrel) = (release,)
  736.             machine = mac_ver()
  737.             system = 'MacOS'
  738.         
  739.     
  740.     if system == 'OpenVMS':
  741.         if not release or release == '0':
  742.             release = version
  743.             version = ''
  744.         
  745.         
  746.         try:
  747.             import vms_lib
  748.         except ImportError:
  749.             pass
  750.  
  751.         (csid, cpu_number) = vms_lib.getsyi('SYI$_CPU', 0)
  752.         if cpu_number >= 128:
  753.             processor = 'Alpha'
  754.         else:
  755.             processor = 'VAX'
  756.     
  757.     if not processor:
  758.         processor = _syscmd_uname('-p', '')
  759.     
  760.     if system == 'unknown':
  761.         system = ''
  762.     
  763.     if node == 'unknown':
  764.         node = ''
  765.     
  766.     if release == 'unknown':
  767.         release = ''
  768.     
  769.     if version == 'unknown':
  770.         version = ''
  771.     
  772.     if machine == 'unknown':
  773.         machine = ''
  774.     
  775.     if processor == 'unknown':
  776.         processor = ''
  777.     
  778.     if system == 'Microsoft' and release == 'Windows':
  779.         system = 'Windows'
  780.         release = 'Vista'
  781.     
  782.     _uname_cache = (system, node, release, version, machine, processor)
  783.     return _uname_cache
  784.  
  785.  
  786. def system():
  787.     return uname()[0]
  788.  
  789.  
  790. def node():
  791.     return uname()[1]
  792.  
  793.  
  794. def release():
  795.     return uname()[2]
  796.  
  797.  
  798. def version():
  799.     return uname()[3]
  800.  
  801.  
  802. def machine():
  803.     return uname()[4]
  804.  
  805.  
  806. def processor():
  807.     return uname()[5]
  808.  
  809. _sys_version_parser = re.compile('([\\w.+]+)\\s*\\(#?([^,]+),\\s*([\\w ]+),\\s*([\\w :]+)\\)\\s*\\[([^\\]]+)\\]?')
  810. _jython_sys_version_parser = re.compile('([\\d\\.]+)')
  811. _ironpython_sys_version_parser = re.compile('IronPython\\s*([\\d\\.]+)(?: \\(([\\d\\.]+)\\))? on (.NET [\\d\\.]+)')
  812. _sys_version_cache = { }
  813.  
  814. def _sys_version(sys_version = None):
  815.     if sys_version is None:
  816.         sys_version = sys.version
  817.     
  818.     result = _sys_version_cache.get(sys_version, None)
  819.     if result is not None:
  820.         return result
  821.     if sys_version[:10] == 'IronPython':
  822.         name = 'IronPython'
  823.         match = _ironpython_sys_version_parser.match(sys_version)
  824.         if match is None:
  825.             raise ValueError('failed to parse IronPython sys.version: %s' % repr(sys_version))
  826.         match is None
  827.         (version, alt_version, compiler) = match.groups()
  828.         branch = ''
  829.         revision = ''
  830.         buildno = ''
  831.         builddate = ''
  832.     elif sys.platform[:4] == 'java':
  833.         name = 'Jython'
  834.         match = _jython_sys_version_parser.match(sys_version)
  835.         if match is None:
  836.             raise ValueError('failed to parse Jython sys.version: %s' % repr(sys_version))
  837.         match is None
  838.         (version,) = match.groups()
  839.         branch = ''
  840.         revision = ''
  841.         compiler = sys.platform
  842.         buildno = ''
  843.         builddate = ''
  844.     else:
  845.         match = _sys_version_parser.match(sys_version)
  846.         if match is None:
  847.             raise ValueError('failed to parse CPython sys.version: %s' % repr(sys_version))
  848.         match is None
  849.         (version, buildno, builddate, buildtime, compiler) = match.groups()
  850.         if hasattr(sys, 'subversion'):
  851.             (name, branch, revision) = sys.subversion
  852.         else:
  853.             name = 'CPython'
  854.             branch = ''
  855.             revision = ''
  856.         builddate = builddate + ' ' + buildtime
  857.     l = string.split(version, '.')
  858.     if len(l) == 2:
  859.         l.append('0')
  860.         version = string.join(l, '.')
  861.     
  862.     result = (name, version, branch, revision, buildno, builddate, compiler)
  863.     _sys_version_cache[sys_version] = result
  864.     return result
  865.  
  866.  
  867. def _test_sys_version():
  868.     _sys_version_cache.clear()
  869.     for input, output in (('2.4.3 (#1, Jun 21 2006, 13:54:21) \n[GCC 3.3.4 (pre 3.3.5 20040809)]', ('CPython', '2.4.3', '', '', '1', 'Jun 21 2006 13:54:21', 'GCC 3.3.4 (pre 3.3.5 20040809)')), ('IronPython 1.0.60816 on .NET 2.0.50727.42', ('IronPython', '1.0.60816', '', '', '', '', '.NET 2.0.50727.42')), ('IronPython 1.0 (1.0.61005.1977) on .NET 2.0.50727.42', ('IronPython', '1.0.0', '', '', '', '', '.NET 2.0.50727.42'))):
  870.         parsed = _sys_version(input)
  871.         if parsed != output:
  872.             print (input, parsed)
  873.             continue
  874.     
  875.  
  876.  
  877. def python_implementation():
  878.     return _sys_version()[0]
  879.  
  880.  
  881. def python_version():
  882.     return _sys_version()[1]
  883.  
  884.  
  885. def python_version_tuple():
  886.     return tuple(string.split(_sys_version()[1], '.'))
  887.  
  888.  
  889. def python_branch():
  890.     return _sys_version()[2]
  891.  
  892.  
  893. def python_revision():
  894.     return _sys_version()[3]
  895.  
  896.  
  897. def python_build():
  898.     return _sys_version()[4:6]
  899.  
  900.  
  901. def python_compiler():
  902.     return _sys_version()[6]
  903.  
  904. _platform_cache = { }
  905.  
  906. def platform(aliased = 0, terse = 0):
  907.     result = _platform_cache.get((aliased, terse), None)
  908.     if result is not None:
  909.         return result
  910.     (system, node, release, version, machine, processor) = uname()
  911.     if machine == processor:
  912.         processor = ''
  913.     
  914.     if aliased:
  915.         (system, release, version) = system_alias(system, release, version)
  916.     
  917.     if system == 'Windows':
  918.         (rel, vers, csd, ptype) = win32_ver(version)
  919.         if terse:
  920.             platform = _platform(system, release)
  921.         else:
  922.             platform = _platform(system, release, version, csd)
  923.     elif system in ('Linux',):
  924.         (distname, distversion, distid) = dist('')
  925.         if distname and not terse:
  926.             platform = _platform(system, release, machine, processor, 'with', distname, distversion, distid)
  927.         else:
  928.             (libcname, libcversion) = libc_ver(sys.executable)
  929.             platform = _platform(system, release, machine, processor, 'with', libcname + libcversion)
  930.     elif system == 'Java':
  931.         (os_name, os_version, os_arch) = (r, v, vminfo)
  932.         if terse or not os_name:
  933.             platform = _platform(system, release, version)
  934.         else:
  935.             platform = _platform(system, release, version, 'on', os_name, os_version, os_arch)
  936.     elif system == 'MacOS':
  937.         if terse:
  938.             platform = _platform(system, release)
  939.         else:
  940.             platform = _platform(system, release, machine)
  941.     elif terse:
  942.         platform = _platform(system, release)
  943.     else:
  944.         (bits, linkage) = architecture(sys.executable)
  945.         platform = _platform(system, release, machine, processor, bits, linkage)
  946.     _platform_cache[(aliased, terse)] = platform
  947.     return platform
  948.  
  949. if __name__ == '__main__':
  950.     if not 'terse' in sys.argv:
  951.         pass
  952.     terse = '--terse' in sys.argv
  953.     if 'nonaliased' not in sys.argv:
  954.         pass
  955.     aliased = '--nonaliased' not in sys.argv
  956.     print platform(aliased, terse)
  957.     sys.exit(0)
  958.  
  959.