Check that no reserved names are overridden by package recipes (#32092)

A few attribute in packages are meant to be reserved for
Spack internal use. This audit checks packages to ensure none
of these attributes are overridden.

- [x] add additional audit check
This commit is contained in:
Massimiliano Culpo 2022-08-16 01:31:10 +02:00 committed by GitHub
parent ce0683db1d
commit 2131bd7e8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,6 +36,7 @@ def _search_duplicate_compilers(error_cls):
as input.
"""
import collections
import inspect
import itertools
import re
@ -252,6 +253,13 @@ def _search_duplicate_specs_in_externals(error_cls):
kwargs=("pkgs",),
)
package_attributes = AuditClass(
group="packages",
tag="PKG-ATTRIBUTES",
description="Sanity checks on reserved attributes of packages",
kwargs=("pkgs",),
)
#: Sanity checks on linting
# This can take some time, so it's run separately from packages
@ -313,6 +321,38 @@ def _check_patch_urls(pkgs, error_cls):
return errors
@package_attributes
def _search_for_reserved_attributes_names_in_packages(pkgs, error_cls):
"""Ensure that packages don't override reserved names"""
RESERVED_NAMES = ("name",)
errors = []
for pkg_name in pkgs:
name_definitions = collections.defaultdict(list)
pkg_cls = spack.repo.path.get_pkg_class(pkg_name)
for cls_item in inspect.getmro(pkg_cls):
for name in RESERVED_NAMES:
current_value = cls_item.__dict__.get(name)
if current_value is None:
continue
name_definitions[name].append((cls_item, current_value))
for name in RESERVED_NAMES:
if len(name_definitions[name]) == 1:
continue
error_msg = (
"Package '{}' overrides the '{}' attribute or method, "
"which is reserved for Spack internal use"
)
definitions = [
"defined in '{}'".format(x[0].__module__) for x in name_definitions[name]
]
errors.append(error_cls(error_msg.format(pkg_name, name), definitions))
return errors
@package_https_directives
def _linting_package_file(pkgs, error_cls):
"""Check for correctness of links"""