Mercurial > mozilla > hg > licenser
annotate licenser/licenses.py @ 5:f7d485cedc80
recursively use licenser on itself; useful and proof of concept
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 10 May 2010 12:28:59 -0700 |
parents | e46374799119 |
children | 0a01ed263c06 |
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 |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
3 # |
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/ |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
8 # |
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. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
13 # |
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. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
15 # |
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. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
20 # |
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) |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
23 # |
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. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
35 # |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
36 # ***** END LICENSE BLOCK ***** |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
37 import os |
0 | 38 from datetime import datetime |
4 | 39 from string import Template |
0 | 40 |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
41 class License(object): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
42 """Abstract base class for a license""" |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
43 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
44 variables = [] # required variables |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
45 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
46 def __init__(self): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
47 if not os.path.isabs(self.template): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
48 self.template = os.path.join(os.path.dirname(__file__), |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
49 'licenses', |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
50 self.template) |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
51 assert os.path.exists(self.template) |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
52 |
3 | 53 def print_license(self): |
54 f = file(self.template) | |
55 print f.read() | |
56 f.close() | |
57 | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
58 def __call__(self, directory, **kw): |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
59 variables = self.obtain_variables(**kw) |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
60 self.interpolate(directory, variables) |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
61 |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
62 def obtain_variables(self, **kw): |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
63 for var in self.variables: |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
64 if var not in kw: |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
65 print 'Enter %s: ' % var, |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
66 kw[var] = raw_input() |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
67 self.pre(kw) |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
68 return kw |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
69 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
70 def pre(self, variables): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
71 """do anything that needs to be done before interpolation""" |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
72 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
73 def interpolate(self, directory, variables): |
3 | 74 for _file in self.files(directory): |
75 | |
76 # get the file lines | |
77 f = file(_file) | |
78 lines = [ i.rstrip() for i in f.readlines() ] | |
79 f.close() | |
80 f = file(_file, 'w') | |
81 | |
82 # print shebang if it exists | |
83 if lines[0].startswith('#!'): | |
84 shebang = lines.pop(0) | |
85 print >> f, shebang | |
86 print >> f | |
87 | |
88 # print the license | |
89 g = file(self.template) | |
90 for line in g.readlines(): | |
4 | 91 line = line.rstrip() |
92 _template = Template(line) | |
93 print >> f, '# %s' % _template.substitute(**variables) | |
3 | 94 g.close() |
95 | |
96 # print the rest of the file | |
97 for line in lines: | |
98 print >> f, line.rstrip() | |
99 f.close() | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
100 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
101 def isempty(self, path): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
102 """ |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
103 determines if a python file is empty; that is, contains only comments |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
104 """ |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
105 for line in file(path, 'r').readlines(): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
106 line = line.strip() |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
107 if line and line[0] != '#': |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
108 return False |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
109 return True |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
110 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
111 def files(self, directory): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
112 files = set() |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
113 for dirpath, _, filenames in os.walk(directory): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
114 for f in filenames: |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
115 if f.endswith('.py'): # could use os.path.splitext() |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
116 path = os.path.join(dirpath, f) |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
117 if not self.isempty(path): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
118 files.add(path) |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
119 return files |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
120 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
121 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
122 class MPL(License): |
0 | 123 """Mozilla Public License""" |
124 template = 'MPL' # could be implicit here | |
3 | 125 variables = [ 'author', 'email' ] |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
4
diff
changeset
|
126 |
0 | 127 def pre(self, variables): |
128 variables['year'] = datetime.now().year |