summaryrefslogtreecommitdiffstats
path: root/Demo/pdist/rcvs.py
blob: eb43bb871c67a367c82f4781ddb845f87c4c2688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from cvslib import CVS, Entry
import RCSProxy
import client
import md5
import os
import string
import sys
import time
import fnmatch


ignored_patterns = ['*.pyc', '.*', '*~', '@*']
def ignored(file):
	if os.path.isdir(file): return 1
	for pat in ignored_patterns:
		if fnmatch.fnmatch(file, pat): return 1
	return 0


class PCVS(CVS):
	
	def __init__(self, proxy):
		CVS.__init__(self)
		self.proxy = proxy
		self.readsums()
		self.calcsums()
	
	def calcsums(self):
		for file in self.keys():
			e = self.entries[file]
			if not e.new and e.sum is None:
				sum = self.proxy.sum((file, e.rev))
				e.setsum(sum)
	
	def fullcheck(self):
		ok = 1
		for file in self.keys():
			e = self.entries[file]
			if e.new:
				if self.proxy.isfile(file):
					print "%s: created by someone else!"
					ok = 0
				continue
			rrev = self.proxy.head(file)
			if rrev != e.rev:
				print "%s: out of date (%s vs. %s)" % \
				      (file, e.rev, rrev)
				ok = 0
		return ok
	
	def update(self):
		for file in self.keys():
			e = self.entries[file]
			if e.new:
				print 'A', file
				continue
			rrev = self.proxy.head(file)
			lsum = sumfile(file)
			if rrev == e.rev:
				if lsum == e.sum:
					print '=', file
				else:
					print 'M', file
				continue
			if e.sum != lsum:
				print "%s: conflict -- not updated" % file
				continue
			print "%s: getting ..." % file
			data = self.proxy.get(file)
			f = open(file, 'w')
			f.write(data)
			f.close()
			nsum = md5.new(data).digest()
			e.setsum(nsum)
			e.rev = rrev
			print 'U', file
		self.writeentries()
		self.writesums()
	
	def commit(self):
		if not self.fullcheck():
			print "correct above errors first"
			return
		needed = []
		for file in self.keys():
			e = self.entries[file]
			if e.new:
				needed.append(file)
				continue
			lsum = sumfile(file)
			if lsum != e.sum:
				needed.append(file)
				continue
		if not needed:
			print "no changes need committing"
			return
		message = raw_input("One-liner: ")
		for file in needed:
			print "%s: putting ..." % file
			e = self.entries[file]
			data = open(file).read()
			self.proxy.put(file, data, message)
			e.rev = self.proxy.head(file)
			e.setsum(self.proxy.sum(file))
			# XXX get it?
			mtime, ctime = os.stat(file)[-2:]
			e.mtime = mtime
			e.ctime = ctime
		self.writeentries()
		self.writesums()
	
	def report(self):
		keys = self.keys()
		files = os.listdir(os.curdir)
		allfiles = files
		for file in keys:
			if file not in allfiles:
				allfiles.append(file)
		allfiles.sort()
		for file in allfiles:
			if file not in keys:
				if not ignored(file):
					print '?', file
				continue
			if file not in files:
				print file, ': lost'
				continue
			e = self.entries[file]
			if not os.path.exists(file):
				print "%s: lost" % file
				continue
			if e.new:
				print 'A', file
				continue
			lsum = sumfile(file)
			rrev = self.proxy.head(file)
			if rrev == e.rev:
				if lsum == e.sum:
					print '=', file
				else:
					print 'M', file
			else:
				if lsum == e.sum:
					print 'U', file
				else:
					print 'C', file
	
	def add(self, file):
		if self.entries.has_key(file):
			print "%s: already known"
		else:
			self.entries[file] = Entry('/%s/0/Initial %s//\n' %
						   (file, file))


def sumfile(file):
	return md5.new(open(file).read()).digest()


def test():
	proxy = RCSProxy.RCSProxyClient(('voorn.cwi.nl', 4127))
	proxy.cd('/ufs/guido/voorn/python-RCS/Demo/pdist')
	x = PCVS(proxy)
	args = sys.argv[1:]
	if args:
		cmd = args[0]
		files = args[1:]
		if cmd == 'add':
			if not files:
				print "add needs at least one file argument"
			else:
				for file in files:
					x.add(file)
				x.writeentries()
		elif cmd in ('update', 'up'):
			if files:
				print "updates wants no file arguments"
			else:
				x.update()
		elif cmd in ('commit', 'com'):
			if files:
				print "commit wants no file arguments"
			else:
				x.commit()
		else:
			print "Unknown command", cmd
	else:
		x.report()
		if sys.argv[1:]: x.writesums()

if __name__ == "__main__":
	test()