interpreter|build: use typed_pos_args and unholder in the interpreter

For generator.process_files. Just cleaner and nicer
pull/8822/head
Dylan Baker 4 years ago
parent 2043461b87
commit 5952dc9818
  1. 7
      mesonbuild/build.py
  2. 56
      mesonbuild/interpreter/interpreterobjects.py

@ -1543,11 +1543,12 @@ class Generator:
relpath = pathlib.PurePath(trial).relative_to(parent)
return relpath.parts[0] != '..' # For subdirs we can only go "down".
def process_files(self, files, state, preserve_path_from=None, extra_args=None):
def process_files(self, files: T.List[T.Union[str, File, 'CustomTarget', 'CustomTargetIndex', 'GeneratedList']],
state, preserve_path_from=None, extra_args=None):
new = False
output = GeneratedList(self, state.subdir, preserve_path_from, extra_args=extra_args if extra_args is not None else [])
#XXX
for e in unholder(files):
for e in files:
fs = [e]
if isinstance(e, CustomTarget):
output.depends.add(e)
@ -1561,8 +1562,6 @@ class Generator:
new = True
elif isinstance(e, str):
fs = [File.from_source_file(state.environment.source_dir, state.subdir, e)]
elif not isinstance(e, File):
raise InvalidArguments(f'{self.name} arguments must be strings, files or CustomTargets, not {e!r}.')
for f in fs:
if preserve_path_from:

@ -14,7 +14,7 @@ from .. import mlog
from ..modules import ModuleReturnValue, ModuleObject, ModuleState, ExtensionModule
from ..backend.backends import TestProtocol
from ..interpreterbase import (InterpreterObject, ObjectHolder, MutableInterpreterObject,
FeatureNewKwargs, FeatureNew, FeatureDeprecated,
FeatureNewKwargs, FeatureNew, FeatureDeprecated, typed_kwargs,
typed_pos_args, stringArgs, permittedKwargs,
noArgsFlattening, noPosargs, TYPE_var, TYPE_nkwargs,
flatten, InterpreterException, InvalidArguments, InvalidCode)
@ -620,32 +620,6 @@ class ExternalLibraryHolder(InterpreterObject, ObjectHolder[ExternalLibrary]):
pdep = self.held_object.get_partial_dependency(**kwargs)
return DependencyHolder(pdep, self.subproject)
class GeneratorHolder(InterpreterObject, ObjectHolder[build.Generator]):
def __init__(self, gen: 'build.Generator', interpreter: 'Interpreter'):
InterpreterObject.__init__(self)
ObjectHolder.__init__(self, gen, interpreter.subproject)
self.interpreter = interpreter
self.methods.update({'process': self.process_method})
@FeatureNewKwargs('generator.process', '0.45.0', ['preserve_path_from'])
@permittedKwargs({'extra_args', 'preserve_path_from'})
def process_method(self, args, kwargs):
extras = mesonlib.stringlistify(kwargs.get('extra_args', []))
if 'preserve_path_from' in kwargs:
preserve_path_from = kwargs['preserve_path_from']
if not isinstance(preserve_path_from, str):
raise InvalidArguments('Preserve_path_from must be a string.')
preserve_path_from = os.path.normpath(preserve_path_from)
if not os.path.isabs(preserve_path_from):
# This is a bit of a hack. Fix properly before merging.
raise InvalidArguments('Preserve_path_from must be an absolute path for now. Sorry.')
else:
preserve_path_from = None
gl = self.held_object.process_files('Generator', args, self.interpreter,
preserve_path_from, extra_args=extras)
return GeneratedListHolder(gl)
class GeneratedListHolder(InterpreterObject, ObjectHolder[build.GeneratedList]):
def __init__(self, arg1, extra_args=None):
@ -1045,3 +1019,31 @@ class RunTargetHolder(TargetHolder):
r = '<{} {}: {}>'
h = self.held_object
return r.format(self.__class__.__name__, h.get_id(), h.command)
class GeneratorHolder(InterpreterObject, ObjectHolder[build.Generator]):
def __init__(self, gen: 'build.Generator', interpreter: 'Interpreter'):
InterpreterObject.__init__(self)
ObjectHolder.__init__(self, gen, interpreter.subproject)
self.interpreter = interpreter
self.methods.update({'process': self.process_method})
@FeatureNewKwargs('generator.process', '0.45.0', ['preserve_path_from'])
@permittedKwargs({'extra_args', 'preserve_path_from'})
@typed_pos_args('generator.process', min_varargs=1, varargs=(str, mesonlib.File, CustomTargetHolder, CustomTargetIndexHolder, GeneratedListHolder))
def process_method(self, args: T.Tuple[T.List[T.Union[str, mesonlib.File, CustomTargetHolder, CustomTargetIndexHolder, GeneratedListHolder]]], kwargs):
extras = mesonlib.stringlistify(kwargs.get('extra_args', []))
if 'preserve_path_from' in kwargs:
preserve_path_from = kwargs['preserve_path_from']
if not isinstance(preserve_path_from, str):
raise InvalidArguments('Preserve_path_from must be a string.')
preserve_path_from = os.path.normpath(preserve_path_from)
if not os.path.isabs(preserve_path_from):
# This is a bit of a hack. Fix properly before merging.
raise InvalidArguments('Preserve_path_from must be an absolute path for now. Sorry.')
else:
preserve_path_from = None
gl = self.held_object.process_files(mesonlib.unholder(args[0]), self.interpreter,
preserve_path_from, extra_args=extras)
return GeneratedListHolder(gl)

Loading…
Cancel
Save