Skip to content

Commit f5d7f6e

Browse files
committed
MAINT: refactor version parsing into an utility function
1 parent fb1761b commit f5d7f6e

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

mesonpy/__init__.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,28 +1108,25 @@ def _project(config_settings: Optional[Dict[Any, Any]]) -> Iterator[Project]:
11081108
yield project
11091109

11101110

1111+
def _parse_version_string(string: str) -> Tuple[int, ...]:
1112+
"""Parse version string."""
1113+
try:
1114+
return tuple(map(int, string.split('.')[:3]))
1115+
except ValueError:
1116+
return (0, )
1117+
1118+
11111119
def _env_ninja_command(*, version: str = _NINJA_REQUIRED_VERSION) -> Optional[str]:
1112-
"""
1113-
Returns the path to ninja, or None if no ninja found.
1114-
"""
1115-
required_version = tuple(int(v) for v in version.split('.'))
1120+
"""Returns the path to ninja, or None if no ninja found."""
1121+
required_version = _parse_version_string(version)
11161122
env_ninja = os.environ.get('NINJA')
11171123
ninja_candidates = [env_ninja] if env_ninja else ['ninja', 'ninja-build', 'samu']
11181124
for ninja in ninja_candidates:
11191125
ninja_path = shutil.which(ninja)
1120-
if ninja_path is None:
1121-
continue
1122-
1123-
result = subprocess.run([ninja_path, '--version'], check=False, text=True, capture_output=True)
1124-
1125-
try:
1126-
candidate_version = tuple(int(x) for x in result.stdout.split('.')[:3])
1127-
except ValueError:
1128-
continue
1129-
if candidate_version < required_version:
1130-
continue
1131-
return ninja_path
1132-
1126+
if ninja_path is not None:
1127+
version = subprocess.run([ninja_path, '--version'], check=False, text=True, capture_output=True).stdout
1128+
if _parse_version_string(version) >= required_version:
1129+
return ninja_path
11331130
return None
11341131

11351132

0 commit comments

Comments
 (0)