diff licenser/main.py @ 0:b0665b243ccd

initial import of licenser
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 09:18:26 -0700
parents
children cc5add25bf83
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/licenser/main.py	Mon May 10 09:18:26 2010 -0700
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+"""
+adds licenses to python packages
+"""
+
+import os
+import sys
+from optparse import OptionParser
+from pkg_resources import iter_entry_points
+
+def license_list(debug=False):
+    licenses = {}
+    for entry_point in iter_entry_points():
+        try:
+            license = entry_point.load()
+        except:
+            if debug:
+                import pdb; pdb.set_trace()
+            else:
+                print >> sys.stderr, "Couldn't load license '%s'" % entry_point.name
+                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('-l', '--license',
+                      help="license to use")
+    parser.add_option('--list', action='store_true', default=False,
+                      help="list available licenses")
+    options, args = parser.parse_args(args)
+
+    licenses = license_list()
+
+    if options.list: # list the licenses
+        for i in licenses:
+            pass
+        sys.exit(0)
+
+    if not options.license:
+        parser.error("Must specify --license")
+
+    for directory in args:
+        filelist = files(directory)
+
+if __name__ == '__main__':
+    main()