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 / ui_cli.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.9 KB  |  188 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import logging
  6. import sys
  7. import textwrap
  8. import traceback
  9. import computerjanitor
  10. import computerjanitorapp
  11. _ = computerjanitorapp.setup_gettext()
  12.  
  13. class UnknownCommand(computerjanitor.Exception):
  14.     
  15.     def __init__(self, name):
  16.         self._str = _('Unknown command: %s') % name
  17.  
  18.  
  19.  
  20. class UnknownCruft(computerjanitor.Exception):
  21.     
  22.     def __init__(self, name):
  23.         self._str = _('Unknown cruft: %s') % name
  24.  
  25.  
  26.  
  27. class MustBeRoot(computerjanitor.Exception):
  28.     
  29.     def __init__(self):
  30.         self._str = _('computer-janitor must be run as root, sorry.')
  31.  
  32.  
  33.  
  34. class CommandLineUserInterface(computerjanitorapp.UserInterface):
  35.     
  36.     def run(self, options, args):
  37.         if self.mustberoot and os.getuid() != 0:
  38.             raise MustBeRoot()
  39.         os.getuid() != 0
  40.         self.app.verify_apt_cache()
  41.         dict = {
  42.             'find': self.show_cruft,
  43.             'cleanup': self.cleanup,
  44.             'ignore': self.ignore,
  45.             'unignore': self.unignore,
  46.             'help': self.help }
  47.         if args:
  48.             cmd = args[0]
  49.             args = args[1:]
  50.         else:
  51.             cmd = 'help'
  52.             args = []
  53.         if cmd in dict:
  54.             app = self.app
  55.             app.state.load(options.state_file)
  56.             
  57.             try:
  58.                 dict[cmd](options, args)
  59.             except Exception:
  60.                 e = None
  61.                 logging.debug(unicode(traceback.format_exc()))
  62.                 logging.critical(unicode(e))
  63.                 sys.exit(1)
  64.             except:
  65.                 None<EXCEPTION MATCH>Exception
  66.             
  67.  
  68.         None<EXCEPTION MATCH>Exception
  69.         raise UnknownCommand(cmd)
  70.  
  71.     
  72.     def find_cruft(self):
  73.         list = []
  74.         for plugin in self.pm.get_plugins():
  75.             for cruft in plugin.get_cruft():
  76.                 list.append(cruft)
  77.             
  78.         
  79.         return self.app.remove_whitelisted(list)
  80.  
  81.     
  82.     def show_one_cruft(self, name, desc, s, width):
  83.         if width is None:
  84.             max = len(name) + len(desc)
  85.         else:
  86.             max = width
  87.             max -= 9
  88.             max -= 2
  89.             max -= 1
  90.         print '%-9s  %.*s ' % (s, max, name)
  91.         if desc:
  92.             paras = desc.split('\n\n')
  93.             for para in paras:
  94.                 for line in textwrap.wrap(para, max):
  95.                     print '%9s  %s' % ('', line)
  96.                 
  97.                 print 
  98.             
  99.         
  100.  
  101.     
  102.     def show_cruft(self, options, args):
  103.         list = []
  104.         maxname = ''
  105.         state = self.app.state
  106.         for cruft in self.find_cruft():
  107.             name = cruft.get_name()
  108.             if options.verbose:
  109.                 desc = cruft.get_description()
  110.             else:
  111.                 desc = None
  112.             if state.is_enabled(name):
  113.                 s = _('removable')
  114.             else:
  115.                 s = _('ignored')
  116.             list.append((name, desc, s))
  117.             if len(name) > len(maxname):
  118.                 maxname = name
  119.                 continue
  120.         
  121.         (rows, cols) = computerjanitorapp.get_terminal_size()
  122.         for name, desc, s in sorted(list):
  123.             self.show_one_cruft(name, desc, s, cols)
  124.         
  125.  
  126.     
  127.     def cleanup(self, options, args):
  128.         crufts = { }
  129.         for cruft in self.find_cruft():
  130.             crufts[cruft.get_name()] = cruft
  131.         
  132.         if args:
  133.             for arg in args:
  134.                 if arg not in crufts:
  135.                     raise UnknownCruft(arg)
  136.                 arg not in crufts
  137.             
  138.         elif options.all:
  139.             state = self.app.state
  140.             args = []
  141.             for name in crufts.keys():
  142.                 if state.is_enabled(name):
  143.                     args.append(name)
  144.                     continue
  145.                 logging.info(_('Ignored: %s') % name)
  146.             
  147.         
  148.         for arg in args:
  149.             if options.no_act:
  150.                 logging.info(_('Pretending to remove cruft: %s') % arg)
  151.                 continue
  152.             logging.info(_('Removing cruft: %s') % arg)
  153.             crufts[arg].cleanup()
  154.         
  155.         for plugin in self.pm.get_plugins():
  156.             if options.no_act:
  157.                 logging.info(_('Pretending to post-cleanup: %s') % plugin)
  158.                 continue
  159.             logging.info(_('Post-cleanup: %s') % plugin)
  160.             plugin.post_cleanup()
  161.         
  162.  
  163.     
  164.     def ignore(self, options, cruft_names):
  165.         state = self.app.state
  166.         for cruft_name in cruft_names:
  167.             state.disable(cruft_name)
  168.         
  169.         if not options.no_act:
  170.             state.save(options.state_file)
  171.         
  172.  
  173.     
  174.     def unignore(self, options, cruft_names):
  175.         state = self.app.state
  176.         for cruft_name in cruft_names:
  177.             state.enable(cruft_name)
  178.         
  179.         if not options.no_act:
  180.             state.save(options.state_file)
  181.         
  182.  
  183.     
  184.     def help(self, options, args):
  185.         self.app.parser.print_help()
  186.  
  187.  
  188.