-
Notifications
You must be signed in to change notification settings - Fork 53
/
program_search.py
137 lines (105 loc) · 3.95 KB
/
program_search.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
from __future__ import annotations
import contextlib
import shutil
import subprocess
from pathlib import Path
from typing import TYPE_CHECKING, NamedTuple
from packaging.version import InvalidVersion, Version
from ._logging import logger
from ._shutil import Run
if TYPE_CHECKING:
from collections.abc import Generator, Iterable
__all__ = ["get_cmake_programs", "get_ninja_programs", "best_program", "Program"]
def __dir__() -> list[str]:
return __all__
class Program(NamedTuple):
path: Path
version: Version | None
def _get_cmake_path(*, module: bool = True) -> Generator[Path, None, None]:
"""
Get the path to CMake.
"""
if module:
with contextlib.suppress(ImportError):
# If a "cmake" directory exists, this will also ImportError
from cmake import CMAKE_BIN_DIR
yield Path(CMAKE_BIN_DIR) / "cmake"
candidates = ("cmake3", "cmake")
for candidate in candidates:
cmake_path = shutil.which(candidate)
if cmake_path is not None:
yield Path(cmake_path)
def _get_ninja_path(*, module: bool = True) -> Generator[Path, None, None]:
"""
Get the path to ninja.
"""
if module:
with contextlib.suppress(ImportError):
from ninja import BIN_DIR
yield Path(BIN_DIR) / "ninja"
# Matches https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/CMakeNinjaFindMake.cmake
candidates = ("ninja-build", "ninja", "samu")
for candidate in candidates:
ninja_path = shutil.which(candidate)
if ninja_path is not None:
yield Path(ninja_path)
def get_cmake_programs(*, module: bool = True) -> Generator[Program, None, None]:
"""
Get the path and version for CMake. If the version cannot be determined,
yiels (path, None). Otherwise, yields (path, version). Best matches are
yielded first.
"""
for cmake_path in _get_cmake_path(module=module):
try:
result = Run().capture(cmake_path, "--version")
except subprocess.CalledProcessError:
yield Program(cmake_path, None)
continue
try:
version = Version(result.stdout.splitlines()[0].split()[-1].split("-")[0])
except (IndexError, InvalidVersion):
logger.warning(f"Could not determine CMake version, got {result.stdout!r}")
yield Program(cmake_path, None)
continue
logger.info("CMake version: {}", version)
yield Program(cmake_path, version)
def get_ninja_programs(*, module: bool = True) -> Generator[Program, None, None]:
"""
Get the path and version for Ninja. If the version cannot be determined,
yields (path, None). Otherwise, yields (path, version). Best matches are
yielded first.
"""
for ninja_path in _get_ninja_path(module=module):
try:
result = Run().capture(ninja_path, "--version")
except subprocess.CalledProcessError:
yield Program(ninja_path, None)
continue
try:
version = Version(".".join(result.stdout.strip().split(".")[:3]))
except ValueError:
yield Program(ninja_path, None)
continue
logger.info("Ninja version: {}", version)
yield Program(ninja_path, version)
def get_make_programs() -> Generator[Path, None, None]:
"""
Get the path to make.
"""
candidates = ("gmake", "make")
for candidate in candidates:
make_path = shutil.which(candidate)
if make_path is not None:
yield Path(make_path)
def best_program(
programs: Iterable[Program], *, minimum_version: Version | None
) -> Program | None:
"""
Select the first program entry that is of a supported version, or None if not found.
"""
for program in programs:
if minimum_version is None:
return program
if program.version is not None and program.version >= minimum_version:
return program
return None