check_copyright.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. pal_copyright_lines = [
  19. r'-------------------------------------------------------------------------------------------------------',
  20. r' ChakraCore/Pal',
  21. r' Contains portions \(c\) copyright Microsoft, portions copyright \(c\) the \.NET Foundation and Contributors',
  22. r' and edits \(c\) copyright the ChakraCore Contributors\.',
  23. r' See THIRD-PARTY-NOTICES\.txt in the project root for \.NET Foundation license',
  24. r' Licensed under the MIT license\. See LICENSE\.txt file in the project root for full license information\.',
  25. r'.*' # the above should always be followed by at least one other line, so make sure that line is present
  26. ]
  27. if len(sys.argv) < 2:
  28. print("Requires passing a filename as an argument.")
  29. exit(1)
  30. file_name = sys.argv[1]
  31. if not os.path.isfile(file_name):
  32. print("File does not exist:", file_name, "(not necessarily an error)")
  33. exit(0)
  34. regexes = []
  35. if file_name[:4] == "pal/":
  36. for line in pal_copyright_lines:
  37. pattern = '^.{1,5}%s$' % line
  38. regexes.append(re.compile(pattern))
  39. else:
  40. for line in copyright_lines:
  41. pattern = '^.{1,5}%s$' % line
  42. regexes.append(re.compile(pattern))
  43. def report_incorrect(file_name, pairs):
  44. # found a problem so report the problem to the caller and exit
  45. print(file_name, "... does not contain a correct copyright notice.\n")
  46. # print the relevant lines to help the reader find the problem
  47. for (_, line) in pairs:
  48. print(" ", line, end="")
  49. print()
  50. linecount = 0
  51. pairs = []
  52. with open(file_name, 'r') as sourcefile:
  53. hashbang = sourcefile.readline()
  54. if not hashbang.startswith("#!"):
  55. sourcefile.seek(0)
  56. pairs += zip(regexes, sourcefile)
  57. for (regex, line) in pairs:
  58. linecount += 1
  59. line = line.rstrip()
  60. matches = regex.match(line)
  61. if not matches:
  62. report_incorrect(file_name, pairs)
  63. exit(1)
  64. if linecount == 0:
  65. # the file was empty (e.g. dummy.js) so no problem
  66. exit(0)
  67. elif linecount != len(regexes):
  68. report_incorrect(file_name, pairs)
  69. exit(1)