copyright_headers.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  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. fname = sys.argv[1]
  36. # Handle replacing $filename with actual filename and keep alignment
  37. fsingle = fname.strip()
  38. if fsingle.find("/") != -1:
  39. fsingle = fsingle[fsingle.rfind("/") + 1 :]
  40. rep_fl = "$filename"
  41. rep_fi = fsingle
  42. len_fl = len(rep_fl)
  43. len_fi = len(rep_fi)
  44. # Pad with spaces to keep alignment
  45. if len_fi < len_fl:
  46. for x in range(len_fl - len_fi):
  47. rep_fi += " "
  48. elif len_fl < len_fi:
  49. for x in range(len_fi - len_fl):
  50. rep_fl += " "
  51. if header.find(rep_fl) != -1:
  52. text = header.replace(rep_fl, rep_fi)
  53. else:
  54. text = header.replace("$filename", fsingle)
  55. text += "\n"
  56. # We now have the proper header, so we want to ignore the one in the original file
  57. # and potentially empty lines and badly formatted lines, while keeping comments that
  58. # come after the header, and then keep everything non-header unchanged.
  59. # To do so, we skip empty lines that may be at the top in a first pass.
  60. # In a second pass, we skip all consecutive comment lines starting with "/*",
  61. # then we can append the rest (step 2).
  62. fileread = open(fname.strip(), "r")
  63. line = fileread.readline()
  64. header_done = False
  65. while line.strip() == "": # Skip empty lines at the top
  66. line = fileread.readline()
  67. if line.find("/**********") == -1: # Godot header starts this way
  68. # Maybe starting with a non-Godot comment, abort header magic
  69. header_done = True
  70. while not header_done: # Handle header now
  71. if line.find("/*") != 0: # No more starting with a comment
  72. header_done = True
  73. if line.strip() != "":
  74. text += line
  75. line = fileread.readline()
  76. while line != "": # Dump everything until EOF
  77. text += line
  78. line = fileread.readline()
  79. fileread.close()
  80. # Write
  81. filewrite = open(fname.strip(), "w")
  82. filewrite.write(text)
  83. filewrite.close()