annotate licenser/licenses.py @ 4:e46374799119

do the interpolation
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 12:27:00 -0700
parents e700bd2ec289
children f7d485cedc80
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
1 import os
0
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 from datetime import datetime
4
e46374799119 do the interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
3 from string import Template
0
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4
1
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
5 class License(object):
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
6 """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
7
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
8 variables = [] # required variables
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
9
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
10 def __init__(self):
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
11 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
12 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
13 'licenses',
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
14 self.template)
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
15 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
16
3
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
17 def print_license(self):
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
18 f = file(self.template)
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
19 print f.read()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
20 f.close()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
21
1
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
22 def __call__(self, directory, **kw):
2
b8d620fa1116 solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
23 variables = self.obtain_variables(**kw)
b8d620fa1116 solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
24 self.interpolate(directory, variables)
b8d620fa1116 solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
25
b8d620fa1116 solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
26 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
27 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
28 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
29 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
30 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
31 self.pre(kw)
2
b8d620fa1116 solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
32 return kw
1
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
33
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
34 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
35 """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
36
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
37 def interpolate(self, directory, variables):
3
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
38 for _file in self.files(directory):
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
39
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
40 # get the file lines
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
41 f = file(_file)
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
42 lines = [ i.rstrip() for i in f.readlines() ]
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
43 f.close()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
44 f = file(_file, 'w')
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
45
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
46 # print shebang if it exists
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
47 if lines[0].startswith('#!'):
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
48 shebang = lines.pop(0)
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
49 print >> f, shebang
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
50 print >> f
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
51
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
52 # print the license
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
53 g = file(self.template)
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
54 for line in g.readlines():
4
e46374799119 do the interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
55 line = line.rstrip()
e46374799119 do the interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
56 _template = Template(line)
e46374799119 do the interpolation
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
57 print >> f, '# %s' % _template.substitute(**variables)
3
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
58 g.close()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
59
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
60 # print the rest of the file
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
61 for line in lines:
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
62 print >> f, line.rstrip()
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
63 f.close()
1
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
64
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
65 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
66 """
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
67 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
68 """
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
69 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
70 line = line.strip()
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
71 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
72 return False
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
73 return True
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
74
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
75 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
76 files = set()
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
77 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
78 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
79 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
80 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
81 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
82 files.add(path)
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
83 return files
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
84
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
85
cc5add25bf83 abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
86 class MPL(License):
0
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 """Mozilla Public License"""
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
88 template = 'MPL' # could be implicit here
3
e700bd2ec289 finish basic structure
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
89 variables = [ 'author', 'email' ]
0
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 def pre(self, variables):
b0665b243ccd initial import of licenser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 variables['year'] = datetime.now().year