comparison licenser/main.py @ 1:cc5add25bf83

abstract License to its own class and do the work there
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 11:24:02 -0700
parents b0665b243ccd
children b8d620fa1116
comparison
equal deleted inserted replaced
0:b0665b243ccd 1:cc5add25bf83
8 from optparse import OptionParser 8 from optparse import OptionParser
9 from pkg_resources import iter_entry_points 9 from pkg_resources import iter_entry_points
10 10
11 def license_list(debug=False): 11 def license_list(debug=False):
12 licenses = {} 12 licenses = {}
13 for entry_point in iter_entry_points(): 13 for entry_point in iter_entry_points('licenser.license'):
14 try: 14 try:
15 license = entry_point.load() 15 license = entry_point.load()
16 except: 16 except Exception, e:
17 if debug: 17 if debug:
18 import pdb; pdb.set_trace() 18 import pdb; pdb.set_trace()
19 else: 19 else:
20 print >> sys.stderr, "Couldn't load license '%s'" % entry_point.name 20 print >> sys.stderr, "Couldn't load license '%s' [%s]" % (entry_point.name, e)
21 continue 21 continue
22 licenses[entry_point.name] = license 22 licenses[entry_point.name] = license
23 return licenses 23 return licenses
24 24
25 def isempty(path):
26 """
27 determines if a python file is empty; that is, contains only comments
28 """
29 for line in file(path, 'r').readlines():
30 line = line.strip()
31 if line and line[0] != '#':
32 return False
33 return True
34
35 def files(directory):
36 files = set()
37 for dirpath, _, filenames in os.walk(directory):
38 for f in filenames:
39 if f.endswith('.py'): # could use os.path.splitext()
40 path = os.path.join(dirpath, f)
41 if not isempty(path):
42 files.add(path)
43 return files
44 25
45 def main(args=sys.argv[1:]): 26 def main(args=sys.argv[1:]):
46 usage = '%prog [options] directory' 27 usage = '%prog [options] directory'
47 parser = OptionParser(usage, description=__doc__) 28 parser = OptionParser(usage, description=__doc__)
29 parser.add_option('-d', '--debug', action='store_true', default=False,
30 help="debug the application")
48 parser.add_option('-l', '--license', 31 parser.add_option('-l', '--license',
49 help="license to use") 32 help="license to use")
50 parser.add_option('--list', action='store_true', default=False, 33 parser.add_option('--list', action='store_true', default=False,
51 help="list available licenses") 34 help="list available licenses")
52 options, args = parser.parse_args(args) 35 options, args = parser.parse_args(args)
53 36
54 licenses = license_list() 37 licenses = license_list()
55 38
56 if options.list: # list the licenses 39 if options.list: # list the licenses
57 for i in licenses: 40 for i in sorted(licenses.keys()):
58 pass 41 doc = getattr(licenses[i], '__doc__')
42 if doc:
43 print '%s: %s' % (i, doc)
44 else:
45 print i
59 sys.exit(0) 46 sys.exit(0)
60 47
61 if not options.license: 48 if not options.license:
62 parser.error("Must specify --license") 49 parser.error("Must specify --license")
63 50