view licenser/main.py @ 3:e700bd2ec289

finish basic structure
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 12:17:38 -0700
parents b8d620fa1116
children f7d485cedc80
line wrap: on
line source

#!/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('licenser.license'):
        try:
            license = entry_point.load()
        except Exception, e:
            if debug:
                import pdb; pdb.set_trace()
            else:
                print >> sys.stderr, "Couldn't load license '%s' [%s]" % (entry_point.name, e)
                continue
        licenses[entry_point.name] = license
    return licenses

def print_licenses(licenses):
    for i in sorted(licenses.keys()):
        doc = getattr(licenses[i], '__doc__')
        if doc:
            print '%s: %s' % (i, doc)
        else:
            print i


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,
                      help="list available licenses")
    parser.add_option('-p', '--print', dest='_print',
                      action='store_true', default=False,
                      help="print the chosen license")
    options, args = parser.parse_args(args)

    licenses = license_list()

    if options.list: # list the licenses
        print_licenses(licenses)
        sys.exit(0)

    if not options.license:
        parser.error("Must specify --license")
    if options.license not in licenses:
        print_licenses(licenses)
        
    license = licenses[options.license]()

    if options._print:
        license.print_license()
        sys.exit(0)
    
    variables = license.obtain_variables()

    for directory in args:
        license.interpolate(directory, variables)

if __name__ == '__main__':
    main()