Mercurial > mozilla > hg > licenser
annotate licenser/main.py @ 28:eb3749dbdbf5 default tip
finish web handler
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 25 Nov 2011 17:30:28 -0800 |
parents | 0b51868bdfb9 |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
2 |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
3 # ***** BEGIN LICENSE BLOCK ***** |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
4 # Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
12
697b6f892e12
get this working in basic form
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
5 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
6 # The contents of this file are subject to the Mozilla Public License Version |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
7 # 1.1 (the "License"); you may not use this file except in compliance with |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
8 # the License. You may obtain a copy of the License at |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
9 # http://www.mozilla.org/MPL/ |
12
697b6f892e12
get this working in basic form
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
10 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
11 # Software distributed under the License is distributed on an "AS IS" basis, |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
13 # for the specific language governing rights and limitations under the |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
14 # License. |
12
697b6f892e12
get this working in basic form
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
15 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
16 # The Original Code is mozilla.org code. |
12
697b6f892e12
get this working in basic form
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
17 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
18 # The Initial Developer of the Original Code is |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
19 # Mozilla.org. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
20 # Portions created by the Initial Developer are Copyright (C) 2010 |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
21 # the Initial Developer. All Rights Reserved. |
12
697b6f892e12
get this working in basic form
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
22 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
23 # Contributor(s): |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
24 # Jeff Hammel <jhammel@mozilla.com> (Original author) |
12
697b6f892e12
get this working in basic form
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
25 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
26 # Alternatively, the contents of this file may be used under the terms of |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
27 # either of the GNU General Public License Version 2 or later (the "GPL"), |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
28 # or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
29 # in which case the provisions of the GPL or the LGPL are applicable instead |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
30 # of those above. If you wish to allow use of your version of this file only |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
31 # under the terms of either the GPL or the LGPL, and not to allow others to |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
32 # use your version of this file under the terms of the MPL, indicate your |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
33 # decision by deleting the provisions above and replace them with the notice |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
34 # and other provisions required by the GPL or the LGPL. If you do not delete |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
35 # the provisions above, a recipient may use your version of this file under |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
36 # the terms of any one of the MPL, the GPL or the LGPL. |
12
697b6f892e12
get this working in basic form
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
37 # |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
38 # ***** END LICENSE BLOCK ***** |
0 | 39 """ |
40 adds licenses to python packages | |
41 """ | |
42 | |
43 import os | |
44 import sys | |
45 from optparse import OptionParser | |
46 from pkg_resources import iter_entry_points | |
47 | |
48 def license_list(debug=False): | |
16 | 49 """get list of licenses""" |
50 | |
0 | 51 licenses = {} |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
52 for entry_point in iter_entry_points('licenser.license'): |
0 | 53 try: |
54 license = entry_point.load() | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
55 except Exception, e: |
0 | 56 if debug: |
57 import pdb; pdb.set_trace() | |
58 else: | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
59 print >> sys.stderr, "Couldn't load license '%s' [%s]" % (entry_point.name, e) |
0 | 60 continue |
61 licenses[entry_point.name] = license | |
62 return licenses | |
63 | |
3 | 64 def print_licenses(licenses): |
16 | 65 """print licenses to stdout""" |
66 | |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
67 for i in sorted(licenses.keys()): |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
68 doc = getattr(licenses[i], '__doc__') |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
69 if doc: |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
70 print '%s: %s' % (i, doc) |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
71 else: |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
72 print i |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
73 |
0 | 74 |
75 def main(args=sys.argv[1:]): | |
16 | 76 |
77 # parse command line options | |
0 | 78 usage = '%prog [options] directory' |
79 parser = OptionParser(usage, description=__doc__) | |
12
697b6f892e12
get this working in basic form
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
80 parser.add_option('-l', '--license', dest='license', default='MPL', |
0 | 81 help="license to use") |
82 parser.add_option('--list', action='store_true', default=False, | |
83 help="list available licenses") | |
3 | 84 parser.add_option('-p', '--print', dest='_print', |
85 action='store_true', default=False, | |
86 help="print the chosen license") | |
0 | 87 options, args = parser.parse_args(args) |
88 | |
16 | 89 # get the licenses |
0 | 90 licenses = license_list() |
91 | |
92 if options.list: # list the licenses | |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
93 print_licenses(licenses) |
0 | 94 sys.exit(0) |
95 | |
16 | 96 # sanity check |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
97 if options.license not in licenses: |
16 | 98 print "License '%s' not found" |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
99 print_licenses(licenses) |
16 | 100 parser.exit(1) |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
101 |
16 | 102 # get the license of choice |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
103 license = licenses[options.license]() |
3 | 104 |
105 if options._print: | |
16 | 106 # print the license if specified |
3 | 107 license.print_license() |
108 sys.exit(0) | |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
109 |
16 | 110 # obtain the variables |
3 | 111 variables = license.obtain_variables() |
0 | 112 |
16 | 113 # interpolate paths |
6
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
114 for arg in args: |
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
115 if os.path.isdir(arg): |
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
116 license.interpolate(arg, variables) |
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
117 elif os.path.isfile(arg): |
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
118 license.interpolate_file(arg, variables) |
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
119 else: |
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
120 raise IOError("File not found: '%s'" % arg) |
0 | 121 |
122 if __name__ == '__main__': | |
123 main() |