From 022632c91b255739c5100601a02b197cb29cd23e Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 12 Jan 2021 10:50:22 -0800 Subject: [PATCH] build/interperter: Add annotations and move input validation to interpreter This moves the user input validation to the interpreter, instead of being in the build module, and adds type annotations. --- mesonbuild/build.py | 10 +++------- mesonbuild/interpreter.py | 19 +++++++++++++------ mesonbuild/modules/cmake.py | 4 ++-- mesonbuild/modules/gnome.py | 2 +- mesonbuild/modules/pkgconfig.py | 2 +- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/mesonbuild/build.py b/mesonbuild/build.py index dacf68b90..736d42c3a 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -2567,19 +2567,15 @@ class ConfigurationData: # A bit poorly named, but this represents plain data files to copy # during install. class Data: - def __init__(self, sources, install_dir, install_mode=None, rename=None): + def __init__(self, sources: T.List[File], install_dir: str, + install_mode: T.Optional['FileMode'] = None, rename: T.List[str] = None): self.sources = sources self.install_dir = install_dir self.install_mode = install_mode - self.sources = listify(self.sources) - for s in self.sources: - assert(isinstance(s, File)) if rename is None: self.rename = [os.path.basename(f.fname) for f in self.sources] else: - self.rename = stringlistify(rename) - if len(self.rename) != len(self.sources): - raise MesonException('Size of rename argument is different from number of sources') + self.rename = rename class RunScript(dict): def __init__(self, script, args): diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 9466fe453..fdb6fa767 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -4300,11 +4300,11 @@ This will become a hard error in the future.''' % kwargs['input'], location=self @FeatureNewKwargs('install_data', '0.46.0', ['rename']) @FeatureNewKwargs('install_data', '0.38.0', ['install_mode']) @permittedKwargs(permitted_kwargs['install_data']) - def func_install_data(self, node, args, kwargs): + def func_install_data(self, node, args: T.List, kwargs: T.Dict[str, T.Any]): kwsource = mesonlib.stringlistify(kwargs.get('sources', [])) raw_sources = args + kwsource - sources = [] - source_strings = [] + sources: T.List[mesonlib.File] = [] + source_strings: T.List[str] = [] for s in raw_sources: if isinstance(s, mesonlib.File): sources.append(s) @@ -4313,11 +4313,18 @@ This will become a hard error in the future.''' % kwargs['input'], location=self else: raise InvalidArguments('Argument must be string or file.') sources += self.source_strings_to_files(source_strings) - install_dir = kwargs.get('install_dir', None) - if not isinstance(install_dir, (str, type(None))): + install_dir: T.Optional[str] = kwargs.get('install_dir', None) + if install_dir is not None and not isinstance(install_dir, str): raise InvalidArguments('Keyword argument install_dir not a string.') install_mode = self._get_kwarg_install_mode(kwargs) - rename = kwargs.get('rename', None) + rename: T.Optional[T.List[str]] = kwargs.get('rename', None) + if rename is not None: + rename = mesonlib.stringlistify(rename) + if len(rename) != len(sources): + raise InvalidArguments( + '"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 = DataHolder(build.Data(sources, install_dir, install_mode, rename)) self.build.data.append(data.held_object) return data diff --git a/mesonbuild/modules/cmake.py b/mesonbuild/modules/cmake.py index f6afaf35c..ef53d9f54 100644 --- a/mesonbuild/modules/cmake.py +++ b/mesonbuild/modules/cmake.py @@ -285,7 +285,7 @@ class CmakeModule(ExtensionModule): } mesonlib.do_conf_file(template_file, version_file, conf, 'meson') - res = build.Data(mesonlib.File(True, state.environment.get_scratch_dir(), version_file), pkgroot) + res = build.Data([mesonlib.File(True, state.environment.get_scratch_dir(), version_file)], pkgroot) return ModuleReturnValue(res, [res]) def create_package_file(self, infile, outfile, PACKAGE_RELATIVE_PATH, extra, confdata): @@ -370,7 +370,7 @@ class CmakeModule(ExtensionModule): if conffile not in interpreter.build_def_files: interpreter.build_def_files.append(conffile) - res = build.Data(mesonlib.File(True, ofile_path, ofile_fname), install_dir) + res = build.Data([mesonlib.File(True, ofile_path, ofile_fname)], install_dir) interpreter.build.data.append(res) return res diff --git a/mesonbuild/modules/gnome.py b/mesonbuild/modules/gnome.py index 21570bd8b..5cad9f52a 100644 --- a/mesonbuild/modules/gnome.py +++ b/mesonbuild/modules/gnome.py @@ -1629,7 +1629,7 @@ G_END_DECLS''' with open(fname, 'w') as ofile: for package in packages: ofile.write(package + '\n') - return build.Data(mesonlib.File(True, outdir, fname), install_dir) + return build.Data([mesonlib.File(True, outdir, fname)], install_dir) def _get_vapi_link_with(self, target): link_with = [] diff --git a/mesonbuild/modules/pkgconfig.py b/mesonbuild/modules/pkgconfig.py index 7d347a6f1..db589a35c 100644 --- a/mesonbuild/modules/pkgconfig.py +++ b/mesonbuild/modules/pkgconfig.py @@ -539,7 +539,7 @@ class PkgConfigModule(ExtensionModule): self.generate_pkgconfig_file(state, deps, subdirs, name, description, url, version, pcfile, conflicts, variables, False, dataonly) - res = build.Data(mesonlib.File(True, state.environment.get_scratch_dir(), pcfile), pkgroot) + res = build.Data([mesonlib.File(True, state.environment.get_scratch_dir(), pcfile)], pkgroot) variables = self.interpreter.extract_variables(kwargs, argname='uninstalled_variables', dict_new=True) variables = parse_variable_list(variables)