| #!/usr/bin/env python |
| # Copyright 2019 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| """Generate a license file for all the individual projects. |
| |
| Usage: |
| python generate_license.py --include file1 --extract file2 2-5 |
| """ |
| |
| import argparse |
| import sys |
| |
| SEPARATOR = """ |
| ============================================================================== |
| |
| """ |
| |
| |
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument( |
| '--include', |
| nargs='+', |
| metavar='FILE', |
| help='include an entire license file') |
| parser.add_argument( |
| '--extract', |
| nargs='+', |
| action='append', |
| metavar='FILE LINES', |
| help='extract the license from a file') |
| args = parser.parse_args() |
| |
| for license_filename in args.include: |
| with open(license_filename) as license_file: |
| sys.stdout.write(license_file.read()) |
| |
| for extract_license in args.extract: |
| sys.stdout.write(SEPARATOR) |
| |
| with open(str(extract_license[0])) as license_file: |
| lines = license_file.readlines() |
| |
| ranges = [] |
| for r in extract_license[1].split(','): |
| s = r.split('-', 1) |
| if len(s) == 1: |
| ranges.append((int(s[0]) - 1, int(s[0]))) |
| else: |
| ranges.append((int(s[0]) - 1 if s[0] else None, |
| int(s[1]) - 1 if s[1] else None)) |
| |
| for line_range in ranges: |
| sys.stdout.write(''.join(lines[line_range[0]:line_range[1]])) |
| |
| return 0 |
| |
| |
| if __name__ == '__main__': |
| sys.exit(main()) |