-
Notifications
You must be signed in to change notification settings - Fork 2
/
add_license_header.py
406 lines (356 loc) · 12.1 KB
/
add_license_header.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from __future__ import annotations
import argparse
import functools
import string
import subprocess
import sys
from datetime import date
from typing import NamedTuple
from typing import Sequence
from typing import TypedDict
from identify import identify
class BlockComment(NamedTuple):
start: str
middle: str
end: str
# NOTE: Must be minimal characters to create a comment. Leading and trailing
# spaces are allowed.
BLOCK_COMMENT = {
'bash': BlockComment('#', '#', '#'),
'c#': BlockComment('/*', ' *', ' */'),
'c': BlockComment('/*', ' *', ' */'),
'c++': BlockComment('/*', ' *', ' */'),
'css': BlockComment('/*', ' *', ' */'),
'gherkin': BlockComment('#', '#', '#'),
'go': BlockComment('/*', ' *', ' */'),
'groovy': BlockComment('/*', ' *', ' */'),
'html': BlockComment('<!--', '', '-->'),
'ini': BlockComment(';', ';', ';'),
'java': BlockComment('/*', ' *', ' */'),
'java-properties': BlockComment('#', '#', '#'),
'javascript': BlockComment('/*', ' *', ' */'),
'jsx': BlockComment('/*', ' *', ' */'),
'kotlin': BlockComment('/*', ' *', ' */'),
'lua': BlockComment('--[[', '', '--]]'),
'makefile': BlockComment('#', '#', '#'),
'markdown': BlockComment('<!--', '', '-->'),
'php': BlockComment('/*', ' *', ' */'),
'python': BlockComment('#', '#', '#'),
'ruby': BlockComment('#', '#', '#'),
'rust': BlockComment('/*', ' *', ' */'),
'scala': BlockComment('/*', ' *', ' */'),
'shell': BlockComment('#', '#', '#'),
'swift': BlockComment('/*', ' *', ' */'),
'terraform': BlockComment('/*', ' *', ' */'),
'toml': BlockComment('#', '#', '#'),
'ts': BlockComment('/*', ' *', ' */'),
'tsx': BlockComment('/*', ' *', ' */'),
'xml': BlockComment('<!--', '', '-->'),
'yaml': BlockComment('#', '#', '#'),
'zsh': BlockComment('#', '#', '#'),
}
ALH_HEADER = 'LICENSE HEADER MANAGED BY add-license-header'
@functools.lru_cache
def wrap_license_in_comments(
formatted_license: str,
comment: BlockComment,
is_managed: bool,
) -> list[str]:
header = formatted_license.splitlines()
if header[-1] == '':
header.pop()
for i in range(len(header)):
if header[i] == '':
header[i] = f'{comment.middle}\n'
elif comment.middle != '':
header[i] = f'{comment.middle} {header[i]}\n'
else:
header[i] += '\n'
if is_managed:
header.insert(0, f'{comment.middle}\n')
header.insert(0, f'{comment.start} {ALH_HEADER}\n')
else:
header.insert(0, f'{comment.start}\n')
header.append(f'{comment.end}\n')
return header
def get_create_year(filepath: str) -> int:
create_year = subprocess.run(
(
'git',
'log',
'--format=%ad',
'--date=format:%Y',
filepath,
),
capture_output=True,
encoding='UTF-8',
)
if create_year.returncode != 0 or not create_year.stdout:
# assuming this is a new file
return date.today().year
return int(create_year.stdout.splitlines()[-1])
class FormatLicenseFields(TypedDict):
author_name: str
create_year: int | None
edit_year: int
year_delimiter: str
def format_license(
filepath: str,
license_template: str,
single_year_if_same: bool,
license_fields: FormatLicenseFields,
) -> str:
author_name = license_fields['author_name']
create_year = license_fields['create_year']
edit_year = license_fields['edit_year']
year_delimiter = license_fields['year_delimiter']
if create_year is None:
create_year = get_create_year(filepath)
if single_year_if_same and create_year == edit_year:
return string.Template(license_template).safe_substitute({
'author_name': author_name,
'create_year': create_year,
'edit_year': '',
'year_delimiter': '',
})
return string.Template(license_template).safe_substitute({
'author_name': author_name,
'create_year': create_year,
'edit_year': edit_year,
'year_delimiter': year_delimiter,
})
def update_license_header(
contents: list[str],
comment: BlockComment,
wrapped_license: list[str],
) -> list[str]:
# NOTE: "Special Comments" are comments that must appear at the top of the
# file (i.e., before the license header). Moving these comments to after
# the license header would break some form of functionality.
special_comments = (
'#!', # shebang
'<?php', # php
)
comment_start = wrapped_license[0].strip()
header_start_index = 0
while (
header_start_index < len(contents) and (
any(
contents[header_start_index].startswith(sc)
for sc in special_comments
) or
not contents[header_start_index].startswith(comment_start)
)
):
header_start_index += 1
if header_start_index == len(contents):
# License header not in file, so add it
new_header_start_index = 0
while new_header_start_index < len(contents) and any(
contents[new_header_start_index].startswith(sc)
for sc in special_comments
):
new_header_start_index += 1
if new_header_start_index > 0:
new_contents = [
*contents[:new_header_start_index],
'\n',
*wrapped_license,
'\n',
*contents[new_header_start_index:],
]
else:
new_contents = wrapped_license + ['\n'] + contents
else:
# License header is in file, so update it
header_end_index = header_start_index + 1
if comment.middle == comment.end:
while (
header_end_index < len(contents) and
contents[header_end_index].startswith(comment.end)
):
header_end_index += 1
else:
commend_end = comment.end.strip()
while (
header_end_index < len(contents) and
not contents[header_end_index].rstrip().endswith(commend_end)
):
header_end_index += 1
header_end_index += 1
# NOTE: The list slicing done here is to preserve the original comment
# block beginning and ending lines. For example, Java's comment blocks
# start with '/*', however, '/**' is a popular starting line. This is
# to preserve this style choice.
new_contents = (
contents[:header_start_index + 1] +
wrapped_license[1:-1] +
contents[header_end_index - 1:]
)
return new_contents
class UnknownFileTypeException(Exception):
pass
class BinaryFileTypeException(Exception):
pass
def get_block_comment(filename: str) -> BlockComment:
identify_tags = identify.tags_from_path(filename)
if 'binary' in identify_tags:
raise BinaryFileTypeException(
f'cannot add license to binary file: {filename}',
)
for tag in identify_tags:
if tag in BLOCK_COMMENT:
return BLOCK_COMMENT[tag]
raise UnknownFileTypeException(f'unsupported file format: {filename}')
def add_license_header(
filepath: str,
license_template: str,
license_fields: FormatLicenseFields,
is_managed: bool,
single_year_if_same: bool,
exit_zero_if_unsupported: bool,
*,
dry_run: bool,
) -> int:
try:
block_comment = get_block_comment(filepath)
except UnknownFileTypeException:
print(f'unsupported file format: {filepath}', file=sys.stderr)
return 0 if exit_zero_if_unsupported else 1
except BinaryFileTypeException:
print(
f'cannot add license to a binary file: {filepath}',
file=sys.stderr,
)
return 0 if exit_zero_if_unsupported else 1
formatted_license = format_license(
filepath,
license_template,
single_year_if_same,
license_fields,
)
license_header = wrap_license_in_comments(
formatted_license,
block_comment,
is_managed,
)
with open(filepath) as f:
contents_text = f.readlines()
new_contents = update_license_header(
contents_text,
block_comment,
license_header,
)
if new_contents != contents_text:
print(f'updating license in {filepath}', file=sys.stderr)
if not dry_run:
with open(filepath, 'w') as f:
f.write(''.join(new_contents))
return 1
return 0
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description=(
'A tool for automatically adding your license as a header in your '
'source code.'
),
)
parser.add_argument(
'--author-name',
default='',
help='Value to replace $author_name. Defaults to empty string.',
)
parser.add_argument(
'--check',
action='store_true',
help=(
'Only checks if files are update to date. Will not apply any '
'changes.'
),
)
parser.add_argument(
'--create-year',
help=(
'Value to replace $create_year. Defaults to looking up creation '
'date in git history. If that fails, will be defaulted to the '
'current year.'
),
type=int,
)
parser.add_argument(
'--exit-zero',
action='store_true',
help='Always exit with a return code of 0.',
)
parser.add_argument(
'--exit-zero-if-unsupported',
action='store_true',
help='Exit with a return code of 0 if file type is unsupported.',
)
parser.add_argument(
'--edit-year',
default=date.today().year,
help='Value to replace $edit_year. Defaults to current year.',
type=int,
)
parser.add_argument(
'--single-year-if-same',
action='store_true',
help=(
'If the $create_year and $edit_year are the same, only the '
'$create_year will be inserted. The $edit_year and '
'$year_delimiter will be replaced with the empty string.'
),
)
parser.add_argument(
'--unmanaged',
action='store_true',
help=(
'When running in unmanaged mode, the "LICENSE HEADER MANAGED BY '
'add-license-header" will not be added to the license header. '
'Instead the first comment in the file will be considered to be '
'the license header (with the exception of shebangs). Running in '
'managed mode enables you to have the license located anywhere in '
'the file.'
),
)
parser.add_argument(
'--year-delimiter',
default=', ',
help='Value to replace $year_delimiter. Defaults to ", ".',
)
license_group = parser.add_mutually_exclusive_group(required=True)
license_group.add_argument(
'--license-file',
help='Path to license template file.',
)
license_group.add_argument('--license', help='License template.')
parser.add_argument('filenames', help='File paths to check.', nargs='*')
args = parser.parse_args(argv)
if args.license_file is not None:
with open(args.license_file) as f:
license_template = f.read()
else:
license_template = bytes(
args.license,
'utf-8',
).decode('unicode_escape')
return_code = 0
for filename in args.filenames:
return_code |= add_license_header(
filename,
license_template,
{
'author_name': args.author_name,
'create_year': args.create_year,
'edit_year': args.edit_year,
'year_delimiter': args.year_delimiter,
},
not args.unmanaged,
args.single_year_if_same,
args.exit_zero_if_unsupported,
dry_run=args.check,
)
return return_code if not args.exit_zero else 0
if __name__ == '__main__':
raise SystemExit(main())