|
1 |
| -#!/usr/bin/env python3 |
2 |
| - |
3 |
| -# This script checks and can optionally update Crane source files. |
4 |
| -# You should always run this script without the "-u" option |
5 |
| -# first to make sure there is a clean dry run of the files that should |
6 |
| -# be updated |
7 |
| -# This is based on a script of the same name in the MOOSE Framework: |
8 |
| -# https://github.com/idaholab/moose/blob/master/framework/scripts/fixup_headers.py |
9 |
| - |
10 |
| -import os, string, re, shutil |
11 |
| -from optparse import OptionParser |
12 |
| - |
13 |
| -global_dir_ignores = ['contrib', '.svn', '.git', 'zapdos', 'moose', 'squirrel'] |
14 |
| -global_file_ignores = ['moosedocs.py'] |
15 |
| - |
16 |
| -unified_header = """\ |
17 |
| -//* This file is part of Crane, an open-source |
18 |
| -//* application for plasma chemistry and thermochemistry |
19 |
| -//* https://github.com/lcpp-org/crane |
20 |
| -//* |
21 |
| -//* Crane is powered by the MOOSE Framework |
22 |
| -//* https://www.mooseframework.org |
23 |
| -//* |
24 |
| -//* Licensed under LGPL 2.1, please see LICENSE for details |
25 |
| -//* https://www.gnu.org/licenses/lgpl-2.1.html""" |
26 |
| - |
27 |
| -python_header = """\ |
28 |
| -#* This file is part of Crane, an open-source |
29 |
| -#* application for plasma chemistry and thermochemistry |
30 |
| -#* https://github.com/lcpp-org/crane |
31 |
| -#* |
32 |
| -#* Crane is powered by the MOOSE Framework |
33 |
| -#* https://www.mooseframework.org |
34 |
| -#* |
35 |
| -#* Licensed under LGPL 2.1, please see LICENSE for details |
36 |
| -#* https://www.gnu.org/licenses/lgpl-2.1.html""" |
37 |
| - |
38 |
| -global_options = {} |
39 |
| - |
40 |
| -def fixupHeader(): |
41 |
| - for dirpath, dirnames, filenames in os.walk(os.getcwd() + ""): |
42 |
| - |
43 |
| - # Don't traverse into ignored directories |
44 |
| - for ignore in global_dir_ignores: |
45 |
| - if ignore in dirnames: |
46 |
| - dirnames.remove(ignore) |
47 |
| - |
48 |
| - # Don't check ignored files |
49 |
| - for ignore in global_file_ignores: |
50 |
| - if ignore in filenames: |
51 |
| - filenames.remove(ignore) |
52 |
| - |
53 |
| - for file in filenames: |
54 |
| - suffix = os.path.splitext(file) |
55 |
| - if (suffix[-1] == '.C' or suffix[-1] == '.h') and not global_options.python_only: |
56 |
| - checkAndUpdateCPlusPlus(os.path.abspath(dirpath + '/' + file)) |
57 |
| - if suffix[-1] == '.py' and not global_options.cxx_only: |
58 |
| - checkAndUpdatePython(os.path.abspath(dirpath + '/' + file)) |
59 |
| - |
60 |
| -def checkAndUpdateCPlusPlus(filename): |
61 |
| - # Don't update soft links |
62 |
| - if os.path.islink(filename): |
63 |
| - return |
64 |
| - |
65 |
| - f = open(filename) |
66 |
| - text = f.read() |
67 |
| - f.close() |
68 |
| - |
69 |
| - header = unified_header |
70 |
| - |
71 |
| - # Check (exact match only) |
72 |
| - if (text.find(header) == -1 or global_options.force == True): |
73 |
| - # print the first 10 lines or so of the file |
74 |
| - if global_options.update == False: # Report only |
75 |
| - print(filename + ' does not contain an up to date header') |
76 |
| - if global_options.verbose == True: |
77 |
| - print('>'*40, '\n', '\n'.join((text.split('\n', 10))[:10]), '\n'*5) |
78 |
| - else: |
79 |
| - # Make sure any previous C-style header version is removed |
80 |
| - text = re.sub(r'^/\*+/$.*^/\*+/$', '', text, flags=re.S | re.M) |
81 |
| - |
82 |
| - # Make sure that any previous C++-style header (with extra character) |
83 |
| - # is also removed. |
84 |
| - text = re.sub(r'(?:^//\*.*\n)*', '', text, flags=re.M) |
85 |
| - |
86 |
| - # Now cleanup extra blank lines |
87 |
| - text = re.sub(r'\A(^\s*\n)', '', text) |
88 |
| - |
89 |
| - # Remove ifdefs in favor of pragmas |
90 |
| - suffix = os.path.splitext(filename) |
91 |
| - if suffix[-1] == '.h': |
92 |
| - text = re.sub(r'^#ifndef\s*\S+_H_?\s*\n#define.*\n', '', text, flags=re.M) |
93 |
| - text = re.sub(r'^#endif.*\n[\s]*\Z', '', text, flags=re.M) |
94 |
| - |
95 |
| - # Update |
96 |
| - f = open(filename + '~tmp', 'w') |
97 |
| - f.write(header + '\n\n') |
98 |
| - |
99 |
| - # Insert pragma once if not already present |
100 |
| - if suffix[-1] == '.h': |
101 |
| - if not re.search(r'#pragma once', text): |
102 |
| - f.write("#pragma once\n") |
103 |
| - |
104 |
| - f.write(text) |
105 |
| - f.close() |
106 |
| - os.rename(filename + '~tmp', filename) |
107 |
| - |
108 |
| -def checkAndUpdatePython(filename): |
109 |
| - f = open(filename) |
110 |
| - text = f.read() |
111 |
| - f.close() |
112 |
| - |
113 |
| - header = python_header |
114 |
| - |
115 |
| - # Check (exact match only) |
116 |
| - if (text.find(header) == -1): |
117 |
| - # print the first 10 lines or so of the file |
118 |
| - if global_options.update == False: # Report only |
119 |
| - print(filename + ' does not contain an up to date header') |
120 |
| - if global_options.verbose == True: |
121 |
| - print('>'*40, '\n', '\n'.join((text.split('\n', 10))[:10]), '\n'*5) |
122 |
| - else: |
123 |
| - # Save off the shebang line if it exists |
124 |
| - m = re.match(r'#!.*\n', text) |
125 |
| - shebang = '' |
126 |
| - if m: |
127 |
| - shebang = m.group(0) |
128 |
| - text = re.sub(r'^.*\n', '', text) |
129 |
| - |
130 |
| - # Save off any pytlint disable directives |
131 |
| - m = re.match(r'\A#pylint:\s+disable.*\n', text) |
132 |
| - pylint_disable = '' |
133 |
| - if m: |
134 |
| - pylint_disable = m.group(0) |
135 |
| - text = re.sub(r'^.*\n', '', text) |
136 |
| - |
137 |
| - pylint_enable = False |
138 |
| - if re.search(r'#pylint: enable=missing-docstring', text) != None: |
139 |
| - pylint_enable = True |
140 |
| - |
141 |
| - # Make sure any previous box-style header version is removed |
142 |
| - text = re.sub(r'\A(?:#.*#\n)*', '', text) |
143 |
| - |
144 |
| - # Make sure any previous version of the new header is removed |
145 |
| - text = re.sub(r'^#\*.*\n', '', text, flags=re.M) |
146 |
| - |
147 |
| - # Discard any pylint missing-docstring commands |
148 |
| - text = re.sub(r'\A#pylint:.*missing-docstring.*\n', '', text) |
149 |
| - |
150 |
| - # Now cleanup extra blank lines at the beginning of the file |
151 |
| - text = re.sub(r'\A(^\s*\n)', '', text) |
152 |
| - |
153 |
| - # Update |
154 |
| - f = open(filename + '~tmp', 'w') |
155 |
| - |
156 |
| - f.write(shebang) |
157 |
| - f.write(pylint_disable) |
158 |
| - f.write(header + '\n') |
159 |
| - if pylint_enable: |
160 |
| - f.write('#pylint: enable=missing-docstring\n') |
161 |
| - |
162 |
| - if len(text) != 0: |
163 |
| - f.write('\n' + text) |
164 |
| - |
165 |
| - f.close() |
166 |
| - |
167 |
| - shutil.copystat(filename, filename + '~tmp') |
168 |
| - os.rename(filename + '~tmp', filename) |
169 |
| - |
170 |
| -if __name__ == '__main__': |
171 |
| - parser = OptionParser() |
172 |
| - parser.add_option("-u", "--update", action="store_true", dest="update", default=False) |
173 |
| - parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False) |
174 |
| - parser.add_option("--python-only", action="store_true", dest="python_only", default=False) |
175 |
| - parser.add_option("--cxx-only", action="store_true", dest="cxx_only", default=False) |
176 |
| - parser.add_option("-f", "--force", action="store_true", dest="force", default=False) |
177 |
| - |
178 |
| - (global_options, args) = parser.parse_args() |
179 |
| - fixupHeader() |
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This script checks and can optionally update Crane source files. |
| 4 | +# You should always run this script without the "-u" option |
| 5 | +# first to make sure there is a clean dry run of the files that should |
| 6 | +# be updated |
| 7 | +# This is based on a script of the same name in the MOOSE Framework: |
| 8 | +# https://github.com/idaholab/moose/blob/master/framework/scripts/fixup_headers.py |
| 9 | + |
| 10 | +import os, string, re, shutil |
| 11 | +from optparse import OptionParser |
| 12 | + |
| 13 | +global_dir_ignores = ['contrib', '.svn', '.git', 'zapdos', 'moose', 'squirrel'] |
| 14 | +global_file_ignores = ['moosedocs.py'] |
| 15 | + |
| 16 | +unified_header = """\ |
| 17 | +//* This file is part of Crane, an open-source |
| 18 | +//* application for plasma chemistry and thermochemistry |
| 19 | +//* https://github.com/lcpp-org/crane |
| 20 | +//* |
| 21 | +//* Crane is powered by the MOOSE Framework |
| 22 | +//* https://www.mooseframework.org |
| 23 | +//* |
| 24 | +//* Licensed under LGPL 2.1, please see LICENSE for details |
| 25 | +//* https://www.gnu.org/licenses/lgpl-2.1.html""" |
| 26 | + |
| 27 | +python_header = """\ |
| 28 | +#* This file is part of Crane, an open-source |
| 29 | +#* application for plasma chemistry and thermochemistry |
| 30 | +#* https://github.com/lcpp-org/crane |
| 31 | +#* |
| 32 | +#* Crane is powered by the MOOSE Framework |
| 33 | +#* https://www.mooseframework.org |
| 34 | +#* |
| 35 | +#* Licensed under LGPL 2.1, please see LICENSE for details |
| 36 | +#* https://www.gnu.org/licenses/lgpl-2.1.html""" |
| 37 | + |
| 38 | +global_options = {} |
| 39 | + |
| 40 | +def fixupHeader(): |
| 41 | + for dirpath, dirnames, filenames in os.walk(os.getcwd() + ""): |
| 42 | + |
| 43 | + # Don't traverse into ignored directories |
| 44 | + for ignore in global_dir_ignores: |
| 45 | + if ignore in dirnames: |
| 46 | + dirnames.remove(ignore) |
| 47 | + |
| 48 | + # Don't check ignored files |
| 49 | + for ignore in global_file_ignores: |
| 50 | + if ignore in filenames: |
| 51 | + filenames.remove(ignore) |
| 52 | + |
| 53 | + for file in filenames: |
| 54 | + suffix = os.path.splitext(file) |
| 55 | + if (suffix[-1] == '.C' or suffix[-1] == '.h') and not global_options.python_only: |
| 56 | + checkAndUpdateCPlusPlus(os.path.abspath(dirpath + '/' + file)) |
| 57 | + if suffix[-1] == '.py' and not global_options.cxx_only: |
| 58 | + checkAndUpdatePython(os.path.abspath(dirpath + '/' + file)) |
| 59 | + |
| 60 | +def checkAndUpdateCPlusPlus(filename): |
| 61 | + # Don't update soft links |
| 62 | + if os.path.islink(filename): |
| 63 | + return |
| 64 | + |
| 65 | + f = open(filename) |
| 66 | + text = f.read() |
| 67 | + f.close() |
| 68 | + |
| 69 | + header = unified_header |
| 70 | + |
| 71 | + # Check (exact match only) |
| 72 | + if (text.find(header) == -1 or global_options.force == True): |
| 73 | + # print the first 10 lines or so of the file |
| 74 | + if global_options.update == False: # Report only |
| 75 | + print(filename + ' does not contain an up to date header') |
| 76 | + if global_options.verbose == True: |
| 77 | + print('>'*40, '\n', '\n'.join((text.split('\n', 10))[:10]), '\n'*5) |
| 78 | + else: |
| 79 | + # Make sure any previous C-style header version is removed |
| 80 | + text = re.sub(r'^/\*+/$.*^/\*+/$', '', text, flags=re.S | re.M) |
| 81 | + |
| 82 | + # Make sure that any previous C++-style header (with extra character) |
| 83 | + # is also removed. |
| 84 | + text = re.sub(r'(?:^//\*.*\n)*', '', text, flags=re.M) |
| 85 | + |
| 86 | + # Now cleanup extra blank lines |
| 87 | + text = re.sub(r'\A(^\s*\n)', '', text) |
| 88 | + |
| 89 | + # Remove ifdefs in favor of pragmas |
| 90 | + suffix = os.path.splitext(filename) |
| 91 | + if suffix[-1] == '.h': |
| 92 | + text = re.sub(r'^#ifndef\s*\S+_H_?\s*\n#define.*\n', '', text, flags=re.M) |
| 93 | + text = re.sub(r'^#endif.*\n[\s]*\Z', '', text, flags=re.M) |
| 94 | + |
| 95 | + # Update |
| 96 | + f = open(filename + '~tmp', 'w') |
| 97 | + f.write(header + '\n\n') |
| 98 | + |
| 99 | + # Insert pragma once if not already present |
| 100 | + if suffix[-1] == '.h': |
| 101 | + if not re.search(r'#pragma once', text): |
| 102 | + f.write("#pragma once\n") |
| 103 | + |
| 104 | + f.write(text) |
| 105 | + f.close() |
| 106 | + os.rename(filename + '~tmp', filename) |
| 107 | + |
| 108 | +def checkAndUpdatePython(filename): |
| 109 | + f = open(filename) |
| 110 | + text = f.read() |
| 111 | + f.close() |
| 112 | + |
| 113 | + header = python_header |
| 114 | + |
| 115 | + # Check (exact match only) |
| 116 | + if (text.find(header) == -1): |
| 117 | + # print the first 10 lines or so of the file |
| 118 | + if global_options.update == False: # Report only |
| 119 | + print(filename + ' does not contain an up to date header') |
| 120 | + if global_options.verbose == True: |
| 121 | + print('>'*40, '\n', '\n'.join((text.split('\n', 10))[:10]), '\n'*5) |
| 122 | + else: |
| 123 | + # Save off the shebang line if it exists |
| 124 | + m = re.match(r'#!.*\n', text) |
| 125 | + shebang = '' |
| 126 | + if m: |
| 127 | + shebang = m.group(0) |
| 128 | + text = re.sub(r'^.*\n', '', text) |
| 129 | + |
| 130 | + # Save off any pytlint disable directives |
| 131 | + m = re.match(r'\A#pylint:\s+disable.*\n', text) |
| 132 | + pylint_disable = '' |
| 133 | + if m: |
| 134 | + pylint_disable = m.group(0) |
| 135 | + text = re.sub(r'^.*\n', '', text) |
| 136 | + |
| 137 | + pylint_enable = False |
| 138 | + if re.search(r'#pylint: enable=missing-docstring', text) != None: |
| 139 | + pylint_enable = True |
| 140 | + |
| 141 | + # Make sure any previous box-style header version is removed |
| 142 | + text = re.sub(r'\A(?:#.*#\n)*', '', text) |
| 143 | + |
| 144 | + # Make sure any previous version of the new header is removed |
| 145 | + text = re.sub(r'^#\*.*\n', '', text, flags=re.M) |
| 146 | + |
| 147 | + # Discard any pylint missing-docstring commands |
| 148 | + text = re.sub(r'\A#pylint:.*missing-docstring.*\n', '', text) |
| 149 | + |
| 150 | + # Now cleanup extra blank lines at the beginning of the file |
| 151 | + text = re.sub(r'\A(^\s*\n)', '', text) |
| 152 | + |
| 153 | + # Update |
| 154 | + f = open(filename + '~tmp', 'w') |
| 155 | + |
| 156 | + f.write(shebang) |
| 157 | + f.write(pylint_disable) |
| 158 | + f.write(header + '\n') |
| 159 | + if pylint_enable: |
| 160 | + f.write('#pylint: enable=missing-docstring\n') |
| 161 | + |
| 162 | + if len(text) != 0: |
| 163 | + f.write('\n' + text) |
| 164 | + |
| 165 | + f.close() |
| 166 | + |
| 167 | + shutil.copystat(filename, filename + '~tmp') |
| 168 | + os.rename(filename + '~tmp', filename) |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + parser = OptionParser() |
| 172 | + parser.add_option("-u", "--update", action="store_true", dest="update", default=False) |
| 173 | + parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False) |
| 174 | + parser.add_option("--python-only", action="store_true", dest="python_only", default=False) |
| 175 | + parser.add_option("--cxx-only", action="store_true", dest="cxx_only", default=False) |
| 176 | + parser.add_option("-f", "--force", action="store_true", dest="force", default=False) |
| 177 | + |
| 178 | + (global_options, args) = parser.parse_args() |
| 179 | + fixupHeader() |
0 commit comments