]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | #! /usr/bin/env python\r |
2 | \r | |
3 | "Remote RCS -- command line interface"\r | |
4 | \r | |
5 | import sys\r | |
6 | import os\r | |
7 | import getopt\r | |
8 | import string\r | |
9 | import md5\r | |
10 | import tempfile\r | |
11 | from rcsclient import openrcsclient\r | |
12 | \r | |
13 | def main():\r | |
14 | sys.stdout = sys.stderr\r | |
15 | try:\r | |
16 | opts, rest = getopt.getopt(sys.argv[1:], 'h:p:d:qvL')\r | |
17 | if not rest:\r | |
18 | cmd = 'head'\r | |
19 | else:\r | |
20 | cmd, rest = rest[0], rest[1:]\r | |
21 | if not commands.has_key(cmd):\r | |
22 | raise getopt.error, "unknown command"\r | |
23 | coptset, func = commands[cmd]\r | |
24 | copts, files = getopt.getopt(rest, coptset)\r | |
25 | except getopt.error, msg:\r | |
26 | print msg\r | |
27 | print "usage: rrcs [options] command [options] [file] ..."\r | |
28 | print "where command can be:"\r | |
29 | print " ci|put # checkin the given files"\r | |
30 | print " co|get # checkout"\r | |
31 | print " info # print header info"\r | |
32 | print " head # print revision of head branch"\r | |
33 | print " list # list filename if valid"\r | |
34 | print " log # print full log"\r | |
35 | print " diff # diff rcs file and work file"\r | |
36 | print "if no files are given, all remote rcs files are assumed"\r | |
37 | sys.exit(2)\r | |
38 | x = openrcsclient(opts)\r | |
39 | if not files:\r | |
40 | files = x.listfiles()\r | |
41 | for fn in files:\r | |
42 | try:\r | |
43 | func(x, copts, fn)\r | |
44 | except (IOError, os.error), msg:\r | |
45 | print "%s: %s" % (fn, msg)\r | |
46 | \r | |
47 | def checkin(x, copts, fn):\r | |
48 | f = open(fn)\r | |
49 | data = f.read()\r | |
50 | f.close()\r | |
51 | new = not x.isvalid(fn)\r | |
52 | if not new and same(x, copts, fn, data):\r | |
53 | print "%s: unchanged since last checkin" % fn\r | |
54 | return\r | |
55 | print "Checking in", fn, "..."\r | |
56 | message = asklogmessage(new)\r | |
57 | messages = x.put(fn, data, message)\r | |
58 | if messages:\r | |
59 | print messages\r | |
60 | \r | |
61 | def checkout(x, copts, fn):\r | |
62 | data = x.get(fn)\r | |
63 | f = open(fn, 'w')\r | |
64 | f.write(data)\r | |
65 | f.close()\r | |
66 | \r | |
67 | def lock(x, copts, fn):\r | |
68 | x.lock(fn)\r | |
69 | \r | |
70 | def unlock(x, copts, fn):\r | |
71 | x.unlock(fn)\r | |
72 | \r | |
73 | def info(x, copts, fn):\r | |
74 | dict = x.info(fn)\r | |
75 | keys = dict.keys()\r | |
76 | keys.sort()\r | |
77 | for key in keys:\r | |
78 | print key + ':', dict[key]\r | |
79 | print '='*70\r | |
80 | \r | |
81 | def head(x, copts, fn):\r | |
82 | head = x.head(fn)\r | |
83 | print fn, head\r | |
84 | \r | |
85 | def list(x, copts, fn):\r | |
86 | if x.isvalid(fn):\r | |
87 | print fn\r | |
88 | \r | |
89 | def log(x, copts, fn):\r | |
90 | flags = ''\r | |
91 | for o, a in copts:\r | |
92 | flags = flags + ' ' + o + a\r | |
93 | flags = flags[1:]\r | |
94 | messages = x.log(fn, flags)\r | |
95 | print messages\r | |
96 | \r | |
97 | def diff(x, copts, fn):\r | |
98 | if same(x, copts, fn):\r | |
99 | return\r | |
100 | flags = ''\r | |
101 | for o, a in copts:\r | |
102 | flags = flags + ' ' + o + a\r | |
103 | flags = flags[1:]\r | |
104 | data = x.get(fn)\r | |
105 | tf = tempfile.NamedTemporaryFile()\r | |
106 | tf.write(data)\r | |
107 | tf.flush()\r | |
108 | print 'diff %s -r%s %s' % (flags, x.head(fn), fn)\r | |
109 | sts = os.system('diff %s %s %s' % (flags, tf.name, fn))\r | |
110 | if sts:\r | |
111 | print '='*70\r | |
112 | \r | |
113 | def same(x, copts, fn, data = None):\r | |
114 | if data is None:\r | |
115 | f = open(fn)\r | |
116 | data = f.read()\r | |
117 | f.close()\r | |
118 | lsum = md5.new(data).digest()\r | |
119 | rsum = x.sum(fn)\r | |
120 | return lsum == rsum\r | |
121 | \r | |
122 | def asklogmessage(new):\r | |
123 | if new:\r | |
124 | print "enter description,",\r | |
125 | else:\r | |
126 | print "enter log message,",\r | |
127 | print "terminate with single '.' or end of file:"\r | |
128 | if new:\r | |
129 | print "NOTE: This is NOT the log message!"\r | |
130 | message = ""\r | |
131 | while 1:\r | |
132 | sys.stderr.write(">> ")\r | |
133 | sys.stderr.flush()\r | |
134 | line = sys.stdin.readline()\r | |
135 | if not line or line == '.\n': break\r | |
136 | message = message + line\r | |
137 | return message\r | |
138 | \r | |
139 | def remove(fn):\r | |
140 | try:\r | |
141 | os.unlink(fn)\r | |
142 | except os.error:\r | |
143 | pass\r | |
144 | \r | |
145 | commands = {\r | |
146 | 'ci': ('', checkin),\r | |
147 | 'put': ('', checkin),\r | |
148 | 'co': ('', checkout),\r | |
149 | 'get': ('', checkout),\r | |
150 | 'info': ('', info),\r | |
151 | 'head': ('', head),\r | |
152 | 'list': ('', list),\r | |
153 | 'lock': ('', lock),\r | |
154 | 'unlock': ('', unlock),\r | |
155 | 'log': ('bhLRtd:l:r:s:w:V:', log),\r | |
156 | 'diff': ('c', diff),\r | |
157 | }\r | |
158 | \r | |
159 | if __name__ == '__main__':\r | |
160 | main()\r |