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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 from datetime import datetime
4
e46374799119 do the interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
39 from string import Template
0
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
53 def print_license(self):
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
54 f = file(self.template)
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
55 print f.read()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
56 f.close()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
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
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
74 for _file in self.files(directory):
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
75
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
76 # get the file lines
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
77 f = file(_file)
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
78 lines = [ i.rstrip() for i in f.readlines() ]
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
79 f.close()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
80 f = file(_file, 'w')
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
81
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
82 # print shebang if it exists
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
83 if lines[0].startswith('#!'):
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
84 shebang = lines.pop(0)
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
85 print >> f, shebang
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
86 print >> f
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
87
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
88 # print the license
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
89 g = file(self.template)
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
90 for line in g.readlines():
4
e46374799119 do the interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
91 line = line.rstrip()
e46374799119 do the interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
92 _template = Template(line)
e46374799119 do the interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
93 print >> f, '# %s' % _template.substitute(**variables)
3
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
94 g.close()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
95
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
96 # print the rest of the file
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
97 for line in lines:
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
98 print >> f, line.rstrip()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
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
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
123 """Mozilla Public License"""
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
124 template = 'MPL' # could be implicit here
3
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
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
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
127 def pre(self, variables):
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
128 variables['year'] = datetime.now().year