diff options
author | Daniel Mensinger <daniel@mensinger-ka.de> | 2021-06-16 23:58:45 +0200 |
---|---|---|
committer | Daniel Mensinger <daniel@mensinger-ka.de> | 2021-06-18 23:48:33 +0200 |
commit | 6879e84c48632bcd0f6c277e81b4032a00bb1d5c (patch) | |
tree | dd23b9d92a5ed77be94e8f601a9b21b4d86898e7 /mesonbuild/programs.py | |
parent | 66b32a45915238230557d8591e4521bdd7ecdb3b (diff) | |
download | meson-6879e84c48632bcd0f6c277e81b4032a00bb1d5c.zip meson-6879e84c48632bcd0f6c277e81b4032a00bb1d5c.tar.gz meson-6879e84c48632bcd0f6c277e81b4032a00bb1d5c.tar.bz2 |
holders: Move get_version from directly to ExternalProgram
Diffstat (limited to 'mesonbuild/programs.py')
-rw-r--r-- | mesonbuild/programs.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/mesonbuild/programs.py b/mesonbuild/programs.py index 06af320..bb14f96 100644 --- a/mesonbuild/programs.py +++ b/mesonbuild/programs.py @@ -19,6 +19,7 @@ import os import shutil import stat import sys +import re import typing as T from pathlib import Path @@ -28,6 +29,7 @@ from .mesonlib import MachineChoice if T.TYPE_CHECKING: from .environment import Environment + from .interpreter import Interpreter class ExternalProgram(mesonlib.HoldableObject): @@ -41,7 +43,8 @@ class ExternalProgram(mesonlib.HoldableObject): silent: bool = False, search_dir: T.Optional[str] = None, extra_search_dirs: T.Optional[T.List[str]] = None): self.name = name - self.path = None # type: T.Optional[str] + self.path: T.Optional[str] = None + self.cached_version: T.Optional[str] = None if command is not None: self.command = mesonlib.listify(command) if mesonlib.is_windows(): @@ -97,6 +100,24 @@ class ExternalProgram(mesonlib.HoldableObject): '''Human friendly description of the command''' return ' '.join(self.command) + def get_version(self, interpreter: 'Interpreter') -> str: + if not self.cached_version: + raw_cmd = self.get_command() + ['--version'] + cmd: T.List[T.Union[str, ExternalProgram]] = [self, '--version'] + res = interpreter.run_command_impl(interpreter.current_node, cmd, {}, True) + if res.returncode != 0: + m = 'Running {!r} failed' + raise mesonlib.MesonException(m.format(raw_cmd)) + output = res.stdout.strip() + if not output: + output = res.stderr.strip() + match = re.search(r'([0-9][0-9\.]+)', output) + if not match: + m = 'Could not find a version number in output of {!r}' + raise mesonlib.MesonException(m.format(raw_cmd)) + self.cached_version = match.group(1) + return self.cached_version + @classmethod def from_bin_list(cls, env: 'Environment', for_machine: MachineChoice, name: str) -> 'ExternalProgram': # There is a static `for_machine` for this class because the binary |