/Demo/pdist/rrcs.py

http://unladen-swallow.googlecode.com/ · Python · 160 lines · 142 code · 17 blank · 1 comment · 28 complexity · 8e83a238c68719b16335f48e09f1d311 MD5 · raw file

  1. #! /usr/bin/env python
  2. "Remote RCS -- command line interface"
  3. import sys
  4. import os
  5. import getopt
  6. import string
  7. import md5
  8. import tempfile
  9. from rcsclient import openrcsclient
  10. def main():
  11. sys.stdout = sys.stderr
  12. try:
  13. opts, rest = getopt.getopt(sys.argv[1:], 'h:p:d:qvL')
  14. if not rest:
  15. cmd = 'head'
  16. else:
  17. cmd, rest = rest[0], rest[1:]
  18. if not commands.has_key(cmd):
  19. raise getopt.error, "unknown command"
  20. coptset, func = commands[cmd]
  21. copts, files = getopt.getopt(rest, coptset)
  22. except getopt.error, msg:
  23. print msg
  24. print "usage: rrcs [options] command [options] [file] ..."
  25. print "where command can be:"
  26. print " ci|put # checkin the given files"
  27. print " co|get # checkout"
  28. print " info # print header info"
  29. print " head # print revision of head branch"
  30. print " list # list filename if valid"
  31. print " log # print full log"
  32. print " diff # diff rcs file and work file"
  33. print "if no files are given, all remote rcs files are assumed"
  34. sys.exit(2)
  35. x = openrcsclient(opts)
  36. if not files:
  37. files = x.listfiles()
  38. for fn in files:
  39. try:
  40. func(x, copts, fn)
  41. except (IOError, os.error), msg:
  42. print "%s: %s" % (fn, msg)
  43. def checkin(x, copts, fn):
  44. f = open(fn)
  45. data = f.read()
  46. f.close()
  47. new = not x.isvalid(fn)
  48. if not new and same(x, copts, fn, data):
  49. print "%s: unchanged since last checkin" % fn
  50. return
  51. print "Checking in", fn, "..."
  52. message = asklogmessage(new)
  53. messages = x.put(fn, data, message)
  54. if messages:
  55. print messages
  56. def checkout(x, copts, fn):
  57. data = x.get(fn)
  58. f = open(fn, 'w')
  59. f.write(data)
  60. f.close()
  61. def lock(x, copts, fn):
  62. x.lock(fn)
  63. def unlock(x, copts, fn):
  64. x.unlock(fn)
  65. def info(x, copts, fn):
  66. dict = x.info(fn)
  67. keys = dict.keys()
  68. keys.sort()
  69. for key in keys:
  70. print key + ':', dict[key]
  71. print '='*70
  72. def head(x, copts, fn):
  73. head = x.head(fn)
  74. print fn, head
  75. def list(x, copts, fn):
  76. if x.isvalid(fn):
  77. print fn
  78. def log(x, copts, fn):
  79. flags = ''
  80. for o, a in copts:
  81. flags = flags + ' ' + o + a
  82. flags = flags[1:]
  83. messages = x.log(fn, flags)
  84. print messages
  85. def diff(x, copts, fn):
  86. if same(x, copts, fn):
  87. return
  88. flags = ''
  89. for o, a in copts:
  90. flags = flags + ' ' + o + a
  91. flags = flags[1:]
  92. data = x.get(fn)
  93. tf = tempfile.NamedTemporaryFile()
  94. tf.write(data)
  95. tf.flush()
  96. print 'diff %s -r%s %s' % (flags, x.head(fn), fn)
  97. sts = os.system('diff %s %s %s' % (flags, tf.name, fn))
  98. if sts:
  99. print '='*70
  100. def same(x, copts, fn, data = None):
  101. if data is None:
  102. f = open(fn)
  103. data = f.read()
  104. f.close()
  105. lsum = md5.new(data).digest()
  106. rsum = x.sum(fn)
  107. return lsum == rsum
  108. def asklogmessage(new):
  109. if new:
  110. print "enter description,",
  111. else:
  112. print "enter log message,",
  113. print "terminate with single '.' or end of file:"
  114. if new:
  115. print "NOTE: This is NOT the log message!"
  116. message = ""
  117. while 1:
  118. sys.stderr.write(">> ")
  119. sys.stderr.flush()
  120. line = sys.stdin.readline()
  121. if not line or line == '.\n': break
  122. message = message + line
  123. return message
  124. def remove(fn):
  125. try:
  126. os.unlink(fn)
  127. except os.error:
  128. pass
  129. commands = {
  130. 'ci': ('', checkin),
  131. 'put': ('', checkin),
  132. 'co': ('', checkout),
  133. 'get': ('', checkout),
  134. 'info': ('', info),
  135. 'head': ('', head),
  136. 'list': ('', list),
  137. 'lock': ('', lock),
  138. 'unlock': ('', unlock),
  139. 'log': ('bhLRtd:l:r:s:w:V:', log),
  140. 'diff': ('c', diff),
  141. }
  142. if __name__ == '__main__':
  143. main()