Mercurial > mozilla > hg > licenser
changeset 27:7e0c931a201d
web licenser now works
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 25 Nov 2011 00:57:38 -0800 |
parents | 0faf3e7b593a |
children | eb3749dbdbf5 |
files | licenser/licenses.py licenser/web.py |
diffstat | 2 files changed, 66 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/licenser/licenses.py Fri Nov 25 00:08:34 2011 -0800 +++ b/licenser/licenses.py Fri Nov 25 00:57:38 2011 -0800 @@ -56,6 +56,16 @@ def __init__(self, filename): self.filename = filename + def fileobj(self): + """return a file object open for writing""" + if isinstance(self.filename, basestring): + return file(self.filename, 'w') + return self.filename + + def close(self, _file): + if isinstance(self.filename, basestring): + _file.close() + def isempty(self): return not bool(file(self.filename).read().strip) @@ -71,12 +81,12 @@ def __call__(self, license): + lines = self.lines() if self.isempty(): return # you're done - lines = self.lines() # open the file for writing - f = file(self.filename, 'w') + f = self.fileobj() # print the license license_lines = license.splitlines() @@ -93,7 +103,7 @@ # print the rest of the file for line in lines: f.write(line) - f.close() + self.close(f) class HashCommentsFile(CommentedFile): @@ -103,12 +113,12 @@ def __call__(self, license): """interpolate the file""" + lines = self.lines() if self.isempty(): return # you're done - lines = self.lines() # open the file for writing - f = file(self.filename, 'w') + f = self.fileobj() # print shebang if it exists if lines[0].startswith('#!'): @@ -123,7 +133,7 @@ # print the rest of the file for line in lines: f.write(line) - f.close() + self.close(f) def isempty(self): """ @@ -171,7 +181,11 @@ def has_license(self, filename): """does the file already have a license?""" token = self.token() - for line in file(filename).readlines(): + if isinstance(filename, basestring): + f = file(filename) + else: + f = filename + for line in f.readlines(): if token in line: return True return False @@ -183,6 +197,7 @@ def obtain_variables(self, **kw): for var in self.variables: if var not in kw: + import pdb; pdb.set_trace() print 'Enter %s: ' % var, kw[var] = raw_input() self.pre(kw) @@ -210,13 +225,16 @@ return # you're done # get the license - # XXX should be cached - license = self.license() - license = Template(license).substitute(**variables) + license = self.interpolate_license(**variables) # add the license to the file filetype(license) + + def interpolate_license(self, **variables): + license = self.license() + return Template(license).substitute(**variables) + def interpolate(self, directory, variables): for _file in self.files(directory): self.interpolate_file(_file, variables)
--- a/licenser/web.py Fri Nov 25 00:08:34 2011 -0800 +++ b/licenser/web.py Fri Nov 25 00:57:38 2011 -0800 @@ -6,11 +6,12 @@ import licenses import tempfile +from StringIO import StringIO from webob import Request, Response, exc class Handler(object): - required = set(['name', 'email', 'url']) + required = set(['author', 'email', 'url']) template = """<html> <head><title>MPL licenser</title></head> @@ -20,13 +21,14 @@ <div><form method="POST" enctype="multipart/form-data"> <p>Enter a file: <input type="file" name="file"/></p> <p>or a URL: <input type="text" name="url" value="%(url)s"/></p> -<p>And enter your name: <input type="text" name="name" value="%(name)s"/></p> +<p>And enter your name: <input type="text" name="author" value="%(author)s"/></p> <p>and email: <input type="text" name="email" value="%(email)s"/></p> <input type="submit"/> </body> </html>""" def __init__(self, **kw): + self.license = licenses.MPL() self.methods = {'GET': self.GET, 'POST': self.POST} @@ -58,16 +60,42 @@ return Response(content_type='text/html', body=(self.template % variables)) def POST(self, request): - pass + variables = dict([(i, request.params[i]) + for i in self.required + if i in request.params]) + if 'file' in request.POST and hasattr(request.POST['file'], 'filename'): + body = request.POST['file'].file.getvalue() + filename = request.POST['file'].filename + elif 'url' in request.POST: + try: + pass + except: + return self.render(**variables) + if not set(['author', 'email']).issubset(request.params): + return self.render(**variables) + author = request.params['author'] + email = request.params['email'] + retval = self.interpolate(filename, body, author, email) + return Response(content_type='text/plain', body=retval) - def interpolate(self, filename, body, name, email): - fd, tf = tempfile.mkstemp() - try: - os.write(fd, body) - os.close(fd) - finally: - os.remove(tf) + def interpolate(self, filename, body, author, email): + buffer = StringIO() + buffer.write(body) + value = buffer.getvalue().splitlines() + if self.license.has_license(buffer): + return body + filetype = self.license.filetype(filename) + if not filetype: + return body + variables = self.license.obtain_variables(author=author, email=email) + license = self.license.interpolate_license(**variables) + value = [i + '\r\n' for i in value] + filetype._lines = value + buffer = StringIO() + filetype.filename = buffer + filetype(license) + return buffer.getvalue() if __name__ == '__main__': from wsgiref import simple_server