copyright_headers.py 4.4 KB

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