blob: 5a665075babe516e81126afd7d631c8aa2fae6f4 [file] [log] [blame]
Guido van Rossum2ba9f301992-03-02 16:20:32 +00001#! /usr/local/python
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
13import sys, posix, path
14
15LINK = '.LINK' # Name of special symlink at the top.
16
17debug = 0
18
19def 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
30 if not path.isdir(oldtree):
31 print oldtree + ': not a directory'
32 return 1
33 try:
34 posix.mkdir(newtree, 0777)
35 except posix.error, msg:
36 print newtree + ': cannot mkdir:', msg
37 return 1
38 linkname = path.cat(newtree, link)
39 try:
40 posix.symlink(path.cat('..', oldtree), linkname)
41 except posix.error, msg:
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
50def linknames(old, new, link):
51 if debug: print 'linknames', (old, new, link)
52 try:
53 names = posix.listdir(old)
54 except posix.error, msg:
55 print old + ': warning: cannot listdir:', msg
56 return
57 for name in names:
58 if name not in ('.', '..'):
59 oldname = path.cat(old, name)
60 linkname = path.cat(link, name)
61 newname = path.cat(new, name)
62 if debug > 1: print oldname, newname, linkname
63 if path.isdir(oldname) and not path.islink(oldname):
64 try:
65 posix.mkdir(newname, 0777)
66 ok = 1
67 except:
68 print newname + ': warning: cannot mkdir:', msg
69 ok = 0
70 if ok:
71 linkname = path.cat('..', linkname)
72 linknames(oldname, newname, linkname)
73 else:
74 posix.symlink(linkname, newname)
75
76sys.exit(main())