Skip to content

Commit e9b7047

Browse files
committed
Refactor projcode to mainly expose a list_code_files helper function which
optionally takes a list of languages to filter on or all by default. Make sure that there's at least a single blank line between license header and actual module content. Minor adjustments to fix style check warnings.
1 parent 50b5412 commit e9b7047

File tree

2 files changed

+62
-36
lines changed

2 files changed

+62
-36
lines changed

bin/addheader.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@
4747
print("Using mig installation in %s" % MIG_ROOT)
4848
sys.path.append(MIG_ROOT)
4949

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
50+
from mig.shared.fileio import read_file_lines, read_head_lines, write_file_lines
51+
from mig.shared.projcode import CODE_ROOT, JAVASCRIPT, list_code_files
5452

5553
# Modify these to fit actual project
5654
PROJ_CONSTS = {}
@@ -112,6 +110,7 @@ def check_header(path, var_dict, preamble_lines=100):
112110

113111
def add_header(path, var_dict, explicit_border=True, block_wrap=False):
114112
"""Add the required copyright and license header to module in path.
113+
115114
The optional explicit_border argument can be set to wrap the license
116115
text in begin and end lines that are easy to find, so that license can
117116
be updated or replaced later.
@@ -120,7 +119,6 @@ def add_header(path, var_dict, explicit_border=True, block_wrap=False):
120119
JavaScript where the per-line commenting using hash (#) won't work.
121120
Creates a '.unlicensed' backup copy of each file changed.
122121
"""
123-
124122
module_lines = read_file_lines(path, None)
125123
if not write_file_lines(module_lines, path + BACKUP_MARKER, None):
126124
print("Failed to create backup of %s - skip!" % path)
@@ -170,8 +168,11 @@ def add_header(path, var_dict, explicit_border=True, block_wrap=False):
170168
)
171169

172170
module_header.append(lic)
171+
# Make sure there's a blank line between license header and code
172+
if module_lines and module_lines[0].strip():
173+
module_header.append("\n")
173174

174-
updated_lines = [i % var_dict for i in module_header + [""] + module_lines]
175+
updated_lines = [i % var_dict for i in module_header + module_lines]
175176

176177
if not write_file_lines(updated_lines, path, None):
177178
print("Failed to write %s with added headers!" % path)
@@ -181,7 +182,7 @@ def add_header(path, var_dict, explicit_border=True, block_wrap=False):
181182

182183

183184
def main(argv):
184-
"""Run header addition for given argv"""
185+
"""Run header addition for given argv."""
185186
target = os.getcwd()
186187
if len(argv) > 1:
187188
target = os.path.abspath(argv[1])
@@ -202,10 +203,10 @@ def main(argv):
202203
if src_path.endswith(BACKUP_MARKER):
203204
continue
204205
print("Inspecting %s" % src_path)
205-
for pattern in py_code_files + sh_code_files + js_code_files:
206-
needs_block = pattern in js_code_files
206+
for pattern in list_code_files():
207+
needs_block = pattern in list_code_files(JAVASCRIPT)
207208
pattern = os.path.normpath(
208-
os.path.join(mig_code_base, code_root, pattern)
209+
os.path.join(mig_code_base, CODE_ROOT, pattern)
209210
)
210211

211212
# print("DEBUG: Testing %s against %s" % (src_path, pattern))

mig/shared/projcode.py

+51-26
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,42 @@
2121
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
2222
# 02110-1301, USA.
2323

24-
"""Helpers to transform or search project code files"""
24+
"""Helpers to transform or search project code files."""
25+
26+
from mig.shared.defaults import keyword_all
27+
28+
# TODO: phase out lowercase names once all scripts switched to get_code_files
2529

2630
# Top dir with all code
27-
code_root = 'mig'
31+
CODE_ROOT = code_root = 'mig'
2832

2933
# Ignore backup and dot files in wild card match
30-
plain = '[a-zA-Z0-9]*.py'
34+
PLAIN = '[a-zA-Z0-9]*.py'
3135
py_code_files = [
3236
# a few scripts are in parent dir of code_root
33-
'../%s' % plain,
34-
'../bin/%s' % plain,
35-
'../sbin/%s' % plain,
36-
'%s' % plain,
37-
'lib/%s' % plain,
38-
'cgi-bin/%s' % plain,
39-
'cgi-sid/%s' % plain,
40-
'install/%s' % plain,
41-
'migfs-fuse/%s' % plain,
42-
'resource/bin/%s' % plain,
43-
'resource/image-scripts/%s' % plain,
44-
'resource/keepalive-scripts/%s' % plain,
45-
'server/%s' % plain,
46-
'shared/%s' % plain,
47-
'shared/functionality/%s' % plain,
48-
'shared/distos/%s' % plain,
49-
'shared/gdp/%s' % plain,
50-
'shared/griddaemons/%s' % plain,
51-
'simulation/%s' % plain,
52-
'user/%s' % plain,
53-
'vm-proxy/%s' % plain,
54-
'webserver/%s' % plain,
55-
'wsgi-bin/%s' % plain,
37+
'../%s' % PLAIN,
38+
'../bin/%s' % PLAIN,
39+
'../sbin/%s' % PLAIN,
40+
'%s' % PLAIN,
41+
'lib/%s' % PLAIN,
42+
'cgi-bin/%s' % PLAIN,
43+
'cgi-sid/%s' % PLAIN,
44+
'install/%s' % PLAIN,
45+
'migfs-fuse/%s' % PLAIN,
46+
'resource/bin/%s' % PLAIN,
47+
'resource/image-scripts/%s' % PLAIN,
48+
'resource/keepalive-scripts/%s' % PLAIN,
49+
'server/%s' % PLAIN,
50+
'shared/%s' % PLAIN,
51+
'shared/functionality/%s' % PLAIN,
52+
'shared/distos/%s' % PLAIN,
53+
'shared/gdp/%s' % PLAIN,
54+
'shared/griddaemons/%s' % PLAIN,
55+
'simulation/%s' % PLAIN,
56+
'user/%s' % PLAIN,
57+
'vm-proxy/%s' % PLAIN,
58+
'webserver/%s' % PLAIN,
59+
'wsgi-bin/%s' % PLAIN,
5660
]
5761
py_code_files += ['cgi-sid/%s' % name for name in ['requestnewjob',
5862
'putrespgid']]
@@ -68,12 +72,16 @@
6872
'walk',
6973
'getrespgid',
7074
]]
75+
PY_CODE_FILES = py_code_files
76+
7177
sh_code_files = [
7278
'resource/frontend_script.sh',
7379
'resource/master_node_script.sh',
7480
'resource/leader_node_script.sh',
7581
'resource/dummy_node_script.sh',
7682
]
83+
SH_CODE_FILES = sh_code_files
84+
7785
js_code_files = [
7886
'images/js/jquery.accountform.js',
7987
'images/js/jquery.ajaxhelpers.js',
@@ -89,4 +97,21 @@
8997
'assets/js/V3/ui-global.js',
9098
'assets/js/V3/ui-extra.js',
9199
]
100+
JS_CODE_FILES = js_code_files
101+
92102
code_files = py_code_files + sh_code_files + js_code_files
103+
CODE_FILES = code_files
104+
105+
PYTHON, SHELL, JAVASCRIPT = "PYTHON", "SHELL", "JAVASCRIPT"
106+
LANG_MAP = {keyword_all: CODE_FILES, PYTHON: PY_CODE_FILES,
107+
JAVASCRIPT: JS_CODE_FILES, SHELL: SH_CODE_FILES}
108+
109+
110+
def list_code_files(code_langs=[keyword_all]):
111+
"""Get list of all code files."""
112+
match = []
113+
for lang in code_langs:
114+
if not lang in code_langs:
115+
print("Warning: no such code lang: %s" % lang)
116+
match += LANG_MAP.get(lang, [])
117+
return match

0 commit comments

Comments
 (0)