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

Python code coverage for Tools/scripts/fixheader.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a# Add some standard cpp magic to a header file
4n/a
5n/aimport sys
6n/a
7n/adef main():
8n/a args = sys.argv[1:]
9n/a for filename in args:
10n/a process(filename)
11n/a
12n/adef process(filename):
13n/a try:
14n/a f = open(filename, 'r')
15n/a except IOError as msg:
16n/a sys.stderr.write('%s: can\'t open: %s\n' % (filename, str(msg)))
17n/a return
18n/a data = f.read()
19n/a f.close()
20n/a if data[:2] != '/*':
21n/a sys.stderr.write('%s does not begin with C comment\n' % filename)
22n/a return
23n/a try:
24n/a f = open(filename, 'w')
25n/a except IOError as msg:
26n/a sys.stderr.write('%s: can\'t write: %s\n' % (filename, str(msg)))
27n/a return
28n/a sys.stderr.write('Processing %s ...\n' % filename)
29n/a magic = 'Py_'
30n/a for c in filename:
31n/a if ord(c)<=0x80 and c.isalnum():
32n/a magic = magic + c.upper()
33n/a else: magic = magic + '_'
34n/a sys.stdout = f
35n/a print('#ifndef', magic)
36n/a print('#define', magic)
37n/a print('#ifdef __cplusplus')
38n/a print('extern "C" {')
39n/a print('#endif')
40n/a print()
41n/a f.write(data)
42n/a print()
43n/a print('#ifdef __cplusplus')
44n/a print('}')
45n/a print('#endif')
46n/a print('#endif /*', '!'+magic, '*/')
47n/a
48n/aif __name__ == '__main__':
49n/a main()