Implement clone, init, add
[yap.git] / yap / yap.py
blobdfc6032b32a02997e5a2cf5cfcdeb5ce2b67c1a6
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 if len(output) == 1:
10 return output[0]
11 return output
13 class YapError(Exception):
14 def __init__(self, msg):
15 self.msg = msg
17 def __str__(self):
18 return self.msg
20 def takes_options(options):
21 def decorator(func):
22 func.options = options
23 return func
24 return decorator
26 class Yap(object):
27 def cmd_clone(self, url, directory=""):
28 # XXX: implement in terms of init + remote add + fetch
29 os.system("git clone '%s' %s" % (url, directory))
31 def cmd_init(self):
32 os.system("git init")
34 def cmd_add(self, file):
35 if not os.access(file, os.R_OK):
36 raise YapError("No such file: %s" % file)
37 x = get_output("git ls-files '%s'" % file)
38 if x != []:
39 raise YapError("File '%s' already in repository" % file)
40 os.system("git update-index --add '%s'" % file)
42 def cmd_version(self):
43 print "Yap version 0.1"
45 def cmd_usage(self):
46 print >> sys.stderr, "usage: %s <command>" % sys.argv[0]
47 print >> sys.stderr, " valid commands: version"
49 def main(self, args):
50 if len(args) < 1:
51 self.cmd_usage()
52 sys.exit(2)
54 command = args[0]
55 args = args[1:]
57 debug = os.getenv('YAP_DEBUG')
59 try:
60 meth = self.__getattribute__("cmd_"+command)
61 try:
62 if "option" in meth.__dict__:
63 flags, args = getopt.getopt(args, meth.options)
64 flags = dict(flags)
65 else:
66 flags = dict()
68 meth(*args, **flags)
69 except (TypeError, getopt.GetoptError), e:
70 if debug:
71 raise e
72 print "%s %s %s" % (sys.argv[0], command, meth.__doc__)
73 except YapError, e:
74 print >> sys.stderr, e
75 sys.exit(1)
76 except AttributeError, e:
77 if debug:
78 raise e
79 self.cmd_usage()
80 sys.exit(2)