Mercurial > hg > gut
comparison gut/main.py @ 0:9688c72a93c3
initial commit of gut, probably doesnt actually work
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 16 Jul 2010 13:55:58 -0700 |
parents | |
children | 54b30e8f6f82 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9688c72a93c3 |
---|---|
1 #!/usr/bin/env python | |
2 """ | |
3 a workflow for git | |
4 """ | |
5 | |
6 import os | |
7 import subprocess | |
8 import sys | |
9 | |
10 from command import CommandParser | |
11 | |
12 def call(command, **kw): | |
13 if isinstance(command, basestring): | |
14 kw['shell'] = True | |
15 output = kw.pop('output', True) | |
16 if output or kw.pop('pipe', False): | |
17 kw['stdout'] = subprocess.PIPE | |
18 kw['stderr'] = subprocess.PIPE | |
19 check = kw.pop('check', True) | |
20 process = subprocess.Popen(command, **kw) | |
21 stdout, stderr = process.communicate() | |
22 code = process.poll() | |
23 if check and code: | |
24 if isinstance(command, basestring): | |
25 cmdstr = command | |
26 else: | |
27 cmdstr = ' '.join(command) | |
28 raise SystemExit("Command `%s` exited with code %d" % (cmdstr, code)) | |
29 if output: | |
30 print stdout | |
31 print stderr | |
32 return dict(stdout=stdout, stderr=stderr, code=code) | |
33 | |
34 class gut(object): | |
35 """ | |
36 a workflow for git | |
37 """ | |
38 | |
39 def __init__(self, remote=None): | |
40 self.remote = remote | |
41 | |
42 def hello(self, name='world'): | |
43 print 'hello %s' % name | |
44 | |
45 def update(self): | |
46 """update the master and the branch you're on""" | |
47 call(['git', 'checkout', 'master']) | |
48 call(['git', 'pull', 'origin', 'master']) | |
49 if self.remote: | |
50 call(['git', 'pull', self.remote, 'master']) | |
51 | |
52 def feature(self, name): | |
53 """make a new feature branch""" | |
54 call(['git', 'checkout', 'master']) | |
55 call(['git', 'checkout', '-b', name]) | |
56 call(['git', 'push', 'origin', name]) | |
57 | |
58 def patch(self, output=None): | |
59 """generate a patch for review""" | |
60 if not output: | |
61 output = self.branch() + '.diff' | |
62 diff = call(['git', 'diff', 'master'], pipe=True, output=False) | |
63 diff = diff['stdout'] | |
64 log = call(['git', 'log', 'master..'], pipe=True, output=False) | |
65 log = log['stdout'] | |
66 f = file(output) # write the output to a patch file | |
67 return log | |
68 | |
69 def branch(self): | |
70 """print what branch you're on""" | |
71 output = call(['git', 'branch'], output=False) | |
72 for line in output['stdout'].splitlines(): | |
73 if line.startswith('*'): | |
74 return line[1:].strip() | |
75 | |
76 def main(args=sys.argv[1:]): | |
77 parser = CommandParser(gut) | |
78 options, args = parser.parse_args(args) | |
79 parser.invoke(args) | |
80 | |
81 if __name__ == '__main__': | |
82 main() |