Skip to content

Commit bfc85c6

Browse files
committed
Reformat with black and fix import order with isort. Rewrapped a bit and
changed strings to use double quotes. Applied `isort` last to revert from the `black` import mangling and preserve our usual hanging indent imports.
1 parent 9034f08 commit bfc85c6

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

bin/addheader.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828

2929
"""Search code tree and add the required header to all python modules."""
3030

31-
from __future__ import print_function
32-
from __future__ import absolute_import
31+
from __future__ import absolute_import, print_function
3332

3433
import datetime
3534
import fnmatch
@@ -48,21 +47,22 @@
4847
print("Using mig installation in %s" % MIG_ROOT)
4948
sys.path.append(MIG_ROOT)
5049

51-
from mig.shared.fileio import read_head_lines, read_file_lines, write_file_lines
52-
from mig.shared.projcode import code_root, py_code_files, sh_code_files, \
53-
js_code_files
50+
from mig.shared.fileio import read_file_lines, read_head_lines, \
51+
write_file_lines
52+
from mig.shared.projcode import code_root, js_code_files, py_code_files, \
53+
sh_code_files
5454

5555
# Modify these to fit actual project
5656
PROJ_CONSTS = {}
57-
PROJ_CONSTS['project_name'] = "MiG"
58-
PROJ_CONSTS['authors'] = 'The MiG Project by the Science HPC Center at UCPH'
57+
PROJ_CONSTS["project_name"] = "MiG"
58+
PROJ_CONSTS["authors"] = "The MiG Project by the Science HPC Center at UCPH"
5959

60-
PROJ_CONSTS['copyright_year'] = '2003-%d' % datetime.date.today().year
60+
PROJ_CONSTS["copyright_year"] = "2003-%d" % datetime.date.today().year
6161

6262
# Set interpreter path and file encoding if not already set in source files
6363
# Use empty string to leave them alone.
64-
PROJ_CONSTS['interpreter_path'] = '/usr/bin/env python'
65-
PROJ_CONSTS['module_encoding'] = 'utf-8'
64+
PROJ_CONSTS["interpreter_path"] = "/usr/bin/env python"
65+
PROJ_CONSTS["module_encoding"] = "utf-8"
6666

6767
BEGIN_MARKER, END_MARKER = "--- BEGIN_HEADER ---", "--- END_HEADER ---"
6868
BACKUP_MARKER = ".unlicensed"
@@ -103,9 +103,11 @@ def check_header(path, var_dict, preamble_lines=100):
103103
"""Check if path already has a credible license header. Only looks inside
104104
the first preamble_size bytes of the file.
105105
"""
106-
module_preamble = '\n'.join(read_head_lines(path, preamble_lines, None))
107-
return (BEGIN_MARKER in module_preamble or
108-
var_dict['authors'] in module_preamble)
106+
module_preamble = "\n".join(read_head_lines(path, preamble_lines, None))
107+
return (
108+
BEGIN_MARKER in module_preamble
109+
or var_dict["authors"] in module_preamble
110+
)
109111

110112

111113
def add_header(path, var_dict, explicit_border=True, block_wrap=False):
@@ -124,23 +126,23 @@ def add_header(path, var_dict, explicit_border=True, block_wrap=False):
124126
print("Failed to create backup of %s - skip!" % path)
125127
return False
126128
# Do not truncate any existing unix executable hint (shebang) and encoding
127-
act = '#!%(interpreter_path)s\n' % var_dict
129+
act = "#!%(interpreter_path)s\n" % var_dict
128130
if block_wrap:
129-
enc = ''
131+
enc = ""
130132
else:
131-
enc = '# -*- coding: %(module_encoding)s -*-' % var_dict
133+
enc = "# -*- coding: %(module_encoding)s -*-" % var_dict
132134
lic = LICENSE_TEXT % var_dict
133135
module_header = []
134136
if module_lines and module_lines[0].startswith("#!"):
135137
module_header.append(module_lines[0])
136138
module_lines = module_lines[1:]
137-
elif var_dict['interpreter_path']:
139+
elif var_dict["interpreter_path"]:
138140
module_header.append(act)
139141

140142
if module_lines and module_lines[0].startswith("# -*- coding"):
141143
module_header.append(module_lines[0])
142144
module_lines = module_lines[1:]
143-
elif var_dict['module_encoding']:
145+
elif var_dict["module_encoding"]:
144146
module_header.append(enc)
145147

146148
if explicit_border:
@@ -152,22 +154,29 @@ def add_header(path, var_dict, explicit_border=True, block_wrap=False):
152154
#
153155
# %s
154156
#
155-
""" % (BEGIN_MARKER, lic, END_MARKER)
157+
""" % (
158+
BEGIN_MARKER,
159+
lic,
160+
END_MARKER,
161+
)
156162
if block_wrap:
157-
lic = """
163+
lic = (
164+
"""
158165
/*
159166
%s
160167
*/
161-
""" % lic
168+
"""
169+
% lic
170+
)
162171

163172
module_header.append(lic)
164173

165-
updated_lines = [i % var_dict for i in module_header + [''] + module_lines]
174+
updated_lines = [i % var_dict for i in module_header + [""] + module_lines]
166175

167176
if not write_file_lines(updated_lines, path, None):
168177
print("Failed to write %s with added headers!" % path)
169178
return False
170-
#print("DEBUG: wrote %s with added headers!" % path)
179+
# print("DEBUG: wrote %s with added headers!" % path)
171180
return True
172181

173182

@@ -180,31 +189,32 @@ def main(argv):
180189
if len(argv) > 2:
181190
mig_code_base = os.path.abspath(argv[2])
182191

183-
for (root, _, files) in os.walk(target):
192+
for root, _, files in os.walk(target):
184193

185194
# skip all dot dirs - they are from repos etc and _not_ jobs
186195

187-
if root.find(os.sep + '.') != -1:
196+
if root.find(os.sep + ".") != -1:
188197
continue
189198
for name in files:
190199
src_path = os.path.join(root, name)
191200
if os.path.islink(src_path):
192201
continue
193202
if src_path.endswith(BACKUP_MARKER):
194203
continue
195-
print('Inspecting %s' % src_path)
204+
print("Inspecting %s" % src_path)
196205
for pattern in py_code_files + sh_code_files + js_code_files:
197-
needs_block = (pattern in js_code_files)
198-
pattern = os.path.normpath(os.path.join(
199-
mig_code_base, code_root, pattern))
206+
needs_block = pattern in js_code_files
207+
pattern = os.path.normpath(
208+
os.path.join(mig_code_base, code_root, pattern)
209+
)
200210

201-
#print("DEBUG: Testing %s against %s" % (src_path, pattern))
211+
# print("DEBUG: Testing %s against %s" % (src_path, pattern))
202212

203213
if src_path == pattern or fnmatch.fnmatch(src_path, pattern):
204-
print('Matched %s against %s' % (src_path, pattern))
205-
PROJ_CONSTS['module_name'] = name.replace('.py', '')
214+
print("Matched %s against %s" % (src_path, pattern))
215+
PROJ_CONSTS["module_name"] = name.replace(".py", "")
206216
if check_header(src_path, PROJ_CONSTS):
207-
print('Skip %s with existing header' % src_path)
217+
print("Skip %s with existing header" % src_path)
208218
continue
209219
add_header(src_path, PROJ_CONSTS, block_wrap=needs_block)
210220
# else:
@@ -218,5 +228,5 @@ def main(argv):
218228
print("if using the default GPL v2 license here.")
219229

220230

221-
if __name__ == '__main__':
231+
if __name__ == "__main__":
222232
main(sys.argv)

0 commit comments

Comments
 (0)