From 0d0d438c119a770ba1d9db3126df1993b70448ca Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Wed, 15 Sep 2021 00:37:36 -0500 Subject: [PATCH] Add a __reduce__ method to Environment (#25678) * Add a __reduce__ method to Environment * Add unit test * Convert Path to str --- lib/spack/spack/environment.py | 12 ++++++++++++ lib/spack/spack/test/environment.py | 15 +++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 lib/spack/spack/test/environment.py 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 @@ 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 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)