/Demo/pdist/cvslib.py

http://unladen-swallow.googlecode.com/ · Python · 364 lines · 344 code · 16 blank · 4 comment · 19 complexity · e7e8d93eb4fb85e1b9ffbb386d72ff19 MD5 · raw file

  1. """Utilities for CVS administration."""
  2. import string
  3. import os
  4. import time
  5. import md5
  6. import fnmatch
  7. if not hasattr(time, 'timezone'):
  8. time.timezone = 0
  9. class File:
  10. """Represent a file's status.
  11. Instance variables:
  12. file -- the filename (no slashes), None if uninitialized
  13. lseen -- true if the data for the local file is up to date
  14. eseen -- true if the data from the CVS/Entries entry is up to date
  15. (this implies that the entry must be written back)
  16. rseen -- true if the data for the remote file is up to date
  17. proxy -- RCSProxy instance used to contact the server, or None
  18. Note that lseen and rseen don't necessary mean that a local
  19. or remote file *exists* -- they indicate that we've checked it.
  20. However, eseen means that this instance corresponds to an
  21. entry in the CVS/Entries file.
  22. If lseen is true:
  23. lsum -- checksum of the local file, None if no local file
  24. lctime -- ctime of the local file, None if no local file
  25. lmtime -- mtime of the local file, None if no local file
  26. If eseen is true:
  27. erev -- revision, None if this is a no revision (not '0')
  28. enew -- true if this is an uncommitted added file
  29. edeleted -- true if this is an uncommitted removed file
  30. ectime -- ctime of last local file corresponding to erev
  31. emtime -- mtime of last local file corresponding to erev
  32. extra -- 5th string from CVS/Entries file
  33. If rseen is true:
  34. rrev -- revision of head, None if non-existent
  35. rsum -- checksum of that revision, Non if non-existent
  36. If eseen and rseen are both true:
  37. esum -- checksum of revision erev, None if no revision
  38. Note
  39. """
  40. def __init__(self, file = None):
  41. if file and '/' in file:
  42. raise ValueError, "no slash allowed in file"
  43. self.file = file
  44. self.lseen = self.eseen = self.rseen = 0
  45. self.proxy = None
  46. def __cmp__(self, other):
  47. return cmp(self.file, other.file)
  48. def getlocal(self):
  49. try:
  50. self.lmtime, self.lctime = os.stat(self.file)[-2:]
  51. except os.error:
  52. self.lmtime = self.lctime = self.lsum = None
  53. else:
  54. self.lsum = md5.new(open(self.file).read()).digest()
  55. self.lseen = 1
  56. def getentry(self, line):
  57. words = string.splitfields(line, '/')
  58. if self.file and words[1] != self.file:
  59. raise ValueError, "file name mismatch"
  60. self.file = words[1]
  61. self.erev = words[2]
  62. self.edeleted = 0
  63. self.enew = 0
  64. self.ectime = self.emtime = None
  65. if self.erev[:1] == '-':
  66. self.edeleted = 1
  67. self.erev = self.erev[1:]
  68. if self.erev == '0':
  69. self.erev = None
  70. self.enew = 1
  71. else:
  72. dates = words[3]
  73. self.ectime = unctime(dates[:24])
  74. self.emtime = unctime(dates[25:])
  75. self.extra = words[4]
  76. if self.rseen:
  77. self.getesum()
  78. self.eseen = 1
  79. def getremote(self, proxy = None):
  80. if proxy:
  81. self.proxy = proxy
  82. try:
  83. self.rrev = self.proxy.head(self.file)
  84. except (os.error, IOError):
  85. self.rrev = None
  86. if self.rrev:
  87. self.rsum = self.proxy.sum(self.file)
  88. else:
  89. self.rsum = None
  90. if self.eseen:
  91. self.getesum()
  92. self.rseen = 1
  93. def getesum(self):
  94. if self.erev == self.rrev:
  95. self.esum = self.rsum
  96. elif self.erev:
  97. name = (self.file, self.erev)
  98. self.esum = self.proxy.sum(name)
  99. else:
  100. self.esum = None
  101. def putentry(self):
  102. """Return a line suitable for inclusion in CVS/Entries.
  103. The returned line is terminated by a newline.
  104. If no entry should be written for this file,
  105. return "".
  106. """
  107. if not self.eseen:
  108. return ""
  109. rev = self.erev or '0'
  110. if self.edeleted:
  111. rev = '-' + rev
  112. if self.enew:
  113. dates = 'Initial ' + self.file
  114. else:
  115. dates = gmctime(self.ectime) + ' ' + \
  116. gmctime(self.emtime)
  117. return "/%s/%s/%s/%s/\n" % (
  118. self.file,
  119. rev,
  120. dates,
  121. self.extra)
  122. def report(self):
  123. print '-'*50
  124. def r(key, repr=repr, self=self):
  125. try:
  126. value = repr(getattr(self, key))
  127. except AttributeError:
  128. value = "?"
  129. print "%-15s:" % key, value
  130. r("file")
  131. if self.lseen:
  132. r("lsum", hexify)
  133. r("lctime", gmctime)
  134. r("lmtime", gmctime)
  135. if self.eseen:
  136. r("erev")
  137. r("enew")
  138. r("edeleted")
  139. r("ectime", gmctime)
  140. r("emtime", gmctime)
  141. if self.rseen:
  142. r("rrev")
  143. r("rsum", hexify)
  144. if self.eseen:
  145. r("esum", hexify)
  146. class CVS:
  147. """Represent the contents of a CVS admin file (and more).
  148. Class variables:
  149. FileClass -- the class to be instantiated for entries
  150. (this should be derived from class File above)
  151. IgnoreList -- shell patterns for local files to be ignored
  152. Instance variables:
  153. entries -- a dictionary containing File instances keyed by
  154. their file name
  155. proxy -- an RCSProxy instance, or None
  156. """
  157. FileClass = File
  158. IgnoreList = ['.*', '@*', ',*', '*~', '*.o', '*.a', '*.so', '*.pyc']
  159. def __init__(self):
  160. self.entries = {}
  161. self.proxy = None
  162. def setproxy(self, proxy):
  163. if proxy is self.proxy:
  164. return
  165. self.proxy = proxy
  166. for e in self.entries.values():
  167. e.rseen = 0
  168. def getentries(self):
  169. """Read the contents of CVS/Entries"""
  170. self.entries = {}
  171. f = self.cvsopen("Entries")
  172. while 1:
  173. line = f.readline()
  174. if not line: break
  175. e = self.FileClass()
  176. e.getentry(line)
  177. self.entries[e.file] = e
  178. f.close()
  179. def putentries(self):
  180. """Write CVS/Entries back"""
  181. f = self.cvsopen("Entries", 'w')
  182. for e in self.values():
  183. f.write(e.putentry())
  184. f.close()
  185. def getlocalfiles(self):
  186. list = self.entries.keys()
  187. addlist = os.listdir(os.curdir)
  188. for name in addlist:
  189. if name in list:
  190. continue
  191. if not self.ignored(name):
  192. list.append(name)
  193. list.sort()
  194. for file in list:
  195. try:
  196. e = self.entries[file]
  197. except KeyError:
  198. e = self.entries[file] = self.FileClass(file)
  199. e.getlocal()
  200. def getremotefiles(self, proxy = None):
  201. if proxy:
  202. self.proxy = proxy
  203. if not self.proxy:
  204. raise RuntimeError, "no RCS proxy"
  205. addlist = self.proxy.listfiles()
  206. for file in addlist:
  207. try:
  208. e = self.entries[file]
  209. except KeyError:
  210. e = self.entries[file] = self.FileClass(file)
  211. e.getremote(self.proxy)
  212. def report(self):
  213. for e in self.values():
  214. e.report()
  215. print '-'*50
  216. def keys(self):
  217. keys = self.entries.keys()
  218. keys.sort()
  219. return keys
  220. def values(self):
  221. def value(key, self=self):
  222. return self.entries[key]
  223. return map(value, self.keys())
  224. def items(self):
  225. def item(key, self=self):
  226. return (key, self.entries[key])
  227. return map(item, self.keys())
  228. def cvsexists(self, file):
  229. file = os.path.join("CVS", file)
  230. return os.path.exists(file)
  231. def cvsopen(self, file, mode = 'r'):
  232. file = os.path.join("CVS", file)
  233. if 'r' not in mode:
  234. self.backup(file)
  235. return open(file, mode)
  236. def backup(self, file):
  237. if os.path.isfile(file):
  238. bfile = file + '~'
  239. try: os.unlink(bfile)
  240. except os.error: pass
  241. os.rename(file, bfile)
  242. def ignored(self, file):
  243. if os.path.isdir(file): return True
  244. for pat in self.IgnoreList:
  245. if fnmatch.fnmatch(file, pat): return True
  246. return False
  247. # hexify and unhexify are useful to print MD5 checksums in hex format
  248. hexify_format = '%02x' * 16
  249. def hexify(sum):
  250. "Return a hex representation of a 16-byte string (e.g. an MD5 digest)"
  251. if sum is None:
  252. return "None"
  253. return hexify_format % tuple(map(ord, sum))
  254. def unhexify(hexsum):
  255. "Return the original from a hexified string"
  256. if hexsum == "None":
  257. return None
  258. sum = ''
  259. for i in range(0, len(hexsum), 2):
  260. sum = sum + chr(string.atoi(hexsum[i:i+2], 16))
  261. return sum
  262. unctime_monthmap = {}
  263. def unctime(date):
  264. if date == "None": return None
  265. if not unctime_monthmap:
  266. months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
  267. 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  268. i = 0
  269. for m in months:
  270. i = i+1
  271. unctime_monthmap[m] = i
  272. words = string.split(date) # Day Mon DD HH:MM:SS YEAR
  273. year = string.atoi(words[4])
  274. month = unctime_monthmap[words[1]]
  275. day = string.atoi(words[2])
  276. [hh, mm, ss] = map(string.atoi, string.splitfields(words[3], ':'))
  277. ss = ss - time.timezone
  278. return time.mktime((year, month, day, hh, mm, ss, 0, 0, 0))
  279. def gmctime(t):
  280. if t is None: return "None"
  281. return time.asctime(time.gmtime(t))
  282. def test_unctime():
  283. now = int(time.time())
  284. t = time.gmtime(now)
  285. at = time.asctime(t)
  286. print 'GMT', now, at
  287. print 'timezone', time.timezone
  288. print 'local', time.ctime(now)
  289. u = unctime(at)
  290. print 'unctime()', u
  291. gu = time.gmtime(u)
  292. print '->', gu
  293. print time.asctime(gu)
  294. def test():
  295. x = CVS()
  296. x.getentries()
  297. x.getlocalfiles()
  298. ## x.report()
  299. import rcsclient
  300. proxy = rcsclient.openrcsclient()
  301. x.getremotefiles(proxy)
  302. x.report()
  303. if __name__ == "__main__":
  304. test()