28
28
29
29
"""Search code tree and add the required header to all python modules."""
30
30
31
- from __future__ import print_function
32
- from __future__ import absolute_import
31
+ from __future__ import absolute_import , print_function
33
32
34
33
import datetime
35
34
import fnmatch
48
47
print ("Using mig installation in %s" % MIG_ROOT )
49
48
sys .path .append (MIG_ROOT )
50
49
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
54
54
55
55
# Modify these to fit actual project
56
56
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"
59
59
60
- PROJ_CONSTS [' copyright_year' ] = ' 2003-%d' % datetime .date .today ().year
60
+ PROJ_CONSTS [" copyright_year" ] = " 2003-%d" % datetime .date .today ().year
61
61
62
62
# Set interpreter path and file encoding if not already set in source files
63
63
# 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"
66
66
67
67
BEGIN_MARKER , END_MARKER = "--- BEGIN_HEADER ---" , "--- END_HEADER ---"
68
68
BACKUP_MARKER = ".unlicensed"
@@ -103,9 +103,11 @@ def check_header(path, var_dict, preamble_lines=100):
103
103
"""Check if path already has a credible license header. Only looks inside
104
104
the first preamble_size bytes of the file.
105
105
"""
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
+ )
109
111
110
112
111
113
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):
124
126
print ("Failed to create backup of %s - skip!" % path )
125
127
return False
126
128
# 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
128
130
if block_wrap :
129
- enc = ''
131
+ enc = ""
130
132
else :
131
- enc = ' # -*- coding: %(module_encoding)s -*-' % var_dict
133
+ enc = " # -*- coding: %(module_encoding)s -*-" % var_dict
132
134
lic = LICENSE_TEXT % var_dict
133
135
module_header = []
134
136
if module_lines and module_lines [0 ].startswith ("#!" ):
135
137
module_header .append (module_lines [0 ])
136
138
module_lines = module_lines [1 :]
137
- elif var_dict [' interpreter_path' ]:
139
+ elif var_dict [" interpreter_path" ]:
138
140
module_header .append (act )
139
141
140
142
if module_lines and module_lines [0 ].startswith ("# -*- coding" ):
141
143
module_header .append (module_lines [0 ])
142
144
module_lines = module_lines [1 :]
143
- elif var_dict [' module_encoding' ]:
145
+ elif var_dict [" module_encoding" ]:
144
146
module_header .append (enc )
145
147
146
148
if explicit_border :
@@ -152,22 +154,29 @@ def add_header(path, var_dict, explicit_border=True, block_wrap=False):
152
154
#
153
155
# %s
154
156
#
155
- """ % (BEGIN_MARKER , lic , END_MARKER )
157
+ """ % (
158
+ BEGIN_MARKER ,
159
+ lic ,
160
+ END_MARKER ,
161
+ )
156
162
if block_wrap :
157
- lic = """
163
+ lic = (
164
+ """
158
165
/*
159
166
%s
160
167
*/
161
- """ % lic
168
+ """
169
+ % lic
170
+ )
162
171
163
172
module_header .append (lic )
164
173
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 ]
166
175
167
176
if not write_file_lines (updated_lines , path , None ):
168
177
print ("Failed to write %s with added headers!" % path )
169
178
return False
170
- #print("DEBUG: wrote %s with added headers!" % path)
179
+ # print("DEBUG: wrote %s with added headers!" % path)
171
180
return True
172
181
173
182
@@ -180,31 +189,32 @@ def main(argv):
180
189
if len (argv ) > 2 :
181
190
mig_code_base = os .path .abspath (argv [2 ])
182
191
183
- for ( root , _ , files ) in os .walk (target ):
192
+ for root , _ , files in os .walk (target ):
184
193
185
194
# skip all dot dirs - they are from repos etc and _not_ jobs
186
195
187
- if root .find (os .sep + '.' ) != - 1 :
196
+ if root .find (os .sep + "." ) != - 1 :
188
197
continue
189
198
for name in files :
190
199
src_path = os .path .join (root , name )
191
200
if os .path .islink (src_path ):
192
201
continue
193
202
if src_path .endswith (BACKUP_MARKER ):
194
203
continue
195
- print (' Inspecting %s' % src_path )
204
+ print (" Inspecting %s" % src_path )
196
205
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
+ )
200
210
201
- #print("DEBUG: Testing %s against %s" % (src_path, pattern))
211
+ # print("DEBUG: Testing %s against %s" % (src_path, pattern))
202
212
203
213
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" , "" )
206
216
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 )
208
218
continue
209
219
add_header (src_path , PROJ_CONSTS , block_wrap = needs_block )
210
220
# else:
@@ -218,5 +228,5 @@ def main(argv):
218
228
print ("if using the default GPL v2 license here." )
219
229
220
230
221
- if __name__ == ' __main__' :
231
+ if __name__ == " __main__" :
222
232
main (sys .argv )
0 commit comments