blob: fb9e9a58b45394a1828d28710f594162f81a3619 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumec758ea1991-06-04 20:36:54 +00002
3# mkreal
4#
5# turn a symlink to a directory into a real directory
6
7import sys
Guido van Rossume7b88e71992-03-30 11:13:59 +00008import os
Guido van Rossumec758ea1991-06-04 20:36:54 +00009from stat import *
10
Guido van Rossume7b88e71992-03-30 11:13:59 +000011join = os.path.join
Guido van Rossumec758ea1991-06-04 20:36:54 +000012
13error = 'mkreal error'
14
15BUFSIZE = 32*1024
16
17def mkrealfile(name):
Tim Peters70c43782001-01-17 08:48:39 +000018 st = os.stat(name) # Get the mode
19 mode = S_IMODE(st[ST_MODE])
20 linkto = os.readlink(name) # Make sure again it's a symlink
21 f_in = open(name, 'r') # This ensures it's a file
22 os.unlink(name)
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
29 os.chmod(name, mode)
Guido van Rossumec758ea1991-06-04 20:36:54 +000030
31def mkrealdir(name):
Tim Peters70c43782001-01-17 08:48:39 +000032 st = os.stat(name) # Get the mode
33 mode = S_IMODE(st[ST_MODE])
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)
40 #
Andrew M. Kuchlingac6df952003-05-13 18:14:25 +000041 for filename in files:
42 if filename not in (os.curdir, os.pardir):
43 os.symlink(join(linkto, filename), join(name, filename))
Guido van Rossumec758ea1991-06-04 20:36:54 +000044
45def main():
Tim Peters70c43782001-01-17 08:48:39 +000046 sys.stdout = sys.stderr
47 progname = os.path.basename(sys.argv[0])
48 if progname == '-c': progname = 'mkreal'
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:
55 if not os.path.islink(name):
56 print progname+':', name+':', 'not a symlink'
57 status = 1
58 else:
59 if os.path.isdir(name):
60 mkrealdir(name)
61 else:
62 mkrealfile(name)
63 sys.exit(status)
Guido van Rossumec758ea1991-06-04 20:36:54 +000064
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000065if __name__ == '__main__':
66 main()