/Demo/scripts/mkrcs.py

http://unladen-swallow.googlecode.com/ · Python · 61 lines · 41 code · 5 blank · 15 comment · 9 complexity · a3e90536a3fd4f06bad58dee78a59beb MD5 · raw file

  1. #! /usr/bin/env python
  2. # A rather specialized script to make sure that a symbolic link named
  3. # RCS exists pointing to a real RCS directory in a parallel tree
  4. # referenced as RCStree in an ancestor directory.
  5. # (I use this because I like my RCS files to reside on a physically
  6. # different machine).
  7. import os
  8. def main():
  9. rcstree = 'RCStree'
  10. rcs = 'RCS'
  11. if os.path.islink(rcs):
  12. print '%r is a symlink to %r' % (rcs, os.readlink(rcs))
  13. return
  14. if os.path.isdir(rcs):
  15. print '%r is an ordinary directory' % (rcs,)
  16. return
  17. if os.path.exists(rcs):
  18. print '%r is a file?!?!' % (rcs,)
  19. return
  20. #
  21. p = os.getcwd()
  22. up = ''
  23. down = ''
  24. # Invariants:
  25. # (1) join(p, down) is the current directory
  26. # (2) up is the same directory as p
  27. # Ergo:
  28. # (3) join(up, down) is the current directory
  29. #print 'p =', repr(p)
  30. while not os.path.isdir(os.path.join(p, rcstree)):
  31. head, tail = os.path.split(p)
  32. #print 'head = %r; tail = %r' % (head, tail)
  33. if not tail:
  34. print 'Sorry, no ancestor dir contains %r' % (rcstree,)
  35. return
  36. p = head
  37. up = os.path.join(os.pardir, up)
  38. down = os.path.join(tail, down)
  39. #print 'p = %r; up = %r; down = %r' % (p, up, down)
  40. there = os.path.join(up, rcstree)
  41. there = os.path.join(there, down)
  42. there = os.path.join(there, rcs)
  43. if os.path.isdir(there):
  44. print '%r already exists' % (there, )
  45. else:
  46. print 'making %r' % (there,)
  47. makedirs(there)
  48. print 'making symlink %r -> %r' % (rcs, there)
  49. os.symlink(there, rcs)
  50. def makedirs(p):
  51. if not os.path.isdir(p):
  52. head, tail = os.path.split(p)
  53. makedirs(head)
  54. os.mkdir(p, 0777)
  55. if __name__ == "__main__":
  56. main()