blob: 02d4e97f5ec4651037c26eddd00e2e174ef2677e [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
55 message = asklogmessage(new)
56 messages = x.put(fn, data, message)
57 if messages:
58 print messages
59
60def checkout(x, copts, fn):
61 data = x.get(fn)
62 f = open(fn, 'w')
63 f.write(data)
64 f.close()
65
66def info(x, copts, fn):
67 dict = x.info(fn)
68 keys = dict.keys()
69 keys.sort()
70 for key in keys:
71 print key + ':', dict[key]
72 print '='*70
73
74def head(x, copts, fn):
75 head = x.head(fn)
76 print fn, head
77
78def list(x, copts, fn):
79 if x.isfile(fn):
80 print fn
81
82def log(x, copts, fn):
83 flags = ''
84 for o, a in copts:
85 flags = flags + ' ' + o + a
86 flags = flags[1:]
87 messages = x.log(fn, flags)
88 print messages
89
90def diff(x, copts, fn):
91 if same(x, copts, fn):
92 return
93 flags = ''
94 for o, a in copts:
95 flags = flags + ' ' + o + a
96 flags = flags[1:]
97 data = x.get(fn)
98 tfn = tempfile.mktemp()
99 try:
100 tf = open(tfn, 'w')
101 tf.write(data)
102 tf.close()
103 print 'diff %s -r%s %s' % (flags, x.head(fn), fn)
104 sts = os.system('diff %s %s %s' % (flags, tfn, fn))
105 if sts:
106 print '='*70
107 finally:
108 remove(tfn)
109
110def same(x, copts, fn, data = None):
111 if data is None:
112 f = open(fn)
113 data = f.read()
114 f.close()
115 lsum = md5.new(data).digest()
116 rsum = x.sum(fn)
117 return lsum == rsum
118
119def asklogmessage(new):
120 if new:
121 print "enter description,",
122 else:
123 print "enter log message,",
124 print "terminate with single '.' or end of file:"
125 if new:
126 print "NOTE: This is NOT the log message!"
127 message = ""
128 while 1:
129 sys.stderr.write(">> ")
130 sys.stderr.flush()
131 line = sys.stdin.readline()
132 if not line or line == '.\n': break
133 message = message + line
134 return message
135
136def remove(fn):
137 try:
138 os.unlink(fn)
139 except os.error:
140 pass
141
142commands = {
143 'ci': ('', checkin),
144 'put': ('', checkin),
145 'co': ('', checkout),
146 'get': ('', checkout),
147 'info': ('', info),
148 'head': ('', head),
149 'list': ('', list),
150 'log': ('bhLRtd:l:r:s:w:V:', log),
151 'diff': ('c', diff),
152 }
153
154main()