- Notifications
You must be signed in to change notification settings - Fork 31.8k
/
Copy pathlinktree.py
executable file
·80 lines (73 loc) · 2.37 KB
/
linktree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /usr/bin/env python
# linktree
#
# Make a copy of a directory tree with symbolic links to all files in the
# original tree.
# All symbolic links go to a special symbolic link at the top, so you
# can easily fix things if the original source tree moves.
# See also "mkreal".
#
# usage: mklinks oldtree newtree
importsys, os
LINK='.LINK'# Name of special symlink at the top.
debug=0
defmain():
ifnot3<=len(sys.argv) <=4:
print'usage:', sys.argv[0], 'oldtree newtree [linkto]'
return2
oldtree, newtree=sys.argv[1], sys.argv[2]
iflen(sys.argv) >3:
link=sys.argv[3]
link_may_fail=1
else:
link=LINK
link_may_fail=0
ifnotos.path.isdir(oldtree):
printoldtree+': not a directory'
return1
try:
os.mkdir(newtree, 0777)
exceptos.error, msg:
printnewtree+': cannot mkdir:', msg
return1
linkname=os.path.join(newtree, link)
try:
os.symlink(os.path.join(os.pardir, oldtree), linkname)
exceptos.error, msg:
ifnotlink_may_fail:
printlinkname+': cannot symlink:', msg
return1
else:
printlinkname+': warning: cannot symlink:', msg
linknames(oldtree, newtree, link)
return0
deflinknames(old, new, link):
ifdebug: print'linknames', (old, new, link)
try:
names=os.listdir(old)
exceptos.error, msg:
printold+': warning: cannot listdir:', msg
return
fornameinnames:
ifnamenotin (os.curdir, os.pardir):
oldname=os.path.join(old, name)
linkname=os.path.join(link, name)
newname=os.path.join(new, name)
ifdebug>1: printoldname, newname, linkname
ifos.path.isdir(oldname) and \
notos.path.islink(oldname):
try:
os.mkdir(newname, 0777)
ok=1
except:
printnewname+ \
': warning: cannot mkdir:', msg
ok=0
ifok:
linkname=os.path.join(os.pardir,
linkname)
linknames(oldname, newname, linkname)
else:
os.symlink(linkname, newname)
if__name__=='__main__':
sys.exit(main())