Ejemplo n.º 1
0
 def save_config(self, cfg_file=None):
     cfg_file = cfg_file or self.cfg_file
     cfg_file = os.path.abspath(cfg_file)
     f = open(cfg_file, 'w')
     json.dump(self.cfg, f, indent=4, sort_keys=True)
     f.close()
     logger.debug("Wrote config to %s." % (cfg_file,))
Ejemplo n.º 2
0
    def __call__(self, pargs=None):
        """If no arguments are specified, we hook into all known scripts

        except buildout and mrsd
        """
        scriptdir = os.path.join(
                self.root or os.curdir,
                self.cfg['scripts_dir']
                )
        for name in os.listdir(scriptdir):
            script = os.path.join(scriptdir, name)
            if name in ('buildout', 'mrsd'):
                logger.debug("Ignoring %s." % (script,))
                continue
            if name[0] == '.':
                logger.debug("Ignoring %s." % (script,))
                continue
            # Will be either hookin or hookout
            self._cmd(script)
Ejemplo n.º 3
0
 def _hookin(self, script):
     """This command will be called by HookCmd.__call__
     """
     f = open(script, 'r')
     content = f.read()
     f.close()
     if self.start_indicator in content:
         self.cmds.hookout()
         f = open(script, 'r')
         content = f.read()
         f.close()
     if self.start_str not in content:
         logger.debug("Not hooking into %s." % (script,))
         return
     idx = content.find(self.start_str) + len(self.start_str)
     idx = content.find(']', idx)+2
     hooked = content[:idx]
     hooked += self.hook % \
             (self.start_indicator, self.stop_indicator)
     hooked += content[idx:]
     f = open(script, 'w')
     f.write(hooked)
     f.close()
     logger.info("Hooked in: %s." % (script,))
Ejemplo n.º 4
0
 def load_config(self):
     """Load config from curdir or parents
     """
     if self.cfg_file is None:
         logger.debug("No config to load, we are rootless.")
         return
     logger.debug("Trying to load config from %s." % (self.cfg_file,))
     f = open(self.cfg_file)
     self.cfg = json.load(f)
     logger.debug("Loaded config from %s." % (self.cfg_file,))
     f.close()
Ejemplo n.º 5
0
    def __call__(self):
        try:
            if sys.argv[1] == "dance":
                print r"""
              .$$$$$.          .
    |/()))))  $)'\$==. %%%%%%\%%\
   / ((((==)  ') - $$$'===%%%%\'.|
   |((((( <(   \-_/$$  )" _)%% )/|
   | ())))_/._\._) //_.\_  %% ( /
   | \_) /.--._\___/__.-.\ (.' /
    \_ (   __._'_..'.__.   ) .'
      \     )  (_.(_)/(     /
       \\  /    \   /  \  //
        )  \     ). \  /  (
       / '  \   /    \|  ' \
      (  |   | / \/   |   )_)
       :-'-- /|  /   /|  /  \
       _\|  / | / .-' | / '. \
     _/  | /  \| (    ( \   '.\
    ( ).'/ |   |  )   |  |   | \
    /.' (_ /   | / )   \'|   |._)
   .oO  | /    | \/     \|    \ |
        |(     \  )   mrf)\    )_\
  _____(__'-.___)/____..'__)___'._'.__
"""
                sys.exit(17)
        except IndexError:
            pass
        try:
            self.load_config()
        except RuntimeError:
            # we run without a config file
            pass
        parser = argparse.ArgumentParser(
                prog=self.name,
                formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                )
        parser.add_argument(
                '-d', '--debug',
                dest='debug',
                action='store_true',
                default=False,
                help="Enable debugging output.",
                )
        subparsers = parser.add_subparsers(help='cmd --help')
        for name, cmd in self.iteritems():
            cmd_parser = subparsers.add_parser(
                    name,
                    help=cmd.__doc__,
                    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                    )
            cmd.init_argparser(cmd_parser)
            cmd_parser.set_defaults(cmd=cmd)
            if isinstance(cmd, CmdWrapper):
                cmd_parser.only_known_args = True
        expargs = []
        try:
            expargs.append(self.aliases.get(sys.argv[1], sys.argv[1]))
        except IndexError:
            pass
        else:
            expargs.extend(sys.argv[2:])
        pargs = parser.parse_args(expargs)
        if pargs.debug:
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.basicConfig(level=logging.INFO)
        if self.root:
            logger.debug(u"Rooted at %s." % (self.root,))
        else:
            logger.debug(u"Running unrooted.")
        output = pargs.cmd(pargs=pargs)
        if output is not None:
            print json.dumps(output, indent=4, sort_keys=True)