Files
pytorch/tools/build_libtorch.py
Xuehai Pan b6bdb67f82 [BE][Easy] use pathlib.Path instead of dirname / ".." / pardir (#129374)
Changes by apply order:

1. Replace all `".."` and `os.pardir` usage with `os.path.dirname(...)`.
2. Replace nested `os.path.dirname(os.path.dirname(...))` call with `str(Path(...).parent.parent)`.
3. Reorder `.absolute()` ~/ `.resolve()`~ and `.parent`: always resolve the path first.

    `.parent{...}.absolute()` -> `.absolute().parent{...}`

4. Replace chained `.parent x N` with `.parents[${N - 1}]`: the code is easier to read (see 5.)

    `.parent.parent.parent.parent` -> `.parents[3]`

5. ~Replace `.parents[${N - 1}]` with `.parents[${N} - 1]`: the code is easier to read and does not introduce any runtime overhead.~

    ~`.parents[3]` -> `.parents[4 - 1]`~

6. ~Replace `.parents[2 - 1]` with `.parent.parent`: because the code is shorter and easier to read.~

Pull Request resolved: https://github.com/pytorch/pytorch/pull/129374
Approved by: https://github.com/justinchuby, https://github.com/malfet
2024-12-29 17:23:13 +00:00

35 lines
1.1 KiB
Python

import argparse
import sys
from pathlib import Path
# By appending REPO_ROOT to sys.path, this module can import other torch
# modules even when run as a standalone script. i.e., it's okay either you
# do `python build_libtorch.py` or `python -m tools.build_libtorch`.
REPO_ROOT = Path(__file__).absolute().parent.parent
sys.path.append(str(REPO_ROOT))
from tools.build_pytorch_libs import build_pytorch
from tools.setup_helpers.cmake import CMake
if __name__ == "__main__":
# Placeholder for future interface. For now just gives a nice -h.
parser = argparse.ArgumentParser(description="Build libtorch")
parser.add_argument("--rerun-cmake", action="store_true", help="rerun cmake")
parser.add_argument(
"--cmake-only",
action="store_true",
help="Stop once cmake terminates. Leave users a chance to adjust build options",
)
options = parser.parse_args()
build_pytorch(
version=None,
cmake_python_library=None,
build_python=False,
rerun_cmake=options.rerun_cmake,
cmake_only=options.cmake_only,
cmake=CMake(),
)