David Brazdil | 6c63a26 | 2019-12-23 13:23:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 2 | # |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 3 | # Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 4 | # |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 5 | # Use of this source code is governed by a BSD-style |
| 6 | # license that can be found in the LICENSE file or at |
| 7 | # https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 8 | |
| 9 | """Add license header to source files. |
| 10 | |
| 11 | If the file doesn't have the license header, add it with the appropriate comment |
| 12 | style. |
| 13 | """ |
| 14 | |
| 15 | import argparse |
Andrew Walbran | 928d8c1 | 2019-01-11 04:29:39 +0000 | [diff] [blame] | 16 | import datetime |
| 17 | import re |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 18 | import sys |
| 19 | |
| 20 | |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 21 | bsd = """{comment} Copyright {year} The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 22 | {comment} |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 23 | {comment} Use of this source code is governed by a BSD-style |
| 24 | {comment} license that can be found in the LICENSE file or at |
| 25 | {comment} https://opensource.org/licenses/BSD-3-Clause.""" |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 26 | |
| 27 | def Main(): |
| 28 | parser = argparse.ArgumentParser() |
| 29 | parser.add_argument("file") |
| 30 | parser.add_argument("--style", choices=["c", "hash"], required=True) |
| 31 | args = parser.parse_args() |
| 32 | header = "/*\n" if args.style == "c" else "" |
Andrew Walbran | 928d8c1 | 2019-01-11 04:29:39 +0000 | [diff] [blame] | 33 | year = str(datetime.datetime.now().year) |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 34 | header += bsd.format(comment=" *" if args.style == "c" else "#", year=year) |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 35 | header += "\n */" if args.style == "c" else "" |
| 36 | header += "\n\n" |
Andrew Walbran | 928d8c1 | 2019-01-11 04:29:39 +0000 | [diff] [blame] | 37 | header_regex = re.escape(header).replace(year, r"\d\d\d\d") |
Olivier Deprez | 830702b | 2021-01-18 10:23:39 +0100 | [diff] [blame] | 38 | with open(args.file, "rb") as f: |
| 39 | try: |
| 40 | contents = f.read().decode('utf-8', 'strict') |
| 41 | except Exception as ex: |
| 42 | print("Failed reading: " + args.file + |
| 43 | " (" + ex.__class__.__name__ + ")") |
| 44 | return |
Andrew Walbran | 928d8c1 | 2019-01-11 04:29:39 +0000 | [diff] [blame] | 45 | if re.search(header_regex, contents): |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 46 | return |
| 47 | with open(args.file, "w") as f: |
| 48 | f.write(header) |
| 49 | f.write(contents) |
| 50 | |
| 51 | if __name__ == "__main__": |
| 52 | sys.exit(Main()) |