home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / CommandNotFound / CommandNotFound.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  9.6 KB  |  259 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import os.path as os
  7. import gdbm
  8. import posix
  9. import grp
  10. import string
  11. from util import gettext_wrapper as _
  12.  
  13. class BinaryDatabase:
  14.     
  15.     def __init__(self, filename):
  16.         self.db = None
  17.         if filename.endswith('.db'):
  18.             
  19.             try:
  20.                 self.db = gdbm.open(filename, 'r')
  21.             except gdbm.error:
  22.                 err = None
  23.                 print >>sys.stderr, 'Unable to open binary database %s: %s' % (filename, err)
  24.             except:
  25.                 None<EXCEPTION MATCH>gdbm.error
  26.             
  27.  
  28.         None<EXCEPTION MATCH>gdbm.error
  29.  
  30.     
  31.     def lookup(self, key):
  32.         if self.db and self.db.has_key(key):
  33.             return self.db[key]
  34.         return None
  35.  
  36.  
  37.  
  38. class FlatDatabase:
  39.     
  40.     def __init__(self, filename):
  41.         self.rows = []
  42.         dbfile = file(filename)
  43.         for line in (lambda .0: for line in .0:
  44. line.strip())(dbfile):
  45.             self.rows.append(line.split('|'))
  46.         
  47.         dbfile.close()
  48.  
  49.     
  50.     def lookup(self, column, text):
  51.         result = []
  52.         for row in self.rows:
  53.             if row[column] == text:
  54.                 result.append(row)
  55.                 continue
  56.         
  57.         return result
  58.  
  59.     
  60.     def createColumnByCallback(self, cb, column):
  61.         for row in self.rows:
  62.             row.append(cb(row[column]))
  63.         
  64.  
  65.     
  66.     def lookupWithCallback(self, column, cb, text):
  67.         result = []
  68.         for row in self.rows:
  69.             if cb(row[column], text):
  70.                 result.append(row)
  71.                 continue
  72.         
  73.         return result
  74.  
  75.  
  76.  
  77. class ProgramDatabase:
  78.     (PACKAGE, BASENAME_PATH) = range(2)
  79.     
  80.     def __init__(self, filename):
  81.         basename = os.path.basename(filename)
  82.         (self.arch, self.component) = basename.split('.')[0].split('-')
  83.         self.db = BinaryDatabase(filename)
  84.  
  85.     
  86.     def lookup(self, command):
  87.         result = self.db.lookup(command)
  88.         if result:
  89.             return result.split('|')
  90.         return []
  91.  
  92.  
  93.  
  94. class CommandNotFound:
  95.     programs_dir = 'programs.d'
  96.     prefixes = ('/bin', '/usr/bin', '/usr/local/bin', '/sbin', '/usr/sbin', '/usr/local/sbin', '/usr/games')
  97.     
  98.     def __init__(self, data_dir = os.sep.join(('/', 'usr', 'share', 'command-not-found'))):
  99.         self.programs = []
  100.         p = os.path.join(data_dir, 'priority.txt')
  101.         if os.path.exists(p):
  102.             self.priority_overrides = map(string.strip, open(p).readlines())
  103.         
  104.         self.components = [
  105.             'main',
  106.             'universe',
  107.             'contrib',
  108.             'restricted',
  109.             'non-free',
  110.             'multiverse']
  111.         self.components.reverse()
  112.         self.sources_list = self._getSourcesList()
  113.         for filename in os.listdir(os.path.sep.join([
  114.             data_dir,
  115.             self.programs_dir])):
  116.             self.programs.append(ProgramDatabase(os.path.sep.join([
  117.                 data_dir,
  118.                 self.programs_dir,
  119.                 filename])))
  120.         
  121.         
  122.         try:
  123.             self.user_can_sudo = grp.getgrnam('admin')[2] in posix.getgroups()
  124.         except KeyError:
  125.             self.user_can_sudo = False
  126.  
  127.  
  128.     
  129.     def getPackages(self, command):
  130.         result = set()
  131.         for db in self.programs:
  132.             []([ (pkg, db.component) for pkg in db.lookup(command) ])
  133.         
  134.         return list(result)
  135.  
  136.     
  137.     def getBlacklist(self):
  138.         
  139.         try:
  140.             blacklist = file(os.sep.join((os.getenv('HOME', '/root'), '.command-not-found.blacklist')))
  141.             return _[1]
  142.         except IOError:
  143.             return []
  144.  
  145.         blacklist.close()
  146.  
  147.     
  148.     def _getSourcesList(self):
  149.         
  150.         try:
  151.             import apt_pkg
  152.             SourcesList = SourcesList
  153.             import aptsources.sourceslist
  154.             apt_pkg.init()
  155.         except (SystemError, ImportError):
  156.             e = None
  157.             return []
  158.  
  159.         sources_list = set([])
  160.         for source in SourcesList():
  161.             if not (source.disabled) and not (source.invalid):
  162.                 for component in source.comps:
  163.                     sources_list.add(component)
  164.                 
  165.         
  166.         return sources_list
  167.  
  168.     
  169.     def sortByComponent(self, x, y):
  170.         if x[0] in self.priority_overrides and y[0] in self.priority_overrides:
  171.             pass
  172.         elif x[0] in self.priority_overrides:
  173.             return -1
  174.         if y[0] in self.priority_overrides:
  175.             return 1
  176.         
  177.         try:
  178.             xidx = self.components.index(x[1])
  179.         except:
  180.             y[0] in self.priority_overrides
  181.             xidx = -1
  182.  
  183.         
  184.         try:
  185.             yidx = self.components.index(y[1])
  186.         except:
  187.             y[0] in self.priority_overrides
  188.             xidx = -1
  189.  
  190.         if not yidx - xidx:
  191.             pass
  192.         return cmp(x, y)
  193.  
  194.     
  195.     def advise(self, command, ignore_installed = False):
  196.         
  197.         def _in_prefix(prefix, command):
  198.             ''' helper that returns if a command is found in the given prefix '''
  199.             if os.path.exists(os.path.join(prefix, command)):
  200.                 pass
  201.             return not os.path.isdir(os.path.join(prefix, command))
  202.  
  203.         if prefixes and not ignore_installed:
  204.             if len(prefixes) == 1:
  205.                 print >>sys.stderr, _("Command '%(command)s' is available in '%(place)s'") % {
  206.                     'command': command,
  207.                     'place': os.path.join(prefixes[0], command) }
  208.             else:
  209.                 print >>sys.stderr, _("Command '%(command)s' is available in the following places") % {
  210.                     'command': command }
  211.                 for prefix in prefixes:
  212.                     print >>sys.stderr, ' * %s' % os.path.join(prefix, command)
  213.                 
  214.             missing = list(set(prefixes) - set(os.getenv('PATH', '').split(':')))
  215.             if len(missing) > 0:
  216.                 print >>sys.stderr, _("The command could not be located because '%s' is not included in the PATH environment variable.") % ':'.join(missing)
  217.                 if 'sbin' in ':'.join(missing):
  218.                     print >>sys.stderr, _('This is most likely caused by the lack of administrative priviledges associated with your user account.')
  219.                 
  220.             
  221.             return False
  222.         if command in self.getBlacklist():
  223.             return False
  224.         packages = self.getPackages(command)
  225.         if len(packages) == 1:
  226.             print >>sys.stderr, _("The program '%s' is currently not installed. ") % command,
  227.             if posix.geteuid() == 0:
  228.                 print >>sys.stderr, _('You can install it by typing:')
  229.                 print >>sys.stderr, 'apt-get install %s' % packages[0][0]
  230.             elif self.user_can_sudo:
  231.                 print >>sys.stderr, _('You can install it by typing:')
  232.                 print >>sys.stderr, 'sudo apt-get install %s' % packages[0][0]
  233.             else:
  234.                 print >>sys.stderr, _("To run '%(command)s' please ask your administrator to install the package '%(package)s'") % {
  235.                     'command': command,
  236.                     'package': packages[0][0] }
  237.             if packages[0][1] not in self.sources_list:
  238.                 print >>sys.stderr, _("You will have to enable the component called '%s'") % packages[0][1]
  239.             
  240.         elif len(packages) > 1:
  241.             packages.sort(self.sortByComponent)
  242.             print >>sys.stderr, _("The program '%s' can be found in the following packages:") % command
  243.             for package in packages:
  244.                 if package[1] in self.sources_list:
  245.                     print >>sys.stderr, ' * %s' % package[0]
  246.                     continue
  247.                 print >>sys.stderr, ' * %s' % package[0] + ' (' + _("You will have to enable component called '%s'") % package[1] + ')'
  248.             
  249.             if posix.geteuid() == 0:
  250.                 print >>sys.stderr, _('Try: %s <selected package>') % 'apt-get install'
  251.             elif self.user_can_sudo:
  252.                 print >>sys.stderr, _('Try: %s <selected package>') % 'sudo apt-get install'
  253.             else:
  254.                 print >>sys.stderr, _('Ask your administrator to install one of them')
  255.         
  256.         return len(packages) > 0
  257.  
  258.  
  259.