summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2024-10-04 17:43:10 -0700
committerGitHub <noreply@github.com>2024-10-04 17:43:10 -0700
commitd763d6f7388216b333bff77d8f79438667003ee8 (patch)
tree211648b6776b960c2e520585320f10c5c4477920 /lib
parentd87464e9955456c2bcdffb9c3b1aa39f3a8f252b (diff)
downloadspack-d763d6f7388216b333bff77d8f79438667003ee8.tar.gz
spack-d763d6f7388216b333bff77d8f79438667003ee8.tar.bz2
spack-d763d6f7388216b333bff77d8f79438667003ee8.tar.xz
spack-d763d6f7388216b333bff77d8f79438667003ee8.zip
`gc`: restrict to specific specs (#46790)
`spack gc` has so far been a global or environment-specific thing. This adds the ability to restrict garbage collection to specific specs, e.g. if you *just* want to get rid of all your unused python installations, you could write: ```console spack gc python ``` - [x] add `constraint` arg to `spack gc` - [x] add a simple test Signed-off-by: Todd Gamblin <tgamblin@llnl.gov>
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/cmd/gc.py8
-rw-r--r--lib/spack/spack/test/cmd/gc.py16
2 files changed, 23 insertions, 1 deletions
diff --git a/lib/spack/spack/cmd/gc.py b/lib/spack/spack/cmd/gc.py
index c7d023602e..63e7aacfb8 100644
--- a/lib/spack/spack/cmd/gc.py
+++ b/lib/spack/spack/cmd/gc.py
@@ -41,7 +41,7 @@ def setup_parser(subparser):
help="do not remove installed build-only dependencies of roots\n"
"(default is to keep only link & run dependencies)",
)
- spack.cmd.common.arguments.add_common_arguments(subparser, ["yes_to_all"])
+ spack.cmd.common.arguments.add_common_arguments(subparser, ["yes_to_all", "constraint"])
def roots_from_environments(args, active_env):
@@ -97,6 +97,12 @@ def gc(parser, args):
root_hashes = None
specs = spack.store.STORE.db.unused_specs(root_hashes=root_hashes, deptype=deptype)
+
+ # limit search to constraint specs if provided
+ if args.constraint:
+ hashes = set(spec.dag_hash() for spec in args.specs())
+ specs = [spec for spec in specs if spec.dag_hash() in hashes]
+
if not specs:
tty.msg("There are no unused specs. Spack's store is clean.")
return
diff --git a/lib/spack/spack/test/cmd/gc.py b/lib/spack/spack/test/cmd/gc.py
index 2eab41a4fb..d997be59b2 100644
--- a/lib/spack/spack/test/cmd/gc.py
+++ b/lib/spack/spack/test/cmd/gc.py
@@ -36,6 +36,22 @@ def test_gc_with_build_dependency(mutable_database):
@pytest.mark.db
+def test_gc_with_constraints(mutable_database):
+ s_cmake1 = spack.spec.Spec("simple-inheritance ^cmake@3.4.3").concretized()
+ s_cmake2 = spack.spec.Spec("simple-inheritance ^cmake@3.23.1").concretized()
+ PackageInstaller([s_cmake1.package], explicit=True, fake=True).install()
+ PackageInstaller([s_cmake2.package], explicit=True, fake=True).install()
+
+ assert "There are no unused specs." in gc("python")
+
+ assert "Successfully uninstalled cmake@3.4.3" in gc("-y", "cmake@3.4.3")
+ assert "There are no unused specs." in gc("-y", "cmake@3.4.3")
+
+ assert "Successfully uninstalled cmake" in gc("-y", "cmake@3.23.1")
+ assert "There are no unused specs." in gc("-y", "cmake")
+
+
+@pytest.mark.db
def test_gc_with_environment(mutable_database, mutable_mock_env_path):
s = spack.spec.Spec("simple-inheritance")
s.concretize()