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