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