Mercurial > mozilla > hg > licenser
annotate 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 |
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 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
2 |
0 | 3 from datetime import datetime |
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 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
17 def __call__(self, directory, **kw): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
18 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
|
19 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
|
20 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
|
21 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
|
22 self.pre(kw) |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
23 self.interpolate(directory, kw) |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
24 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
25 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
|
26 """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
|
27 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
28 def interpolate(self, directory, variables): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
29 for file in self.files(directory): |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
30 pass |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
31 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
32 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
|
33 """ |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
34 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
|
35 """ |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
36 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
|
37 line = line.strip() |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
38 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
|
39 return False |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
40 return True |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
41 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
42 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
|
43 files = set() |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
44 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
|
45 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
|
46 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
|
47 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
|
48 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
|
49 files.add(path) |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
50 return files |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
51 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
52 |
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
53 class MPL(License): |
0 | 54 """Mozilla Public License""" |
55 template = 'MPL' # could be implicit here | |
56 variables = [ 'author' ] | |
57 | |
58 def pre(self, variables): | |
59 variables['year'] = datetime.now().year |