Mercurial > mozilla > hg > licenser
comparison licenser/licenses.py @ 27:7e0c931a201d
web licenser now works
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 25 Nov 2011 00:57:38 -0800 |
parents | 0faf3e7b593a |
children |
comparison
equal
deleted
inserted
replaced
26:0faf3e7b593a | 27:7e0c931a201d |
---|---|
54 return cls(filename) | 54 return cls(filename) |
55 | 55 |
56 def __init__(self, filename): | 56 def __init__(self, filename): |
57 self.filename = filename | 57 self.filename = filename |
58 | 58 |
59 def fileobj(self): | |
60 """return a file object open for writing""" | |
61 if isinstance(self.filename, basestring): | |
62 return file(self.filename, 'w') | |
63 return self.filename | |
64 | |
65 def close(self, _file): | |
66 if isinstance(self.filename, basestring): | |
67 _file.close() | |
68 | |
59 def isempty(self): | 69 def isempty(self): |
60 return not bool(file(self.filename).read().strip) | 70 return not bool(file(self.filename).read().strip) |
61 | 71 |
62 def lines(self): | 72 def lines(self): |
63 if hasattr(self, '_lines'): | 73 if hasattr(self, '_lines'): |
69 | 79 |
70 extensions = set(['c']) | 80 extensions = set(['c']) |
71 | 81 |
72 def __call__(self, license): | 82 def __call__(self, license): |
73 | 83 |
84 lines = self.lines() | |
74 if self.isempty(): | 85 if self.isempty(): |
75 return # you're done | 86 return # you're done |
76 lines = self.lines() | |
77 | 87 |
78 # open the file for writing | 88 # open the file for writing |
79 f = file(self.filename, 'w') | 89 f = self.fileobj() |
80 | 90 |
81 # print the license | 91 # print the license |
82 license_lines = license.splitlines() | 92 license_lines = license.splitlines() |
83 for index, line in enumerate(license_lines): | 93 for index, line in enumerate(license_lines): |
84 prefix = ' *' | 94 prefix = ' *' |
91 print >> f | 101 print >> f |
92 | 102 |
93 # print the rest of the file | 103 # print the rest of the file |
94 for line in lines: | 104 for line in lines: |
95 f.write(line) | 105 f.write(line) |
96 f.close() | 106 self.close(f) |
97 | 107 |
98 | 108 |
99 class HashCommentsFile(CommentedFile): | 109 class HashCommentsFile(CommentedFile): |
100 | 110 |
101 extensions = set(['py', 'sh']) | 111 extensions = set(['py', 'sh']) |
102 | 112 |
103 def __call__(self, license): | 113 def __call__(self, license): |
104 """interpolate the file""" | 114 """interpolate the file""" |
105 | 115 |
116 lines = self.lines() | |
106 if self.isempty(): | 117 if self.isempty(): |
107 return # you're done | 118 return # you're done |
108 lines = self.lines() | |
109 | 119 |
110 # open the file for writing | 120 # open the file for writing |
111 f = file(self.filename, 'w') | 121 f = self.fileobj() |
112 | 122 |
113 # print shebang if it exists | 123 # print shebang if it exists |
114 if lines[0].startswith('#!'): | 124 if lines[0].startswith('#!'): |
115 shebang = lines.pop(0).strip() | 125 shebang = lines.pop(0).strip() |
116 print >> f, shebang | 126 print >> f, shebang |
121 print >> f, '# %s' % line | 131 print >> f, '# %s' % line |
122 | 132 |
123 # print the rest of the file | 133 # print the rest of the file |
124 for line in lines: | 134 for line in lines: |
125 f.write(line) | 135 f.write(line) |
126 f.close() | 136 self.close(f) |
127 | 137 |
128 def isempty(self): | 138 def isempty(self): |
129 """ | 139 """ |
130 determines if a file is empty; that is, contains only comments | 140 determines if a file is empty; that is, contains only comments |
131 """ | 141 """ |
169 return self.license().splitlines()[0].strip() | 179 return self.license().splitlines()[0].strip() |
170 | 180 |
171 def has_license(self, filename): | 181 def has_license(self, filename): |
172 """does the file already have a license?""" | 182 """does the file already have a license?""" |
173 token = self.token() | 183 token = self.token() |
174 for line in file(filename).readlines(): | 184 if isinstance(filename, basestring): |
185 f = file(filename) | |
186 else: | |
187 f = filename | |
188 for line in f.readlines(): | |
175 if token in line: | 189 if token in line: |
176 return True | 190 return True |
177 return False | 191 return False |
178 | 192 |
179 def __call__(self, directory, **kw): | 193 def __call__(self, directory, **kw): |
181 self.interpolate(directory, variables) | 195 self.interpolate(directory, variables) |
182 | 196 |
183 def obtain_variables(self, **kw): | 197 def obtain_variables(self, **kw): |
184 for var in self.variables: | 198 for var in self.variables: |
185 if var not in kw: | 199 if var not in kw: |
200 import pdb; pdb.set_trace() | |
186 print 'Enter %s: ' % var, | 201 print 'Enter %s: ' % var, |
187 kw[var] = raw_input() | 202 kw[var] = raw_input() |
188 self.pre(kw) | 203 self.pre(kw) |
189 return kw | 204 return kw |
190 | 205 |
208 filetype = self.filetype(_file) | 223 filetype = self.filetype(_file) |
209 if not filetype: | 224 if not filetype: |
210 return # you're done | 225 return # you're done |
211 | 226 |
212 # get the license | 227 # get the license |
213 # XXX should be cached | 228 license = self.interpolate_license(**variables) |
214 license = self.license() | |
215 license = Template(license).substitute(**variables) | |
216 | 229 |
217 # add the license to the file | 230 # add the license to the file |
218 filetype(license) | 231 filetype(license) |
232 | |
233 | |
234 def interpolate_license(self, **variables): | |
235 license = self.license() | |
236 return Template(license).substitute(**variables) | |
219 | 237 |
220 def interpolate(self, directory, variables): | 238 def interpolate(self, directory, variables): |
221 for _file in self.files(directory): | 239 for _file in self.files(directory): |
222 self.interpolate_file(_file, variables) | 240 self.interpolate_file(_file, variables) |
223 | 241 |