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