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

Python code coverage for Tools/scripts/svneol.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/ar"""
4n/aSVN helper script.
5n/a
6n/aTry to set the svn:eol-style property to "native" on every .py, .txt, .c and
7n/a.h file in the directory tree rooted at the current directory.
8n/a
9n/aFiles with the svn:eol-style property already set (to anything) are skipped.
10n/a
11n/asvn will itself refuse to set this property on a file that's not under SVN
12n/acontrol, or that has a binary mime-type property set. This script inherits
13n/athat behavior, and passes on whatever warning message the failing "svn
14n/apropset" command produces.
15n/a
16n/aIn the Python project, it's safe to invoke this script from the root of
17n/aa checkout.
18n/a
19n/aNo output is produced for files that are ignored. For a file that gets
20n/asvn:eol-style set, output looks like:
21n/a
22n/a property 'svn:eol-style' set on 'Lib\ctypes\__init__.py'
23n/a
24n/aFor a file not under version control:
25n/a
26n/a svn: warning: 'patch-finalizer.txt' is not under version control
27n/a
28n/aand for a file with a binary mime-type property:
29n/a
30n/a svn: File 'Lib\test\test_pep263.py' has binary mime type property
31n/a"""
32n/a
33n/aimport re
34n/aimport os
35n/aimport sys
36n/aimport subprocess
37n/a
38n/a
39n/adef propfiles(root, fn):
40n/a default = os.path.join(root, ".svn", "props", fn + ".svn-work")
41n/a try:
42n/a format = int(open(os.path.join(root, ".svn", "format")).read().strip())
43n/a except IOError:
44n/a return []
45n/a if format in (8, 9):
46n/a # In version 8 and 9, committed props are stored in prop-base, local
47n/a # modifications in props
48n/a return [os.path.join(root, ".svn", "prop-base", fn + ".svn-base"),
49n/a os.path.join(root, ".svn", "props", fn + ".svn-work")]
50n/a raise ValueError("Unknown repository format")
51n/a
52n/a
53n/adef proplist(root, fn):
54n/a """Return a list of property names for file fn in directory root."""
55n/a result = []
56n/a for path in propfiles(root, fn):
57n/a try:
58n/a f = open(path)
59n/a except IOError:
60n/a # no properties file: not under version control,
61n/a # or no properties set
62n/a continue
63n/a while True:
64n/a # key-value pairs, of the form
65n/a # K <length>
66n/a # <keyname>NL
67n/a # V length
68n/a # <value>NL
69n/a # END
70n/a line = f.readline()
71n/a if line.startswith("END"):
72n/a break
73n/a assert line.startswith("K ")
74n/a L = int(line.split()[1])
75n/a key = f.read(L)
76n/a result.append(key)
77n/a f.readline()
78n/a line = f.readline()
79n/a assert line.startswith("V ")
80n/a L = int(line.split()[1])
81n/a value = f.read(L)
82n/a f.readline()
83n/a f.close()
84n/a return result
85n/a
86n/a
87n/adef set_eol_native(path):
88n/a cmd = 'svn propset svn:eol-style native "{}"'.format(path)
89n/a propset = subprocess.Popen(cmd, shell=True)
90n/a propset.wait()
91n/a
92n/a
93n/apossible_text_file = re.compile(r"\.([hc]|py|txt|sln|vcproj)$").search
94n/a
95n/a
96n/adef main():
97n/a for arg in sys.argv[1:] or [os.curdir]:
98n/a if os.path.isfile(arg):
99n/a root, fn = os.path.split(arg)
100n/a if 'svn:eol-style' not in proplist(root, fn):
101n/a set_eol_native(arg)
102n/a elif os.path.isdir(arg):
103n/a for root, dirs, files in os.walk(arg):
104n/a if '.svn' in dirs:
105n/a dirs.remove('.svn')
106n/a for fn in files:
107n/a if possible_text_file(fn):
108n/a if 'svn:eol-style' not in proplist(root, fn):
109n/a path = os.path.join(root, fn)
110n/a set_eol_native(path)
111n/a
112n/a
113n/aif __name__ == '__main__':
114n/a main()