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