home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / python / pyversions.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  9.4 KB  |  292 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  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.  
  16. def parse_versions(vstring):
  17.     import operator
  18.     operators = {
  19.         None: operator.eq,
  20.         '=': operator.eq,
  21.         '>=': operator.ge,
  22.         '<=': operator.le,
  23.         '<<': operator.lt }
  24.     vinfo = { }
  25.     exact_versions = set([])
  26.     version_range = set(supported_versions(version_only = True))
  27.     relop_seen = False
  28.     for field in vstring.split(','):
  29.         field = field.strip()
  30.         if field == 'all':
  31.             vinfo['all'] = 'all'
  32.             continue
  33.         
  34.         if field in ('current', 'current_ext'):
  35.             vinfo['current'] = field
  36.             continue
  37.         
  38.         vinfo.setdefault('versions', set())
  39.         ve = re.compile('(>=|<=|<<|=)? *(\\d\\.\\d)$')
  40.         m = ve.match(field)
  41.         
  42.         try:
  43.             op = m.group(1)
  44.             v = m.group(2)
  45.             if op in (None, '='):
  46.                 exact_versions.add(v)
  47.             else:
  48.                 relop_seen = True
  49.                 filtop = operators[op]
  50.                 version_range = _[1]
  51.         continue
  52.         except Exception:
  53.             raise ValueError, 'error parsing Python-Version attribute'
  54.             continue
  55.         
  56.  
  57.     
  58.     if 'versions' in vinfo:
  59.         vinfo['versions'] = exact_versions
  60.         if relop_seen:
  61.             vinfo['versions'] = exact_versions.union(version_range)
  62.         
  63.     
  64.     return vinfo
  65.  
  66. _supported_versions = None
  67.  
  68. def supported_versions(version_only = False):
  69.     global _supported_versions, _supported_versions
  70.     pass
  71.  
  72. _default_version = None
  73.  
  74. def default_version(version_only = False):
  75.     global _default_version
  76.     if not _default_version:
  77.         _default_version = link = os.readlink('/usr/bin/python')
  78.     
  79.     if version_only:
  80.         return _default_version[6:]
  81.     else:
  82.         return _default_version
  83.  
  84.  
  85. def requested_versions(vstring, version_only = False):
  86.     versions = None
  87.     vinfo = parse_versions(vstring)
  88.     supported = supported_versions(version_only = True)
  89.     if len(vinfo) == 1:
  90.         if 'all' in vinfo:
  91.             versions = supported
  92.         elif 'current' in vinfo:
  93.             versions = [
  94.                 default_version(version_only = True)]
  95.         else:
  96.             versions = vinfo['versions'].intersection(supported)
  97.     elif 'all' in vinfo and 'current' in vinfo:
  98.         raise ValueError, "both `current' and `all' in version string"
  99.     elif 'all' in vinfo:
  100.         versions = versions = vinfo['versions'].intersection(supported)
  101.     elif 'current' in vinfo:
  102.         current = default_version(version_only = True)
  103.         if current not in vinfo['versions']:
  104.             raise ValueError, "`current' version not in supported versions"
  105.         
  106.         versions = [
  107.             current]
  108.     else:
  109.         raise ValueError, 'error in version string'
  110.     if not versions:
  111.         raise ValueError, 'empty set of versions'
  112.     
  113.  
  114.  
  115. def installed_versions(version_only = False):
  116.     import glob
  117.     supported = supported_versions()
  118.     versions = _[1]
  119.     versions.sort()
  120.  
  121.  
  122. class ControlFileValueError(ValueError):
  123.     pass
  124.  
  125.  
  126. class MissingVersionValueError(ValueError):
  127.     pass
  128.  
  129.  
  130. def extract_pyversion_attribute(fn, pkg):
  131.     '''read the debian/control file, extract the XS-Python-Version
  132.     field; check that XB-Python-Version exists for the package.'''
  133.     version = None
  134.     sversion = None
  135.     section = None
  136.     for line in file(fn):
  137.         line = line.strip()
  138.         if line == '':
  139.             if pkg == 'Source':
  140.                 break
  141.             
  142.             section = None
  143.             continue
  144.         if line.startswith('Source:'):
  145.             section = 'Source'
  146.             continue
  147.         if line.startswith('Package: ' + pkg):
  148.             section = pkg
  149.             continue
  150.         if line.startswith('XS-Python-Version:'):
  151.             if section != 'Source':
  152.                 raise ValueError, 'attribute XS-Python-Version not in Source section'
  153.             
  154.             sversion = line.split(':', 1)[1].strip()
  155.             continue
  156.         if line.startswith('XB-Python-Version:'):
  157.             if section == pkg:
  158.                 version = line.split(':', 1)[1].strip()
  159.             
  160.         section == pkg
  161.     
  162.     if section == None:
  163.         raise ControlFileValueError, 'not a control file'
  164.     
  165.     if pkg == 'Source':
  166.         if sversion == None:
  167.             raise MissingVersionValueError, 'missing XS-Python-Version in control file'
  168.         
  169.         return sversion
  170.     
  171.     if version == None:
  172.         raise MissingVersionValueError, 'missing XB-Python-Version for package `%s' % pkg
  173.     
  174.     return version
  175.  
  176.  
  177. def version_cmp(ver1, ver2):
  178.     v1 = [ int(i) for i in ver1.split('.') ]
  179.     v2 = [ int(i) for i in ver2.split('.') ]
  180.     return cmp(v1, v2)
  181.  
  182.  
  183. def requested_versions_bis(vstring, version_only = False):
  184.     versions = []
  185.     py_supported_short = supported_versions(version_only = True)
  186.     for item in vstring.split(','):
  187.         v = item.split('-')
  188.         if len(v) > 1:
  189.             if not v[0]:
  190.                 v[0] = py_supported_short[0]
  191.             
  192.             if not v[1]:
  193.                 v[1] = py_supported_short[-1]
  194.             
  195.             for ver in py_supported_short:
  196.                 
  197.                 try:
  198.                     if version_cmp(ver, v[0]) >= 0 and version_cmp(ver, v[1]) <= 0:
  199.                         versions.append(ver)
  200.                 continue
  201.                 except ValueError:
  202.                     continue
  203.                 
  204.  
  205.             
  206.         None<EXCEPTION MATCH>ValueError
  207.         if v[0] in py_supported_short:
  208.             versions.append(v[0])
  209.             continue
  210.     
  211.     versions.sort(version_cmp)
  212.     if not versions:
  213.         raise ValueError, 'empty set of versions'
  214.     
  215.     return versions
  216.  
  217.  
  218. def extract_pyversion_attribute_bis(fn):
  219.     vstring = file(fn).readline().rstrip('\n')
  220.     return vstring
  221.  
  222.  
  223. def main():
  224.     OptionParser = OptionParser
  225.     import optparse
  226.     usage = '[-v] [-h] [-d|--default] [-s|--supported] [-i|--installed] [-r|--requested <version string>|<control file>]'
  227.     parser = OptionParser(usage = usage)
  228.     parser.add_option('-d', '--default', help = 'print the default python version', action = 'store_true', dest = 'default')
  229.     parser.add_option('-s', '--supported', help = 'print the supported python versions', action = 'store_true', dest = 'supported')
  230.     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')
  231.     parser.add_option('-i', '--installed', help = 'print the installed supported python versions', action = 'store_true', dest = 'installed')
  232.     parser.add_option('-v', '--version', help = 'print just the version number(s)', default = False, action = 'store_true', dest = 'version_only')
  233.     (opts, args) = parser.parse_args()
  234.     program = os.path.basename(sys.argv[0])
  235.     if opts.default and len(args) == 0:
  236.         print default_version(opts.version_only)
  237.     elif opts.supported and len(args) == 0:
  238.         print ' '.join(supported_versions(opts.version_only))
  239.     elif opts.installed and len(args) == 0:
  240.         print ' '.join(installed_versions(opts.version_only))
  241.     elif opts.requested and len(args) <= 1:
  242.         if len(args) == 0:
  243.             versions = 'debian/control'
  244.         else:
  245.             versions = args[0]
  246.         
  247.         try:
  248.             if os.path.isfile(versions):
  249.                 fn = versions
  250.                 
  251.                 try:
  252.                     vstring = extract_pyversion_attribute(fn, 'Source')
  253.                     vs = requested_versions(vstring, opts.version_only)
  254.                 except ControlFileValueError:
  255.                     sys.stderr.write('%s: not a control file: %s, ' % (program, fn))
  256.                     sys.exit(1)
  257.                 except MissingVersionValueError:
  258.                     fn = os.path.join(os.path.dirname(fn), 'pyversions')
  259.                     sys.stderr.write('%s: missing XS-Python-Version in control file, fall back to %s\n' % (program, fn))
  260.                     
  261.                     try:
  262.                         vstring = extract_pyversion_attribute_bis(fn)
  263.                         vs = requested_versions_bis(vstring, opts.version_only)
  264.                     except IOError:
  265.                         sys.stderr.write('%s: missing debian/pyversions file, fall back to supported versions\n' % program)
  266.                         vs = supported_versions(opts.version_only)
  267.                     except:
  268.                         None<EXCEPTION MATCH>IOError
  269.                     
  270.  
  271.                     None<EXCEPTION MATCH>IOError
  272.                 
  273.  
  274.             None<EXCEPTION MATCH>ControlFileValueError
  275.             vs = requested_versions(versions, opts.version_only)
  276.             print ' '.join(vs)
  277.         except ValueError:
  278.             msg = None
  279.             sys.stderr.write('%s: %s\n' % (program, msg))
  280.             sys.exit(1)
  281.         except:
  282.             None<EXCEPTION MATCH>ValueError
  283.         
  284.  
  285.     None<EXCEPTION MATCH>ValueError
  286.     sys.stderr.write('usage: %s %s\n' % (program, usage))
  287.     sys.exit(1)
  288.  
  289. if __name__ == '__main__':
  290.     main()
  291.  
  292.