Mercurial > mozilla > hg > licenser
annotate licenser/licenses.py @ 24:d3e3a506dd29
tests now pass
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 24 Nov 2011 15:29:05 -0800 |
parents | ba47f936e9f6 |
children | 8729685a2cf6 |
rev | line source |
---|---|
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
1 # ***** BEGIN LICENSE BLOCK ***** |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
10 | 3 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
4 # The contents of this file are subject to the Mozilla Public License Version |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
5 # 1.1 (the "License"); you may not use this file except in compliance with |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
6 # the License. You may obtain a copy of the License at |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
7 # http://www.mozilla.org/MPL/ |
10 | 8 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
9 # Software distributed under the License is distributed on an "AS IS" basis, |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
11 # for the specific language governing rights and limitations under the |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
12 # License. |
10 | 13 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
14 # The Original Code is mozilla.org code. |
10 | 15 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
16 # The Initial Developer of the Original Code is |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
17 # Mozilla.org. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
18 # Portions created by the Initial Developer are Copyright (C) 2010 |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
19 # the Initial Developer. All Rights Reserved. |
10 | 20 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
21 # Contributor(s): |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
22 # Jeff Hammel <jhammel@mozilla.com> (Original author) |
10 | 23 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
24 # Alternatively, the contents of this file may be used under the terms of |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
25 # either of the GNU General Public License Version 2 or later (the "GPL"), |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
26 # or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
27 # in which case the provisions of the GPL or the LGPL are applicable instead |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
28 # of those above. If you wish to allow use of your version of this file only |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
29 # under the terms of either the GPL or the LGPL, and not to allow others to |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
30 # use your version of this file under the terms of the MPL, indicate your |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
31 # decision by deleting the provisions above and replace them with the notice |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
32 # and other provisions required by the GPL or the LGPL. If you do not delete |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
33 # the provisions above, a recipient may use your version of this file under |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
34 # the terms of any one of the MPL, the GPL or the LGPL. |
10 | 35 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
36 # ***** END LICENSE BLOCK ***** |
10 | 37 |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
38 import os |
11 | 39 import urllib2 |
0 | 40 from datetime import datetime |
4 | 41 from string import Template |
0 | 42 |
14 | 43 # types of comments |
44 | |
17 | 45 class CommentedFile(object): |
46 | |
47 extensions = set() | |
48 | |
49 @classmethod | |
50 def match(cls, filename): | |
51 basename, extension = os.path.splitext(os.path.basename(filename)) | |
20 | 52 extension = extension.lstrip('.') |
19 | 53 if extension in cls.extensions: |
17 | 54 return cls(filename) |
55 | |
56 def __init__(self, filename): | |
57 self.filename = filename | |
58 | |
59 def isempty(self): | |
24 | 60 return not bool(file(self.filename).read().strip) |
17 | 61 |
62 def lines(self): | |
63 if hasattr(self, '_lines'): | |
64 return self._lines | |
65 self._lines = file(self.filename).readlines() | |
66 return self._lines | |
67 | |
68 class AsteriskCommentsFile(CommentedFile): | |
69 | |
70 extensions = set(['c']) | |
71 | |
72 def __call__(self, license): | |
73 | |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
74 if self.isempty(): |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
75 return # you're done |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
76 lines = self.lines() |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
77 |
17 | 78 # open the file for writing |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
79 f = file(self.filename, 'w') |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
80 |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
81 # print the license |
22 | 82 license_lines = license.splitlines() |
24 | 83 for index, line in enumerate(license_lines): |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
84 prefix = ' *' |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
85 suffix = '' |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
86 if index == len(license_lines) - 1: |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
87 suffix = ' */' |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
88 if not index: |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
89 prefix = '/*' |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
90 print >> f, '%s %s%s' % (prefix, line, suffix) |
24 | 91 print >> f |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
92 |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
93 # print the rest of the file |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
94 for line in lines: |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
95 f.write(line) |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
96 f.close() |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
97 |
17 | 98 |
19 | 99 class HashCommentsFile(CommentedFile): |
17 | 100 |
101 extensions = set(['py', 'sh']) | |
14 | 102 |
17 | 103 def __call__(self, license): |
104 """interpolate the file""" | |
105 | |
106 if self.isempty(): | |
107 return # you're done | |
108 lines = self.lines() | |
109 | |
110 # open the file for writing | |
111 f = file(self.filename, 'w') | |
112 | |
113 # print shebang if it exists | |
114 if lines[0].startswith('#!'): | |
115 shebang = lines.pop(0).strip() | |
116 print >> f, shebang | |
117 print >> f | |
118 | |
119 # print the license | |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
120 for line in license.splitlines(): |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
121 print >> f, '# %s' % line |
14 | 122 |
17 | 123 # print the rest of the file |
124 for line in lines: | |
125 f.write(line) | |
126 f.close() | |
127 | |
23 | 128 def isempty(self): |
17 | 129 """ |
130 determines if a file is empty; that is, contains only comments | |
131 """ | |
18
c04a53145f19
interpolate the files, i hope
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
132 for line in self.lines(): |
17 | 133 line = line.strip() |
134 if line and line[0] != '#': | |
135 return False | |
136 return True | |
137 | |
138 | |
139 filetypes = [HashCommentsFile, AsteriskCommentsFile] | |
14 | 140 |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
141 class License(object): |
15 | 142 """Abstract base class for a license""" |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
143 |
15 | 144 variables = [] # required variables |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
145 |
18
c04a53145f19
interpolate the files, i hope
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
146 def __init__(self, filetypes=filetypes[:]): |
15 | 147 if self.template: |
148 if not os.path.isabs(self.template): | |
149 self.template = os.path.join(os.path.dirname(__file__), | |
150 'licenses', | |
151 self.template) | |
152 assert os.path.exists(self.template) | |
17 | 153 self.filetypes = filetypes |
15 | 154 |
155 def license(self): | |
156 return file(self.template).read() | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
157 |
15 | 158 def print_license(self): |
159 print self.license() | |
11 | 160 |
15 | 161 def token(self): |
162 """a token indicative of the license, by default the first line""" | |
19 | 163 return self.license().splitlines()[0].strip() |
3 | 164 |
15 | 165 def has_license(self, filename): |
166 """does the file already have a license?""" | |
167 token = self.token() | |
168 for line in file(filename).readlines(): | |
169 if token in line: | |
170 return True | |
171 return False | |
14 | 172 |
15 | 173 def __call__(self, directory, **kw): |
174 variables = self.obtain_variables(**kw) | |
175 self.interpolate(directory, variables) | |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
176 |
15 | 177 def obtain_variables(self, **kw): |
178 for var in self.variables: | |
179 if var not in kw: | |
180 print 'Enter %s: ' % var, | |
181 kw[var] = raw_input() | |
182 self.pre(kw) | |
183 return kw | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
184 |
17 | 185 def pre(self, variables): |
186 """do anything that needs to be done with the variables before interpolation""" | |
6
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
187 |
17 | 188 def filetype(self, _file): |
189 """determine the filetype for a given file""" | |
190 for filetype in self.filetypes: | |
191 match = filetype.match(_file) | |
192 if match: | |
193 return match | |
6
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
194 |
17 | 195 def interpolate_file(self, _file, variables): |
196 | |
197 # if the file is licensed, no need to proceed | |
19 | 198 if self.has_license(_file): |
17 | 199 return # you're done |
6
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
200 |
17 | 201 # get the filetype |
202 filetype = self.filetype(_file) | |
203 if not filetype: | |
204 return # you're done | |
10 | 205 |
17 | 206 # get the license |
207 # XXX should be cached | |
208 license = self.license() | |
209 license = Template(license).substitute(**variables) | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
210 |
17 | 211 # add the license to the file |
212 filetype(license) | |
213 | |
214 def interpolate(self, directory, variables): | |
215 for _file in self.files(directory): | |
216 self.interpolate_file(_file, variables) | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
217 |
17 | 218 def files(self, directory): |
19 | 219 files = [] |
17 | 220 for dirpath, _, filenames in os.walk(directory): |
221 for f in filenames: | |
19 | 222 files.append(os.path.join(dirpath, f)) |
17 | 223 return files |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
224 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
225 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
226 class MPL(License): |
17 | 227 """Mozilla Public License""" |
228 template = 'MPL' # could be implicit here | |
229 variables = [ 'author', 'email' ] | |
230 url = 'http://www.mozilla.org/MPL/boilerplate-1.1/mpl-tri-license-txt' | |
231 | |
232 def license(self): | |
11 | 233 |
17 | 234 if hasattr(self, '_license'): |
235 # return the cached copy | |
236 return self._license | |
237 | |
238 # get the raw license | |
239 raw = urllib2.urlopen(self.url).read() | |
11 | 240 |
17 | 241 # now make it something not crazy |
242 lines = raw.splitlines() | |
243 contributor_index = None | |
244 for index, line in enumerate(lines): | |
245 if line.startswith('The Original Code is'): | |
246 lines[index] = 'The Original Code is mozilla.org code.' | |
247 if line.startswith('_'): | |
248 lines[index] = 'the Mozilla Foundation.' | |
249 if '2___' in line: | |
250 lines[index] = line.replace('2___', '${year}') | |
251 if line.startswith('Contributor'): | |
252 contributor_index = index | |
11 | 253 |
17 | 254 assert contributor_index |
255 lines.insert(contributor_index+1, ' ${author} <${email}>') | |
256 self._license = '\n'.join(lines) | |
257 return self._license | |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
258 |
17 | 259 def pre(self, variables): |
260 variables['year'] = datetime.now().year | |
11 | 261 |