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
193
194
195
|
"""Utilities to read and write CVS admin files (esp. CVS/Entries)"""
import string
import os
import time
class Entry:
"""Class representing one (parsed) line from CVS/Entries"""
def __init__(self, line):
words = string.splitfields(line, '/')
self.file = words[1]
self.rev = words[2]
dates = words[3] # ctime, mtime
if len(dates) != 49 or dates[:7] == 'Initial':
self.ctime = None
self.mtime = None
self.new = dates[:7] == 'Initial'
else:
self.ctime = unctime(dates[:24])
self.mtime = unctime(dates[25:])
self.new = 0
self.extra = words[4]
self.sum = None
def unparse(self):
if self.new:
dates = "Initial %s" % self.file
else:
dates = gmctime(self.ctime) + ' ' + gmctime(self.mtime)
return "/%s/%s/%s/%s/\n" % (
self.file,
self.rev,
dates,
self.extra)
def setsum(self, sum):
self.sum = sum
def getsum(self):
return self.sum
def sethexsum(self, hexsum):
self.setsum(unhexify(hexsum))
def gethexsum(self):
if self.sum:
return hexify(self.sum)
else:
return None
def dump(self):
keys = self.__dict__.keys()
keys.sort()
for key in keys:
print "%-15s: %s" % (key, `self.__dict__[key]`)
print '-'*45
class CVS:
"""Class representing the contents of CVS/Entries (and CVS/Sums)"""
def __init__(self):
self.readentries()
def readentries(self):
self.entries = {}
f = self.cvsopen("Entries")
while 1:
line = f.readline()
if not line: break
e = Entry(line)
self.entries[e.file] = e
f.close()
def readsums(self):
try:
f = self.cvsopen("Sums")
except IOError:
return
while 1:
line = f.readline()
if not line: break
words = string.split(line)
[file, rev, hexsum] = words
if not self.entries.has_key(file):
continue
e = self.entries[file]
if e.rev == rev:
e.sethexsum(hexsum)
f.close()
def writeentries(self):
f = self.cvsopen("Entries", 'w')
for file in self.keys():
f.write(self.entries[file].unparse())
f.close()
def writesums(self):
if self.cvsexists("Sums"):
f = self.cvsopen("Sums", 'w')
else:
f = None
for file in self.keys():
e = self.entries[file]
hexsum = e.gethexsum()
if hexsum:
if not f:
f = self.cvsopen("Sums", 'w')
f.write("%s %s %s\n" % (file, e.rev, hexsum))
if f:
f.close()
def keys(self):
keys = self.entries.keys()
keys.sort()
return keys
def cvsexists(self, file):
file = os.path.join("CVS", file)
return os.path.exists(file)
def cvsopen(self, file, mode = 'r'):
file = os.path.join("CVS", file)
if 'r' not in mode:
self.backup(file)
return open(file, mode)
def backup(self, file):
if os.path.isfile(file):
bfile = file + '~'
os.rename(file, bfile)
hexify_format = '%02x' * 16
def hexify(sum):
"Return a hex representation of a 16-byte string (e.g. an MD5 digest)"
return hexify_format % tuple(map(ord, sum))
def unhexify(hexsum):
"Return the original from a hexified string"
sum = ''
for i in range(0, len(hexsum), 2):
sum = sum + chr(string.atoi(hexsum[i:i+2], 16))
return sum
unctime_monthmap = {}
def unctime(date):
if not unctime_monthmap:
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
i = 0
for m in months:
i = i+1
unctime_monthmap[m] = i
words = string.split(date) # Day Mon DD HH:MM:SS YEAR
year = string.atoi(words[4])
month = unctime_monthmap[words[1]]
day = string.atoi(words[2])
[hh, mm, ss] = map(string.atoi, string.splitfields(words[3], ':'))
ss = ss - time.timezone
return time.mktime((year, month, day, hh, mm, ss, 0, 0, 0))
def gmctime(t):
return time.asctime(time.gmtime(t))
def test_unctime():
now = int(time.time())
t = time.gmtime(now)
at = time.asctime(t)
print 'GMT', now, at
print 'timezone', time.timezone
print 'local', time.ctime(now)
u = unctime(at)
print 'unctime()', u
gu = time.gmtime(u)
print '->', gu
print time.asctime(gu)
def test():
x = CVS()
keys = x.entries.keys()
keys.sort()
for file in keys:
e = x.entries[file]
print file, e.rev, gmctime(e.ctime), gmctime(e.mtime), e.extra,
print e.gethexsum()
if __name__ == "__main__":
test()
|