Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/misc/scripts/copyright_headers.py
9896 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import os
5
import sys
6
7
header = """\
8
/**************************************************************************/
9
/* $filename */
10
/**************************************************************************/
11
/* This file is part of: */
12
/* GODOT ENGINE */
13
/* https://godotengine.org */
14
/**************************************************************************/
15
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
16
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
17
/* */
18
/* Permission is hereby granted, free of charge, to any person obtaining */
19
/* a copy of this software and associated documentation files (the */
20
/* "Software"), to deal in the Software without restriction, including */
21
/* without limitation the rights to use, copy, modify, merge, publish, */
22
/* distribute, sublicense, and/or sell copies of the Software, and to */
23
/* permit persons to whom the Software is furnished to do so, subject to */
24
/* the following conditions: */
25
/* */
26
/* The above copyright notice and this permission notice shall be */
27
/* included in all copies or substantial portions of the Software. */
28
/* */
29
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
30
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
31
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
32
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
33
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
34
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
35
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
36
/**************************************************************************/
37
"""
38
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)
42
43
for f in sys.argv[1:]:
44
fname = f
45
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"
64
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
72
with open(fname.strip(), "r", encoding="utf-8") as fileread:
73
line = fileread.readline()
74
header_done = False
75
76
while line.strip() == "" and line != "": # Skip empty lines at the top
77
line = fileread.readline()
78
79
if line.find("/**********") == -1: # Godot header starts this way
80
# Maybe starting with a non-Godot comment, abort header magic
81
header_done = True
82
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()
93
94
# Write
95
with open(fname.strip(), "w", encoding="utf-8", newline="\n") as filewrite:
96
filewrite.write(text)
97
98