home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / python / pyversions.py < prev    next >
Encoding:
Python Source  |  2006-07-05  |  10.2 KB  |  277 lines

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