[upstream_utils] Use pathlib instead of os.path (#7983)

A noteworthy change is the replacement of the `dp.startswith(os.path.join(".", "subdir"))` pattern. pathlib doesn't offer something with similar semantics besides `match` and `full_match`, so there's now a helper function that replicates the behavior.

Other notable changes include the addition of type annotations to ensure code correctness, using == to check file names instead of `endswith` for clarity (`endswith` is still used to check extensions), manual walking and copying being refactored in googletest, json, memory, nanopb, protobuf, and sleipnir to use `walk_cwd_and_copy_if`, and matching functions being shortened to the point where they can just be inlined into the lambda.

Co-authored-by: Tyler Veness <calcmogul@gmail.com>
Co-authored-by: David Vo <auscompgeek@users.noreply.github.com>
This commit is contained in:
Gold856
2025-05-29 22:05:22 +00:00
committed by GitHub
parent de718f7ae5
commit ca05ffa1b9
26 changed files with 625 additions and 1045 deletions

View File

@@ -1,62 +1,57 @@
#!/usr/bin/env python3
import os
import shutil
from pathlib import Path
from upstream_utils import Lib, walk_cwd_and_copy_if
def matches(dp, f, allowed_files):
path = os.path.join(dp, f)
return path in allowed_files
def walk_and_copy_if_matches(allowed_files, output_directory):
def walk_and_copy_if_matches(allowed_files: list[Path], output_directory: Path):
walk_cwd_and_copy_if(
lambda dp, f: matches(dp, f, allowed_files),
lambda dp, f: dp / f in allowed_files,
output_directory,
)
def copy_upstream_src(wpilib_root):
imgui = os.path.join(wpilib_root, "thirdparty", "imgui_suite", "imgui")
def copy_upstream_src(wpilib_root: Path):
imgui = wpilib_root / "thirdparty/imgui_suite/imgui"
# Delete old install
for d in ["include", "cpp"]:
shutil.rmtree(os.path.join(imgui, d), ignore_errors=True)
shutil.rmtree(imgui / d, ignore_errors=True)
hdr_allow_list = [
"./imgui.h",
"./imstb_truetype.h",
"./imgui_internal.h",
"./imstb_rectpack.h",
"./imconfig.h",
"./imstb_textedit.h",
"./backends/imgui_impl_glfw.h",
"./backends/imgui_impl_metal.h",
"./backends/imgui_impl_opengl3.h",
"./backends/imgui_impl_dx11.h",
"./backends/imgui_impl_opengl3_loader.h",
"./backends/imgui_impl_opengl2.h",
"./misc/cpp/imgui_stdlib.h",
Path("imgui.h"),
Path("imstb_truetype.h"),
Path("imgui_internal.h"),
Path("imstb_rectpack.h"),
Path("imconfig.h"),
Path("imstb_textedit.h"),
Path("backends/imgui_impl_glfw.h"),
Path("backends/imgui_impl_metal.h"),
Path("backends/imgui_impl_opengl3.h"),
Path("backends/imgui_impl_dx11.h"),
Path("backends/imgui_impl_opengl3_loader.h"),
Path("backends/imgui_impl_opengl2.h"),
Path("misc/cpp/imgui_stdlib.h"),
]
src_allow_list = [
"./backends/imgui_impl_dx11.cpp",
"./backends/imgui_impl_glfw.cpp",
"./backends/imgui_impl_metal.mm",
"./backends/imgui_impl_opengl2.cpp",
"./backends/imgui_impl_opengl3.cpp",
"./imgui.cpp",
"./imgui_demo.cpp",
"./imgui_draw.cpp",
"./imgui_tables.cpp",
"./imgui_widgets.cpp",
"./misc/cpp/imgui_stdlib.cpp",
Path("backends/imgui_impl_dx11.cpp"),
Path("backends/imgui_impl_glfw.cpp"),
Path("backends/imgui_impl_metal.mm"),
Path("backends/imgui_impl_opengl2.cpp"),
Path("backends/imgui_impl_opengl3.cpp"),
Path("imgui.cpp"),
Path("imgui_demo.cpp"),
Path("imgui_draw.cpp"),
Path("imgui_tables.cpp"),
Path("imgui_widgets.cpp"),
Path("misc/cpp/imgui_stdlib.cpp"),
]
walk_and_copy_if_matches(hdr_allow_list, os.path.join(imgui, "include"))
walk_and_copy_if_matches(src_allow_list, os.path.join(imgui, "cpp"))
walk_and_copy_if_matches(hdr_allow_list, imgui / "include")
walk_and_copy_if_matches(src_allow_list, imgui / "cpp")
def main():