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.
6 """Build the NOTICE file distributed with the NaCl SDK from a set of given
22 found
= [f
for f
in files
if os
.path
.exists(f
)]
25 for f
in sorted(set(files
) - set(found
)):
26 Trace('Skipping %s. File doesn\'t exist.\n' % (f
,))
31 def CreateLicenseDict(files
):
32 # Many of the license files are duplicates. Create a map of license text to
35 for filename
in files
:
36 license_text
= open(filename
).read()
37 license_text
= license_text
.replace('\r\n', '\n')
38 license_dict
.setdefault(license_text
, []).append(filename
)
40 # Flip the dictionary (map tuple of filenames -> license text).
41 return dict((tuple(value
), key
) for key
, value
in license_dict
.iteritems())
44 def WriteLicense(output_file
, root
, license_text
, license_filenames
):
45 Trace('Writing license for files:\n' + '\n'.join(license_filenames
))
46 output_file
.write('=' * 70 + '\n')
47 for filename
in sorted(license_filenames
):
48 filename
= os
.path
.relpath(filename
, root
)
49 license_dir
= os
.path
.dirname(filename
)
51 license_dir
= 'native_client_sdk'
53 output_file
.write('%s is licensed as follows\n' % (license_dir
,))
54 output_file
.write(' (Cf. %s):\n' % (filename
,))
55 output_file
.write('=' * 70 + '\n')
56 output_file
.write(license_text
)
59 def Generate(output_filename
, root
, files
):
60 found_files
= FindFiles(files
)
61 license_dict
= CreateLicenseDict(found_files
)
62 with
open(output_filename
, 'w') as output_file
:
63 for i
, license_filenames
in enumerate(sorted(license_dict
.iterkeys())):
64 license_text
= license_dict
[license_filenames
]
66 output_file
.write('\n\n\n')
67 WriteLicense(output_file
, root
, license_text
, license_filenames
)
71 parser
= argparse
.ArgumentParser(description
=__doc__
)
72 parser
.add_argument('-v', '--verbose', help='Verbose output.',
74 parser
.add_argument('-o', '--output', help='Output file')
75 parser
.add_argument('--root', help='Root for all paths')
76 parser
.add_argument('files', nargs
='*')
78 options
= parser
.parse_args(args
)
79 Trace
.verbose
= options
.verbose
81 if not options
.output
:
82 parser
.error('No output file given. See -o.')
84 parser
.error('No root directory given. See --root.')
86 Generate(options
.output
, options
.root
, options
.files
)
92 if __name__
== '__main__':
93 sys
.exit(main(sys
.argv
[1:]))