|
1266
|
1 #! /usr/local/bin/python
|
|
274
|
2
|
|
|
3 # mkreal
|
|
|
4 #
|
|
|
5 # turn a symlink to a directory into a real directory
|
|
|
6
|
|
|
7 import sys
|
|
755
|
8 import os
|
|
274
|
9 from stat import *
|
|
|
10
|
|
755
|
11 join = os.path.join
|
|
274
|
12
|
|
|
13 error = 'mkreal error'
|
|
|
14
|
|
|
15 BUFSIZE = 32*1024
|
|
|
16
|
|
|
17 def mkrealfile(name):
|
|
755
|
18 st = os.stat(name) # Get the mode
|
|
274
|
19 mode = S_IMODE(st[ST_MODE])
|
|
755
|
20 linkto = os.readlink(name) # Make sure again it's a symlink
|
|
274
|
21 f_in = open(name, 'r') # This ensures it's a file
|
|
755
|
22 os.unlink(name)
|
|
274
|
23 f_out = open(name, 'w')
|
|
|
24 while 1:
|
|
|
25 buf = f_in.read(BUFSIZE)
|
|
|
26 if not buf: break
|
|
|
27 f_out.write(buf)
|
|
|
28 del f_out # Flush data to disk before changing mode
|
|
755
|
29 os.chmod(name, mode)
|
|
274
|
30
|
|
|
31 def mkrealdir(name):
|
|
755
|
32 st = os.stat(name) # Get the mode
|
|
274
|
33 mode = S_IMODE(st[ST_MODE])
|
|
755
|
34 linkto = os.readlink(name)
|
|
|
35 files = os.listdir(name)
|
|
|
36 os.unlink(name)
|
|
|
37 os.mkdir(name, mode)
|
|
|
38 os.chmod(name, mode)
|
|
|
39 linkto = join(os.pardir, linkto)
|
|
274
|
40 #
|
|
|
41 for file in files:
|
|
755
|
42 if file not in (os.curdir, os.pardir):
|
|
|
43 os.symlink(join(linkto, file), join(name, file))
|
|
274
|
44
|
|
|
45 def main():
|
|
|
46 sys.stdout = sys.stderr
|
|
755
|
47 progname = os.path.basename(sys.argv[0])
|
|
1266
|
48 if progname == '-c': progname = 'mkreal'
|
|
274
|
49 args = sys.argv[1:]
|
|
|
50 if not args:
|
|
|
51 print 'usage:', progname, 'path ...'
|
|
|
52 sys.exit(2)
|
|
|
53 status = 0
|
|
|
54 for name in args:
|
|
755
|
55 if not os.path.islink(name):
|
|
274
|
56 print progname+':', name+':', 'not a symlink'
|
|
|
57 status = 1
|
|
|
58 else:
|
|
755
|
59 if os.path.isdir(name):
|
|
274
|
60 mkrealdir(name)
|
|
|
61 else:
|
|
|
62 mkrealfile(name)
|
|
|
63 sys.exit(status)
|
|
|
64
|
|
|
65 main()
|