5 # Make a copy of a directory tree with symbolic links to all files in the
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.
11 # usage: mklinks oldtree newtree
15 LINK = '.LINK' # Name of special symlink at the top.
20 if not 3 <= len(sys.argv) <= 4:
21 print 'usage:', sys.argv[0], 'oldtree newtree [linkto]'
23 oldtree, newtree = sys.argv[1], sys.argv[2]
30 if not os.path.isdir(oldtree):
31 print oldtree + ': not a directory'
34 os.mkdir(newtree, 0777)
36 print newtree + ': cannot mkdir:', msg
38 linkname = os.path.join(newtree, link)
40 os.symlink(os.path.join(os.pardir, oldtree), linkname)
43 print linkname + ': cannot symlink:', msg
46 print linkname + ': warning: cannot symlink:', msg
47 linknames(oldtree, newtree, link)
50 def linknames(old, new, link):
51 if debug: print 'linknames', (old, new, link)
53 names = os.listdir(old)
55 print old + ': warning: cannot listdir:', msg
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):
66 os.mkdir(newname, 0777)
70 ': warning: cannot mkdir:', msg
73 linkname = os.path.join(os.pardir,
75 linknames(oldname, newname, linkname)
77 os.symlink(linkname, newname)
79 if __name__ == '__main__':