Mercurial > mozilla > hg > licenser
annotate licenser/main.py @ 6:0a01ed263c06
add ability to interpolate a single file; should move to makeitso
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 13 Dec 2010 13:26:31 -0800 |
parents | f7d485cedc80 |
children | 697b6f892e12 |
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 |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
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/ |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
10 # |
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. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
15 # |
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. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
17 # |
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. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
22 # |
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) |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
25 # |
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. |
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
37 # |
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): | |
49 licenses = {} | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
50 for entry_point in iter_entry_points('licenser.license'): |
0 | 51 try: |
52 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
|
53 except Exception, e: |
0 | 54 if debug: |
55 import pdb; pdb.set_trace() | |
56 else: | |
1
cc5add25bf83
abstract License to its own class and do the work there
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
57 print >> sys.stderr, "Couldn't load license '%s' [%s]" % (entry_point.name, e) |
0 | 58 continue |
59 licenses[entry_point.name] = license | |
60 return licenses | |
61 | |
3 | 62 def print_licenses(licenses): |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
63 for i in sorted(licenses.keys()): |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
64 doc = getattr(licenses[i], '__doc__') |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
65 if doc: |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
66 print '%s: %s' % (i, doc) |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
67 else: |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
68 print i |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
69 |
0 | 70 |
71 def main(args=sys.argv[1:]): | |
72 usage = '%prog [options] directory' | |
73 parser = OptionParser(usage, description=__doc__) | |
3 | 74 # parser.add_option('-d', '--debug', action='store_true', default=False, |
75 # help="debug the application") | |
0 | 76 parser.add_option('-l', '--license', |
77 help="license to use") | |
78 parser.add_option('--list', action='store_true', default=False, | |
79 help="list available licenses") | |
3 | 80 parser.add_option('-p', '--print', dest='_print', |
81 action='store_true', default=False, | |
82 help="print the chosen license") | |
0 | 83 options, args = parser.parse_args(args) |
84 | |
85 licenses = license_list() | |
86 | |
87 if options.list: # list the licenses | |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
88 print_licenses(licenses) |
0 | 89 sys.exit(0) |
90 | |
91 if not options.license: | |
92 parser.error("Must specify --license") | |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
93 if options.license not in licenses: |
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
94 print_licenses(licenses) |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
95 |
2
b8d620fa1116
solidify refactored workflow"
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
96 license = licenses[options.license]() |
3 | 97 |
98 if options._print: | |
99 license.print_license() | |
100 sys.exit(0) | |
5
f7d485cedc80
recursively use licenser on itself; useful and proof of concept
Jeff Hammel <jhammel@mozilla.com>
parents:
3
diff
changeset
|
101 |
3 | 102 variables = license.obtain_variables() |
0 | 103 |
6
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 else: |
0a01ed263c06
add ability to interpolate a single file; should move to makeitso
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
110 raise IOError("File not found: '%s'" % arg) |
0 | 111 |
112 if __name__ == '__main__': | |
113 main() |