modules/python: simplify a number of interfaces

Including not calling back into `Interpreter.func_*`, which is not a
good idea both from a type saftey and perforamance point of view.
Instead there's now a shared _impl method
pull/8992/head
Dylan Baker 3 years ago
parent 9eec2a131b
commit a881e849b5
  1. 9
      mesonbuild/interpreter/interpreter.py
  2. 60
      mesonbuild/modules/python.py

@ -2032,9 +2032,12 @@ This will become a hard error in the future.''' % kwargs['input'], location=self
'"rename" and "sources" argument lists must be the same length if "rename" is given. '
f'Rename has {len(rename)} elements and sources has {len(sources)}.')
data = build.Data(
sources, kwargs['install_dir'], kwargs['install_mode'],
self.subproject, rename)
return self.install_data_impl(sources, kwargs['install_dir'], kwargs['install_mode'], rename)
def install_data_impl(self, sources: T.List[mesonlib.File], install_dir: str,
install_mode: FileMode, rename: T.Optional[str]) -> build.Data:
"""Just the implementation with no validation."""
data = build.Data(sources, install_dir, install_mode, self.subproject, rename)
self.build.data.append(data)
return data

@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from os import name
from pathlib import Path
import os
import json
@ -28,7 +27,7 @@ from ..environment import detect_cpu_family
from ..interpreter import ExternalProgramHolder, extract_required_kwarg, permitted_dependency_kwargs
from ..interpreterbase import (
noPosargs, noKwargs, permittedKwargs,
InvalidArguments, typed_pos_args,
InvalidArguments, typed_pos_args, typed_kwargs, KwargInfo,
FeatureNew, FeatureNewKwargs, disablerIfNotFound
)
from ..mesonlib import MachineChoice, MesonException
@ -331,6 +330,17 @@ class PythonExternalProgram(ExternalProgram):
}
_PURE_KW = KwargInfo('pure', bool, default=True)
_SUBDIR_KW = KwargInfo('subdir', str, default='')
if T.TYPE_CHECKING:
class PyInstallKw(TypedDict):
pure: bool
subdir: str
class PythonInstallation(ExternalProgramHolder):
def __init__(self, python: 'PythonExternalProgram', interpreter: 'Interpreter'):
ExternalProgramHolder.__init__(self, python, interpreter)
@ -409,40 +419,24 @@ class PythonInstallation(ExternalProgramHolder):
raise mesonlib.MesonException('Python dependency not found')
return dep
@permittedKwargs(['pure', 'subdir'])
def install_sources_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs') -> 'Data':
pure = kwargs.pop('pure', True)
if not isinstance(pure, bool):
raise InvalidArguments('"pure" argument must be a boolean.')
subdir = kwargs.pop('subdir', '')
if not isinstance(subdir, str):
raise InvalidArguments('"subdir" argument must be a string.')
if pure:
kwargs['install_dir'] = os.path.join(self.purelib_install_path, subdir)
else:
kwargs['install_dir'] = os.path.join(self.platlib_install_path, subdir)
return self.interpreter.func_install_data(None, args, kwargs)
@typed_pos_args('install_data', varargs=(str, mesonlib.File))
@typed_kwargs('python_installation.install_sources', _PURE_KW, _SUBDIR_KW)
def install_sources_method(self, args: T.Tuple[T.List[T.Union[str, mesonlib.File]]],
kwargs: 'PyInstallKw') -> 'Data':
return self.interpreter.install_data_impl(
self.interpreter.source_strings_to_files(args[0]),
self._get_install_dir_impl(kwargs['pure'], kwargs['subdir']),
mesonlib.FileMode(),
None)
@noPosargs
@permittedKwargs(['pure', 'subdir'])
def get_install_dir_method(self, args: T.List['TYPE_var'], kwargs: 'TYPE_kwargs') -> str:
pure = kwargs.pop('pure', True)
if not isinstance(pure, bool):
raise InvalidArguments('"pure" argument must be a boolean.')
subdir = kwargs.pop('subdir', '')
if not isinstance(subdir, str):
raise InvalidArguments('"subdir" argument must be a string.')
if pure:
res = os.path.join(self.purelib_install_path, subdir)
else:
res = os.path.join(self.platlib_install_path, subdir)
@typed_kwargs('python_installation.install_dir', _PURE_KW, _SUBDIR_KW)
def get_install_dir_method(self, args: T.List['TYPE_var'], kwargs: 'PyInstallKw') -> str:
return self._get_install_dir_impl(kwargs['pure'], kwargs['subdir'])
return res
def _get_install_dir_impl(self, pure: bool, subdir: str) -> str:
return os.path.join(
self.purelib_install_path if pure else self.platlib_install_path, subdir)
@noPosargs
@noKwargs

Loading…
Cancel
Save