[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,13 +1,17 @@
#!/usr/bin/env python3
import os
import re
import shutil
from pathlib import Path
from upstream_utils import Lib, comment_out_invalid_includes, walk_cwd_and_copy_if
from upstream_utils import (
Lib,
comment_out_invalid_includes,
has_prefix,
walk_cwd_and_copy_if,
)
def eigen_inclusions(dp, f):
def eigen_inclusions(dp: Path, f: str):
"""Returns true if the given file in the "Eigen" include directory of the
Eigen git repo should be copied into allwpilib
@@ -15,11 +19,9 @@ def eigen_inclusions(dp, f):
dp -- directory path
f -- filename
"""
if not dp.startswith(os.path.join(".", "Eigen")):
if not has_prefix(dp, Path("Eigen")):
return False
abspath = os.path.join(dp, f)
# Exclude NonMPL2.h since all non-MPL2 code will be excluded anyway
if f == "NonMPL2.h":
return False
@@ -36,13 +38,13 @@ def eigen_inclusions(dp, f):
if "MKL" in f:
return False
# Include architectures we care about
if "Core/arch/" in abspath:
# Include architectures we care about by filtering for Core/arch
if "Core" in dp.parts and "arch" in dp.parts:
return (
"arch/AVX/" in abspath
or "arch/Default" in abspath
or "arch/NEON" in abspath
or "arch/SSE" in abspath
"AVX" in dp.parts
or "Default" in dp.parts
or "NEON" in dp.parts
or "SSE" in dp.parts
)
# Include the following modules
@@ -65,10 +67,13 @@ def eigen_inclusions(dp, f):
"misc",
"plugins",
]
return bool(re.search(r"|".join("/" + m for m in modules), abspath))
for m in modules:
if m in dp.parts or f == m:
return True
return False
def unsupported_inclusions(dp, f):
def unsupported_inclusions(dp: Path, f: str):
"""Returns true if the given file in the "unsupported" include directory of
the Eigen git repo should be copied into allwpilib
@@ -76,50 +81,50 @@ def unsupported_inclusions(dp, f):
dp -- directory path
f -- filename
"""
if not dp.startswith(os.path.join(".", "unsupported")):
if not has_prefix(dp, Path("unsupported")):
return False
abspath = os.path.join(dp, f)
abspath = dp / f
# Exclude build system and READMEs
if f == "CMakeLists.txt" or "README" in f:
return False
# Include the MatrixFunctions module
return "MatrixFunctions" in abspath
return "MatrixFunctions" in abspath.parts
def copy_upstream_src(wpilib_root):
wpimath = os.path.join(wpilib_root, "wpimath")
def copy_upstream_src(wpilib_root: Path):
wpimath = wpilib_root / "wpimath"
# Delete old install
for d in ["src/main/native/thirdparty/eigen/include"]:
shutil.rmtree(os.path.join(wpimath, d), ignore_errors=True)
shutil.rmtree(wpimath / d, ignore_errors=True)
# Copy Eigen headers into allwpilib
eigen_files = walk_cwd_and_copy_if(
eigen_inclusions,
os.path.join(wpimath, "src/main/native/thirdparty/eigen/include"),
wpimath / "src/main/native/thirdparty/eigen/include",
)
# Copy unsupported headers into allwpilib
unsupported_files = walk_cwd_and_copy_if(
unsupported_inclusions,
os.path.join(wpimath, "src/main/native/thirdparty/eigen/include"),
wpimath / "src/main/native/thirdparty/eigen/include",
)
for f in eigen_files:
comment_out_invalid_includes(
f, [os.path.join(wpimath, "src/main/native/thirdparty/eigen/include")]
f, [wpimath / "src/main/native/thirdparty/eigen/include"]
)
for f in unsupported_files:
comment_out_invalid_includes(
f, [os.path.join(wpimath, "src/main/native/thirdparty/eigen/include")]
f, [wpimath / "src/main/native/thirdparty/eigen/include"]
)
shutil.copyfile(
".clang-format",
os.path.join(wpimath, "src/main/native/thirdparty/eigen/include/.clang-format"),
wpimath / "src/main/native/thirdparty/eigen/include/.clang-format",
)