check_copyright.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. # Copyright (c) 2016 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Checks for copyright notices in all the files that need them under the
  17. current directory. Optionally insert them. When inserting, replaces
  18. an MIT or Khronos free use license with Apache 2.
  19. """
  20. import argparse
  21. import fileinput
  22. import fnmatch
  23. import inspect
  24. import os
  25. import re
  26. import sys
  27. # List of designated copyright owners.
  28. AUTHORS = ['The Khronos Group Inc.',
  29. 'LunarG Inc.',
  30. 'Google Inc.',
  31. 'Google LLC',
  32. 'Pierre Moreau',
  33. 'Samsung Inc',
  34. 'André Perez Maselco',
  35. 'Vasyl Teliman',
  36. 'Advanced Micro Devices, Inc.',
  37. 'Stefano Milizia',
  38. 'Alastair F. Donaldson',
  39. 'Mostafa Ashraf',
  40. 'Shiyu Liu',
  41. 'ZHOU He',
  42. 'Nintendo',
  43. 'Epic Games, Inc.',
  44. 'NVIDIA Corporation']
  45. CURRENT_YEAR = 2023
  46. FIRST_YEAR = 2014
  47. FINAL_YEAR = CURRENT_YEAR + 5
  48. # A regular expression to match the valid years in the copyright information.
  49. YEAR_REGEX = '(' + '|'.join(
  50. str(year) for year in range(FIRST_YEAR, FINAL_YEAR + 1)) + ')'
  51. # A regular expression to make a range of years in the form <year1>-<year2>.
  52. YEAR_RANGE_REGEX = '('
  53. for year1 in range(FIRST_YEAR, FINAL_YEAR + 1):
  54. for year2 in range(year1 + 1, FINAL_YEAR + 1):
  55. YEAR_RANGE_REGEX += str(year1) + '-' + str(year2) + '|'
  56. YEAR_RANGE_REGEX = YEAR_RANGE_REGEX[:-1] + ')'
  57. # In the copyright info, the year can be a single year or a range. This is a
  58. # regex to make sure it matches one of them.
  59. YEAR_OR_RANGE_REGEX = '(' + YEAR_REGEX + '|' + YEAR_RANGE_REGEX + ')'
  60. # The final regular expression to match a valid copyright line.
  61. COPYRIGHT_RE = re.compile('Copyright \(c\) {} ({})'.format(
  62. YEAR_OR_RANGE_REGEX, '|'.join(AUTHORS)))
  63. MIT_BEGIN_RE = re.compile('Permission is hereby granted, '
  64. 'free of charge, to any person obtaining a')
  65. MIT_END_RE = re.compile('MATERIALS OR THE USE OR OTHER DEALINGS IN '
  66. 'THE MATERIALS.')
  67. APACHE2_BEGIN_RE = re.compile('Licensed under the Apache License, '
  68. 'Version 2.0 \(the "License"\);')
  69. APACHE2_END_RE = re.compile('limitations under the License.')
  70. LICENSED = """Licensed under the Apache License, Version 2.0 (the "License");
  71. you may not use this file except in compliance with the License.
  72. You may obtain a copy of the License at
  73. http://www.apache.org/licenses/LICENSE-2.0
  74. Unless required by applicable law or agreed to in writing, software
  75. distributed under the License is distributed on an "AS IS" BASIS,
  76. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  77. See the License for the specific language governing permissions and
  78. limitations under the License."""
  79. LICENSED_LEN = 10 # Number of lines in LICENSED
  80. def find(top, filename_glob, skip_glob_dir_list, skip_glob_files_list):
  81. """Returns files in the tree rooted at top matching filename_glob but not
  82. in directories matching skip_glob_dir_list nor files matching
  83. skip_glob_dir_list."""
  84. file_list = []
  85. for path, dirs, files in os.walk(top):
  86. for glob in skip_glob_dir_list:
  87. for match in fnmatch.filter(dirs, glob):
  88. dirs.remove(match)
  89. for filename in fnmatch.filter(files, filename_glob):
  90. full_file = os.path.join(path, filename)
  91. if full_file not in skip_glob_files_list:
  92. file_list.append(full_file)
  93. return file_list
  94. def filtered_descendants(glob):
  95. """Returns glob-matching filenames under the current directory, but skips
  96. some irrelevant paths."""
  97. return find('.', glob, ['third_party', 'external', 'CompilerIdCXX',
  98. 'build*', 'out*'], ['./utils/clang-format-diff.py'])
  99. def skip(line):
  100. """Returns true if line is all whitespace or shebang."""
  101. stripped = line.lstrip()
  102. return stripped == '' or stripped.startswith('#!')
  103. def comment(text, prefix):
  104. """Returns commented-out text.
  105. Each line of text will be prefixed by prefix and a space character. Any
  106. trailing whitespace will be trimmed.
  107. """
  108. accum = ['{} {}'.format(prefix, line).rstrip() for line in text.split('\n')]
  109. return '\n'.join(accum)
  110. def insert_copyright(author, glob, comment_prefix):
  111. """Finds all glob-matching files under the current directory and inserts the
  112. copyright message, and license notice. An MIT license or Khronos free
  113. use license (modified MIT) is replaced with an Apache 2 license.
  114. The copyright message goes into the first non-whitespace, non-shebang line
  115. in a file. The license notice follows it. Both are prefixed on each line
  116. by comment_prefix and a space.
  117. """
  118. copyright = comment('Copyright (c) {} {}'.format(CURRENT_YEAR, author),
  119. comment_prefix) + '\n\n'
  120. licensed = comment(LICENSED, comment_prefix) + '\n\n'
  121. for file in filtered_descendants(glob):
  122. # Parsing states are:
  123. # 0 Initial: Have not seen a copyright declaration.
  124. # 1 Seen a copyright line and no other interesting lines
  125. # 2 In the middle of an MIT or Khronos free use license
  126. # 9 Exited any of the above
  127. state = 0
  128. update_file = False
  129. for line in fileinput.input(file, inplace=1):
  130. emit = True
  131. if state == 0:
  132. if COPYRIGHT_RE.search(line):
  133. state = 1
  134. elif skip(line):
  135. pass
  136. else:
  137. # Didn't see a copyright. Inject copyright and license.
  138. sys.stdout.write(copyright)
  139. sys.stdout.write(licensed)
  140. # Assume there isn't a previous license notice.
  141. state = 1
  142. elif state == 1:
  143. if MIT_BEGIN_RE.search(line):
  144. state = 2
  145. emit = False
  146. elif APACHE2_BEGIN_RE.search(line):
  147. # Assume an Apache license is preceded by a copyright
  148. # notice. So just emit it like the rest of the file.
  149. state = 9
  150. elif state == 2:
  151. # Replace the MIT license with Apache 2
  152. emit = False
  153. if MIT_END_RE.search(line):
  154. state = 9
  155. sys.stdout.write(licensed)
  156. if emit:
  157. sys.stdout.write(line)
  158. def alert_if_no_copyright(glob, comment_prefix):
  159. """Prints names of all files missing either a copyright or Apache 2 license.
  160. Finds all glob-matching files under the current directory and checks if they
  161. contain the copyright message and license notice. Prints the names of all the
  162. files that don't meet both criteria.
  163. Returns the total number of file names printed.
  164. """
  165. printed_count = 0
  166. for file in filtered_descendants(glob):
  167. has_copyright = False
  168. has_apache2 = False
  169. line_num = 0
  170. apache_expected_end = 0
  171. with open(file, encoding='utf-8') as contents:
  172. for line in contents:
  173. line_num += 1
  174. if COPYRIGHT_RE.search(line):
  175. has_copyright = True
  176. if APACHE2_BEGIN_RE.search(line):
  177. apache_expected_end = line_num + LICENSED_LEN
  178. if (line_num is apache_expected_end) and APACHE2_END_RE.search(line):
  179. has_apache2 = True
  180. if not (has_copyright and has_apache2):
  181. message = file
  182. if not has_copyright:
  183. message += ' has no copyright'
  184. if not has_apache2:
  185. message += ' has no Apache 2 license notice'
  186. print(message)
  187. printed_count += 1
  188. return printed_count
  189. class ArgParser(argparse.ArgumentParser):
  190. def __init__(self):
  191. super(ArgParser, self).__init__(
  192. description=inspect.getdoc(sys.modules[__name__]))
  193. self.add_argument('--update', dest='author', action='store',
  194. help='For files missing a copyright notice, insert '
  195. 'one for the given author, and add a license '
  196. 'notice. The author must be in the AUTHORS '
  197. 'list in the script.')
  198. def main():
  199. glob_comment_pairs = [('*.h', '//'), ('*.hpp', '//'), ('*.sh', '#'),
  200. ('*.py', '#'), ('*.cpp', '//'),
  201. ('CMakeLists.txt', '#')]
  202. argparser = ArgParser()
  203. args = argparser.parse_args()
  204. if args.author:
  205. if args.author not in AUTHORS:
  206. print('error: --update argument must be in the AUTHORS list in '
  207. 'check_copyright.py: {}'.format(AUTHORS))
  208. sys.exit(1)
  209. for pair in glob_comment_pairs:
  210. insert_copyright(args.author, *pair)
  211. sys.exit(0)
  212. else:
  213. count = sum([alert_if_no_copyright(*p) for p in glob_comment_pairs])
  214. sys.exit(count > 0)
  215. if __name__ == '__main__':
  216. main()