Mercurial > mozilla > hg > licenser
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 |
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 | 2 from datetime import datetime |
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 | 16 def print_license(self): |
17 f = file(self.template) | |
18 print f.read() | |
19 f.close() | |
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 | 37 for _file in self.files(directory): |
38 | |
39 # get the file lines | |
40 f = file(_file) | |
41 lines = [ i.rstrip() for i in f.readlines() ] | |
42 f.close() | |
43 f = file(_file, 'w') | |
44 | |
45 # print shebang if it exists | |
46 if lines[0].startswith('#!'): | |
47 shebang = lines.pop(0) | |
48 print >> f, shebang | |
49 print >> f | |
50 | |
51 # print the license | |
52 g = file(self.template) | |
53 for line in g.readlines(): | |
54 print >> f, '# %s' % line.rstrip() | |
55 g.close() | |
56 | |
57 # print the rest of the file | |
58 for line in lines: | |
59 print >> f, line.rstrip() | |
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 | 84 """Mozilla Public License""" |
85 template = 'MPL' # could be implicit here | |
3 | 86 variables = [ 'author', 'email' ] |
0 | 87 |
88 def pre(self, variables): | |
89 variables['year'] = datetime.now().year |