summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/mkreal.py
blob: 3fd4b03a8af56639ff7b1015e78dac0beea33c5f (plain)
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
#! /usr/local/python

# mkreal
#
# turn a symlink to a directory into a real directory

import sys
import posix
import path
from stat import *

cat = path.cat

error = 'mkreal error'

BUFSIZE = 32*1024

def mkrealfile(name):
	st = posix.stat(name) # Get the mode
	mode = S_IMODE(st[ST_MODE])
	linkto = posix.readlink(name) # Make sure again it's a symlink
	f_in = open(name, 'r') # This ensures it's a file
	posix.unlink(name)
	f_out = open(name, 'w')
	while 1:
		buf = f_in.read(BUFSIZE)
		if not buf: break
		f_out.write(buf)
	del f_out # Flush data to disk before changing mode
	posix.chmod(name, mode)

def mkrealdir(name):
	st = posix.stat(name) # Get the mode
	mode = S_IMODE(st[ST_MODE])
	linkto = posix.readlink(name)
	files = posix.listdir(name)
	posix.unlink(name)
	posix.mkdir(name, mode)
	posix.chmod(name, mode)
	linkto = cat('..', linkto)
	#
	for file in files:
		if file not in ('.', '..'):
			posix.symlink(cat(linkto, file), cat(name, file))

def main():
	sys.stdout = sys.stderr
	progname = path.basename(sys.argv[0])
	args = sys.argv[1:]
	if not args:
		print 'usage:', progname, 'path ...'
		sys.exit(2)
	status = 0
	for name in args:
		if not path.islink(name):
			print progname+':', name+':', 'not a symlink'
			status = 1
		else:
			if path.isdir(name):
				mkrealdir(name)
			else:
				mkrealfile(name)
	sys.exit(status)

main()