Correctly handle shebangs (#29315)

pull/29342/head
Richard Belleville 3 years ago committed by GitHub
parent f3897a5f7a
commit 311648e532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      tools/distrib/check_copyright.py

@ -200,6 +200,18 @@ def log(cond, why, filename):
print(filename) print(filename)
def write_copyright(license_text, file_text, filename):
shebang = ""
lines = file_text.split("\n")
if lines and lines[0].startswith("#!"):
shebang = lines[0] + "\n"
file_text = file_text[len(shebang):]
rewritten_text = shebang + license_text + "\n" + file_text
with open(filename, 'w') as f:
f.write(rewritten_text)
# scan files, validate the text # scan files, validate the text
ok = True ok = True
filename_list = [] filename_list = []
@ -236,8 +248,7 @@ for filename in filename_list:
pass pass
elif 'DO NOT EDIT' not in text: elif 'DO NOT EDIT' not in text:
if args.fix: if args.fix:
text = license_text + '\n' + text write_copyright(license_text, text, filename)
open(filename, 'w').write(text)
log(1, 'copyright missing (fixed)', filename) log(1, 'copyright missing (fixed)', filename)
else: else:
log(1, 'copyright missing', filename) log(1, 'copyright missing', filename)

Loading…
Cancel
Save