Add a __reduce__ method to Environment (#25678)

* Add a __reduce__ method to Environment
* Add unit test
* Convert Path to str
This commit is contained in:
Adam J. Stewart 2021-09-15 00:37:36 -05:00 committed by GitHub
parent ef5ad4eb34
commit 0d0d438c11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View file

@ -581,6 +581,10 @@ def regenerate(self, all_specs, roots):
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 @@ def __init__(self, path, init_file=None, with_view=None, keep_relative=False):
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 @@ def __init__(self, path, init_file=None, with_view=None, keep_relative=False):
# 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

View file

@ -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)