Fixup debugging path
[yap.git] / yap / yap.py
blobc81175ac808cd40958e161119306b7caf9ea8502
1 import sys
2 import os
3 import getopt
5 def get_output(cmd):
6 fd = os.popen(cmd)
7 output = fd.readlines()
8 rc = fd.close()
9 return [x.strip() for x in output]
11 def run_command(cmd):
12 rc = os.system("%s > /dev/null 2>&1" % cmd)
13 rc >>= 8
14 return rc
16 class YapError(Exception):
17 def __init__(self, msg):
18 self.msg = msg
20 def __str__(self):
21 return self.msg
23 def takes_options(options):
24 def decorator(func):
25 func.options = options
26 return func
27 return decorator
29 class Yap(object):
30 def cmd_clone(self, url, directory=""):
31 # XXX: implement in terms of init + remote add + fetch
32 os.system("git clone '%s' %s" % (url, directory))
34 def cmd_init(self):
35 os.system("git init")
37 def cmd_add(self, file):
38 if not os.access(file, os.R_OK):
39 raise YapError("No such file: %s" % file)
40 x = get_output("git ls-files '%s'" % file)
41 if x != []:
42 raise YapError("File '%s' already in repository" % file)
43 os.system("git update-index --add '%s'" % file)
44 self.cmd_status()
46 def cmd_stage(self, file):
47 if not os.access(file, os.R_OK):
48 raise YapError("No such file: %s" % file)
49 os.system("git update-index --add '%s'" % file)
50 self.cmd_status()
52 def cmd_unstage(self, file):
53 if not os.access(file, os.R_OK):
54 raise YapError("No such file: %s" % file)
55 if run_command("git rev-parse HEAD"):
56 os.system("git update-index --force-remove '%s'" % file)
57 else:
58 os.system("git diff-index HEAD '%s' | git apply -R --cached" % file)
59 self.cmd_status()
61 def cmd_status(self):
62 branch = get_output("git symbolic-ref HEAD")[0]
63 branch = branch.replace('refs/heads/', '')
64 print "Current branch: %s" % branch
66 print "Files with staged changes:"
68 if run_command("git rev-parse HEAD"):
69 files = get_output("git ls-files --cached")
70 else:
71 files = get_output("git diff-index --name-only HEAD")
72 for f in files:
73 print "\t%s" % f
74 if not files:
75 print "\t(none)"
77 print "Files with unstages changes:"
78 files = get_output("git ls-files -m")
79 for f in files:
80 print "\t%s" % f
81 if not files:
82 print "\t(none)"
84 def cmd_version(self):
85 print "Yap version 0.1"
87 def cmd_usage(self):
88 print >> sys.stderr, "usage: %s <command>" % sys.argv[0]
89 print >> sys.stderr, " valid commands: version"
91 def main(self, args):
92 if len(args) < 1:
93 self.cmd_usage()
94 sys.exit(2)
96 command = args[0]
97 args = args[1:]
99 debug = os.getenv('YAP_DEBUG')
101 try:
102 meth = self.__getattribute__("cmd_"+command)
103 try:
104 if "option" in meth.__dict__:
105 flags, args = getopt.getopt(args, meth.options)
106 flags = dict(flags)
107 else:
108 flags = dict()
110 meth(*args, **flags)
111 except (TypeError, getopt.GetoptError):
112 if debug:
113 raise
114 print "%s %s %s" % (sys.argv[0], command, meth.__doc__)
115 except YapError, e:
116 print >> sys.stderr, e
117 sys.exit(1)
118 except AttributeError:
119 if debug:
120 raise
121 self.cmd_usage()
122 sys.exit(2)