|
1 #!/usr/bin/python |
|
2 # Copyright (c) 2009 Symbian Foundation. |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of the License "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Symbian Foundation - Initial contribution |
|
11 # |
|
12 # Description: |
|
13 # Map the SFL license to the EPL license |
|
14 |
|
15 import os |
|
16 import os.path |
|
17 import re |
|
18 import codecs |
|
19 from optparse import OptionParser |
|
20 import sys |
|
21 |
|
22 oldtext0 = re.compile('terms of the License "Symbian Foundation License v1.0"(to Symbian Foundation)?') |
|
23 oldtext1 = re.compile('the URL "http:..www.symbianfoundation.org/legal/sfl-v10.html"') |
|
24 |
|
25 newtext = [ |
|
26 'terms of the License "Eclipse Public License v1.0"', |
|
27 'the URL "http://www.eclipse.org/legal/epl-v10.html"' |
|
28 ] |
|
29 |
|
30 errorfiles = [] |
|
31 multinoticefiles = [] |
|
32 shadowroot = 'shadow_epoc32' |
|
33 |
|
34 def file_type(file) : |
|
35 f = open(file, 'r') |
|
36 data = f.read(256) |
|
37 f.close() |
|
38 if len(data) < 2: |
|
39 return None # too short to be worth bothering about anyway |
|
40 if data[0] == chr(255) and data[1] == chr(254) : |
|
41 return 'utf_16_le' |
|
42 if data.find(chr(0)) >= 0 : |
|
43 return None # zero byte implies binary file |
|
44 return 'text' |
|
45 |
|
46 def map_eula(dir, name, encoded) : |
|
47 global oldtext0 |
|
48 global newtext1 |
|
49 global newtext |
|
50 file = os.path.join(dir, name) |
|
51 if encoded == 'text': |
|
52 f = open(file, 'r') |
|
53 else: |
|
54 f = codecs.open(file, 'r', encoding=encoded) |
|
55 lines = f.readlines() |
|
56 # print ">> %s encoded as %s" % (file, f.encoding) |
|
57 f.close() |
|
58 |
|
59 updated = 0 |
|
60 newlines = [] |
|
61 while len(lines) > 0: |
|
62 line = lines.pop(0) |
|
63 pos1 = oldtext0.search(line) |
|
64 if pos1 != None: |
|
65 # be careful - oldtext is a prefix of newtext |
|
66 if pos1.group(1) != None: |
|
67 # line already converted - nothing to do |
|
68 newlines.append(line) |
|
69 continue |
|
70 midlines = [] |
|
71 midlinecount = 1 |
|
72 while len(lines) > 0: |
|
73 nextline = lines.pop(0) |
|
74 if not re.match('^\s$', nextline): |
|
75 # non-blank line |
|
76 if midlinecount == 0: |
|
77 break |
|
78 midlinecount -= 1 |
|
79 midlines.append(nextline) |
|
80 urlline = nextline |
|
81 pos2 = oldtext1.search(urlline) |
|
82 if pos2 != None: |
|
83 # found it - assume that there's only one instance |
|
84 newline = oldtext0.sub(newtext[0], line) |
|
85 newurl = oldtext1.sub(newtext[1], urlline) |
|
86 newlines.append(newline) |
|
87 newlines.extend(midlines) |
|
88 newlines.append(newurl) |
|
89 updated += 1 |
|
90 continue |
|
91 else: |
|
92 if updated != 0: |
|
93 lineno = 1 + len(newlines) |
|
94 print "Problem in " + file + " at " + lineno + ": incorrectly formatted >" |
|
95 print line |
|
96 print midlines |
|
97 print urlline |
|
98 global errorfiles |
|
99 errorfiles.append(file) |
|
100 break |
|
101 newlines.append(line) |
|
102 |
|
103 if updated == 0: |
|
104 # print " = no change to " + file |
|
105 return 0 |
|
106 |
|
107 if updated > 1: |
|
108 global multinoticefiles |
|
109 multinoticefiles.append(file) |
|
110 print '! found %d SFL notices in %s' % (updated, file) |
|
111 |
|
112 # global shadowroot |
|
113 # shadowdir = os.path.join(shadowroot, dir) |
|
114 # if not os.path.exists(shadowdir) : |
|
115 # os.makedirs(shadowdir) |
|
116 # newfile = os.path.join(shadowroot,file) |
|
117 # os.rename(file, newfile) |
|
118 |
|
119 global options |
|
120 if not options.dryrun: |
|
121 if encoded == 'text': |
|
122 f = open(file, 'w') |
|
123 else: |
|
124 f = codecs.open(file, 'w', encoding=encoded) |
|
125 f.writelines(newlines) |
|
126 f.close() |
|
127 print "* updated %s (encoding %s)" % (file, encoded) |
|
128 return 1 |
|
129 |
|
130 parser = OptionParser(version="%prog 0.2", usage="Usage: %prog [options]") |
|
131 parser.add_option("-n", "--check", action="store_true", dest="dryrun", |
|
132 help="report the files which would be updated, but don't change anything") |
|
133 parser.set_defaults(dryrun=False) |
|
134 |
|
135 (options, args) = parser.parse_args() |
|
136 if len(args) != 0: |
|
137 parser.error("Unexpected commandline arguments") |
|
138 |
|
139 # process tree |
|
140 |
|
141 update_count = 0 |
|
142 for root, dirs, files in os.walk('.', topdown=True): |
|
143 if '.hg' in dirs: |
|
144 dirs.remove('.hg') # don't recurse into the Mercurial repository storage |
|
145 for name in files: |
|
146 encoding = file_type(os.path.join(root, name)) |
|
147 if encoding: |
|
148 update_count += map_eula(root, name, encoding) |
|
149 |
|
150 print '%d problem files' % len(errorfiles) |
|
151 print errorfiles |
|
152 |
|
153 print '%d files with multiple notices' % len(multinoticefiles) |
|
154 print multinoticefiles |
|
155 |
|
156 if options.dryrun and update_count > 0: |
|
157 print "%d files need updating" % update_count |
|
158 sys.exit(1) |
|
159 |
|
160 |