-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathpy_presubmit.py
More file actions
executable file
·101 lines (81 loc) · 2.64 KB
/
Copy pathpy_presubmit.py
File metadata and controls
executable file
·101 lines (81 loc) · 2.64 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
#!/usr/bin/env python3
# Copyright 2025 Google LLC
#
# 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.
#
################################################################################
"""Pre submit checks in same style as OSS-Fuzz"""
import os
import subprocess
import sys
def do_checks(changed_files):
"""Runs all presubmit checks. Returns False if any fails."""
checks = [
check_license,
]
return all([check(changed_files) for check in checks])
_CHECK_LICENSE_FILENAMES = ["Dockerfile"]
_CHECK_LICENSE_EXTENSIONS = [
".bash",
".Dockerfile",
".go",
".h",
".htm",
".html",
".proto",
".py",
".rs",
".sh",
".ts",
]
THIRD_PARTY_DIR_NAME = "third_party"
_LICENSE_STRING = "http://www.apache.org/licenses/LICENSE-2.0"
def check_license(paths):
"""Validates license header."""
if not paths:
return True
success = True
for path in paths:
path_parts = str(path).split(os.sep)
if any(path_part == THIRD_PARTY_DIR_NAME for path_part in path_parts):
continue
filename = os.path.basename(path)
extension = os.path.splitext(path)[1]
if (
filename not in _CHECK_LICENSE_FILENAMES
and extension not in _CHECK_LICENSE_EXTENSIONS
):
continue
with open(path) as file_handle:
if _LICENSE_STRING not in file_handle.read():
print("Missing license header in file %s." % str(path))
success = False
return success
def bool_to_returncode(success):
"""Returns 0 if |success|. Otherwise returns 1."""
if success:
print("Success.")
return 0
print("Failed.")
return 1
def get_all_files():
"""Returns a list of absolute paths of files in this repo."""
get_all_files_command = ["git", "ls-files"]
output = subprocess.check_output(get_all_files_command).decode().splitlines()
return [os.path.abspath(path) for path in output if os.path.isfile(path)]
def main():
relevant_files = get_all_files()
success = do_checks(relevant_files)
return bool_to_returncode(success)
if __name__ == "__main__":
sys.exit(main())