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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import ConfigParser
  5. import logging
  6. import logging.handlers as logging
  7. import optparse
  8. import os
  9. import sys
  10. import computerjanitor
  11. import computerjanitorapp
  12. _ = computerjanitorapp.setup_gettext()
  13. DEFAULT_PLUGINS_DIRS = '/usr/share/computerjanitor/plugins'
  14. DEFAULT_STATE_FILE = '/var/lib/computer-janitor/state.dat'
  15.  
  16. class SourcesListProblem(computerjanitor.Exception):
  17.     
  18.     def __init__(self, missing_package):
  19.         self._str = _('Essential package %s is missing. There may be problems with apt sources.list or Packages files may be missing?') % missing_package
  20.  
  21.  
  22.  
  23. class AsciiFormatter(logging.Formatter):
  24.     
  25.     def format(self, record):
  26.         msg = logging.Formatter.format(self, record)
  27.         return msg.encode('ascii', 'replace')
  28.  
  29.  
  30.  
  31. def setup_logging():
  32.     if os.environ.get('COMPUTER_JANITOR_DEBUG', None) == 'yes':
  33.         level = logging.DEBUG
  34.     elif os.environ.get('COMPUTER_JANITOR_UNITTEST', None) == 'yes':
  35.         level = logging.CRITICAL
  36.     else:
  37.         level = logging.INFO
  38.     logging.basicConfig(level = level, format = '%(levelname)s: %(message)s')
  39.     
  40.     try:
  41.         formatter = AsciiFormatter('computer-janitor %(levelname)s: %(message)s')
  42.         handler = logging.handlers.SysLogHandler('/dev/log')
  43.         handler.setLevel(level)
  44.         handler.setFormatter(formatter)
  45.         logging.getLogger().addHandler(handler)
  46.     except:
  47.         logging.debug(_('Logging to syslog cannot be set up.'))
  48.  
  49.  
  50.  
  51. class Application(object):
  52.     '''The main class for the program.'''
  53.     whitelist_dirs = [
  54.         '/etc/computer-janitor.d']
  55.     
  56.     def __init__(self, apt = None):
  57.         self.state = computerjanitorapp.State()
  58.         self.parser = self.create_option_parser()
  59.         self.apt = computerjanitor.apt
  60.         if apt is not None:
  61.             self.apt = apt
  62.         
  63.         self.refresh_apt_cache()
  64.         self.config = ConfigParser.ConfigParser()
  65.  
  66.     
  67.     def run(self, ui_class, plugin_manager_class = computerjanitor.PluginManager):
  68.         setup_logging()
  69.         logging.debug(_('Running application, with:'))
  70.         logging.debug('  plugin_manager_class=%s' % plugin_manager_class)
  71.         (options, args) = self.parse_options()
  72.         pluginpath = os.environ.get('COMPUTER_JANITOR_PLUGINS', DEFAULT_PLUGINS_DIRS)
  73.         plugindirs = pluginpath.split(':')
  74.         pm = plugin_manager_class(self, plugindirs)
  75.         ui = ui_class(self, pm)
  76.         ui.run(options, args)
  77.  
  78.     
  79.     def create_option_parser(self):
  80.         parser = optparse.OptionParser(version = '%%prog %s' % computerjanitorapp.VERSION)
  81.         parser.usage = _("\n%prog [options] find\n%prog [options] cleanup [CRUFT]...\n%prog [options] ignore [CRUFT]...\n%prog [options] unignore [CRUFT]...\n\n%prog finds and removes cruft from your system. \n\nCruft is anything that shouldn't be on the system, but is. Stretching\nthe definition, it is also things that should be on the system, but\naren't.")
  82.         parser.add_option('--all', action = 'store_true', help = _("Make the 'cleanup' command remove all packages, if none are given on the command line."))
  83.         parser.add_option('--state-file', metavar = 'FILE', default = DEFAULT_STATE_FILE, help = _('Store state of each piece of cruft in FILE. (Default is %default).'))
  84.         parser.add_option('--no-act', action = 'store_true', help = _("Don't actually remove anything, just pretend to do so. This is useful for testing stuff."))
  85.         parser.add_option('--verbose', action = 'store_true', help = _('Verbose operation: make find show an explanation for each piece of cruft found.'))
  86.         return parser
  87.  
  88.     
  89.     def parse_options(self, args = None):
  90.         return self.parser.parse_args(args = args)
  91.  
  92.     
  93.     def refresh_apt_cache(self):
  94.         self.apt_cache = self.apt.Cache()
  95.         self.apt_cache._depcache.ReadPinFile('/var/lib/synaptic/preferences')
  96.  
  97.     
  98.     def verify_apt_cache(self):
  99.         for name in [
  100.             'dash',
  101.             'gzip']:
  102.             if name not in self.apt_cache:
  103.                 raise SourcesListProblem(name)
  104.             name not in self.apt_cache
  105.             if not self.apt_cache[name].candidateDownloadable:
  106.                 raise SourcesListProblem(name)
  107.             self.apt_cache[name].candidateDownloadable
  108.         
  109.  
  110.     
  111.     def whitelist_files(self, dirnames):
  112.         '''Find files with whitelists in a list of directories.
  113.  
  114.         The directory is scanned for files ending in ".whitelist".
  115.         Subdirectories are not scanned.
  116.  
  117.         '''
  118.         list = []
  119.         for dirname in dirnames:
  120.             
  121.             try:
  122.                 basenames = os.listdir(dirname)
  123.                 whitelists = _[1]
  124.                 [] += [ os.path.join(dirname, x) for x in whitelists ]
  125.             continue
  126.             except os.error:
  127.                 continue
  128.             
  129.  
  130.         
  131.         return list
  132.  
  133.     
  134.     def whitelisted_cruft(self, dirnames = None):
  135.         '''Return list of cruft that is whitelisted.'''
  136.         if not dirnames:
  137.             pass
  138.         dirnames = self.whitelist_dirs
  139.         list = []
  140.         for filename in self.whitelist_files(dirnames):
  141.             f = file(filename, 'r')
  142.             for line in f.readlines():
  143.                 line = line.strip()
  144.                 if line and not line.startswith('#'):
  145.                     list.append(line)
  146.                     continue
  147.             
  148.             f.close()
  149.         
  150.         return list
  151.  
  152.     
  153.     def remove_whitelisted(self, crufts, dirnames = None):
  154.         whitelisted = self.whitelisted_cruft(dirnames = dirnames)
  155.         return _[1]
  156.  
  157.  
  158.