Merge pull request #832 from GreYFoX/oypull1
[twcon.git] / scripts / copyright.py
blob269ac7a3d10de6eefbc0dd58dd76fdf291dbf08a
1 import os, re, sys
2 match = re.search('(.*)/', sys.argv[0])
3 if match != None:
4 os.chdir(match.group(1))
5 os.chdir('../')
7 notice = [b"/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */\n", b"/* If you are missing that file, acquire a complete release at teeworlds.com. */\n"]
8 exclude = ["src%sengine%sexternal" % (os.sep, os.sep), "src%sosxlaunch" % os.sep]
9 updated_files = 0
11 def fix_copyright_notice(filename):
12 global updated_files
13 f = open(filename, "rb")
14 lines = f.readlines()
15 f.close()
17 i = 0
18 length_lines = len(lines)
19 if length_lines > 0:
20 while i <= length_lines and (lines[i].decode("utf-8").lstrip()[:2] == "//" or lines[i].decode("utf-8").lstrip()[:2] == "/*" and lines[i].decode("utf-8").rstrip()[-2:] == "*/") and ("Magnus" in lines[i].decode("utf-8") or "magnus" in lines[i].decode("utf-8") or "Auvinen" in lines[i].decode("utf-8") or "auvinen" in lines[i].decode("utf-8") or "license" in lines[i].decode("utf-8") or "teeworlds" in lines[i].decode("utf-8")):
21 i += 1
22 length_notice = len(notice)
23 if i > 0:
24 j = 0
25 while lines[j] == notice[j]:
26 j += 1
27 if j == length_notice:
28 return
29 k = j
30 j = 0
31 while j < length_notice -1 - k:
32 lines = [notice[j]] + lines
33 j += 1
34 while j < length_notice:
35 lines[j] = notice[j]
36 j += 1
37 if length_lines == 0 or i == 0:
38 j = length_notice - 1
39 while j >= 0:
40 lines = [notice[j]] + lines
41 j -= 1
42 open(filename, "wb").writelines(lines)
43 updated_files += 1
45 skip = False
46 for root, dirs, files in os.walk("src"):
47 for excluding in exclude:
48 if root[:len(excluding)] == excluding:
49 skip = True
50 break
51 if skip == True:
52 skip = False
53 continue
54 for name in files:
55 filename = os.path.join(root, name)
57 if filename[-2:] != ".c" and filename[-4:] != ".cpp" and filename[-2:] != ".h":
58 continue
60 fix_copyright_notice(filename)
62 output = "file"
63 if updated_files != 1:
64 output += "s"
65 print("*** updated %d %s ***" % (updated_files, output))