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

Python code coverage for Tools/scripts/untabify.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a"Replace tabs with spaces in argument files. Print names of changed files."
4n/a
5n/aimport os
6n/aimport sys
7n/aimport getopt
8n/aimport tokenize
9n/a
10n/adef main():
11n/a tabsize = 8
12n/a try:
13n/a opts, args = getopt.getopt(sys.argv[1:], "t:")
14n/a if not args:
15n/a raise getopt.error("At least one file argument required")
16n/a except getopt.error as msg:
17n/a print(msg)
18n/a print("usage:", sys.argv[0], "[-t tabwidth] file ...")
19n/a return
20n/a for optname, optvalue in opts:
21n/a if optname == '-t':
22n/a tabsize = int(optvalue)
23n/a
24n/a for filename in args:
25n/a process(filename, tabsize)
26n/a
27n/a
28n/adef process(filename, tabsize, verbose=True):
29n/a try:
30n/a with tokenize.open(filename) as f:
31n/a text = f.read()
32n/a encoding = f.encoding
33n/a except IOError as msg:
34n/a print("%r: I/O error: %s" % (filename, msg))
35n/a return
36n/a newtext = text.expandtabs(tabsize)
37n/a if newtext == text:
38n/a return
39n/a backup = filename + "~"
40n/a try:
41n/a os.unlink(backup)
42n/a except OSError:
43n/a pass
44n/a try:
45n/a os.rename(filename, backup)
46n/a except OSError:
47n/a pass
48n/a with open(filename, "w", encoding=encoding) as f:
49n/a f.write(newtext)
50n/a if verbose:
51n/a print(filename)
52n/a
53n/a
54n/aif __name__ == '__main__':
55n/a main()