blob: e09cbead5280a0460aa71aef688bfa39134f0c93 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001#!/usr/bin/env python
2#
Andrew Walbran692b3252019-03-07 15:51:31 +00003# Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01004#
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
19If the file doesn't have the license header, add it with the appropriate comment
20style.
21"""
22
23import argparse
Andrew Walbran928d8c12019-01-11 04:29:39 +000024import datetime
25import re
Andrew Scull18834872018-10-12 11:48:09 +010026import sys
27
28
Andrew Walbran692b3252019-03-07 15:51:31 +000029apache2 = """{comment} Copyright {year} The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +010030{comment}
31{comment} Licensed under the Apache License, Version 2.0 (the "License");
32{comment} you may not use this file except in compliance with the License.
33{comment} You may obtain a copy of the License at
34{comment}
35{comment} https://www.apache.org/licenses/LICENSE-2.0
36{comment}
37{comment} Unless required by applicable law or agreed to in writing, software
38{comment} distributed under the License is distributed on an "AS IS" BASIS,
39{comment} WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40{comment} See the License for the specific language governing permissions and
41{comment} limitations under the License."""
42
43def Main():
44 parser = argparse.ArgumentParser()
45 parser.add_argument("file")
46 parser.add_argument("--style", choices=["c", "hash"], required=True)
47 args = parser.parse_args()
48 header = "/*\n" if args.style == "c" else ""
Andrew Walbran928d8c12019-01-11 04:29:39 +000049 year = str(datetime.datetime.now().year)
50 header += apache2.format(comment=" *" if args.style == "c" else "#", year=year)
Andrew Scull18834872018-10-12 11:48:09 +010051 header += "\n */" if args.style == "c" else ""
52 header += "\n\n"
Andrew Walbran928d8c12019-01-11 04:29:39 +000053 header_regex = re.escape(header).replace(year, r"\d\d\d\d")
Andrew Scull18834872018-10-12 11:48:09 +010054 with open(args.file, "r") as f:
55 contents = f.read()
Andrew Walbran928d8c12019-01-11 04:29:39 +000056 if re.search(header_regex, contents):
Andrew Scull18834872018-10-12 11:48:09 +010057 return
58 with open(args.file, "w") as f:
59 f.write(header)
60 f.write(contents)
61
62if __name__ == "__main__":
63 sys.exit(Main())