blob: 70895ea567f8d0d0eb0744d18ebdfa696ca241f4 [file] [log] [blame]
Guido van Rossum5f07b841995-04-26 22:57:11 +00001#! /usr/local/bin/python
2
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 Rossum4d4ee8b1995-04-27 18:07:20 +000016 opts, rest = getopt.getopt(sys.argv[1:], 'h:p:d:qv')
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()
51 new = not x.isfile(fn)
52 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
67def info(x, copts, fn):
68 dict = x.info(fn)
69 keys = dict.keys()
70 keys.sort()
71 for key in keys:
72 print key + ':', dict[key]
73 print '='*70
74
75def head(x, copts, fn):
76 head = x.head(fn)
77 print fn, head
78
79def list(x, copts, fn):
80 if x.isfile(fn):
81 print fn
82
83def log(x, copts, fn):
84 flags = ''
85 for o, a in copts:
86 flags = flags + ' ' + o + a
87 flags = flags[1:]
88 messages = x.log(fn, flags)
89 print messages
90
91def diff(x, copts, fn):
92 if same(x, copts, fn):
93 return
94 flags = ''
95 for o, a in copts:
96 flags = flags + ' ' + o + a
97 flags = flags[1:]
98 data = x.get(fn)
99 tfn = tempfile.mktemp()
100 try:
101 tf = open(tfn, 'w')
102 tf.write(data)
103 tf.close()
104 print 'diff %s -r%s %s' % (flags, x.head(fn), fn)
105 sts = os.system('diff %s %s %s' % (flags, tfn, fn))
106 if sts:
107 print '='*70
108 finally:
109 remove(tfn)
110
111def same(x, copts, fn, data = None):
112 if data is None:
113 f = open(fn)
114 data = f.read()
115 f.close()
116 lsum = md5.new(data).digest()
117 rsum = x.sum(fn)
118 return lsum == rsum
119
120def asklogmessage(new):
121 if new:
122 print "enter description,",
123 else:
124 print "enter log message,",
125 print "terminate with single '.' or end of file:"
126 if new:
127 print "NOTE: This is NOT the log message!"
128 message = ""
129 while 1:
130 sys.stderr.write(">> ")
131 sys.stderr.flush()
132 line = sys.stdin.readline()
133 if not line or line == '.\n': break
134 message = message + line
135 return message
136
137def remove(fn):
138 try:
139 os.unlink(fn)
140 except os.error:
141 pass
142
143commands = {
144 'ci': ('', checkin),
145 'put': ('', checkin),
146 'co': ('', checkout),
147 'get': ('', checkout),
148 'info': ('', info),
149 'head': ('', head),
150 'list': ('', list),
151 'log': ('bhLRtd:l:r:s:w:V:', log),
152 'diff': ('c', diff),
153 }
154
Guido van Rossum547e8d81995-04-27 21:28:20 +0000155if __name__ == '__main__':
156 main()