typing: Fully annotate dependencies.configtool

pull/8837/head
Daniel Mensinger 4 years ago
parent 1d0bd562f1
commit 969ee9d85b
  1. 39
      mesonbuild/dependencies/configtool.py
  2. 1
      run_mypy.py

@ -19,6 +19,11 @@ from .. import mlog
import re
import typing as T
from mesonbuild import mesonlib
if T.TYPE_CHECKING:
from ..environment import Environment
class ConfigToolDependency(ExternalDependency):
"""Class representing dependencies found using a config tool.
@ -30,12 +35,12 @@ class ConfigToolDependency(ExternalDependency):
Because some tools are stupid and don't return 0
"""
tools = None
tool_name = None
tools: T.Optional[T.List[str]] = None
tool_name: T.Optional[str] = None
version_arg = '--version'
__strip_version = re.compile(r'^[0-9][0-9.]+')
def __init__(self, name, environment, kwargs, language: T.Optional[str] = None):
def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any], language: T.Optional[str] = None):
super().__init__('config-tool', environment, kwargs, language=language)
self.name = name
# You may want to overwrite the class version in some cases
@ -45,7 +50,11 @@ class ConfigToolDependency(ExternalDependency):
if 'version_arg' in kwargs:
self.version_arg = kwargs['version_arg']
req_version = kwargs.get('version', None)
req_version_raw = kwargs.get('version', None)
if req_version_raw is not None:
req_version = mesonlib.stringlistify(req_version_raw)
else:
req_version = []
tool, version = self.find_config(req_version, kwargs.get('returncode_value', 0))
self.config = tool
self.is_found = self.report_config(version, req_version)
@ -54,7 +63,7 @@ class ConfigToolDependency(ExternalDependency):
return
self.version = version
def _sanitize_version(self, version):
def _sanitize_version(self, version: str) -> str:
"""Remove any non-numeric, non-point version suffixes."""
m = self.__strip_version.match(version)
if m:
@ -63,14 +72,12 @@ class ConfigToolDependency(ExternalDependency):
return m.group(0).rstrip('.')
return version
def find_config(self, versions: T.Optional[T.List[str]] = None, returncode: int = 0) \
-> T.Tuple[T.Optional[str], T.Optional[str]]:
def find_config(self, versions: T.List[str], returncode: int = 0) \
-> T.Tuple[T.Optional[T.List[str]], T.Optional[str]]:
"""Helper method that searches for config tool binaries in PATH and
returns the one that best matches the given version requirements.
"""
if not isinstance(versions, list) and versions is not None:
versions = listify(versions)
best_match = (None, None) # type: T.Tuple[T.Optional[str], T.Optional[str]]
best_match: T.Tuple[T.Optional[T.List[str]], T.Optional[str]] = (None, None)
for potential_bin in find_external_program(
self.env, self.for_machine, self.tool_name,
self.tool_name, self.tools, allow_default_for_cross=False):
@ -105,14 +112,14 @@ class ConfigToolDependency(ExternalDependency):
return best_match
def report_config(self, version, req_version):
def report_config(self, version: T.Optional[str], req_version: T.List[str]) -> bool:
"""Helper method to print messages about the tool."""
found_msg = [mlog.bold(self.tool_name), 'found:']
found_msg: T.List[T.Union[str, mlog.AnsiDecorator]] = [mlog.bold(self.tool_name), 'found:']
if self.config is None:
found_msg.append(mlog.red('NO'))
if version is not None and req_version is not None:
if version is not None and req_version:
found_msg.append(f'found {version!r} but need {req_version!r}')
elif req_version:
found_msg.append(f'need {req_version!r}')
@ -134,10 +141,10 @@ class ConfigToolDependency(ExternalDependency):
return split_args(out)
@staticmethod
def get_methods():
def get_methods() -> T.List[DependencyMethods]:
return [DependencyMethods.AUTO, DependencyMethods.CONFIG_TOOL]
def get_configtool_variable(self, variable_name):
def get_configtool_variable(self, variable_name: str) -> str:
p, out, _ = Popen_safe(self.config + [f'--{variable_name}'])
if p.returncode != 0:
if self.required:
@ -148,7 +155,7 @@ class ConfigToolDependency(ExternalDependency):
mlog.debug(f'Got config-tool variable {variable_name} : {variable}')
return variable
def log_tried(self):
def log_tried(self) -> str:
return self.type_name
def get_variable(self, *, cmake: T.Optional[str] = None, pkgconfig: T.Optional[str] = None,

@ -20,6 +20,7 @@ modules = [
# 'mesonbuild/coredata.py',
'mesonbuild/dependencies/base.py',
'mesonbuild/dependencies/coarrays.py',
'mesonbuild/dependencies/configtool.py',
'mesonbuild/dependencies/boost.py',
'mesonbuild/dependencies/hdf5.py',
'mesonbuild/dependencies/mpi.py',

Loading…
Cancel
Save