Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 1 | #! /usr/local/python |
| 2 | |
| 3 | # mkreal |
| 4 | # |
| 5 | # turn a symlink to a directory into a real directory |
| 6 | |
| 7 | import sys |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 8 | import os |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 9 | from stat import * |
| 10 | |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 11 | join = os.path.join |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 12 | |
| 13 | error = 'mkreal error' |
| 14 | |
| 15 | BUFSIZE = 32*1024 |
| 16 | |
| 17 | def mkrealfile(name): |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 18 | st = os.stat(name) # Get the mode |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 19 | mode = S_IMODE(st[ST_MODE]) |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 20 | linkto = os.readlink(name) # Make sure again it's a symlink |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 21 | f_in = open(name, 'r') # This ensures it's a file |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 22 | os.unlink(name) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 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 |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 29 | os.chmod(name, mode) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 30 | |
| 31 | def mkrealdir(name): |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 32 | st = os.stat(name) # Get the mode |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 33 | mode = S_IMODE(st[ST_MODE]) |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 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) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 40 | # |
| 41 | for file in files: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 42 | if file not in (os.curdir, os.pardir): |
| 43 | os.symlink(join(linkto, file), join(name, file)) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 44 | |
| 45 | def main(): |
| 46 | sys.stdout = sys.stderr |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 47 | progname = os.path.basename(sys.argv[0]) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 48 | args = sys.argv[1:] |
| 49 | if not args: |
| 50 | print 'usage:', progname, 'path ...' |
| 51 | sys.exit(2) |
| 52 | status = 0 |
| 53 | for name in args: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 54 | if not os.path.islink(name): |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 55 | print progname+':', name+':', 'not a symlink' |
| 56 | status = 1 |
| 57 | else: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 58 | if os.path.isdir(name): |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 59 | mkrealdir(name) |
| 60 | else: |
| 61 | mkrealfile(name) |
| 62 | sys.exit(status) |
| 63 | |
| 64 | main() |