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 / bin / lsb_release < prev    next >
Encoding:
Text File  |  2006-11-19  |  9.2 KB  |  283 lines

  1. #!/usr/bin/python
  2.  
  3. # lsb_release command for Debian
  4. # (C) 2005-06 Chris Lawrence <lawrencc@debian.org>
  5.  
  6. #    This package is free software; you can redistribute it and/or modify
  7. #    it under the terms of the GNU General Public License as published by
  8. #    the Free Software Foundation; version 2 dated June, 1991.
  9.  
  10. #    This package is distributed in the hope that it will be useful,
  11. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #    GNU General Public License for more details.
  14.  
  15. #    You should have received a copy of the GNU General Public License
  16. #    along with this package; if not, write to the Free Software
  17. #    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  18. #    02111-1307, USA.
  19.  
  20. from optparse import OptionParser
  21. import sys
  22. import commands
  23. import os
  24. import re
  25.  
  26. # XXX: Update as needed
  27. # This should really be included in apt-cache policy output... it is already
  28. # in the Release file...
  29. RELEASE_CODENAME_LOOKUP = {
  30.     '1.1' : 'buzz',
  31.     '1.2' : 'rex',
  32.     '1.3' : 'bo',
  33.     '2.0' : 'hamm',
  34.     '2.1' : 'slink',
  35.     '2.2' : 'potato',
  36.     '3.0' : 'woody',
  37.     '3.1' : 'sarge',
  38.     '4.0' : 'etch',
  39.     }
  40.  
  41. TESTING_CODENAME = 'etch'
  42.  
  43. def lookup_codename(release, unknown=None):
  44.     m = re.match(r'(\d+)\.(\d+)(r(\d+))?', release)
  45.     if not m:
  46.         return unknown
  47.  
  48.     shortrelease = '%s.%s' % m.group(1,2)
  49.     return RELEASE_CODENAME_LOOKUP.get(shortrelease, unknown)
  50.  
  51. # LSB compliance packages... may grow eventually
  52. PACKAGES = 'lsb-core lsb-cxx lsb-graphics lsb-desktop lsb-qt4'
  53.  
  54. modnamere = re.compile(r'lsb-(?P<module>[a-z]+)-(?P<arch>[^ ]+)(?: \(= (?P<version>[0-9.]+)\))?')
  55.  
  56. def valid_lsb_versions(version, module):
  57.     # If a module is ever released that only appears in >= version, deal
  58.     # with that here
  59.     if version == '3.0':
  60.         return ['2.0', '3.0']
  61.     elif version == '3.1':
  62.         if module in ('desktop', 'qt4'):
  63.             return ['3.1']
  64.         else:
  65.             return ['2.0', '3.0', '3.1']
  66.  
  67.     return [version]
  68.  
  69. # This is Debian-specific at present
  70. def check_modules_installed():
  71.     # Find which LSB modules are installed on this system
  72.     output = commands.getoutput("dpkg-query -f '${Version} ${Provides}\n' -W %s 2>/dev/null" % PACKAGES)
  73.     if not output:
  74.         return []
  75.  
  76.     modules = []
  77.     for line in output.split(os.linesep):
  78.         version, provides = line.split(' ', 1)
  79.         version = version.split('-', 1)[0]
  80.         for pkg in provides.split(','):
  81.             mob = modnamere.search(pkg)
  82.             if not mob:
  83.                 continue
  84.  
  85.             mgroups = mob.groupdict()
  86.             # If no versioned provides...
  87.             if mgroups.get('version'):
  88.                 module = '%(module)s-%(version)s-%(arch)s' % mgroups
  89.                 modules += [module]
  90.             else:
  91.                 module = mgroups['module']
  92.                 for v in valid_lsb_versions(version, module):
  93.                     mgroups['version'] = v
  94.                     module = '%(module)s-%(version)s-%(arch)s' % mgroups
  95.                     modules += [module]
  96.                     
  97.     return modules
  98.  
  99. longnames = {'v' : 'version', 'o': 'origin', 'a': 'suite',
  100.              'c' : 'component', 'l': 'label'}
  101.  
  102. def parse_policy_line(data):
  103.     retval = {}
  104.     bits = data.split(',')
  105.     for bit in bits:
  106.         kv = bit.split('=', 1)
  107.         if len(kv) > 1:
  108.             k, v = kv[:2]
  109.             if k in longnames:
  110.                 retval[longnames[k]] = v
  111.     return retval
  112.  
  113. def parse_apt_policy():
  114.     data = []
  115.     
  116.     policy = commands.getoutput('apt-cache policy 2>/dev/null')
  117.     for line in policy.split('\n'):
  118.         line = line.strip()
  119.         m = re.match(r'(\d+)', line)
  120.         if m:
  121.             priority = int(m.group(1))
  122.         if line.startswith('release'):
  123.             bits = line.split(' ', 1)
  124.             if len(bits) > 1:
  125.                 data.append( (priority, parse_policy_line(bits[1])) )
  126.  
  127.     return data
  128.  
  129. def guess_release_from_apt(origin='Debian', component='main',
  130.                            ignoresuites=('experimental')):
  131.     releases = parse_apt_policy()
  132.  
  133.     if not releases:
  134.         return None
  135.  
  136.     # We only care about the specified origin and component
  137.     releases = [x for x in releases if (
  138.         x[1].get('origin', '') == origin and
  139.         x[1].get('component', '') == component)]
  140.     
  141.     releases.sort()
  142.     releases.reverse()
  143.  
  144.     for (pri, rinfo) in releases:
  145.         if rinfo.get('suite', '') not in ignoresuites:
  146.             return rinfo
  147.  
  148.     return None
  149.  
  150. def guess_debian_release():
  151.     distinfo = {'ID' : 'Debian'}
  152.  
  153.     kern = os.uname()[0]
  154.     if kern in ('Linux', 'Hurd', 'NetBSD'):
  155.         distinfo['OS'] = 'GNU/'+kern
  156.     elif kern == 'FreeBSD':
  157.         distinfo['OS'] = 'GNU/k'+kern
  158.     else:
  159.         distinfo['OS'] = 'GNU'
  160.  
  161.     distinfo['DESCRIPTION'] = '%(ID)s %(OS)s' % distinfo
  162.  
  163.     rinfo = guess_release_from_apt()
  164.     if rinfo:
  165.         release = rinfo.get('version')
  166.         if release:
  167.             codename = lookup_codename(release, 'n/a')
  168.         else:
  169.             release = rinfo.get('suite', 'unstable')
  170.             if release == 'testing':
  171.                 # Would be nice if I didn't have to hardcode this.
  172.                 codename = TESTING_CODENAME
  173.             else:
  174.                 codename = 'sid'
  175.         distinfo.update({ 'RELEASE' : release, 'CODENAME' : codename })
  176.     elif os.path.exists('/etc/debian_version'):
  177.         release = open('/etc/debian_version').read().strip()
  178.         if not release[0:1].isalpha():
  179.             # /etc/debian_version should be numeric
  180.             codename = lookup_codename(release, 'n/a')
  181.             distinfo.update({ 'RELEASE' : release, 'CODENAME' : codename })
  182.         else:
  183.             distinfo['RELEASE'] = release
  184.  
  185.     if 'RELEASE' in distinfo:
  186.         distinfo['DESCRIPTION'] += ' %(RELEASE)s' % distinfo
  187.     if 'CODENAME' in distinfo:
  188.         distinfo['DESCRIPTION'] += ' (%(CODENAME)s)' % distinfo
  189.  
  190.     return distinfo
  191.  
  192. # Whatever is guessed above can be overridden in /etc/lsb-release
  193. def get_lsb_information():
  194.     distinfo = {}
  195.     if os.path.exists('/etc/lsb-release'):
  196.         for line in open('/etc/lsb-release'):
  197.             line = line.strip()
  198.             if not line:
  199.                 continue
  200.             var, arg = line.split('=', 1)
  201.             if var.startswith('DISTRIB_'):
  202.                 var = var[8:]
  203.                 if arg.startswith('"') and arg.endswith('"'):
  204.                     arg = arg[1:-1]
  205.                 distinfo[var] = arg
  206.     return distinfo
  207.  
  208. def get_distro_information():
  209.     distinfo = guess_debian_release()
  210.     distinfo.update(get_lsb_information())
  211.     return distinfo
  212.     
  213. def main():
  214.     parser = OptionParser()
  215.     parser.add_option('-v', '--version', dest='version', action='store_true',
  216.                       default=False,
  217.                       help="show LSB modules this system supports")
  218.     parser.add_option('-i', '--id', dest='id', action='store_true',
  219.                       default=False,
  220.                       help="show distributor ID")
  221.     parser.add_option('-d', '--description', dest='description',
  222.                       default=False, action='store_true',
  223.                       help="show description of this distribution")
  224.     parser.add_option('-r', '--release', dest='release',
  225.                       default=False, action='store_true',
  226.                       help="show release number of this distribution")
  227.     parser.add_option('-c', '--codename', dest='codename',
  228.                       default=False, action='store_true',
  229.                       help="show code name of this distribution")
  230.     parser.add_option('-a', '--all', dest='all',
  231.                       default=False, action='store_true',
  232.                       help="show all of the above information")
  233.     parser.add_option('-s', '--short', dest='short',
  234.                       action='store_true', default=False,
  235.                       help="show all of the above information in short format")
  236.     
  237.     (options, args) = parser.parse_args()
  238.     if args:
  239.         parser.error("No arguments are permitted")
  240.  
  241.     short = (options.short)
  242.     all = (options.all)
  243.     none = not (options.all or options.version or options.id or
  244.                 options.description or options.codename or options.release)
  245.  
  246.     distinfo = get_distro_information()
  247.  
  248.     if none or all or options.version:
  249.         verinfo = check_modules_installed()
  250.         if not verinfo:
  251.             print >> sys.stderr, "No LSB modules are available."
  252.         elif short:
  253.             print ':'.join(verinfo)
  254.         else:
  255.             print 'LSB Version:\t' + ':'.join(verinfo)
  256.  
  257.     if options.id or all:
  258.         if short:
  259.             print distinfo.get('ID', 'n/a')
  260.         else:
  261.             print 'Distributor ID:\t%s' % distinfo.get('ID', 'n/a')
  262.  
  263.     if options.description or all:
  264.         if short:
  265.             print distinfo.get('DESCRIPTION', 'n/a')
  266.         else:
  267.             print 'Description:\t%s' % distinfo.get('DESCRIPTION', 'n/a')
  268.  
  269.     if options.release or all:
  270.         if short:
  271.             print distinfo.get('RELEASE', 'n/a')
  272.         else:
  273.             print 'Release:\t%s' % distinfo.get('RELEASE', 'n/a')
  274.  
  275.     if options.codename or all:
  276.         if short:
  277.             print distinfo.get('CODENAME', 'n/a')
  278.         else:
  279.             print 'Codename:\t%s' % distinfo.get('CODENAME', 'n/a')
  280.  
  281. if __name__ == '__main__':
  282.     main()
  283.