-
Notifications
You must be signed in to change notification settings - Fork 551
Expand file tree
/
Copy pathregenerate_license.py
More file actions
135 lines (105 loc) · 3.7 KB
/
Copy pathregenerate_license.py
File metadata and controls
135 lines (105 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
# Copyright 2026 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Regenerate the top-level LICENSE file from individual model licenses.
The top-level LICENSE file is a concatenation of all individual model LICENSE
files plus the base project license. Run this script whenever a model is added,
removed, or has its license changed.
Usage:
python regenerate_license.py # Regenerate LICENSE in-place
python regenerate_license.py --check # Check if LICENSE is up to date
"""
import argparse
import pathlib
import sys
HLINE = '=' * 80 + '\n'
def get_base_license(root: pathlib.Path) -> str:
"""Extract the base project license.
Looks for opensource/LICENSE first (used in internal repos), then falls back
to extracting the last section from the existing concatenated LICENSE.
Args:
root: The root directory of the repository.
Returns:
The base project license text.
"""
opensource_license = root / 'opensource' / 'LICENSE'
if opensource_license.exists():
return opensource_license.read_text(encoding='utf-8')
# Fall back to extracting from the existing concatenated LICENSE.
existing = root / 'LICENSE'
if existing.exists():
sections = existing.read_text(encoding='utf-8').split(HLINE + '\n')
if sections:
return sections[-1]
print('ERROR: Cannot find base license.', file=sys.stderr)
print(
'Expected either opensource/LICENSE or an existing top-level LICENSE.',
file=sys.stderr,
)
sys.exit(1)
def generate_license(root: pathlib.Path) -> str:
"""Generate the concatenated LICENSE content.
Args:
root: The root directory of the repository.
Returns:
The concatenated LICENSE content.
"""
license_files = sorted(
root.glob('*/LICENSE'),
key=lambda f: f.parent.name,
)
license_files = [f for f in license_files if f.parent.name != 'opensource']
base_license = get_base_license(root)
out = ''
for lf in license_files:
out += HLINE
out += f"License for contents in the directory '{lf.parent.name}/'\n"
out += HLINE + '\n'
out += lf.read_text(encoding='utf-8') + '\n\n'
out += HLINE
out += 'The following license applies to all other contents\n'
out += HLINE + '\n'
out += base_license
return out
def main():
parser = argparse.ArgumentParser(
description='Regenerate the top-level LICENSE file.'
)
parser.add_argument(
'--check',
action='store_true',
help='Check if the LICENSE is up to date without modifying it.',
)
args = parser.parse_args()
root = pathlib.Path(__file__).resolve().parent
generated = generate_license(root)
license_path = root / 'LICENSE'
if args.check:
if not license_path.exists():
print('FAIL: LICENSE file does not exist.', file=sys.stderr)
sys.exit(1)
current = license_path.read_text(encoding='utf-8')
if current != generated:
print(
'FAIL: LICENSE file is out of date. '
"Run 'python regenerate_license.py' to fix.",
file=sys.stderr,
)
sys.exit(1)
print('OK: LICENSE file is up to date.')
return
license_path.write_text(generated, encoding='utf-8')
print(f'LICENSE file regenerated at {license_path}')
if __name__ == '__main__':
main()