Chromium Code Reviews| Index: native_client_sdk/src/build_tools/generate_notice.py |
| diff --git a/native_client_sdk/src/build_tools/generate_notice.py b/native_client_sdk/src/build_tools/generate_notice.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..7f8a68f9700c3312ba73b77660efbe5d12118f51 |
| --- /dev/null |
| +++ b/native_client_sdk/src/build_tools/generate_notice.py |
| @@ -0,0 +1,85 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Build the NOTICE file distributed with the NaCl SDK from a set of given |
| +license files.""" |
| + |
| +import optparse |
| +import os |
| +import sys |
| + |
| + |
| +def Trace(msg): |
| + if Trace.verbose: |
| + print msg |
| + |
| + |
| +def FindFiles(files): |
| + found = [f for f in files if os.path.exists(f)] |
| + |
| + if Trace.verbose: |
| + for f in sorted(set(files) - set(found)): |
| + Trace('Skipping %s. File doesn\'t exist.\n' % (f,)) |
| + |
| + return found |
| + |
| + |
| +def CreateLicenseDict(files): |
| + # Many of the license files are duplicates. Create a map of license text to |
| + # filename. |
| + license_dict = {} |
| + for filename in files: |
| + 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.
|
| + license_dict.setdefault(license_text, []).append(filename) |
| + |
| + # Flip the dictionary (map tuple of filenames -> license text). |
| + return dict((tuple(value), key) for key, value in license_dict.iteritems()) |
| + |
| + |
| +def WriteLicense(output_file, root, license_text, license_filenames): |
| + Trace('Writing license for files:\n' + '\n'.join(license_filenames)) |
| + output_file.write('=' * 70 + '\n') |
| + for filename in sorted(license_filenames): |
| + filename = os.path.relpath(filename, root) |
| + license_dir = os.path.dirname(filename) |
| + if not license_dir: |
| + license_dir = 'native_client_sdk' |
| + |
| + output_file.write('%s is licensed as follows\n' % (license_dir,)) |
| + output_file.write(' (Cf. %s):\n' % (filename,)) |
| + output_file.write('=' * 70 + '\n') |
| + output_file.write(license_text) |
| + output_file.write('\n\n\n') |
| + |
| + |
| +def main(args): |
| + parser = optparse.OptionParser() |
| + parser.add_option('-v', '--verbose', help='Verbose output.', |
| + action='store_true') |
| + parser.add_option('-o', '--output', help='Output file') |
| + parser.add_option('--root', help='Root for all paths') |
| + |
| + options, args = parser.parse_args(args) |
| + Trace.verbose = options.verbose |
| + |
| + if not options.output: |
| + parser.error('No output file given. See -o.') |
| + if not options.root: |
| + parser.error('No root directory given. See --root.') |
| + |
| + found_files = FindFiles(args) |
| + license_dict = CreateLicenseDict(found_files) |
| + with open(options.output, 'w') as output_file: |
| + for license_filenames in sorted(license_dict.iterkeys()): |
| + license_text = license_dict[license_filenames] |
| + WriteLicense(output_file, options.root, license_text, license_filenames) |
| + |
| + Trace('Done.') |
| + |
| + return 0 |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv[1:])) |