Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2018 Google LLC |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """Add license header to source files. |
| 18 | |
| 19 | If the file doesn't have the license header, add it with the appropriate comment |
| 20 | style. |
| 21 | """ |
| 22 | |
| 23 | import argparse |
| 24 | import sys |
| 25 | |
| 26 | |
| 27 | apache2 = """{comment} Copyright 2018 Google LLC |
| 28 | {comment} |
| 29 | {comment} Licensed under the Apache License, Version 2.0 (the "License"); |
| 30 | {comment} you may not use this file except in compliance with the License. |
| 31 | {comment} You may obtain a copy of the License at |
| 32 | {comment} |
| 33 | {comment} https://www.apache.org/licenses/LICENSE-2.0 |
| 34 | {comment} |
| 35 | {comment} Unless required by applicable law or agreed to in writing, software |
| 36 | {comment} distributed under the License is distributed on an "AS IS" BASIS, |
| 37 | {comment} WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 38 | {comment} See the License for the specific language governing permissions and |
| 39 | {comment} limitations under the License.""" |
| 40 | |
| 41 | def Main(): |
| 42 | parser = argparse.ArgumentParser() |
| 43 | parser.add_argument("file") |
| 44 | parser.add_argument("--style", choices=["c", "hash"], required=True) |
| 45 | args = parser.parse_args() |
| 46 | header = "/*\n" if args.style == "c" else "" |
| 47 | header += apache2.format(comment=" *" if args.style == "c" else "#") |
| 48 | header += "\n */" if args.style == "c" else "" |
| 49 | header += "\n\n" |
| 50 | with open(args.file, "r") as f: |
| 51 | contents = f.read() |
| 52 | if header in contents: |
| 53 | return |
| 54 | with open(args.file, "w") as f: |
| 55 | f.write(header) |
| 56 | f.write(contents) |
| 57 | |
| 58 | if __name__ == "__main__": |
| 59 | sys.exit(Main()) |