Guido van Rossum | 5f07b84 | 1995-04-26 22:57:11 +0000 | [diff] [blame^] | 1 | from cvslib import CVS, Entry |
| 2 | import RCSProxy |
| 3 | import client |
| 4 | import md5 |
| 5 | import os |
| 6 | import string |
| 7 | import sys |
| 8 | import time |
| 9 | import fnmatch |
| 10 | |
| 11 | |
| 12 | ignored_patterns = ['*.pyc', '.*', '*~', '@*'] |
| 13 | def ignored(file): |
| 14 | if os.path.isdir(file): return 1 |
| 15 | for pat in ignored_patterns: |
| 16 | if fnmatch.fnmatch(file, pat): return 1 |
| 17 | return 0 |
| 18 | |
| 19 | |
| 20 | class PCVS(CVS): |
| 21 | |
| 22 | def __init__(self, proxy): |
| 23 | CVS.__init__(self) |
| 24 | self.proxy = proxy |
| 25 | self.readsums() |
| 26 | self.calcsums() |
| 27 | |
| 28 | def calcsums(self): |
| 29 | for file in self.keys(): |
| 30 | e = self.entries[file] |
| 31 | if not e.new and e.sum is None: |
| 32 | sum = self.proxy.sum((file, e.rev)) |
| 33 | e.setsum(sum) |
| 34 | |
| 35 | def fullcheck(self): |
| 36 | ok = 1 |
| 37 | for file in self.keys(): |
| 38 | e = self.entries[file] |
| 39 | if e.new: |
| 40 | if self.proxy.isfile(file): |
| 41 | print "%s: created by someone else!" |
| 42 | ok = 0 |
| 43 | continue |
| 44 | rrev = self.proxy.head(file) |
| 45 | if rrev != e.rev: |
| 46 | print "%s: out of date (%s vs. %s)" % \ |
| 47 | (file, e.rev, rrev) |
| 48 | ok = 0 |
| 49 | return ok |
| 50 | |
| 51 | def update(self): |
| 52 | for file in self.keys(): |
| 53 | e = self.entries[file] |
| 54 | if e.new: |
| 55 | print 'A', file |
| 56 | continue |
| 57 | rrev = self.proxy.head(file) |
| 58 | lsum = sumfile(file) |
| 59 | if rrev == e.rev: |
| 60 | if lsum == e.sum: |
| 61 | print '=', file |
| 62 | else: |
| 63 | print 'M', file |
| 64 | continue |
| 65 | if e.sum != lsum: |
| 66 | print "%s: conflict -- not updated" % file |
| 67 | continue |
| 68 | print "%s: getting ..." % file |
| 69 | data = self.proxy.get(file) |
| 70 | f = open(file, 'w') |
| 71 | f.write(data) |
| 72 | f.close() |
| 73 | nsum = md5.new(data).digest() |
| 74 | e.setsum(nsum) |
| 75 | e.rev = rrev |
| 76 | print 'U', file |
| 77 | self.writeentries() |
| 78 | self.writesums() |
| 79 | |
| 80 | def commit(self): |
| 81 | if not self.fullcheck(): |
| 82 | print "correct above errors first" |
| 83 | return |
| 84 | needed = [] |
| 85 | for file in self.keys(): |
| 86 | e = self.entries[file] |
| 87 | if e.new: |
| 88 | needed.append(file) |
| 89 | continue |
| 90 | lsum = sumfile(file) |
| 91 | if lsum != e.sum: |
| 92 | needed.append(file) |
| 93 | continue |
| 94 | if not needed: |
| 95 | print "no changes need committing" |
| 96 | return |
| 97 | message = raw_input("One-liner: ") |
| 98 | for file in needed: |
| 99 | print "%s: putting ..." % file |
| 100 | e = self.entries[file] |
| 101 | data = open(file).read() |
| 102 | self.proxy.put(file, data, message) |
| 103 | e.rev = self.proxy.head(file) |
| 104 | e.setsum(self.proxy.sum(file)) |
| 105 | # XXX get it? |
| 106 | mtime, ctime = os.stat(file)[-2:] |
| 107 | e.mtime = mtime |
| 108 | e.ctime = ctime |
| 109 | self.writeentries() |
| 110 | self.writesums() |
| 111 | |
| 112 | def report(self): |
| 113 | keys = self.keys() |
| 114 | files = os.listdir(os.curdir) |
| 115 | allfiles = files |
| 116 | for file in keys: |
| 117 | if file not in allfiles: |
| 118 | allfiles.append(file) |
| 119 | allfiles.sort() |
| 120 | for file in allfiles: |
| 121 | if file not in keys: |
| 122 | if not ignored(file): |
| 123 | print '?', file |
| 124 | continue |
| 125 | if file not in files: |
| 126 | print file, ': lost' |
| 127 | continue |
| 128 | e = self.entries[file] |
| 129 | if not os.path.exists(file): |
| 130 | print "%s: lost" % file |
| 131 | continue |
| 132 | if e.new: |
| 133 | print 'A', file |
| 134 | continue |
| 135 | lsum = sumfile(file) |
| 136 | rrev = self.proxy.head(file) |
| 137 | if rrev == e.rev: |
| 138 | if lsum == e.sum: |
| 139 | print '=', file |
| 140 | else: |
| 141 | print 'M', file |
| 142 | else: |
| 143 | if lsum == e.sum: |
| 144 | print 'U', file |
| 145 | else: |
| 146 | print 'C', file |
| 147 | |
| 148 | def add(self, file): |
| 149 | if self.entries.has_key(file): |
| 150 | print "%s: already known" |
| 151 | else: |
| 152 | self.entries[file] = Entry('/%s/0/Initial %s//\n' % |
| 153 | (file, file)) |
| 154 | |
| 155 | |
| 156 | def sumfile(file): |
| 157 | return md5.new(open(file).read()).digest() |
| 158 | |
| 159 | |
| 160 | def test(): |
| 161 | proxy = RCSProxy.RCSProxyClient(('voorn.cwi.nl', 4127)) |
| 162 | proxy.cd('/ufs/guido/voorn/python-RCS/Demo/pdist') |
| 163 | x = PCVS(proxy) |
| 164 | args = sys.argv[1:] |
| 165 | if args: |
| 166 | cmd = args[0] |
| 167 | files = args[1:] |
| 168 | if cmd == 'add': |
| 169 | if not files: |
| 170 | print "add needs at least one file argument" |
| 171 | else: |
| 172 | for file in files: |
| 173 | x.add(file) |
| 174 | x.writeentries() |
| 175 | elif cmd in ('update', 'up'): |
| 176 | if files: |
| 177 | print "updates wants no file arguments" |
| 178 | else: |
| 179 | x.update() |
| 180 | elif cmd in ('commit', 'com'): |
| 181 | if files: |
| 182 | print "commit wants no file arguments" |
| 183 | else: |
| 184 | x.commit() |
| 185 | else: |
| 186 | print "Unknown command", cmd |
| 187 | else: |
| 188 | x.report() |
| 189 | if sys.argv[1:]: x.writesums() |
| 190 | |
| 191 | if __name__ == "__main__": |
| 192 | test() |