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