ยปCore Development>Code coverage>Tools/scripts/mkreal.py

Python code coverage for Tools/scripts/mkreal.py

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