summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam J. Stewart <ajstewart426@gmail.com>2021-09-15 00:37:36 -0500
committerGitHub <noreply@github.com>2021-09-14 22:37:36 -0700
commit0d0d438c119a770ba1d9db3126df1993b70448ca (patch)
treec2d5417663d9c90ed8b6ca37ca1bbee9df7ec223
parentef5ad4eb34b351ba650ca92409c0295d5569efe4 (diff)
downloadspack-0d0d438c119a770ba1d9db3126df1993b70448ca.tar.gz
spack-0d0d438c119a770ba1d9db3126df1993b70448ca.tar.bz2
spack-0d0d438c119a770ba1d9db3126df1993b70448ca.tar.xz
spack-0d0d438c119a770ba1d9db3126df1993b70448ca.zip
Add a __reduce__ method to Environment (#25678)
* Add a __reduce__ method to Environment * Add unit test * Convert Path to str
-rw-r--r--lib/spack/spack/environment.py12
-rw-r--r--lib/spack/spack/test/environment.py15
2 files changed, 27 insertions, 0 deletions
diff --git a/lib/spack/spack/environment.py b/lib/spack/spack/environment.py
index fae23335fc..7a9a4735fc 100644
--- a/lib/spack/spack/environment.py
+++ b/lib/spack/spack/environment.py
@@ -581,6 +581,10 @@ class ViewDescriptor(object):
tty.warn(msg)
+def _create_environment(*args, **kwargs):
+ return Environment(*args, **kwargs)
+
+
class Environment(object):
def __init__(self, path, init_file=None, with_view=None, keep_relative=False):
"""Create a new environment.
@@ -601,6 +605,9 @@ class Environment(object):
directory.
"""
self.path = os.path.abspath(path)
+ self.init_file = init_file
+ self.with_view = with_view
+ self.keep_relative = keep_relative
self.txlock = lk.Lock(self._transaction_lock_path)
@@ -643,6 +650,11 @@ class Environment(object):
# If with_view is None, then defer to the view settings determined by
# the manifest file
+ def __reduce__(self):
+ return _create_environment, (
+ self.path, self.init_file, self.with_view, self.keep_relative
+ )
+
def _rewrite_relative_paths_on_relocation(self, init_file_dir):
"""When initializing the environment from a manifest file and we plan
to store the environment in a different directory, we have to rewrite
diff --git a/lib/spack/spack/test/environment.py b/lib/spack/spack/test/environment.py
new file mode 100644
index 0000000000..145f390b28
--- /dev/null
+++ b/lib/spack/spack/test/environment.py
@@ -0,0 +1,15 @@
+# Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
+# Spack Project Developers. See the top-level COPYRIGHT file for details.
+#
+# SPDX-License-Identifier: (Apache-2.0 OR MIT)
+
+import pickle
+
+from spack.environment import Environment
+
+
+def test_environment_pickle(tmpdir):
+ env1 = Environment(str(tmpdir))
+ obj = pickle.dumps(env1)
+ env2 = pickle.loads(obj)
+ assert isinstance(env2, Environment)