Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 2 | |
| 3 | # linktree |
| 4 | # |
| 5 | # Make a copy of a directory tree with symbolic links to all files in the |
| 6 | # original tree. |
| 7 | # All symbolic links go to a special symbolic link at the top, so you |
| 8 | # can easily fix things if the original source tree moves. |
| 9 | # See also "mkreal". |
| 10 | # |
| 11 | # usage: mklinks oldtree newtree |
| 12 | |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 13 | import sys, os |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 14 | |
| 15 | LINK = '.LINK' # Name of special symlink at the top. |
| 16 | |
| 17 | debug = 0 |
| 18 | |
| 19 | def main(): |
| 20 | if not 3 <= len(sys.argv) <= 4: |
| 21 | print 'usage:', sys.argv[0], 'oldtree newtree [linkto]' |
| 22 | return 2 |
| 23 | oldtree, newtree = sys.argv[1], sys.argv[2] |
| 24 | if len(sys.argv) > 3: |
| 25 | link = sys.argv[3] |
| 26 | link_may_fail = 1 |
| 27 | else: |
| 28 | link = LINK |
| 29 | link_may_fail = 0 |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 30 | if not os.path.isdir(oldtree): |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 31 | print oldtree + ': not a directory' |
| 32 | return 1 |
| 33 | try: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 34 | os.mkdir(newtree, 0777) |
| 35 | except os.error, msg: |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 36 | print newtree + ': cannot mkdir:', msg |
| 37 | return 1 |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 38 | linkname = os.path.join(newtree, link) |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 39 | try: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 40 | os.symlink(os.path.join(os.pardir, oldtree), linkname) |
| 41 | except os.error, msg: |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 42 | if not link_may_fail: |
| 43 | print linkname + ': cannot symlink:', msg |
| 44 | return 1 |
| 45 | else: |
| 46 | print linkname + ': warning: cannot symlink:', msg |
| 47 | linknames(oldtree, newtree, link) |
| 48 | return 0 |
| 49 | |
| 50 | def linknames(old, new, link): |
| 51 | if debug: print 'linknames', (old, new, link) |
| 52 | try: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 53 | names = os.listdir(old) |
| 54 | except os.error, msg: |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 55 | print old + ': warning: cannot listdir:', msg |
| 56 | return |
| 57 | for name in names: |
Guido van Rossum | ed5b3d8 | 1998-03-24 05:30:29 +0000 | [diff] [blame] | 58 | if name not in (os.curdir, os.pardir): |
| 59 | oldname = os.path.join(old, name) |
| 60 | linkname = os.path.join(link, name) |
| 61 | newname = os.path.join(new, name) |
| 62 | if debug > 1: print oldname, newname, linkname |
| 63 | if os.path.isdir(oldname) and \ |
| 64 | not os.path.islink(oldname): |
| 65 | try: |
| 66 | os.mkdir(newname, 0777) |
| 67 | ok = 1 |
| 68 | except: |
| 69 | print newname + \ |
| 70 | ': warning: cannot mkdir:', msg |
| 71 | ok = 0 |
| 72 | if ok: |
| 73 | linkname = os.path.join(os.pardir, |
| 74 | linkname) |
| 75 | linknames(oldname, newname, linkname) |
| 76 | else: |
| 77 | os.symlink(linkname, newname) |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 78 | |
| 79 | sys.exit(main()) |