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