forked from mbert/mulberry-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
license-change.py
executable file
·113 lines (94 loc) · 3.25 KB
/
license-change.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
import getopt
import os
import sys
newlicense = """/*
Copyright (c) 2007 Cyrus Daboo. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
"""
def process_directory(path, delete_unlicensed, undo):
for file in os.listdir(path):
fpath = os.path.join(path, file)
if os.path.isfile(fpath):
if undo:
undo_file(fpath)
else:
process_file(fpath, delete_unlicensed)
elif os.path.isdir(fpath):
process_directory(fpath, delete_unlicensed, undo)
def undo_file(fpath):
if (fpath.endswith(".cp.unlicensed") or
fpath.endswith(".cc.unlicensed") or
fpath.endswith(".cpp.unlicensed") or
fpath.endswith(".c.unlicensed") or
fpath.endswith(".h.unlicensed")):
fpath = fpath[:-11]
print fpath
os.rename(fpath, "%s.licensed" % fpath)
os.rename("%s.unlicensed" % fpath, fpath)
os.remove("%s.licensed" % fpath)
def process_file(fpath, delete_unlicensed):
if delete_unlicensed and \
(fpath.endswith(".cp.unlicensed") or
fpath.endswith(".cc.unlicensed") or
fpath.endswith(".cpp.unlicensed") or
fpath.endswith(".c.unlicensed") or
fpath.endswith(".h.unlicensed")):
os.remove(fpath)
elif not delete_unlicensed and \
(fpath.endswith(".cp") or
fpath.endswith(".cc") or
fpath.endswith(".cpp") or
fpath.endswith(".c") or
fpath.endswith(".h")):
replace_license(fpath)
def replace_license(fpath):
print fpath
fin = open(fpath, "rU")
fout = open("%s.licensed" % fpath, "w")
first = True
ignore = False
reallyignore = False
ignored = []
for line in fin:
if first:
fout.write(newlicense)
if line.startswith("/*"):
ignore = True
first = False
if not ignore:
fout.write(line)
else:
ignored.append(line)
if ignore:
if line.find("Copyright") != -1:
reallyignore = True
if line.find("*/") != -1:
ignore = False
if not reallyignore:
fout.write("".join(ignored))
fin.close()
fout.close()
os.rename(fpath, "%s.unlicensed" % fpath)
os.rename("%s.licensed" % fpath, fpath)
if __name__ == "__main__":
delete_unlicensed = False
undo = False
options, args = getopt.getopt(sys.argv[1:], "ru")
for option, value in options:
if option == "-r":
delete_unlicensed = True
elif option == "-r":
undo = True
paths = args
for path in paths:
process_directory(path, delete_unlicensed, undo)