|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# Copyright Pionix GmbH and Contributors to EVerest |
| 6 | +# |
| 7 | +""" |
| 8 | +author: kai-uwe.hermann@pionix.de |
| 9 | +Replace licenses with Apache 2.0 |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +from datetime import date |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + parser = argparse.ArgumentParser( |
| 19 | + description='create an isolated snapshot with edm') |
| 20 | + |
| 21 | + parser.add_argument('--working-dir', '-wd', type=str, |
| 22 | + help='Working directory (default: .)', default=str(Path.cwd())) |
| 23 | + parser.add_argument('--no-year', action='store_true', help='Do not include years in license header') |
| 24 | + |
| 25 | + args = parser.parse_args() |
| 26 | + |
| 27 | + working_dir = Path(args.working_dir).expanduser().resolve() |
| 28 | + |
| 29 | + files = [file for file in working_dir.rglob('*') if file.suffix in ['.cpp', '.hpp']] |
| 30 | + |
| 31 | + year = '' |
| 32 | + if not args.no_year: |
| 33 | + year = f'2020 - {date.today().year} ' |
| 34 | + |
| 35 | + license_text = f"""// SPDX-License-Identifier: Apache-2.0 |
| 36 | +// Copyright {year}Pionix GmbH and Contributors to EVerest |
| 37 | +""" |
| 38 | + |
| 39 | + success = 0 |
| 40 | + failure = 0 |
| 41 | + count = len(files) |
| 42 | + |
| 43 | + for file in files: |
| 44 | + content = file.read_text() |
| 45 | + if content.startswith('/*'): |
| 46 | + needle = '*/\n' |
| 47 | + end = content.find(needle) + len(needle) |
| 48 | + new_content = license_text + content[end:] |
| 49 | + file.write_text(new_content) |
| 50 | + print(f'Modified {file} with new license header') |
| 51 | + success += 1 |
| 52 | + else: |
| 53 | + content_lines = content.splitlines() |
| 54 | + end = 0 |
| 55 | + for line in content_lines: |
| 56 | + if line.startswith('//'): |
| 57 | + end += 1 |
| 58 | + else: |
| 59 | + break |
| 60 | + new_content = license_text + '\n'.join(content_lines[end:]) + '\n' |
| 61 | + file.write_text(new_content) |
| 62 | + success += 1 |
| 63 | + |
| 64 | + if success != count: |
| 65 | + print('ERROR during license replacement') |
| 66 | + else: |
| 67 | + print('Everything went well') |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + main() |
0 commit comments