fix_copyright_headers.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import argparse
  9. import os
  10. import re
  11. copyrightre = re.compile(r'All or portions of this file Copyright \(c\) Amazon\.com')
  12. copyright_text = """
  13. Copyright (c) Contributors to the Open 3D Engine Project.
  14. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  15. SPDX-License-Identifier: Apache-2.0 OR MIT
  16. """.splitlines(keepends=True)
  17. class copyright_builder:
  18. def get(self):
  19. pass
  20. def apply(self, file_contents):
  21. new_file_contents = self.get()
  22. new_file_contents += '\n' + file_contents
  23. return new_file_contents
  24. class copyright_builder_first_last_line(copyright_builder):
  25. def __init__(self, comment_str):
  26. self.comment_str = comment_str
  27. def get(self):
  28. copyright_str = self.comment_str + copyright_text[0]
  29. for i in range(1, len(copyright_text)-1):
  30. copyright_str += copyright_text[i]
  31. copyright_str += self.comment_str + copyright_text[len(copyright_text)-1]
  32. return copyright_str
  33. class copyright_builder_each_line(copyright_builder):
  34. def __init__(self, comment_str):
  35. self.comment_str = comment_str
  36. def get(self):
  37. copyright_str = ''
  38. for i in range(0, len(copyright_text)):
  39. copyright_str += self.comment_str + copyright_text[i]
  40. return copyright_str
  41. class copyright_builder_each_line_bat(copyright_builder_each_line):
  42. def __init__(self, comment_str):
  43. self.comment_str = comment_str
  44. self.echo_off_re = re.compile('@echo off\n', re.IGNORECASE)
  45. def apply(self, file_contents):
  46. re_result = self.echo_off_re.search(file_contents)
  47. if re_result:
  48. return self.echo_off_re.sub(re_result.group(0) + '\n' + self.get() + '\n', file_contents)
  49. return super().apply(file_contents)
  50. class copyright_builder_each_line_sh(copyright_builder_each_line):
  51. def __init__(self, comment_str):
  52. self.comment_str = comment_str
  53. self.bin_str = [
  54. '#!/bin/sh\n',
  55. '#!/bin/bash\n'
  56. ]
  57. def apply(self, file_contents):
  58. for bin_str in self.bin_str:
  59. if bin_str in file_contents:
  60. return file_contents.replace(bin_str, bin_str + '\n' + self.get() + '\n')
  61. return super().apply(file_contents)
  62. class copyright_builder_c(copyright_builder):
  63. def get(self):
  64. copyright_str = '/*' + copyright_text[0]
  65. for i in range(1, len(copyright_text)-1):
  66. copyright_str += '*' + copyright_text[i]
  67. copyright_str += '*\n'
  68. copyright_str += '*/' + copyright_text[len(copyright_text)-1]
  69. return copyright_str
  70. copyright_comment_by_extension = {
  71. 'lua': copyright_builder_each_line('-- '),
  72. 'py': copyright_builder_first_last_line('"""'),
  73. 'bat': copyright_builder_each_line_bat('REM '),
  74. 'cmd': copyright_builder_each_line_bat('REM '),
  75. 'sh': copyright_builder_each_line_sh('# '),
  76. 'cs': copyright_builder_c(),
  77. }
  78. def fixCopyright(input_file):
  79. try:
  80. extension = os.path.splitext(input_file)[1].replace('.','')
  81. if not extension in copyright_comment_by_extension:
  82. print(f'[WARN] Extension {extension} not handled')
  83. else:
  84. copyright_builder = copyright_comment_by_extension[extension]
  85. with open(input_file, 'r') as source_file:
  86. fileContents = source_file.read()
  87. reResult = copyrightre.search(fileContents)
  88. if reResult:
  89. # Copyright found, skip
  90. return
  91. newFileContents = copyright_builder.apply(fileContents)
  92. with open(input_file, 'w') as destination_file:
  93. destination_file.write(newFileContents)
  94. print(f'[INFO] Patched {input_file}')
  95. except (IOError, UnicodeDecodeError) as err:
  96. print('[ERROR] reading {}: {}'.format(input_file, err))
  97. return
  98. def main():
  99. """script main function"""
  100. parser = argparse.ArgumentParser(description='This script fixes copyright headers',
  101. formatter_class=argparse.RawTextHelpFormatter)
  102. parser.add_argument('file_or_dir', type=str, nargs='+',
  103. help='list of files or directories to search within for files to fix up copyright headers')
  104. args = parser.parse_args()
  105. for input_file in args.file_or_dir:
  106. if os.path.isdir(input_file):
  107. for dp, dn, filenames in os.walk(input_file):
  108. for f in filenames:
  109. fixCopyright(os.path.join(dp, f))
  110. else:
  111. fixCopyright(input_file)
  112. #entrypoint
  113. if __name__ == '__main__':
  114. main()