comparison licenser/licenses.py @ 1:cc5add25bf83

abstract License to its own class and do the work there
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 11:24:02 -0700
parents b0665b243ccd
children b8d620fa1116
comparison
equal deleted inserted replaced
0:b0665b243ccd 1:cc5add25bf83
1 import os
2
1 from datetime import datetime 3 from datetime import datetime
2 4
3 class MPL(object): 5 class License(object):
6 """Abstract base class for a license"""
7
8 variables = [] # required variables
9
10 def __init__(self):
11 if not os.path.isabs(self.template):
12 self.template = os.path.join(os.path.dirname(__file__),
13 'licenses',
14 self.template)
15 assert os.path.exists(self.template)
16
17 def __call__(self, directory, **kw):
18 for var in self.variables:
19 if var not in kw:
20 print 'Enter %s: ' % var,
21 kw[var] = raw_input()
22 self.pre(kw)
23 self.interpolate(directory, kw)
24
25 def pre(self, variables):
26 """do anything that needs to be done before interpolation"""
27
28 def interpolate(self, directory, variables):
29 for file in self.files(directory):
30 pass
31
32 def isempty(self, path):
33 """
34 determines if a python file is empty; that is, contains only comments
35 """
36 for line in file(path, 'r').readlines():
37 line = line.strip()
38 if line and line[0] != '#':
39 return False
40 return True
41
42 def files(self, directory):
43 files = set()
44 for dirpath, _, filenames in os.walk(directory):
45 for f in filenames:
46 if f.endswith('.py'): # could use os.path.splitext()
47 path = os.path.join(dirpath, f)
48 if not self.isempty(path):
49 files.add(path)
50 return files
51
52
53 class MPL(License):
4 """Mozilla Public License""" 54 """Mozilla Public License"""
5 template = 'MPL' # could be implicit here 55 template = 'MPL' # could be implicit here
6 variables = [ 'author' ] 56 variables = [ 'author' ]
7 57
8 def pre(self, variables): 58 def pre(self, variables):