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