-
Notifications
You must be signed in to change notification settings - Fork 2
/
check.py
223 lines (180 loc) · 7.5 KB
/
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Copyright (c) 2023-2024 Advanced Robotics at the University of Washington <[email protected]>
#
# This file is part of aruw-mcb.
#
# aruw-mcb is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# aruw-mcb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with aruw-mcb. If not, see <https://www.gnu.org/licenses/>.
from enum import Enum
import subprocess, os
from typing import Callable, List, Optional, Dict
import argparse
import pathlib
import platform
def run(cmd: List[str], cwd: Optional[str] = None, add_env: Optional[Dict[str, str]] = None):
env = os.environ.copy() | add_env if add_env else None
subprocess.run(cmd, check=True, cwd=cwd, env=env)
PROJECT_NAME = "aruw-mcb"
ORGANIZATION_NAME = "Advanced Robotics at the University of Washington"
# Project directory relative to root repo directory
PROJECT_DIR = "aruw-mcb-project"
def clang_format():
CLANG_FORMAT_DIRS = [
"aruw-mcb-project/src/",
"aruw-mcb-project/test/",
]
print("Running clang format...")
run(["python", "taproot-scripts/clang_format_all.py", *CLANG_FORMAT_DIRS])
def check_singleton_drivers():
print("Checking singleton drivers...")
run(["python", "taproot-scripts/check_singleton_drivers.py", "DoNotUse_getDrivers", "-p", "src"])
def check_license_headers():
IGNORE_LICENSE = [
'./**/__init__.py',
'taproot/**/*',
'taproot-scripts/**/*',
'aruw-mcb-project/taproot/**/*',
'docs/**/*',
'aruw-mcb-project/robot-type/robot_type.hpp',
]
print("Checking license headers...")
run(["python", "taproot-scripts/check_license_headers.py", "-p", PROJECT_NAME, "-o", ORGANIZATION_NAME, "-i", *IGNORE_LICENSE])
def check_header_guards():
HEADER_GUARD_CHECK_DIRS = [
"aruw-mcb-project/src",
"aruw-mcb-project/test/",
]
IGNORE_HEADER = []
HEADER_PREFIX = None
print("Checking header guards...")
run(["python", "./taproot-scripts/check_header_guard.py", *HEADER_GUARD_CHECK_DIRS, *(["-p", HEADER_PREFIX] if HEADER_PREFIX else []), "-i", *IGNORE_HEADER])
# def check_taproot_submodule():
# VALID_BRANCHES = ["release", "develop"]
# print("Checking taproot submodule...")
# FIXME: Newly written powershell script relies on lbuild build command which is broken for windows
# Need to replace with below wrapper for lbuild build
# run(["bash" "./taproot-scripts/check_taproot_submodule.sh", PROJECT_DIR, "taproot", " ".join(VALID_BRANCHES)])
def run_lbuild():
print("Running lbuild...")
run(["pipenv", "run", "lbuild", "build"], cwd=PROJECT_DIR)
def override_windows():
# Note: The LF/CRLF change should be undone by git automatically when the change is staged but we do it manually to reduce confusion
LF_TO_CRLF = ["aruw-mcb-project/taproot/modm/ext/gcc/cabi.c"]
DOUBLE_BACKSLASHES_TO_FORWARD_SLASHES = ["aruw-mcb-project/taproot/modm/openocd.cfg"]
BACKSLASHES_TO_FORWARD_SLASHES = [
os.path.join(PROJECT_DIR, "taproot", dir, file) for file in [
pathlib.Path("project.xml"),
pathlib.Path("modm/SConscript"),
pathlib.Path("modm/ext/printf/printf.h"),
] for dir in [
pathlib.Path("."),
pathlib.Path("sim-modm/hosted-darwin"),
pathlib.Path("sim-modm/hosted-linux"),
pathlib.Path("sim-modm/hosted-windows"),
]
]
for file_path in LF_TO_CRLF:
with open(file_path, "rb+") as f:
content = f.read()
content = content.replace(b"\r\n", b"\n")
f.seek(0)
f.write(content)
f.truncate()
for file_path in DOUBLE_BACKSLASHES_TO_FORWARD_SLASHES:
with open(file_path, "r+", encoding="utf8") as f:
content = f.read()
content = content.replace("\\\\", "/")
f.seek(0)
f.write(content)
f.truncate()
for file_path in BACKSLASHES_TO_FORWARD_SLASHES:
with open(file_path, "r+", encoding="utf8") as f:
content = f.read()
content = content.replace("\\", "/")
f.seek(0)
f.write(content)
f.truncate()
if platform.system() == "Windows":
print("Replacing lbuild windows jankness...")
override_windows()
class BuildTarget(Enum):
STANDARD_ORION = "STANDARD_ORION"
STANDARD_CYGNUS = "STANDARD_CYGNUS"
STANDARD_SPIDER = "STANDARD_SPIDER"
HERO_CYCLONE = "HERO_PERSEUS"
SENTRY_HYDRA = "SENTRY_HYDRA"
DART = "DART"
ENGINEER = "ENGINEER"
DRONE = "DRONE"
TESTBED = "TESTBED"
BLANK = "BLANK"
MOTOR_TESTER = "MOTOR_TESTER"
all = "all"
def build_mcb(target : Optional[BuildTarget] = None):
print(f"Checking MCB build for {target.value if target else 'all'}...")
if not target or target == BuildTarget.all:
for t in BuildTarget:
if t != BuildTarget.all:
build_mcb(t)
else:
run(["pipenv", "run", "scons", "build", f"robot={target.value}", "additional-ccflags=-Werror"], cwd=PROJECT_DIR)
def build_and_run_tests(target : Optional[BuildTarget] = None):
print(f"Checking tests for {target.value if target else 'all'}...")
if not target or target == BuildTarget.all:
for t in BuildTarget:
if t != BuildTarget.all:
build_and_run_tests(t)
else:
run(["pipenv", "run", "scons", "run-tests", f"robot={target.value}"], cwd=PROJECT_DIR)
action_to_method : Dict[str, Callable] = {
"format" : clang_format,
"singleton_drivers" : check_singleton_drivers,
"license" : check_license_headers,
"header_guards" : check_header_guards,
# "taproot" : check_taproot_submodule,
"lbuild" : run_lbuild,
"build" : build_mcb,
"test" : build_and_run_tests
}
def main():
args = parse_args()
# Switch cwd to aruw-mcb
print("Switching to aruw-mcb working directory...")
aruw_mcb_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(aruw_mcb_path)
# TODO: clear cached files for scons using --clean flag
if args.action != "all":
if args.action in {"build", "test", "sim"}:
action_to_method[args.action](BuildTarget(args.robot))
else:
action_to_method[args.action]()
else:
# Format
clang_format()
# Policy checks
check_singleton_drivers()
check_license_headers()
check_header_guards()
# check_taproot_submodule()
# Build
run_lbuild()
build_mcb()
build_and_run_tests()
# TODO: idk how docs work
def parse_args():
arg = argparse.ArgumentParser(
description="Runs all checks.")
arg.add_argument("action", default=None, help=f"Action to take. If not specified, runs all checks. Must be one of {action_to_method.keys()} or all.")
arg.add_argument("-r", "--robot", default=None, help="If action is either build, test, or sim, a robot target must be specified.")
return arg.parse_args()
if __name__ == "__main__":
main()