def __init__(self, program_name): self.__program_name = program_name pattern = re.compile('[A-Z][^A-Z]*') self.classname = self.__class__.__name__ tokens = [ t for t in pattern.findall(self.classname) ] assert tokens[-2:] == [ 'Command', 'Line' ] if self._command_ is not None: self.command_class = self._command_ self.command_classname = self._command_.__name__ else: ccn = ''.join(tokens[:-1]) ccl = getattr(self.commands_module, ccn) self.command_classname = ccn self.command_class = ccl self.command = self.command_class(sys.stdin, sys.stdout, sys.stderr) tokens = [ t.lower() for t in tokens[:-2] ] self.name = '-'.join(t for t in tokens) self.shortname = None if self._shortname_ is not None: self.shortname = self._shortname_ elif len(tokens) > 1: self.shortname = ''.join(t[0] for t in tokens) self.aliases = None if self._aliases_: self.aliases = self._aliases_ self.conf = Config() self.repo_path = None self.parser = None
def create_conf(self, repo_name=None): text = dedent( """ [main] verbose=1 fallback=1 [foo] verbose=foo [bar] verbose=bar [repo:main] verbose=main """ ) tmpfile = NamedTemporaryFile() with open(tmpfile.name, 'w') as f: f.write(text) f.flush() f.close() conf = Config() if repo_name: conf._repo_name = repo_name conf.load(tmpfile.name) return conf
def create_conf(self, repo_name=None): text = dedent(""" [main] verbose=1 fallback=1 [foo] verbose=foo [bar] verbose=bar [repo:main] verbose=main """) tmpfile = NamedTemporaryFile() with open(tmpfile.name, 'w') as f: f.write(text) f.flush() f.close() conf = Config() if repo_name: conf._repo_name = repo_name conf.load(tmpfile.name) return conf
def get_changeset(cls, path, rev_or_txn, **kwds): k = DecayDict(**kwds) estream = k.get('estream', sys.stderr) ostream = k.get('ostream', sys.stdout) istream = k.get('istream', sys.stdout) c = ChangeSetCommand(ostream, estream) c.path = path c.conf = k.get('conf', Config()) c.options = k.get('options', Options()) c.rev_or_txn = rev_or_txn k.assert_empty(cls) with c: c.run() return c.result
def find_merges(cls, path, revision, **kwds): k = DecayDict(**kwds) estream = kwds.get('estream', sys.stderr) ostream = kwds.get('ostream', sys.stdout) istream = kwds.get('istream', sys.stdout) c = FindMergesCommand(istream, ostream, estream) c.revision = revision c.path = path c.conf = kwds.get('conf', Config()) c.options = kwds.get('options', Options()) k.assert_empty(cls) c.yield_values = True with c: c.run() return c
class CommandLine: """ The `CommandLine` class exposes `Command` classes via the `CLI` class. """ __metaclass__ = ABCMeta _rev_ = None _conf_ = False _repo_ = False _hook_ = False _argc_ = 0 _vargc_ = None _usage_ = None _quiet_ = None _epilog_ = None _verbose_ = None _command_ = None _aliases_ = None _rev_range_ = None _shortname_ = None _description_ = None @abstractproperty def commands_module(self): raise NotImplementedError() def __init__(self, program_name): self.__program_name = program_name pattern = re.compile('[A-Z][^A-Z]*') self.classname = self.__class__.__name__ tokens = [ t for t in pattern.findall(self.classname) ] assert tokens[-2:] == [ 'Command', 'Line' ] if self._command_ is not None: self.command_class = self._command_ self.command_classname = self._command_.__name__ else: ccn = ''.join(tokens[:-1]) ccl = getattr(self.commands_module, ccn) self.command_classname = ccn self.command_class = ccl self.command = self.command_class(sys.stdin, sys.stdout, sys.stderr) tokens = [ t.lower() for t in tokens[:-2] ] self.name = '-'.join(t for t in tokens) self.shortname = None if self._shortname_ is not None: self.shortname = self._shortname_ elif len(tokens) > 1: self.shortname = ''.join(t[0] for t in tokens) self.aliases = None if self._aliases_: self.aliases = self._aliases_ self.conf = Config() self.repo_path = None self.parser = None @property def program_name(self): return self.__program_name @property def _subcommand(self): return '%s %s' % (self.program_name, self.name) def _add_parser_options(self): pass def _pre_process_parser_results(self): pass def _process_parser_results(self): pass def _post_run(self): pass def usage_error(self, msg): self.parser.print_help() sys.stderr.write("\nerror: %s\n" % msg) self.parser.exit(status=1) def run(self, args): k = Dict() k.prog = self._subcommand if self._usage_: k.usage = self._usage_ elif self._repo_: k.usage = '%prog [ options ] REPO_PATH' if self._description_: k.description = self._description_ if self._epilog_: k.epilog = self._epilog_ k.formatter = CommandHelpFormatter() self.parser = optparse.OptionParser(**k) if self._verbose_: assert self._quiet_ is None self.parser.add_option( '-v', '--verbose', dest='verbose', action='store_true', default=False, help="run in verbose mode [default: %default]" ) if self._quiet_: assert self._verbose_ is None self.parser.add_option( '-q', '--quiet', dest='quiet', action='store_true', default=False, help="run in quiet mode [default: %default]" ) if self._conf_: self.parser.add_option( '-c', '--conf', metavar='FILE', help="use alternate configuration file FILE" ) if self._hook_: self.parser.add_option( '-k', '--hook', #type='string', dest='hook_name', metavar='NAME', action='store', choices=list(self.conf.hook_names), help="hook name (i.e. 'pre-commit')" ) if self._rev_: assert self._rev_range_ is None self.parser.add_option( '-r', '--revision', dest='revision', metavar='ARG', action='store', default=None, help="revision [default: evn:last_rev]" ) if self._rev_range_: assert self._rev_ is None self.parser.add_option( '-r', dest='revision_range', metavar='ARG', action='store', default='0:HEAD', help="revision range [default: %default]" ) self._add_parser_options() (opts, self.args) = self.parser.parse_args(args) # Ignore variable argument commands altogether. if self._vargc_ is not True: arglen = len(self.args) if arglen == 0 and self._argc_ != 0: self.parser.print_help() self.parser.exit(status=1) if len(self.args) != self._argc_ and self._argc_ != 0: self.usage_error("invalid number of arguments") self.options = Options(opts.__dict__) self._pre_process_parser_results() f = None if self._conf_: f = self.options.conf if f and not os.path.exists(f): self.usage_error("configuration file '%s' does not exist" % f) self.conf.load(filename=f) self.command.conf = self.conf if self._repo_: if len(self.args) < 1: self.usage_error("missing REPO_PATH argument") self.command.path = self.args.pop(0) if self._hook_: hn = self.options.hook_name if not hn: self.usage_error("missing option: -k/--hook") self.command.hook_name = hn if self._rev_: self.command.rev_str = self.options.revision if self._rev_range_: assert self.options.revision_range self.command.revision_range = self.options.revision_range self.command.args = self.args self.command.options = self.options self._process_parser_results() with self.command: self.command.run() self._post_run()
def run(self): cf = Config() cf.write(self.ostream)
def reload_conf(self): conf = Config() conf.load() conf.load_repo(self.path) self.conf = conf return conf