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

Python code coverage for Tools/scripts/fixnotice.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a"""(Ostensibly) fix copyright notices in files.
4n/a
5n/aActually, this script will simply replace a block of text in a file from one
6n/astring to another. It will only do this once though, i.e. not globally
7n/athroughout the file. It writes a backup file and then does an os.rename()
8n/adance for atomicity.
9n/a
10n/aUsage: fixnotices.py [options] [filenames]
11n/aOptions:
12n/a -h / --help
13n/a Print this message and exit
14n/a
15n/a --oldnotice=file
16n/a Use the notice in the file as the old (to be replaced) string, instead
17n/a of the hard coded value in the script.
18n/a
19n/a --newnotice=file
20n/a Use the notice in the file as the new (replacement) string, instead of
21n/a the hard coded value in the script.
22n/a
23n/a --dry-run
24n/a Don't actually make the changes, but print out the list of files that
25n/a would change. When used with -v, a status will be printed for every
26n/a file.
27n/a
28n/a -v / --verbose
29n/a Print a message for every file looked at, indicating whether the file
30n/a is changed or not.
31n/a"""
32n/a
33n/aOLD_NOTICE = """/***********************************************************
34n/aCopyright (c) 2000, BeOpen.com.
35n/aCopyright (c) 1995-2000, Corporation for National Research Initiatives.
36n/aCopyright (c) 1990-1995, Stichting Mathematisch Centrum.
37n/aAll rights reserved.
38n/a
39n/aSee the file "Misc/COPYRIGHT" for information on usage and
40n/aredistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
41n/a******************************************************************/
42n/a"""
43n/aimport os
44n/aimport sys
45n/aimport getopt
46n/a
47n/aNEW_NOTICE = ""
48n/aDRYRUN = 0
49n/aVERBOSE = 0
50n/a
51n/a
52n/adef usage(code, msg=''):
53n/a print(__doc__ % globals())
54n/a if msg:
55n/a print(msg)
56n/a sys.exit(code)
57n/a
58n/a
59n/adef main():
60n/a global DRYRUN, OLD_NOTICE, NEW_NOTICE, VERBOSE
61n/a try:
62n/a opts, args = getopt.getopt(sys.argv[1:], 'hv',
63n/a ['help', 'oldnotice=', 'newnotice=',
64n/a 'dry-run', 'verbose'])
65n/a except getopt.error as msg:
66n/a usage(1, msg)
67n/a
68n/a for opt, arg in opts:
69n/a if opt in ('-h', '--help'):
70n/a usage(0)
71n/a elif opt in ('-v', '--verbose'):
72n/a VERBOSE = 1
73n/a elif opt == '--dry-run':
74n/a DRYRUN = 1
75n/a elif opt == '--oldnotice':
76n/a fp = open(arg)
77n/a OLD_NOTICE = fp.read()
78n/a fp.close()
79n/a elif opt == '--newnotice':
80n/a fp = open(arg)
81n/a NEW_NOTICE = fp.read()
82n/a fp.close()
83n/a
84n/a for arg in args:
85n/a process(arg)
86n/a
87n/a
88n/adef process(file):
89n/a f = open(file)
90n/a data = f.read()
91n/a f.close()
92n/a i = data.find(OLD_NOTICE)
93n/a if i < 0:
94n/a if VERBOSE:
95n/a print('no change:', file)
96n/a return
97n/a elif DRYRUN or VERBOSE:
98n/a print(' change:', file)
99n/a if DRYRUN:
100n/a # Don't actually change the file
101n/a return
102n/a data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):]
103n/a new = file + ".new"
104n/a backup = file + ".bak"
105n/a f = open(new, "w")
106n/a f.write(data)
107n/a f.close()
108n/a os.rename(file, backup)
109n/a os.rename(new, file)
110n/a
111n/a
112n/aif __name__ == '__main__':
113n/a main()