check_copyright.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #-------------------------------------------------------------------------------------------------------
  2. # Copyright (C) Microsoft. All rights reserved.
  3. # Copyright (c) ChakraCore Project Contributors. All rights reserved.
  4. # Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  5. #-------------------------------------------------------------------------------------------------------
  6. # Python 2.7 and 3.x compatibility for easier testing regardless of python installation
  7. from __future__ import print_function
  8. import sys
  9. import os.path
  10. import re
  11. copyright_lines = [
  12. r'-------------------------------------------------------------------------------------------------------',
  13. r' Copyright \(C\) Microsoft( Corporation and contributors)?\. All rights reserved\.',
  14. r' Copyright \(c\)( 2022)? ChakraCore Project Contributors\. All rights reserved\.',
  15. r' Licensed under the MIT license\. See LICENSE\.txt file in the project root for full license information\.',
  16. r'.*' # the above should always be followed by at least one other line, so make sure that line is present
  17. ]
  18. regexes = []
  19. for line in copyright_lines:
  20. pattern = '^.{1,5}%s$' % line
  21. regexes.append(re.compile(pattern))
  22. if len(sys.argv) < 2:
  23. print("Requires passing a filename as an argument.")
  24. exit(1)
  25. file_name = sys.argv[1]
  26. if not os.path.isfile(file_name):
  27. print("File does not exist:", file_name, "(not necessarily an error)")
  28. exit(0)
  29. def report_incorrect(file_name, pairs):
  30. # found a problem so report the problem to the caller and exit
  31. print(file_name, "... does not contain a correct copyright notice.\n")
  32. # print the relevant lines to help the reader find the problem
  33. for (_, line) in pairs:
  34. print(" ", line, end="")
  35. print()
  36. linecount = 0
  37. pairs = []
  38. with open(file_name, 'r') as sourcefile:
  39. hashbang = sourcefile.readline()
  40. if not hashbang.startswith("#!"):
  41. sourcefile.seek(0)
  42. pairs += zip(regexes, sourcefile)
  43. for (regex, line) in pairs:
  44. linecount += 1
  45. line = line.rstrip()
  46. matches = regex.match(line)
  47. if not matches:
  48. report_incorrect(file_name, pairs)
  49. exit(1)
  50. if linecount == 0:
  51. # the file was empty (e.g. dummy.js) so no problem
  52. exit(0)
  53. elif linecount != len(regexes):
  54. report_incorrect(file_name, pairs)
  55. exit(1)