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

Python code coverage for Tools/scripts/linktree.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a# linktree
4n/a#
5n/a# Make a copy of a directory tree with symbolic links to all files in the
6n/a# original tree.
7n/a# All symbolic links go to a special symbolic link at the top, so you
8n/a# can easily fix things if the original source tree moves.
9n/a# See also "mkreal".
10n/a#
11n/a# usage: mklinks oldtree newtree
12n/a
13n/aimport sys, os
14n/a
15n/aLINK = '.LINK' # Name of special symlink at the top.
16n/a
17n/adebug = 0
18n/a
19n/adef main():
20n/a if not 3 <= len(sys.argv) <= 4:
21n/a print('usage:', sys.argv[0], 'oldtree newtree [linkto]')
22n/a return 2
23n/a oldtree, newtree = sys.argv[1], sys.argv[2]
24n/a if len(sys.argv) > 3:
25n/a link = sys.argv[3]
26n/a link_may_fail = 1
27n/a else:
28n/a link = LINK
29n/a link_may_fail = 0
30n/a if not os.path.isdir(oldtree):
31n/a print(oldtree + ': not a directory')
32n/a return 1
33n/a try:
34n/a os.mkdir(newtree, 0o777)
35n/a except OSError as msg:
36n/a print(newtree + ': cannot mkdir:', msg)
37n/a return 1
38n/a linkname = os.path.join(newtree, link)
39n/a try:
40n/a os.symlink(os.path.join(os.pardir, oldtree), linkname)
41n/a except OSError as msg:
42n/a if not link_may_fail:
43n/a print(linkname + ': cannot symlink:', msg)
44n/a return 1
45n/a else:
46n/a print(linkname + ': warning: cannot symlink:', msg)
47n/a linknames(oldtree, newtree, link)
48n/a return 0
49n/a
50n/adef linknames(old, new, link):
51n/a if debug: print('linknames', (old, new, link))
52n/a try:
53n/a names = os.listdir(old)
54n/a except OSError as msg:
55n/a print(old + ': warning: cannot listdir:', msg)
56n/a return
57n/a for name in names:
58n/a if name not in (os.curdir, os.pardir):
59n/a oldname = os.path.join(old, name)
60n/a linkname = os.path.join(link, name)
61n/a newname = os.path.join(new, name)
62n/a if debug > 1: print(oldname, newname, linkname)
63n/a if os.path.isdir(oldname) and \
64n/a not os.path.islink(oldname):
65n/a try:
66n/a os.mkdir(newname, 0o777)
67n/a ok = 1
68n/a except:
69n/a print(newname + \
70n/a ': warning: cannot mkdir:', msg)
71n/a ok = 0
72n/a if ok:
73n/a linkname = os.path.join(os.pardir,
74n/a linkname)
75n/a linknames(oldname, newname, linkname)
76n/a else:
77n/a os.symlink(linkname, newname)
78n/a
79n/aif __name__ == '__main__':
80n/a sys.exit(main())