home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / pycentral-data / pyversions.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  12.0 KB  |  431 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import os
  5. import re
  6. import sys
  7.  
  8. try:
  9.     SetType = set
  10. except NameError:
  11.     import sets
  12.     SetType = sets.Set
  13.     set = sets.Set
  14.  
  15. _defaults = None
  16.  
  17. def read_default(name = None):
  18.     global _defaults
  19.     SafeConfigParser = SafeConfigParser
  20.     NoOptionError = NoOptionError
  21.     import ConfigParser
  22.     if not _defaults:
  23.         if os.path.exists('/usr/share/python/debian_defaults'):
  24.             config = SafeConfigParser()
  25.             config.readfp(file('/usr/share/python/debian_defaults'))
  26.             _defaults = config
  27.         
  28.     
  29.     if _defaults and name:
  30.         
  31.         try:
  32.             value = _defaults.get('DEFAULT', name)
  33.         except NoOptionError:
  34.             raise ValueError
  35.  
  36.         return value
  37.     
  38.  
  39.  
  40. def parse_versions(vstring):
  41.     import operator
  42.     operators = {
  43.         None: operator.eq,
  44.         '=': operator.eq,
  45.         '>=': operator.ge,
  46.         '<=': operator.le,
  47.         '<<': operator.lt }
  48.     vinfo = { }
  49.     exact_versions = set([])
  50.     version_range = set(supported_versions(version_only = True))
  51.     relop_seen = False
  52.     for field in vstring.split(','):
  53.         field = field.strip()
  54.         if field == 'all':
  55.             vinfo['all'] = 'all'
  56.             continue
  57.         
  58.         if field in ('current', 'current_ext'):
  59.             vinfo['current'] = field
  60.             continue
  61.         
  62.         vinfo.setdefault('versions', set())
  63.         ve = re.compile('(>=|<=|<<|=)? *(\\d\\.\\d)$')
  64.         m = ve.match(field)
  65.         
  66.         try:
  67.             op = m.group(1)
  68.             v = m.group(2)
  69.             if op in (None, '='):
  70.                 exact_versions.add(v)
  71.             else:
  72.                 relop_seen = True
  73.                 filtop = operators[op]
  74.                 version_range = _[1]
  75.         continue
  76.         except Exception:
  77.             raise ValueError, 'error parsing Python-Version attribute'
  78.             continue
  79.         
  80.  
  81.     
  82.     if 'versions' in vinfo:
  83.         vinfo['versions'] = exact_versions
  84.         if relop_seen:
  85.             vinfo['versions'] = exact_versions.union(version_range)
  86.         
  87.     
  88.     return vinfo
  89.  
  90. _old_versions = None
  91.  
  92. def old_versions(version_only = False):
  93.     global _old_versions, _old_versions
  94.     if not _old_versions:
  95.         
  96.         try:
  97.             value = read_default('old-versions')
  98.             _old_versions = [ s.strip() for s in value.split(',') ]
  99.         except ValueError:
  100.             _old_versions = []
  101.         except:
  102.             None<EXCEPTION MATCH>ValueError
  103.         
  104.  
  105.     None<EXCEPTION MATCH>ValueError
  106.  
  107. _unsupported_versions = None
  108.  
  109. def unsupported_versions(version_only = False):
  110.     global _unsupported_versions, _unsupported_versions
  111.     if not _unsupported_versions:
  112.         
  113.         try:
  114.             value = read_default('unsupported-versions')
  115.             _unsupported_versions = [ s.strip() for s in value.split(',') ]
  116.         except ValueError:
  117.             _unsupported_versions = []
  118.         except:
  119.             None<EXCEPTION MATCH>ValueError
  120.         
  121.  
  122.     None<EXCEPTION MATCH>ValueError
  123.  
  124. _supported_versions = None
  125.  
  126. def supported_versions(version_only = False):
  127.     global _supported_versions, _supported_versions
  128.     if not _supported_versions:
  129.         
  130.         try:
  131.             value = read_default('supported-versions')
  132.             _supported_versions = [ s.strip() for s in value.split(',') ]
  133.         except ValueError:
  134.             cmd = [
  135.                 '/usr/bin/apt-cache',
  136.                 '--no-all-versions',
  137.                 'show',
  138.                 'python-all']
  139.             
  140.             try:
  141.                 import subprocess
  142.                 p = subprocess.Popen(cmd, bufsize = 1, shell = False, stdout = subprocess.PIPE)
  143.                 fd = p.stdout
  144.             except ImportError:
  145.                 fd = os.popen(' '.join(cmd))
  146.  
  147.             depends = None
  148.             for line in fd:
  149.                 if line.startswith('Depends:'):
  150.                     depends = line.split(':', 1)[1].strip().split(',')
  151.                     continue
  152.             
  153.             fd.close()
  154.             depends = [ re.sub('\\s*(\\S+)[ (]?.*', '\\1', s) for s in depends ]
  155.             _supported_versions = depends
  156.         except:
  157.             None<EXCEPTION MATCH>ValueError
  158.         
  159.  
  160.     None<EXCEPTION MATCH>ValueError
  161.  
  162. _default_version = None
  163.  
  164. def default_version(version_only = False):
  165.     global _default_version, _default_version
  166.     if not _default_version:
  167.         _default_version = link = os.readlink('/usr/bin/python')
  168.     
  169.     debian_default = read_default('default-version')
  170.     if _default_version not in (debian_default, os.path.join('/usr/bin', debian_default)):
  171.         raise ValueError, 'the symlink /usr/bin/python does not point to the python default version. It must be reset to point to %s' % debian_default
  172.     
  173.     _default_version = debian_default
  174.     if version_only:
  175.         return _default_version[6:]
  176.     else:
  177.         return _default_version
  178.  
  179.  
  180. def requested_versions(vstring, version_only = False):
  181.     versions = None
  182.     vinfo = parse_versions(vstring)
  183.     supported = supported_versions(version_only = True)
  184.     if len(vinfo) == 1:
  185.         if 'all' in vinfo:
  186.             versions = supported
  187.         elif 'current' in vinfo:
  188.             versions = [
  189.                 default_version(version_only = True)]
  190.         else:
  191.             versions = vinfo['versions'].intersection(supported)
  192.     elif 'all' in vinfo and 'current' in vinfo:
  193.         raise ValueError, "both `current' and `all' in version string"
  194.     elif 'all' in vinfo:
  195.         versions = versions = vinfo['versions'].intersection(supported)
  196.     elif 'current' in vinfo:
  197.         current = default_version(version_only = True)
  198.         if current not in vinfo['versions']:
  199.             raise ValueError, "`current' version not in supported versions"
  200.         
  201.         versions = [
  202.             current]
  203.     else:
  204.         raise ValueError, 'error in version string'
  205.     if not versions:
  206.         raise ValueError, 'empty set of versions'
  207.     
  208.  
  209.  
  210. def requested_versions_for_runtime(vstring, version_only = False):
  211.     versions = None
  212.     vinfo = parse_versions(vstring)
  213.     old = old_versions(version_only = True)
  214.     unsupported = unsupported_versions(version_only = True)
  215.     supported = supported_versions(version_only = True)
  216.     supported.extend(old)
  217.     if len(vinfo) == 1:
  218.         if 'all' in vinfo:
  219.             versions = supported
  220.         elif 'current' in vinfo:
  221.             versions = [
  222.                 default_version(version_only = True)]
  223.         else:
  224.             versions = vinfo['versions'].intersection(supported)
  225.     elif 'all' in vinfo and 'current' in vinfo:
  226.         raise ValueError, "both `current' and `all' in version string"
  227.     elif 'all' in vinfo:
  228.         versions = versions = vinfo['versions'].intersection(supported)
  229.     elif 'current' in vinfo:
  230.         current = default_version(version_only = True)
  231.         if current not in vinfo['versions']:
  232.             raise ValueError, "`current' version not in supported versions"
  233.         
  234.         versions = [
  235.             current]
  236.     else:
  237.         raise ValueError, 'error in version string'
  238.     if not versions:
  239.         raise ValueError, 'empty set of versions'
  240.     
  241.  
  242.  
  243. def installed_versions(version_only = False):
  244.     import glob
  245.     supported = supported_versions()
  246.     versions = _[1]
  247.     versions.sort()
  248.  
  249.  
  250. class ControlFileValueError(ValueError):
  251.     pass
  252.  
  253.  
  254. class MissingVersionValueError(ValueError):
  255.     pass
  256.  
  257.  
  258. def extract_pyversion_attribute(fn, pkg):
  259.     '''read the debian/control file, extract the XS-Python-Version
  260.     field; check that XB-Python-Version exists for the package.'''
  261.     version = None
  262.     sversion = None
  263.     section = None
  264.     for line in file(fn):
  265.         line = line.strip()
  266.         if line == '':
  267.             if pkg == 'Source':
  268.                 break
  269.             
  270.             section = None
  271.             continue
  272.         if line.startswith('Source:'):
  273.             section = 'Source'
  274.             continue
  275.         if line.startswith('Package: ' + pkg):
  276.             section = pkg
  277.             continue
  278.         if line.startswith('XS-Python-Version:'):
  279.             if section != 'Source':
  280.                 raise ValueError, 'attribute XS-Python-Version not in Source section'
  281.             
  282.             sversion = line.split(':', 1)[1].strip()
  283.             continue
  284.         if line.startswith('XB-Python-Version:'):
  285.             if section == pkg:
  286.                 version = line.split(':', 1)[1].strip()
  287.             
  288.         section == pkg
  289.     
  290.     if section == None:
  291.         raise ControlFileValueError, 'not a control file'
  292.     
  293.     if pkg == 'Source':
  294.         if sversion == None:
  295.             raise MissingVersionValueError, 'missing XS-Python-Version in control file'
  296.         
  297.         return sversion
  298.     
  299.     if version == None:
  300.         raise MissingVersionValueError, 'missing XB-Python-Version for package `%s' % pkg
  301.     
  302.     return version
  303.  
  304.  
  305. def version_cmp(ver1, ver2):
  306.     v1 = [ int(i) for i in ver1.split('.') ]
  307.     v2 = [ int(i) for i in ver2.split('.') ]
  308.     return cmp(v1, v2)
  309.  
  310.  
  311. def requested_versions_bis(vstring, version_only = False):
  312.     versions = []
  313.     py_supported_short = supported_versions(version_only = True)
  314.     for item in vstring.split(','):
  315.         v = item.split('-')
  316.         if len(v) > 1:
  317.             if not v[0]:
  318.                 v[0] = py_supported_short[0]
  319.             
  320.             if not v[1]:
  321.                 v[1] = py_supported_short[-1]
  322.             
  323.             for ver in py_supported_short:
  324.                 
  325.                 try:
  326.                     if version_cmp(ver, v[0]) >= 0 and version_cmp(ver, v[1]) <= 0:
  327.                         versions.append(ver)
  328.                 continue
  329.                 except ValueError:
  330.                     continue
  331.                 
  332.  
  333.             
  334.         None<EXCEPTION MATCH>ValueError
  335.         if v[0] in py_supported_short:
  336.             versions.append(v[0])
  337.             continue
  338.     
  339.     versions.sort(version_cmp)
  340.     if not versions:
  341.         raise ValueError, 'empty set of versions'
  342.     
  343.     return versions
  344.  
  345.  
  346. def extract_pyversion_attribute_bis(fn):
  347.     vstring = file(fn).readline().rstrip('\n')
  348.     return vstring
  349.  
  350.  
  351. def main():
  352.     OptionParser = OptionParser
  353.     import optparse
  354.     usage = '[-v] [-h] [-d|--default] [-s|--supported] [-i|--installed] [-r|--requested <version string>|<control file>]'
  355.     parser = OptionParser(usage = usage)
  356.     parser.add_option('-d', '--default', help = 'print the default python version', action = 'store_true', dest = 'default')
  357.     parser.add_option('-s', '--supported', help = 'print the supported python versions', action = 'store_true', dest = 'supported')
  358.     parser.add_option('-r', '--requested', help = 'print the python versions requested by a build; the argument is either the name of a control file or the value of the XS-Python-Version attribute', action = 'store_true', dest = 'requested')
  359.     parser.add_option('-i', '--installed', help = 'print the installed supported python versions', action = 'store_true', dest = 'installed')
  360.     parser.add_option('-v', '--version', help = 'print just the version number(s)', default = False, action = 'store_true', dest = 'version_only')
  361.     (opts, args) = parser.parse_args()
  362.     program = os.path.basename(sys.argv[0])
  363.     if opts.default and len(args) == 0:
  364.         
  365.         try:
  366.             print default_version(opts.version_only)
  367.         except ValueError:
  368.             msg = None
  369.             print '%s:' % program, msg
  370.             sys.exit(1)
  371.         except:
  372.             None<EXCEPTION MATCH>ValueError
  373.         
  374.  
  375.     None<EXCEPTION MATCH>ValueError
  376.     if opts.supported and len(args) == 0:
  377.         print ' '.join(supported_versions(opts.version_only))
  378.     elif opts.installed and len(args) == 0:
  379.         print ' '.join(installed_versions(opts.version_only))
  380.     elif opts.requested and len(args) <= 1:
  381.         if len(args) == 0:
  382.             versions = 'debian/control'
  383.         else:
  384.             versions = args[0]
  385.         
  386.         try:
  387.             if os.path.isfile(versions):
  388.                 fn = versions
  389.                 
  390.                 try:
  391.                     vstring = extract_pyversion_attribute(fn, 'Source')
  392.                     vs = requested_versions(vstring, opts.version_only)
  393.                 except ControlFileValueError:
  394.                     sys.stderr.write('%s: not a control file: %s, ' % (program, fn))
  395.                     sys.exit(1)
  396.                 except MissingVersionValueError:
  397.                     fn = os.path.join(os.path.dirname(fn), 'pyversions')
  398.                     sys.stderr.write('%s: missing XS-Python-Version in control file, fall back to %s\n' % (program, fn))
  399.                     
  400.                     try:
  401.                         vstring = extract_pyversion_attribute_bis(fn)
  402.                         vs = requested_versions_bis(vstring, opts.version_only)
  403.                     except IOError:
  404.                         sys.stderr.write('%s: missing debian/pyversions file, fall back to supported versions\n' % program)
  405.                         vs = supported_versions(opts.version_only)
  406.                     except:
  407.                         None<EXCEPTION MATCH>IOError
  408.                     
  409.  
  410.                     None<EXCEPTION MATCH>IOError
  411.                 
  412.  
  413.             None<EXCEPTION MATCH>ControlFileValueError
  414.             vs = requested_versions(versions, opts.version_only)
  415.             print ' '.join(vs)
  416.         except ValueError:
  417.             msg = None
  418.             sys.stderr.write('%s: %s\n' % (program, msg))
  419.             sys.exit(1)
  420.         except:
  421.             None<EXCEPTION MATCH>ValueError
  422.         
  423.  
  424.     None<EXCEPTION MATCH>ValueError
  425.     sys.stderr.write('usage: %s %s\n' % (program, usage))
  426.     sys.exit(1)
  427.  
  428. if __name__ == '__main__':
  429.     main()
  430.  
  431.