Skip to content

Commit 112e47c

Browse files
alalazotgamblin
authored andcommitted
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
1 parent 901cea7 commit 112e47c

File tree

6 files changed

+34
-38
lines changed

6 files changed

+34
-38
lines changed

lib/spack/spack/repo.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -81,43 +81,6 @@ def namespace_from_fullname(fullname):
8181
return namespace
8282

8383

84-
class _PrependFileLoader(importlib.machinery.SourceFileLoader):
85-
def __init__(self, fullname, path, prepend=None):
86-
super(_PrependFileLoader, self).__init__(fullname, path)
87-
self.prepend = prepend
88-
89-
def path_stats(self, path):
90-
stats = super(_PrependFileLoader, self).path_stats(path)
91-
if self.prepend:
92-
stats["size"] += len(self.prepend) + 1
93-
return stats
94-
95-
def get_data(self, path):
96-
data = super(_PrependFileLoader, self).get_data(path)
97-
if path != self.path or self.prepend is None:
98-
return data
99-
else:
100-
return self.prepend.encode() + b"\n" + data
101-
102-
103-
class RepoLoader(_PrependFileLoader):
104-
"""Loads a Python module associated with a package in specific repository"""
105-
106-
#: Code in ``_package_prepend`` is prepended to imported packages.
107-
#:
108-
#: Spack packages are expected to call `from spack.package import *`
109-
#: themselves, but we are allowing a deprecation period before breaking
110-
#: external repos that don't do this yet.
111-
_package_prepend = "from spack.package import *"
112-
113-
def __init__(self, fullname, repo, package_name):
114-
self.repo = repo
115-
self.package_name = package_name
116-
self.package_py = repo.filename_for_package_name(package_name)
117-
self.fullname = fullname
118-
super().__init__(self.fullname, self.package_py, prepend=self._package_prepend)
119-
120-
12184
class SpackNamespaceLoader:
12285
def create_module(self, spec):
12386
return SpackNamespace(spec.name)
@@ -187,7 +150,8 @@ def compute_loader(self, fullname):
187150
# With 2 nested conditionals we can call "repo.real_name" only once
188151
package_name = repo.real_name(module_name)
189152
if package_name:
190-
return RepoLoader(fullname, repo, package_name)
153+
module_path = repo.filename_for_package_name(package_name)
154+
return importlib.machinery.SourceFileLoader(fullname, module_path)
191155

192156
# We are importing a full namespace like 'spack.pkg.builtin'
193157
if fullname == repo.full_namespace:

lib/spack/spack/test/cmd/diff.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
_p1 = (
2121
"p1",
2222
"""\
23+
from spack.package import *
24+
2325
class P1(Package):
2426
version("1.0")
2527
@@ -35,6 +37,8 @@ class P1(Package):
3537
_p2 = (
3638
"p2",
3739
"""\
40+
from spack.package import *
41+
3842
class P2(Package):
3943
version("1.0")
4044
@@ -48,6 +52,8 @@ class P2(Package):
4852
_p3 = (
4953
"p3",
5054
"""\
55+
from spack.package import *
56+
5157
class P3(Package):
5258
version("1.0")
5359
@@ -58,6 +64,8 @@ class P3(Package):
5864
_i1 = (
5965
"i1",
6066
"""\
67+
from spack.package import *
68+
6169
class I1(Package):
6270
version("1.0")
6371
@@ -73,6 +81,8 @@ class I1(Package):
7381
_i2 = (
7482
"i2",
7583
"""\
84+
from spack.package import *
85+
7686
class I2(Package):
7787
version("1.0")
7888
@@ -89,6 +99,8 @@ class I2(Package):
8999
_p4 = (
90100
"p4",
91101
"""\
102+
from spack.package import *
103+
92104
class P4(Package):
93105
version("1.0")
94106

lib/spack/spack/test/cmd/find.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ def test_environment_with_version_range_in_compiler_doesnt_fail(tmp_path):
462462
_pkga = (
463463
"a0",
464464
"""\
465+
from spack.package import *
466+
465467
class A0(Package):
466468
version("1.2")
467469
version("1.1")
@@ -475,6 +477,8 @@ class A0(Package):
475477
_pkgb = (
476478
"b0",
477479
"""\
480+
from spack.package import *
481+
478482
class B0(Package):
479483
version("1.2")
480484
version("1.1")
@@ -485,6 +489,8 @@ class B0(Package):
485489
_pkgc = (
486490
"c0",
487491
"""\
492+
from spack.package import *
493+
488494
class C0(Package):
489495
version("1.2")
490496
version("1.1")
@@ -497,6 +503,8 @@ class C0(Package):
497503
_pkgd = (
498504
"d0",
499505
"""\
506+
from spack.package import *
507+
500508
class D0(Package):
501509
version("1.2")
502510
version("1.1")
@@ -510,6 +518,8 @@ class D0(Package):
510518
_pkge = (
511519
"e0",
512520
"""\
521+
from spack.package import *
522+
513523
class E0(Package):
514524
tags = ["tag1", "tag2"]
515525

lib/spack/spack/test/concretization/core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def repo_with_changing_recipe(tmp_path_factory, mutable_mock_repo):
188188

189189
packages_dir = repo_dir / "packages"
190190
root_pkg_str = """
191+
from spack.package import *
192+
191193
class Root(Package):
192194
homepage = "http://www.example.com"
193195
url = "http://www.example.com/root-1.0.tar.gz"
@@ -202,6 +204,8 @@ class Root(Package):
202204
package_py.write_text(root_pkg_str)
203205

204206
changing_template = """
207+
from spack.package import *
208+
205209
class Changing(Package):
206210
homepage = "http://www.example.com"
207211
url = "http://www.example.com/changing-1.0.tar.gz"

lib/spack/spack/test/directives.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ def test_version_type_validation():
148148
_pkgx = (
149149
"x",
150150
"""\
151+
from spack.package import *
152+
151153
class X(Package):
152154
version("1.3")
153155
version("1.2")
@@ -166,6 +168,8 @@ class X(Package):
166168
_pkgy = (
167169
"y",
168170
"""\
171+
from spack.package import *
172+
169173
class Y(Package):
170174
version("2.1")
171175
version("2.0")

share/spack/templates/mock-repository/package.pyt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from spack.package import *
2+
13
class {{ cls_name }}(Package):
24
homepage = "http://www.example.com"
35
url = "http://www.example.com/root-1.0.tar.gz"

0 commit comments

Comments
 (0)