# HG changeset patch # User Jeff Hammel # Date 1273515842 25200 # Node ID cc5add25bf83891812d50aef72eb39f1d82c0ad4 # Parent b0665b243ccd531fc5182ba5c7fa27d7c89c5046 abstract License to its own class and do the work there diff -r b0665b243ccd -r cc5add25bf83 licenser/licenses.py --- a/licenser/licenses.py Mon May 10 09:18:26 2010 -0700 +++ b/licenser/licenses.py Mon May 10 11:24:02 2010 -0700 @@ -1,6 +1,56 @@ +import os + from datetime import datetime -class MPL(object): +class License(object): + """Abstract base class for a license""" + + variables = [] # required variables + + def __init__(self): + if not os.path.isabs(self.template): + self.template = os.path.join(os.path.dirname(__file__), + 'licenses', + self.template) + assert os.path.exists(self.template) + + def __call__(self, directory, **kw): + for var in self.variables: + if var not in kw: + print 'Enter %s: ' % var, + kw[var] = raw_input() + self.pre(kw) + self.interpolate(directory, kw) + + def pre(self, variables): + """do anything that needs to be done before interpolation""" + + def interpolate(self, directory, variables): + for file in self.files(directory): + pass + + def isempty(self, path): + """ + determines if a python file is empty; that is, contains only comments + """ + for line in file(path, 'r').readlines(): + line = line.strip() + if line and line[0] != '#': + return False + return True + + def files(self, directory): + files = set() + for dirpath, _, filenames in os.walk(directory): + for f in filenames: + if f.endswith('.py'): # could use os.path.splitext() + path = os.path.join(dirpath, f) + if not self.isempty(path): + files.add(path) + return files + + +class MPL(License): """Mozilla Public License""" template = 'MPL' # could be implicit here variables = [ 'author' ] diff -r b0665b243ccd -r cc5add25bf83 licenser/main.py --- a/licenser/main.py Mon May 10 09:18:26 2010 -0700 +++ b/licenser/main.py Mon May 10 11:24:02 2010 -0700 @@ -10,41 +10,24 @@ def license_list(debug=False): licenses = {} - for entry_point in iter_entry_points(): + for entry_point in iter_entry_points('licenser.license'): try: license = entry_point.load() - except: + except Exception, e: if debug: import pdb; pdb.set_trace() else: - print >> sys.stderr, "Couldn't load license '%s'" % entry_point.name + print >> sys.stderr, "Couldn't load license '%s' [%s]" % (entry_point.name, e) continue licenses[entry_point.name] = license return licenses -def isempty(path): - """ - determines if a python file is empty; that is, contains only comments - """ - for line in file(path, 'r').readlines(): - line = line.strip() - if line and line[0] != '#': - return False - return True - -def files(directory): - files = set() - for dirpath, _, filenames in os.walk(directory): - for f in filenames: - if f.endswith('.py'): # could use os.path.splitext() - path = os.path.join(dirpath, f) - if not isempty(path): - files.add(path) - return files def main(args=sys.argv[1:]): usage = '%prog [options] directory' parser = OptionParser(usage, description=__doc__) + parser.add_option('-d', '--debug', action='store_true', default=False, + help="debug the application") parser.add_option('-l', '--license', help="license to use") parser.add_option('--list', action='store_true', default=False, @@ -54,8 +37,12 @@ licenses = license_list() if options.list: # list the licenses - for i in licenses: - pass + for i in sorted(licenses.keys()): + doc = getattr(licenses[i], '__doc__') + if doc: + print '%s: %s' % (i, doc) + else: + print i sys.exit(0) if not options.license: