Mercurial > mozilla > hg > licenser
annotate licenser/licenses.py @ 27:7e0c931a201d
web licenser now works
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 25 Nov 2011 00:57:38 -0800 |
parents | 0faf3e7b593a |
children |
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 | |
27 | 59 def fileobj(self): |
60 """return a file object open for writing""" | |
61 if isinstance(self.filename, basestring): | |
62 return file(self.filename, 'w') | |
63 return self.filename | |
64 | |
65 def close(self, _file): | |
66 if isinstance(self.filename, basestring): | |
67 _file.close() | |
68 | |
17 | 69 def isempty(self): |
24 | 70 return not bool(file(self.filename).read().strip) |
17 | 71 |
72 def lines(self): | |
73 if hasattr(self, '_lines'): | |
74 return self._lines | |
75 self._lines = file(self.filename).readlines() | |
76 return self._lines | |
77 | |
78 class AsteriskCommentsFile(CommentedFile): | |
79 | |
80 extensions = set(['c']) | |
81 | |
82 def __call__(self, license): | |
83 | |
27 | 84 lines = self.lines() |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
85 if self.isempty(): |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
86 return # you're done |
25 | 87 |
17 | 88 # open the file for writing |
27 | 89 f = self.fileobj() |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
90 |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
91 # print the license |
22 | 92 license_lines = license.splitlines() |
24 | 93 for index, line in enumerate(license_lines): |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
94 prefix = ' *' |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
95 suffix = '' |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
96 if index == len(license_lines) - 1: |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
97 suffix = ' */' |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
98 if not index: |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
99 prefix = '/*' |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
100 print >> f, '%s %s%s' % (prefix, line, suffix) |
24 | 101 print >> f |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
102 |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
103 # print the rest of the file |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
104 for line in lines: |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
105 f.write(line) |
27 | 106 self.close(f) |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
107 |
17 | 108 |
19 | 109 class HashCommentsFile(CommentedFile): |
17 | 110 |
111 extensions = set(['py', 'sh']) | |
14 | 112 |
17 | 113 def __call__(self, license): |
114 """interpolate the file""" | |
115 | |
27 | 116 lines = self.lines() |
17 | 117 if self.isempty(): |
118 return # you're done | |
119 | |
120 # open the file for writing | |
27 | 121 f = self.fileobj() |
17 | 122 |
123 # print shebang if it exists | |
124 if lines[0].startswith('#!'): | |
125 shebang = lines.pop(0).strip() | |
126 print >> f, shebang | |
127 print >> f | |
128 | |
129 # print the license | |
21
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
130 for line in license.splitlines(): |
80193ae99aaf
fill out asterisk-style licenses
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
131 print >> f, '# %s' % line |
14 | 132 |
17 | 133 # print the rest of the file |
134 for line in lines: | |
135 f.write(line) | |
27 | 136 self.close(f) |
17 | 137 |
23 | 138 def isempty(self): |
17 | 139 """ |
140 determines if a file is empty; that is, contains only comments | |
141 """ | |
18
c04a53145f19
interpolate the files, i hope
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
142 for line in self.lines(): |
17 | 143 line = line.strip() |
144 if line and line[0] != '#': | |
145 return False | |
146 return True | |
147 | |
148 | |
149 filetypes = [HashCommentsFile, AsteriskCommentsFile] | |
14 | 150 |
25 | 151 def filetypes_string(filetypes=filetypes[:]): |
152 extensions = set() | |
153 for filetype in filetypes: | |
154 extensions.update(filetype.extensions) | |
155 return ', '.join([i for i in extensions]) | |
156 | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
157 class License(object): |
15 | 158 """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
|
159 |
15 | 160 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
|
161 |
18
c04a53145f19
interpolate the files, i hope
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
162 def __init__(self, filetypes=filetypes[:]): |
15 | 163 if self.template: |
164 if not os.path.isabs(self.template): | |
165 self.template = os.path.join(os.path.dirname(__file__), | |
166 'licenses', | |
167 self.template) | |
168 assert os.path.exists(self.template) | |
17 | 169 self.filetypes = filetypes |
25 | 170 |
15 | 171 def license(self): |
172 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
|
173 |
15 | 174 def print_license(self): |
175 print self.license() | |
11 | 176 |
15 | 177 def token(self): |
178 """a token indicative of the license, by default the first line""" | |
19 | 179 return self.license().splitlines()[0].strip() |
3 | 180 |
15 | 181 def has_license(self, filename): |
182 """does the file already have a license?""" | |
183 token = self.token() | |
27 | 184 if isinstance(filename, basestring): |
185 f = file(filename) | |
186 else: | |
187 f = filename | |
188 for line in f.readlines(): | |
15 | 189 if token in line: |
190 return True | |
191 return False | |
14 | 192 |
15 | 193 def __call__(self, directory, **kw): |
194 variables = self.obtain_variables(**kw) | |
195 self.interpolate(directory, variables) | |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
196 |
15 | 197 def obtain_variables(self, **kw): |
198 for var in self.variables: | |
199 if var not in kw: | |
27 | 200 import pdb; pdb.set_trace() |
15 | 201 print 'Enter %s: ' % var, |
202 kw[var] = raw_input() | |
203 self.pre(kw) | |
204 return kw | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
205 |
17 | 206 def pre(self, variables): |
207 """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
|
208 |
17 | 209 def filetype(self, _file): |
210 """determine the filetype for a given file""" | |
211 for filetype in self.filetypes: | |
212 match = filetype.match(_file) | |
213 if match: | |
214 return match | |
6
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
215 |
17 | 216 def interpolate_file(self, _file, variables): |
217 | |
218 # if the file is licensed, no need to proceed | |
19 | 219 if self.has_license(_file): |
17 | 220 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
|
221 |
17 | 222 # get the filetype |
223 filetype = self.filetype(_file) | |
224 if not filetype: | |
225 return # you're done | |
10 | 226 |
17 | 227 # get the license |
27 | 228 license = self.interpolate_license(**variables) |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
229 |
17 | 230 # add the license to the file |
231 filetype(license) | |
232 | |
27 | 233 |
234 def interpolate_license(self, **variables): | |
235 license = self.license() | |
236 return Template(license).substitute(**variables) | |
237 | |
17 | 238 def interpolate(self, directory, variables): |
239 for _file in self.files(directory): | |
240 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
|
241 |
17 | 242 def files(self, directory): |
19 | 243 files = [] |
17 | 244 for dirpath, _, filenames in os.walk(directory): |
245 for f in filenames: | |
19 | 246 files.append(os.path.join(dirpath, f)) |
17 | 247 return files |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
248 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
249 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
250 class MPL(License): |
17 | 251 """Mozilla Public License""" |
252 template = 'MPL' # could be implicit here | |
253 variables = [ 'author', 'email' ] | |
254 url = 'http://www.mozilla.org/MPL/boilerplate-1.1/mpl-tri-license-txt' | |
255 | |
256 def license(self): | |
11 | 257 |
17 | 258 if hasattr(self, '_license'): |
259 # return the cached copy | |
260 return self._license | |
26 | 261 |
17 | 262 # get the raw license |
263 raw = urllib2.urlopen(self.url).read() | |
11 | 264 |
17 | 265 # now make it something not crazy |
266 lines = raw.splitlines() | |
267 contributor_index = None | |
268 for index, line in enumerate(lines): | |
269 if line.startswith('The Original Code is'): | |
270 lines[index] = 'The Original Code is mozilla.org code.' | |
271 if line.startswith('_'): | |
272 lines[index] = 'the Mozilla Foundation.' | |
273 if '2___' in line: | |
274 lines[index] = line.replace('2___', '${year}') | |
275 if line.startswith('Contributor'): | |
276 contributor_index = index | |
11 | 277 |
17 | 278 assert contributor_index |
279 lines.insert(contributor_index+1, ' ${author} <${email}>') | |
280 self._license = '\n'.join(lines) | |
281 return self._license | |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
282 |
17 | 283 def pre(self, variables): |
284 variables['year'] = datetime.now().year | |
11 | 285 |