# HG changeset patch # User Jeff Hammel # Date 1322211458 28800 # Node ID 7e0c931a201dfdd850a4c2d66eb4a3b7fa4ce13e # Parent 0faf3e7b593a5cd79be23e1cbdfb4f8e3f2f544f web licenser now works diff -r 0faf3e7b593a -r 7e0c931a201d licenser/licenses.py --- 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) diff -r 0faf3e7b593a -r 7e0c931a201d licenser/web.py --- 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 = """ MPL licenser @@ -20,13 +21,14 @@

Enter a file:

or a URL:

-

And enter your name:

+

And enter your name:

and email:

""" 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