Mercurial > mozilla > hg > licenser
comparison licenser/licenses.py @ 15:cf920f85fb98
more stubbing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 24 Nov 2011 12:58:32 -0800 |
parents | ed9d64d1fd32 |
children | 4566b3ac9838 |
comparison
equal
deleted
inserted
replaced
14:ed9d64d1fd32 | 15:cf920f85fb98 |
---|---|
46 pass | 46 pass |
47 | 47 |
48 def hash_comments(): | 48 def hash_comments(): |
49 pass | 49 pass |
50 | 50 |
51 type = {'py': hash_comments, | 51 suffixes = {'py': hash_comments, |
52 'sh': hash_comments, | 52 'sh': hash_comments, |
53 'c': asterisk_comments} | 53 'c': asterisk_comments} |
54 | 54 |
55 class License(object): | 55 class License(object): |
56 """Abstract base class for a license""" | 56 """Abstract base class for a license""" |
57 | 57 |
58 variables = [] # required variables | 58 variables = [] # required variables |
59 | 59 |
60 def __init__(self): | 60 def __init__(self): |
61 if self.template and not os.path.isabs(self.template): | 61 if self.template: |
62 self.template = os.path.join(os.path.dirname(__file__), | 62 if not os.path.isabs(self.template): |
63 'licenses', | 63 self.template = os.path.join(os.path.dirname(__file__), |
64 self.template) | 64 'licenses', |
65 assert os.path.exists(self.template) | 65 self.template) |
66 assert os.path.exists(self.template) | |
67 | |
68 def license(self): | |
69 return file(self.template).read() | |
66 | 70 |
67 def license(self): | 71 def print_license(self): |
68 return file(self.template).read() | 72 print self.license() |
69 | 73 |
70 def print_license(self): | 74 def token(self): |
71 print self.license() | 75 """a token indicative of the license, by default the first line""" |
76 return self.license.splitlines()[0].strip() | |
72 | 77 |
73 def has_license(filename): | 78 def has_license(self, filename): |
74 """does the file already have a license?""" | 79 """does the file already have a license?""" |
75 raise NotImplementedError | 80 token = self.token() |
81 for line in file(filename).readlines(): | |
82 if token in line: | |
83 return True | |
84 return False | |
76 | 85 |
77 def __call__(self, directory, **kw): | 86 def __call__(self, directory, **kw): |
78 variables = self.obtain_variables(**kw) | 87 variables = self.obtain_variables(**kw) |
79 self.interpolate(directory, variables) | 88 self.interpolate(directory, variables) |
80 | 89 |
81 def obtain_variables(self, **kw): | 90 def obtain_variables(self, **kw): |
82 for var in self.variables: | 91 for var in self.variables: |
83 if var not in kw: | 92 if var not in kw: |
84 print 'Enter %s: ' % var, | 93 print 'Enter %s: ' % var, |
85 kw[var] = raw_input() | 94 kw[var] = raw_input() |
86 self.pre(kw) | 95 self.pre(kw) |
87 return kw | 96 return kw |
88 | 97 |
89 def pre(self, variables): | 98 def pre(self, variables): |
90 """do anything that needs to be done before interpolation""" | 99 """do anything that needs to be done with the variables before interpolation""" |
91 | 100 |
92 def interpolate_file(self, _file, variables): | 101 def interpolate_file(self, _file, variables): |
93 | 102 |
94 # get the file lines | 103 # get the file lines |
95 f = file(_file) | 104 f = file(_file) |
96 lines = [ i.rstrip() for i in f.readlines() ] | 105 lines = [ i.rstrip() for i in f.readlines() ] |
97 f.close() | 106 f.close() |
107 | |
108 # open the file for writing | |
109 f = file(_file, 'w') | |
98 | 110 |
99 # open the file for writing | 111 # print shebang if it exists |
100 f = file(_file, 'w') | 112 if lines[0].startswith('#!'): |
113 shebang = lines.pop(0) | |
114 print >> f, shebang | |
115 print >> f | |
101 | 116 |
102 # print shebang if it exists | 117 # print the license |
103 if lines[0].startswith('#!'): | 118 g = self.license() |
104 shebang = lines.pop(0) | 119 for line in g.splitlines(): |
105 print >> f, shebang | 120 line = line.rstrip() |
106 print >> f | 121 _template = Template(line) |
122 print >> f, '# %s' % _template.substitute(**variables) | |
123 g.close() | |
107 | 124 |
108 # print the license | 125 # print the rest of the file |
109 g = self.license() | 126 for line in lines: |
110 for line in g.splitlines(): | 127 print >> f, line.rstrip() |
111 line = line.rstrip() | |
112 _template = Template(line) | |
113 print >> f, '# %s' % _template.substitute(**variables) | |
114 g.close() | |
115 | |
116 # print the rest of the file | |
117 for line in lines: | |
118 print >> f, line.rstrip() | |
119 f.close() | 128 f.close() |
120 | 129 |
121 def interpolate(self, directory, variables): | 130 def interpolate(self, directory, variables): |
122 for _file in self.files(directory): | 131 for _file in self.files(directory): |
123 self.interpolate_file(_file, variables) | 132 self.interpolate_file(_file, variables) |