copyright_headers.py 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. header = """\
  6. /**************************************************************************/
  7. /* $filename */
  8. /**************************************************************************/
  9. /* This file is part of: */
  10. /* GODOT ENGINE */
  11. /* https://godotengine.org */
  12. /**************************************************************************/
  13. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  14. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  15. /* */
  16. /* Permission is hereby granted, free of charge, to any person obtaining */
  17. /* a copy of this software and associated documentation files (the */
  18. /* "Software"), to deal in the Software without restriction, including */
  19. /* without limitation the rights to use, copy, modify, merge, publish, */
  20. /* distribute, sublicense, and/or sell copies of the Software, and to */
  21. /* permit persons to whom the Software is furnished to do so, subject to */
  22. /* the following conditions: */
  23. /* */
  24. /* The above copyright notice and this permission notice shall be */
  25. /* included in all copies or substantial portions of the Software. */
  26. /* */
  27. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  28. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  29. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  30. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  31. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  32. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  33. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  34. /**************************************************************************/
  35. """
  36. if len(sys.argv) < 2:
  37. print("Invalid usage of copyright_headers.py, it should be called with a path to one or multiple files.")
  38. sys.exit(1)
  39. for f in sys.argv[1:]:
  40. fname = f
  41. # Handle replacing $filename with actual filename and keep alignment
  42. fsingle = os.path.basename(fname.strip())
  43. rep_fl = "$filename"
  44. rep_fi = fsingle
  45. len_fl = len(rep_fl)
  46. len_fi = len(rep_fi)
  47. # Pad with spaces to keep alignment
  48. if len_fi < len_fl:
  49. for x in range(len_fl - len_fi):
  50. rep_fi += " "
  51. elif len_fl < len_fi:
  52. for x in range(len_fi - len_fl):
  53. rep_fl += " "
  54. if header.find(rep_fl) != -1:
  55. text = header.replace(rep_fl, rep_fi)
  56. else:
  57. text = header.replace("$filename", fsingle)
  58. text += "\n"
  59. # We now have the proper header, so we want to ignore the one in the original file
  60. # and potentially empty lines and badly formatted lines, while keeping comments that
  61. # come after the header, and then keep everything non-header unchanged.
  62. # To do so, we skip empty lines that may be at the top in a first pass.
  63. # In a second pass, we skip all consecutive comment lines starting with "/*",
  64. # then we can append the rest (step 2).
  65. with open(fname.strip(), "r", encoding="utf-8") as fileread:
  66. line = fileread.readline()
  67. header_done = False
  68. while line.strip() == "" and line != "": # Skip empty lines at the top
  69. line = fileread.readline()
  70. if line.find("/**********") == -1: # Godot header starts this way
  71. # Maybe starting with a non-Godot comment, abort header magic
  72. header_done = True
  73. while not header_done: # Handle header now
  74. if line.find("/*") != 0: # No more starting with a comment
  75. header_done = True
  76. if line.strip() != "":
  77. text += line
  78. line = fileread.readline()
  79. while line != "": # Dump everything until EOF
  80. text += line
  81. line = fileread.readline()
  82. # Write
  83. with open(fname.strip(), "w", encoding="utf-8", newline="\n") as filewrite:
  84. filewrite.write(text)