# HG changeset patch # User Jeff Hammel # Date 1279313758 25200 # Node ID 9688c72a93c3c9d7d499f1332c8d5098236b1b04 initial commit of gut, probably doesnt actually work diff -r 000000000000 -r 9688c72a93c3 gut/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gut/__init__.py Fri Jul 16 13:55:58 2010 -0700 @@ -0,0 +1,1 @@ +# diff -r 000000000000 -r 9688c72a93c3 gut/command.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gut/command.py Fri Jul 16 13:55:58 2010 -0700 @@ -0,0 +1,191 @@ +""" +a command-line interface to the command line, a la pythonpaste +""" + +import inspect +import sys +from optparse import OptionParser +from pprint import pprint + +class CommandParser(OptionParser): + # TODO: add `help` command + + def __init__(self, _class, description=None): + self._class = _class + self.commands = {} + usage = '%prog [options] command [command-options]' + description = description or _class.__doc__ + OptionParser.__init__(self, usage=usage, description=description) + commands = [ getattr(_class, i) for i in dir(_class) + if not i.startswith('_') ] + commands = [ method for method in commands + if hasattr(method, '__call__') ] + for _command in commands: + c = self.command(_command) + self.commands[c['name']] = c + + # get class options + init = self.command(_class.__init__) + self.command2parser(init, self) + + self.disable_interspersed_args() + + def print_help(self): + # XXX should probably use the optparse formatters to help out here + + OptionParser.print_help(self) + + # short descriptions for commands + command_descriptions = [dict(name=i, + description=self.commands[i]['doc'].strip().split('\n',1)[0]) + for i in sorted(self.commands.keys())] + max_len = max([len(i['name']) for i in command_descriptions]) + description = "Commands: \n%s" % ('\n'.join([' %s%s %s' % (description['name'], ' ' * (max_len - len(description['name'])), description['description']) + for description in command_descriptions])) + + print + print description + + def parse(self, args=sys.argv[1:]): + """global parse step""" + + self.options, args = self.parse_args(args) + + # help/sanity check -- should probably be separated + if not len(args): + self.print_help() + sys.exit(0) + if args[0] == 'help': + if len(args) == 2: + if args[1] in self.commands: + name = args[1] + commandparser = self.command2parser(name) + commandparser.print_help() + else: + self.error("No command '%s'" % args[1]) + else: + self.print_help() + sys.exit(0) + command = args[0] + if command not in self.commands: + self.error("No command '%s'" % command) + return command, args[1:] + + def invoke(self, args=sys.argv[1:]): + """ + invoke + """ + + # parse + name, args = self.parse(args) + + # setup + _object = self._class(**self.options.__dict__) + + # command specific args + command = self.commands[name] + commandparser = self.command2parser(name) + command_options, command_args = commandparser.parse_args(args) + if len(command_args) < len(command['args']): + commandparser.error("Not enough arguments given") + if len(command_args) != len(command['args']) and not command['varargs']: + commandparser.error("Too many arguments given") + + # invoke the command + retval = getattr(_object, name)(*command_args, **command_options.__dict__) + if isinstance(retval, basestring): + print retval + elif retval is None: + pass + else: + pprint(retval) + return retval + + def command(self, function): + name = function.func_name + if function.__doc__: + doc = inspect.cleandoc(function.__doc__) + else: + doc = '' + argspec = inspect.getargspec(function) + defaults = argspec.defaults + if defaults: + args = argspec.args[1:-len(defaults)] + optional = dict(zip(argspec.args[-len(defaults):], defaults)) + else: + args = argspec.args[1:] + optional = None + command = { 'doc': doc, + 'name': name, + 'args': args, + 'optional': optional, + 'varargs': argspec.varargs + } + return command + + def commandargs2str(self, command): + if isinstance(command, basestring): + command = self.commands[command] + retval = [] + retval.extend(['<%s>' % arg for arg in command['args']]) + varargs = command['varargs'] + if varargs: + retval.append('<%s> [%s] [...]' % (varargs, varargs)) + if command['optional']: + retval.append('[options]') + return ' '.join(retval) + + def doc2arghelp(self, docstring, decoration='-', delimeter=':'): + """ + Parse a docstring and get at the section describing arguments + - decoration: decoration character + - delimeter: delimter character + + Yields a tuple of the stripped docstring and the arguments help + dictionary + """ + lines = [ i.strip() for i in docstring.split('\n') ] + argdict = {} + doc = [] + option = None + for line in lines: + if not line and option: # blank lines terminate [?] + break + if line.startswith(decoration) and delimeter in line: + name, description = line.split(delimeter, 1) + name = name.lstrip(decoration).strip() + description = description.strip() + argdict[name] = [ description ] + option = name + else: + if option: + argdict[name].append(line) + else: + doc.append(line) + argdict = dict([(key, ' '.join(value)) + for key, value in argdict.items()]) + return ('\n'.join(doc), argdict) + + def command2parser(self, command, parser=None): + if isinstance(command, basestring): + command = self.commands[command] + doc, argdict = self.doc2arghelp(command['doc']) + if parser is None: + parser = OptionParser('%%prog %s %s' % (command, self.commandargs2str(command)), + description=doc, add_help_option=False) + if command['optional']: + for key, value in command['optional'].items(): + help = argdict.get(key, '') + if value is True: + parser.add_option('--no-%s' % key, dest=key, + action='store_false', default=True, + help=help) + elif value is False: + parser.add_option('--%s' % key, action='store_true', + default=False, help=help) + else: + if value is not None: + help += ' [DEFAULT: %s]' % value + parser.add_option('--%s' % key, help=help, default=value) + + return parser diff -r 000000000000 -r 9688c72a93c3 gut/main.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gut/main.py Fri Jul 16 13:55:58 2010 -0700 @@ -0,0 +1,82 @@ +#!/usr/bin/env python +""" +a workflow for git +""" + +import os +import subprocess +import sys + +from command import CommandParser + +def call(command, **kw): + if isinstance(command, basestring): + kw['shell'] = True + output = kw.pop('output', True) + if output or kw.pop('pipe', False): + kw['stdout'] = subprocess.PIPE + kw['stderr'] = subprocess.PIPE + check = kw.pop('check', True) + process = subprocess.Popen(command, **kw) + stdout, stderr = process.communicate() + code = process.poll() + if check and code: + if isinstance(command, basestring): + cmdstr = command + else: + cmdstr = ' '.join(command) + raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) + if output: + print stdout + print stderr + return dict(stdout=stdout, stderr=stderr, code=code) + +class gut(object): + """ + a workflow for git + """ + + def __init__(self, remote=None): + self.remote = remote + + def hello(self, name='world'): + print 'hello %s' % name + + def update(self): + """update the master and the branch you're on""" + call(['git', 'checkout', 'master']) + call(['git', 'pull', 'origin', 'master']) + if self.remote: + call(['git', 'pull', self.remote, 'master']) + + def feature(self, name): + """make a new feature branch""" + call(['git', 'checkout', 'master']) + call(['git', 'checkout', '-b', name]) + call(['git', 'push', 'origin', name]) + + def patch(self, output=None): + """generate a patch for review""" + if not output: + output = self.branch() + '.diff' + diff = call(['git', 'diff', 'master'], pipe=True, output=False) + diff = diff['stdout'] + log = call(['git', 'log', 'master..'], pipe=True, output=False) + log = log['stdout'] + f = file(output) # write the output to a patch file + return log + + def branch(self): + """print what branch you're on""" + output = call(['git', 'branch'], output=False) + for line in output['stdout'].splitlines(): + if line.startswith('*'): + return line[1:].strip() + +def main(args=sys.argv[1:]): + parser = CommandParser(gut) + options, args = parser.parse_args(args) + parser.invoke(args) + +if __name__ == '__main__': + main() diff -r 000000000000 -r 9688c72a93c3 setup.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Fri Jul 16 13:55:58 2010 -0700 @@ -0,0 +1,28 @@ +from setuptools import setup, find_packages +import sys, os + +version = '0.0' + +setup(name='gut', + version=version, + description='a workflow for git', + long_description="""\ +""", + classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers + keywords='', + author='Jeff Hammel', + author_email='jhammel@mozilla.com', + url='http://k0s.org/', + license='MPL', + packages=find_packages(exclude=['ez_setup', 'examples', 'tests']), + include_package_data=True, + zip_safe=False, + install_requires=[ + # -*- Extra requirements: -*- + ], + entry_points=""" + # -*- Entry points: -*- + [console_scripts] + gut = gut.main:main + """, + )