]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | from __future__ import ( |
2 | absolute_import, print_function, division, unicode_literals | |
3 | ) | |
4 | ||
5 | import logging | |
6 | import re | |
7 | import sys | |
8 | from datetime import datetime | |
9 | ||
10 | logging.basicConfig(level=logging.DEBUG) | |
11 | logger = logging.getLogger(__name__) | |
12 | ||
13 | CURRENT_YEAR = datetime.today().year | |
14 | ||
15 | LICENSE_BLOB = """Copyright (c) %d Uber Technologies, Inc. | |
16 | ||
17 | Licensed under the Apache License, Version 2.0 (the "License"); | |
18 | you may not use this file except in compliance with the License. | |
19 | You may obtain a copy of the License at | |
20 | ||
21 | http://www.apache.org/licenses/LICENSE-2.0 | |
22 | ||
23 | Unless required by applicable law or agreed to in writing, software | |
24 | distributed under the License is distributed on an "AS IS" BASIS, | |
25 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
26 | See the License for the specific language governing permissions and | |
27 | limitations under the License.""" % CURRENT_YEAR | |
28 | ||
29 | ||
30 | def comment_block(license_blob): | |
31 | lines = ['/*\n'] | |
32 | lines += [(' * ' + l).rstrip() + '\n' | |
33 | for l in license_blob.split('\n')] | |
34 | lines.append(' */\n') | |
35 | return lines | |
36 | ||
37 | ||
38 | LICENSE_BLOB_LINES_CPP = comment_block(LICENSE_BLOB) | |
39 | ||
40 | COPYRIGHT_RE = re.compile(r'Copyright \(c\) (\d+)', re.I) | |
41 | ||
42 | ||
43 | def update_cpp_license(name, force=False): | |
44 | with open(name) as f: | |
45 | orig_lines = list(f) | |
46 | lines = list(orig_lines) | |
47 | ||
48 | found = False | |
49 | changed = False | |
50 | for i, line in enumerate(lines[:5]): | |
51 | m = COPYRIGHT_RE.search(line) | |
52 | if not m: | |
53 | continue | |
54 | ||
55 | found = True | |
56 | year = int(m.group(1)) | |
57 | if year == CURRENT_YEAR: | |
58 | break | |
59 | ||
60 | new_line = COPYRIGHT_RE.sub('Copyright (c) %d' % CURRENT_YEAR, line) | |
61 | assert line != new_line, ('Could not change year in: %s' % line) | |
62 | lines[i] = new_line | |
63 | changed = True | |
64 | break | |
65 | ||
66 | if not found: | |
67 | if 'Code generated by' in lines[0]: | |
68 | lines[1:1] = ['\n'] + LICENSE_BLOB_LINES_CPP | |
69 | else: | |
70 | lines[0:0] = LICENSE_BLOB_LINES_CPP + ['\n'] | |
71 | changed = True | |
72 | ||
73 | if changed: | |
74 | with open(name, 'w') as f: | |
75 | for line in lines: | |
76 | f.write(line) | |
77 | print(name) | |
78 | ||
79 | ||
80 | def main(): | |
81 | if len(sys.argv) == 1: | |
82 | print('USAGE: %s FILE ...' % sys.argv[0]) | |
83 | sys.exit(1) | |
84 | ||
85 | for name in sys.argv[1:]: | |
86 | if name.endswith('.cpp') or \ | |
87 | name.endswith('.h') or \ | |
88 | name.endswith('.h.in'): | |
89 | try: | |
90 | update_cpp_license(name) | |
91 | except Exception as error: | |
92 | logger.error('Failed to process file %s', name) | |
93 | logger.exception(error) | |
94 | raise error | |
95 | else: | |
96 | raise NotImplementedError('Unsupported file type: %s' % name) | |
97 | ||
98 | ||
99 | if __name__ == "__main__": | |
100 | main() |