OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Build the NOTICE file distributed with the NaCl SDK from a set of given | |
7 license files.""" | |
8 | |
9 import optparse | |
10 import os | |
11 import sys | |
12 | |
13 | |
14 def Trace(msg): | |
15 if Trace.verbose: | |
16 print msg | |
17 | |
18 | |
19 def FindFiles(files): | |
20 found = [f for f in files if os.path.exists(f)] | |
21 | |
22 if Trace.verbose: | |
23 for f in sorted(set(files) - set(found)): | |
24 Trace('Skipping %s. File doesn\'t exist.\n' % (f,)) | |
25 | |
26 return found | |
27 | |
28 | |
29 def CreateLicenseDict(files): | |
30 # Many of the license files are duplicates. Create a map of license text to | |
31 # filename. | |
32 license_dict = {} | |
33 for filename in files: | |
34 license_text = open(filename, 'r').read() | |
Sam Clegg
2012/10/06 01:05:59
super minor: 'r' is the default so can be omitted.
binji
2012/10/08 17:51:48
Done.
| |
35 license_dict.setdefault(license_text, []).append(filename) | |
36 | |
37 # Flip the dictionary (map tuple of filenames -> license text). | |
38 return dict((tuple(value), key) for key, value in license_dict.iteritems()) | |
39 | |
40 | |
41 def WriteLicense(output_file, root, license_text, license_filenames): | |
42 Trace('Writing license for files:\n' + '\n'.join(license_filenames)) | |
43 output_file.write('=' * 70 + '\n') | |
44 for filename in sorted(license_filenames): | |
45 filename = os.path.relpath(filename, root) | |
46 license_dir = os.path.dirname(filename) | |
47 if not license_dir: | |
48 license_dir = 'native_client_sdk' | |
49 | |
50 output_file.write('%s is licensed as follows\n' % (license_dir,)) | |
51 output_file.write(' (Cf. %s):\n' % (filename,)) | |
52 output_file.write('=' * 70 + '\n') | |
53 output_file.write(license_text) | |
54 output_file.write('\n\n\n') | |
55 | |
56 | |
57 def main(args): | |
58 parser = optparse.OptionParser() | |
59 parser.add_option('-v', '--verbose', help='Verbose output.', | |
60 action='store_true') | |
61 parser.add_option('-o', '--output', help='Output file') | |
62 parser.add_option('--root', help='Root for all paths') | |
63 | |
64 options, args = parser.parse_args(args) | |
65 Trace.verbose = options.verbose | |
66 | |
67 if not options.output: | |
68 parser.error('No output file given. See -o.') | |
69 if not options.root: | |
70 parser.error('No root directory given. See --root.') | |
71 | |
72 found_files = FindFiles(args) | |
73 license_dict = CreateLicenseDict(found_files) | |
74 with open(options.output, 'w') as output_file: | |
75 for license_filenames in sorted(license_dict.iterkeys()): | |
76 license_text = license_dict[license_filenames] | |
77 WriteLicense(output_file, options.root, license_text, license_filenames) | |
78 | |
79 Trace('Done.') | |
80 | |
81 return 0 | |
82 | |
83 | |
84 if __name__ == '__main__': | |
85 sys.exit(main(sys.argv[1:])) | |
OLD | NEW |