view licenser/main.py @ 5:f7d485cedc80

recursively use licenser on itself; useful and proof of concept
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 12:28:59 -0700
parents e700bd2ec289
children 0a01ed263c06
line wrap: on
line source

#!/usr/bin/env python

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
# 
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
# 
# The Original Code is mozilla.org code.
# 
# The Initial Developer of the Original Code is
# Mozilla.org.
# Portions created by the Initial Developer are Copyright (C) 2010
# the Initial Developer. All Rights Reserved.
# 
# Contributor(s):
#     Jeff Hammel <jhammel@mozilla.com>     (Original author)
# 
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
# 
# ***** END LICENSE BLOCK *****
"""
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()