in scripts/license-header.py [0:0]
def main():
fail = False
log_to = None
args = parse_args()
if args.verbose:
log_to = sys.stderr
if args.check:
log_to = None
if args.verbose:
log_to = sys.stdout
header_text = file_lines(args.header)
if len(args.files) == 1 and args.files[0] == "-":
files = [file.strip() for file in sys.stdin.readlines()]
else:
files = args.files
for filepath in files:
filename = get_filename(filepath)
matched = file_pattern.match(filename)
if not matched:
message(log_to, "Skip : " + filepath)
continue
content = file_read(filepath)
wrap = get_wrapper(filename)
header_comment = wrap.wrapper(header_text, args)
start = 0
end = 0
# Look for an exact substr match
#
found = content.find(header_comment, 0, len(header_comment) + args.extra)
if found >= 0:
if not args.remove:
message(log_to, "OK : " + filepath)
continue
start = found
end = found + len(header_comment)
else:
# Look for a fuzzy match in the first 60 chars
#
found = regex.search(
"(?be)(%s){e<=%d}" % (regex.escape(header_comment[0:60]), 6),
content[0 : 80 + args.extra],
)
if found:
fuzzy = regex.compile(
"(?be)(%s){e<=%d}" % (regex.escape(header_comment), args.editdist)
)
# If the first 80 chars match - try harder for the rest of the header
#
found = fuzzy.search(
content[0 : len(header_comment) + args.extra], found.start()
)
if found:
start = found.start()
end = found.end()
if args.remove:
if start == 0 and end == 0:
if not args.inplace:
print(content, end="")
message(log_to, "OK : " + filepath)
continue
# If removing the header text, zero it out there.
header_comment = ""
message(log_to, "Fix : " + filepath)
if args.check:
fail = True
continue
# Remove any partially matching header
#
content = content[0:start] + content[end:]
if wrap.hashbang:
search = regex.search("^#!.*\n", content)
if search:
content = (
content[search.start() : search.end()]
+ header_comment
+ content[search.end() :]
)
else:
content = header_comment + content
else:
content = header_comment + content
if args.inplace:
with open(filepath, "w") as file:
print(content, file=file, end="")
else:
print(content, end="")
if fail:
return 1
return 0