Move context manager to swap the current store into spack.store
The context manager can be used to swap the current store temporarily, for any use case that may need it.
This commit is contained in:
parent
1a8963b0f4
commit
cb2c233a97
@ -23,6 +23,7 @@
|
|||||||
configuration.
|
configuration.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
import contextlib
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import six
|
import six
|
||||||
@ -227,3 +228,28 @@ def _construct_upstream_dbs_from_install_roots(
|
|||||||
accumulated_upstream_dbs.insert(0, next_db)
|
accumulated_upstream_dbs.insert(0, next_db)
|
||||||
|
|
||||||
return accumulated_upstream_dbs
|
return accumulated_upstream_dbs
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def use_store(store_or_path):
|
||||||
|
"""Use the store passed as argument within the context manager.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
store_or_path: either a Store object ot a path to where the store resides
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Store object associated with the context manager's store
|
||||||
|
"""
|
||||||
|
global store
|
||||||
|
|
||||||
|
# Normalize input arguments
|
||||||
|
temporary_store = store_or_path
|
||||||
|
if not isinstance(store_or_path, Store):
|
||||||
|
temporary_store = Store(store_or_path)
|
||||||
|
|
||||||
|
# Swap the store with the one just constructed and return it
|
||||||
|
original_store, store = store, temporary_store
|
||||||
|
yield temporary_store
|
||||||
|
|
||||||
|
# Restore the original store
|
||||||
|
store = original_store
|
||||||
|
@ -10,7 +10,8 @@
|
|||||||
|
|
||||||
import spack.main
|
import spack.main
|
||||||
import spack.modules
|
import spack.modules
|
||||||
from spack.test.conftest import use_store, use_configuration
|
import spack.store
|
||||||
|
from spack.test.conftest import use_configuration
|
||||||
|
|
||||||
module = spack.main.SpackCommand('module')
|
module = spack.main.SpackCommand('module')
|
||||||
|
|
||||||
@ -21,7 +22,7 @@ def ensure_module_files_are_there(
|
|||||||
mock_repo_path, mock_store, mock_configuration):
|
mock_repo_path, mock_store, mock_configuration):
|
||||||
"""Generate module files for module tests."""
|
"""Generate module files for module tests."""
|
||||||
module = spack.main.SpackCommand('module')
|
module = spack.main.SpackCommand('module')
|
||||||
with use_store(mock_store):
|
with spack.store.use_store(mock_store):
|
||||||
with use_configuration(mock_configuration):
|
with use_configuration(mock_configuration):
|
||||||
with spack.repo.use_repositories(mock_repo_path):
|
with spack.repo.use_repositories(mock_repo_path):
|
||||||
module('tcl', 'refresh', '-y')
|
module('tcl', 'refresh', '-y')
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
import spack.platforms.test
|
import spack.platforms.test
|
||||||
import spack.repo
|
import spack.repo
|
||||||
import spack.stage
|
import spack.stage
|
||||||
|
import spack.store
|
||||||
import spack.util.executable
|
import spack.util.executable
|
||||||
import spack.util.gpg
|
import spack.util.gpg
|
||||||
import spack.subprocess_context
|
import spack.subprocess_context
|
||||||
@ -373,15 +374,6 @@ def use_configuration(config):
|
|||||||
spack.compilers._cache_config_file = saved_compiler_cache
|
spack.compilers._cache_config_file = saved_compiler_cache
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
|
||||||
def use_store(store):
|
|
||||||
"""Context manager to swap out the global Spack store."""
|
|
||||||
saved = spack.store.store
|
|
||||||
spack.store.store = store
|
|
||||||
yield
|
|
||||||
spack.store.store = saved
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Test-specific fixtures
|
# Test-specific fixtures
|
||||||
#
|
#
|
||||||
@ -630,12 +622,11 @@ def mock_store(tmpdir_factory, mock_repo_path, mock_configuration,
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
store_path, store_cache = _store_dir_and_cache
|
store_path, store_cache = _store_dir_and_cache
|
||||||
store = spack.store.Store(str(store_path))
|
|
||||||
|
|
||||||
# If the cache does not exist populate the store and create it
|
# If the cache does not exist populate the store and create it
|
||||||
if not os.path.exists(str(store_cache.join('.spack-db'))):
|
if not os.path.exists(str(store_cache.join('.spack-db'))):
|
||||||
with use_configuration(mock_configuration):
|
with use_configuration(mock_configuration):
|
||||||
with use_store(store):
|
with spack.store.use_store(str(store_path)) as store:
|
||||||
with spack.repo.use_repositories(mock_repo_path):
|
with spack.repo.use_repositories(mock_repo_path):
|
||||||
_populate(store.db)
|
_populate(store.db)
|
||||||
store_path.copy(store_cache, mode=True, stat=True)
|
store_path.copy(store_cache, mode=True, stat=True)
|
||||||
@ -660,12 +651,11 @@ def mutable_mock_store(tmpdir_factory, mock_repo_path, mock_configuration,
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
store_path, store_cache = _store_dir_and_cache
|
store_path, store_cache = _store_dir_and_cache
|
||||||
store = spack.store.Store(str(store_path))
|
|
||||||
|
|
||||||
# If the cache does not exist populate the store and create it
|
# If the cache does not exist populate the store and create it
|
||||||
if not os.path.exists(str(store_cache.join('.spack-db'))):
|
if not os.path.exists(str(store_cache.join('.spack-db'))):
|
||||||
with use_configuration(mock_configuration):
|
with use_configuration(mock_configuration):
|
||||||
with use_store(store):
|
with spack.store.use_store(str(store_path)) as store:
|
||||||
with spack.repo.use_repositories(mock_repo_path):
|
with spack.repo.use_repositories(mock_repo_path):
|
||||||
_populate(store.db)
|
_populate(store.db)
|
||||||
store_path.copy(store_cache, mode=True, stat=True)
|
store_path.copy(store_cache, mode=True, stat=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user