-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlicense-check.py
executable file
·115 lines (96 loc) · 3.46 KB
/
license-check.py
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
#!/usr/bin/env python
import sys
import os
from pathlib import Path
import subprocess
ALL_RIGHT_RESERVED_HEADER = """
// Copyright (c) {YEAR} RISC Zero, Inc.
//
// All rights reserved.
""".strip().splitlines()
APACHE_HEADER = """
// Copyright {YEAR} RISC Zero, Inc.
//
// 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.
""".strip().splitlines()
EXTENSIONS = [
".cpp",
".h",
".rs",
'.sol',
]
SKIP_PATHS = [
# ImageID.sol is automatically generated.
str(Path.cwd()) + "/contracts/src/SetBuilderImageID.sol",
str(Path.cwd()) + "/contracts/src/libraries/AssessorImageID.sol",
str(Path.cwd()) + "/contracts/src/libraries/UtilImageID.sol",
str(Path.cwd()) + "/crates/boundless-market/src/contracts/artifacts",
str(Path.cwd()) + "/crates/boundless-market/src/contracts/bytecode.rs",
]
APACHE_PATHS = [
str(Path.cwd()) + "/crates/boundless-market",
str(Path.cwd()) + "/crates/boundless-cli",
str(Path.cwd()) + "/crates/assessor",
str(Path.cwd()) + "/crates/balance-alerts-layer",
str(Path.cwd()) + "/contracts/src/IBoundlessMarket.sol",
str(Path.cwd()) + "/contracts/src/HitPoints.sol",
str(Path.cwd()) + "/contracts/src/IHitPoints.sol"
]
def check_header(file, expected_year, lines_actual):
if any(map(lambda path: file.is_relative_to(path), APACHE_PATHS)):
header = APACHE_HEADER
else:
header = ALL_RIGHT_RESERVED_HEADER
for expected, actual in zip(header, lines_actual):
expected = expected.replace("{YEAR}", expected_year)
if expected != actual:
return (expected, actual)
return None
def check_file(root, file):
cmd = ["git", "log", "-1", "--format=%ad", "--date=format:%Y", file]
expected_year = subprocess.check_output(cmd, encoding="UTF-8").strip()
rel_path = file.relative_to(root)
lines = file.read_text().splitlines()
result = check_header(file, expected_year, lines)
if result:
print(f"{rel_path}: invalid header!")
print(f" expected: {result[0]}")
print(f" actual: {result[1]}")
return 1
return 0
def repo_root():
"""Return an absolute Path to the repo root"""
cmd = ["git", "rev-parse", "--show-toplevel"]
return Path(subprocess.check_output(cmd, encoding="UTF-8").strip())
def tracked_files():
"""Yield all file paths tracked by git"""
cmd = ["git", "ls-tree", "--full-tree", "--name-only", "-r", "HEAD"]
tree = subprocess.check_output(cmd, encoding="UTF-8").strip()
for path in tree.splitlines():
yield (repo_root() / Path(path)).absolute()
def main():
root = repo_root()
ret = 0
for path in tracked_files():
if path.suffix in EXTENSIONS:
skip = False
for path_start in SKIP_PATHS:
if str(path).startswith(path_start):
skip = True
break
if skip:
continue
ret |= check_file(root, path)
sys.exit(ret)
if __name__ == "__main__":
main()