Mercurial > mozilla > hg > licenser
annotate licenser/licenses.py @ 26:0faf3e7b593a
more stubbing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 25 Nov 2011 00:08:34 -0800 |
parents | 8729685a2cf6 |
children | 7e0c931a201d |
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() |
25 | 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 |
25 | 141 def filetypes_string(filetypes=filetypes[:]): |
142 extensions = set() | |
143 for filetype in filetypes: | |
144 extensions.update(filetype.extensions) | |
145 return ', '.join([i for i in extensions]) | |
146 | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
147 class License(object): |
15 | 148 """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
|
149 |
15 | 150 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
|
151 |
18
c04a53145f19
interpolate the files, i hope
Jeff Hammel <jhammel@mozilla.com>
parents:
17
diff
changeset
|
152 def __init__(self, filetypes=filetypes[:]): |
15 | 153 if self.template: |
154 if not os.path.isabs(self.template): | |
155 self.template = os.path.join(os.path.dirname(__file__), | |
156 'licenses', | |
157 self.template) | |
158 assert os.path.exists(self.template) | |
17 | 159 self.filetypes = filetypes |
25 | 160 |
15 | 161 def license(self): |
162 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
|
163 |
15 | 164 def print_license(self): |
165 print self.license() | |
11 | 166 |
15 | 167 def token(self): |
168 """a token indicative of the license, by default the first line""" | |
19 | 169 return self.license().splitlines()[0].strip() |
3 | 170 |
15 | 171 def has_license(self, filename): |
172 """does the file already have a license?""" | |
173 token = self.token() | |
174 for line in file(filename).readlines(): | |
175 if token in line: | |
176 return True | |
177 return False | |
14 | 178 |
15 | 179 def __call__(self, directory, **kw): |
180 variables = self.obtain_variables(**kw) | |
181 self.interpolate(directory, variables) | |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
182 |
15 | 183 def obtain_variables(self, **kw): |
184 for var in self.variables: | |
185 if var not in kw: | |
186 print 'Enter %s: ' % var, | |
187 kw[var] = raw_input() | |
188 self.pre(kw) | |
189 return kw | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
190 |
17 | 191 def pre(self, variables): |
192 """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
|
193 |
17 | 194 def filetype(self, _file): |
195 """determine the filetype for a given file""" | |
196 for filetype in self.filetypes: | |
197 match = filetype.match(_file) | |
198 if match: | |
199 return match | |
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 def interpolate_file(self, _file, variables): |
202 | |
203 # if the file is licensed, no need to proceed | |
19 | 204 if self.has_license(_file): |
17 | 205 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
|
206 |
17 | 207 # get the filetype |
208 filetype = self.filetype(_file) | |
209 if not filetype: | |
210 return # you're done | |
10 | 211 |
17 | 212 # get the license |
213 # XXX should be cached | |
214 license = self.license() | |
215 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
|
216 |
17 | 217 # add the license to the file |
218 filetype(license) | |
219 | |
220 def interpolate(self, directory, variables): | |
221 for _file in self.files(directory): | |
222 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
|
223 |
17 | 224 def files(self, directory): |
19 | 225 files = [] |
17 | 226 for dirpath, _, filenames in os.walk(directory): |
227 for f in filenames: | |
19 | 228 files.append(os.path.join(dirpath, f)) |
17 | 229 return files |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
230 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
231 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
232 class MPL(License): |
17 | 233 """Mozilla Public License""" |
234 template = 'MPL' # could be implicit here | |
235 variables = [ 'author', 'email' ] | |
236 url = 'http://www.mozilla.org/MPL/boilerplate-1.1/mpl-tri-license-txt' | |
237 | |
238 def license(self): | |
11 | 239 |
17 | 240 if hasattr(self, '_license'): |
241 # return the cached copy | |
242 return self._license | |
26 | 243 |
17 | 244 # get the raw license |
245 raw = urllib2.urlopen(self.url).read() | |
11 | 246 |
17 | 247 # now make it something not crazy |
248 lines = raw.splitlines() | |
249 contributor_index = None | |
250 for index, line in enumerate(lines): | |
251 if line.startswith('The Original Code is'): | |
252 lines[index] = 'The Original Code is mozilla.org code.' | |
253 if line.startswith('_'): | |
254 lines[index] = 'the Mozilla Foundation.' | |
255 if '2___' in line: | |
256 lines[index] = line.replace('2___', '${year}') | |
257 if line.startswith('Contributor'): | |
258 contributor_index = index | |
11 | 259 |
17 | 260 assert contributor_index |
261 lines.insert(contributor_index+1, ' ${author} <${email}>') | |
262 self._license = '\n'.join(lines) | |
263 return self._license | |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
264 |
17 | 265 def pre(self, variables): |
266 variables['year'] = datetime.now().year | |
11 | 267 |