annotate licenser/licenses.py @ 3:e700bd2ec289

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