From 112e47cc2367b888f7cd09fe37973b1843d67984 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Thu, 5 Dec 2024 20:06:41 +0100 Subject: Don't inject import statements in package recipes Remove a hack done by RepoLoader, which was injecting an extra ``` from spack.package import * ``` at the beginning of each package.py --- lib/spack/spack/repo.py | 40 ++--------------------------- lib/spack/spack/test/cmd/diff.py | 12 +++++++++ lib/spack/spack/test/cmd/find.py | 10 ++++++++ lib/spack/spack/test/concretization/core.py | 4 +++ lib/spack/spack/test/directives.py | 4 +++ 5 files changed, 32 insertions(+), 38 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/repo.py b/lib/spack/spack/repo.py index f3872aed8d..c4a104c6b9 100644 --- a/lib/spack/spack/repo.py +++ b/lib/spack/spack/repo.py @@ -81,43 +81,6 @@ def namespace_from_fullname(fullname): return namespace -class _PrependFileLoader(importlib.machinery.SourceFileLoader): - def __init__(self, fullname, path, prepend=None): - super(_PrependFileLoader, self).__init__(fullname, path) - self.prepend = prepend - - def path_stats(self, path): - stats = super(_PrependFileLoader, self).path_stats(path) - if self.prepend: - stats["size"] += len(self.prepend) + 1 - return stats - - def get_data(self, path): - data = super(_PrependFileLoader, self).get_data(path) - if path != self.path or self.prepend is None: - return data - else: - return self.prepend.encode() + b"\n" + data - - -class RepoLoader(_PrependFileLoader): - """Loads a Python module associated with a package in specific repository""" - - #: Code in ``_package_prepend`` is prepended to imported packages. - #: - #: Spack packages are expected to call `from spack.package import *` - #: themselves, but we are allowing a deprecation period before breaking - #: external repos that don't do this yet. - _package_prepend = "from spack.package import *" - - def __init__(self, fullname, repo, package_name): - self.repo = repo - self.package_name = package_name - self.package_py = repo.filename_for_package_name(package_name) - self.fullname = fullname - super().__init__(self.fullname, self.package_py, prepend=self._package_prepend) - - class SpackNamespaceLoader: def create_module(self, spec): return SpackNamespace(spec.name) @@ -187,7 +150,8 @@ class ReposFinder: # With 2 nested conditionals we can call "repo.real_name" only once package_name = repo.real_name(module_name) if package_name: - return RepoLoader(fullname, repo, package_name) + module_path = repo.filename_for_package_name(package_name) + return importlib.machinery.SourceFileLoader(fullname, module_path) # We are importing a full namespace like 'spack.pkg.builtin' if fullname == repo.full_namespace: diff --git a/lib/spack/spack/test/cmd/diff.py b/lib/spack/spack/test/cmd/diff.py index e352ce1352..125a646253 100644 --- a/lib/spack/spack/test/cmd/diff.py +++ b/lib/spack/spack/test/cmd/diff.py @@ -20,6 +20,8 @@ find_cmd = spack.main.SpackCommand("find") _p1 = ( "p1", """\ +from spack.package import * + class P1(Package): version("1.0") @@ -35,6 +37,8 @@ class P1(Package): _p2 = ( "p2", """\ +from spack.package import * + class P2(Package): version("1.0") @@ -48,6 +52,8 @@ class P2(Package): _p3 = ( "p3", """\ +from spack.package import * + class P3(Package): version("1.0") @@ -58,6 +64,8 @@ class P3(Package): _i1 = ( "i1", """\ +from spack.package import * + class I1(Package): version("1.0") @@ -73,6 +81,8 @@ class I1(Package): _i2 = ( "i2", """\ +from spack.package import * + class I2(Package): version("1.0") @@ -89,6 +99,8 @@ class I2(Package): _p4 = ( "p4", """\ +from spack.package import * + class P4(Package): version("1.0") diff --git a/lib/spack/spack/test/cmd/find.py b/lib/spack/spack/test/cmd/find.py index 5398dff469..656ddac442 100644 --- a/lib/spack/spack/test/cmd/find.py +++ b/lib/spack/spack/test/cmd/find.py @@ -462,6 +462,8 @@ def test_environment_with_version_range_in_compiler_doesnt_fail(tmp_path): _pkga = ( "a0", """\ +from spack.package import * + class A0(Package): version("1.2") version("1.1") @@ -475,6 +477,8 @@ class A0(Package): _pkgb = ( "b0", """\ +from spack.package import * + class B0(Package): version("1.2") version("1.1") @@ -485,6 +489,8 @@ class B0(Package): _pkgc = ( "c0", """\ +from spack.package import * + class C0(Package): version("1.2") version("1.1") @@ -497,6 +503,8 @@ class C0(Package): _pkgd = ( "d0", """\ +from spack.package import * + class D0(Package): version("1.2") version("1.1") @@ -510,6 +518,8 @@ class D0(Package): _pkge = ( "e0", """\ +from spack.package import * + class E0(Package): tags = ["tag1", "tag2"] diff --git a/lib/spack/spack/test/concretization/core.py b/lib/spack/spack/test/concretization/core.py index e33f9761da..8f136d46a4 100644 --- a/lib/spack/spack/test/concretization/core.py +++ b/lib/spack/spack/test/concretization/core.py @@ -188,6 +188,8 @@ repo: packages_dir = repo_dir / "packages" root_pkg_str = """ +from spack.package import * + class Root(Package): homepage = "http://www.example.com" url = "http://www.example.com/root-1.0.tar.gz" @@ -202,6 +204,8 @@ class Root(Package): package_py.write_text(root_pkg_str) changing_template = """ +from spack.package import * + class Changing(Package): homepage = "http://www.example.com" url = "http://www.example.com/changing-1.0.tar.gz" diff --git a/lib/spack/spack/test/directives.py b/lib/spack/spack/test/directives.py index fb75b2c0ed..6b38b282f4 100644 --- a/lib/spack/spack/test/directives.py +++ b/lib/spack/spack/test/directives.py @@ -148,6 +148,8 @@ def test_version_type_validation(): _pkgx = ( "x", """\ +from spack.package import * + class X(Package): version("1.3") version("1.2") @@ -166,6 +168,8 @@ class X(Package): _pkgy = ( "y", """\ +from spack.package import * + class Y(Package): version("2.1") version("2.0") -- cgit v1.2.3-70-g09d2