-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangelicense.py
executable file
·130 lines (112 loc) · 4.27 KB
/
changelicense.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
#***************************************************************************
# This file is part of Hawaii.
#
# Copyright (c) 2012-2015 Pier Luigi Fiorini
#
# Author(s):
# Pier Luigi Fiorini <[email protected]>
#
# $BEGIN_LICENSE:BSD$
#
# You may use this file under the terms of the BSD license as follows:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Hawaii Project nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Pier Luigi Fiorini BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $END_LICENSE$
#**************************************************************************/
srclicense = dstlicense = ""
srctext = dsttext = ""
def read_license(filename):
import re
text = license = ""
ignore = True
f = open(filename, "r")
for line in f.readlines():
# Read from the BEGIN_LICENSE line included
m = re.search(r'\$BEGIN_LICENSE:(.+)\$$', line)
if m:
ignore = False
license = m.group(1)
# Save the line when we are actually reading a license line
if not ignore:
text += line
# Stop reading when we hit the END_LICENSE line
m = re.search(r'\$END_LICENSE\$$', line)
if m:
break
f.close()
return (license, text)
def search(root, d):
for pattern in options.glob.split(";"):
g = os.path.join(root, d, pattern)
for f in glob.glob(g):
if f[:-len(pattern)] != pattern:
massage(f)
def massage(filename):
# Read the original text
f = open(filename, "r")
code = f.read()
f.close()
# Perform the substitution
code = code.replace(srctext, dsttext)
# Write the code
f = open(filename, "w")
f.write(code)
f.close()
if __name__ == "__main__":
import sys, os, glob
from optparse import OptionParser
parser = OptionParser(usage="usage: %prog [options] PATH .. PATHN")
parser.add_option("-s", "--source-license", dest="source_license",
help="read source license text from FILE", metavar="FILE")
parser.add_option("-d", "--dest-license", dest="dest_license",
help="read destination license text from FILE", metavar="FILE")
parser.add_option("-g", "--glob", dest="glob",
help="source code files glob, default: %default", default="*.cpp;*.h")
(options, args) = parser.parse_args()
# Source and destination license files are mandatory
if not options.source_license or not options.dest_license:
parser.print_help()
sys.exit(1)
# Also the argument
if not args:
parser.print_help()
sys.exit(1)
# Read the license files
(srclicense, srctext) = read_license(options.source_license)
(dstlicense, dsttext) = read_license(options.dest_license)
# Can't continue if we haven't read the licenses correctly
if not srclicense or not srctext:
print >> sys.stderr, "Couldn't read the source license!"
sys.exit(1)
if not dstlicense or not dsttext:
print >> sys.stderr, "Couldn't read the destination license!"
sys.exit(1)
# Walk through all the directories passed as argument and replace
# the original license text with the new one
for path in args:
for root, dirs, files in os.walk(path):
search(root, "")
for d in dirs:
search(root, d)