fix_headers.py 4.3 KB

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