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