From 35e1eb67631c9b47ac174a431d7543b558190171 Mon Sep 17 00:00:00 2001 From: Jack Betteridge Date: Fri, 1 Nov 2024 12:52:04 +0000 Subject: [PATCH] Remove firedrake_configuration from init --- firedrake/__init__.py | 17 +-------- firedrake/scripts/firedrake_clean | 30 +++++++-------- firedrake_configuration/__init__.py | 58 ----------------------------- 3 files changed, 17 insertions(+), 88 deletions(-) delete mode 100644 firedrake_configuration/__init__.py diff --git a/firedrake/__init__.py b/firedrake/__init__.py index 0fc9aeeed6..3c89e61429 100644 --- a/firedrake/__init__.py +++ b/firedrake/__init__.py @@ -1,22 +1,9 @@ -import firedrake_configuration import os import sys -config = firedrake_configuration.get_config() -if "PETSC_DIR" in os.environ and not config["options"]["honour_petsc_dir"]: - if os.environ["PETSC_DIR"] != os.path.join(sys.prefix, "src", "petsc")\ - or os.environ["PETSC_ARCH"] != "default": - raise ImportError("PETSC_DIR is set, but you did not install with --honour-petsc-dir.\n" - "Please unset PETSC_DIR (and PETSC_ARCH) before using Firedrake.") -elif "PETSC_DIR" not in os.environ and config["options"]["honour_petsc_dir"]: - raise ImportError("Firedrake was installed with --honour-petsc-dir, but PETSC_DIR is not set.\n" - "Please set PETSC_DIR (and PETSC_ARCH) before using Firedrake.") -elif not config["options"]["honour_petsc_dir"]: # Using our own PETSC. - os.environ["PETSC_DIR"] = os.path.join(sys.prefix, "src", "petsc") - os.environ["PETSC_ARCH"] = "default" -del config +from firedrake.configuration import setup_cache_dirs # Set up the cache directories before importing PyOP2. -firedrake_configuration.setup_cache_dirs() +setup_cache_dirs() # Ensure petsc is initialised by us before anything else gets in there. # diff --git a/firedrake/scripts/firedrake_clean b/firedrake/scripts/firedrake_clean index d3b2643aa2..47563c1823 100755 --- a/firedrake/scripts/firedrake_clean +++ b/firedrake/scripts/firedrake_clean @@ -1,8 +1,9 @@ #!/usr/bin/env python3 import os import shutil -import tempfile -import firedrake_configuration +from firedrake.configuration import setup_cache_dirs +from pyop2.compilation import clear_compiler_disk_cache as pyop2_clear_cache +from firedrake.tsfc_interface import clear_cache as tsfc_clear_cache try: import platformdirs as appdirs except ImportError: @@ -10,20 +11,19 @@ except ImportError: def main(): - firedrake_configuration.setup_cache_dirs() - tsfc_cache = os.environ.get('FIREDRAKE_TSFC_KERNEL_CACHE_DIR', - os.path.join(tempfile.gettempdir(), - 'firedrake-tsfc-kernel-cache-uid%d' % os.getuid())) - pyop2_cache = os.environ.get('PYOP2_CACHE_DIR', - os.path.join(tempfile.gettempdir(), - 'pyop2-cache-uid%d' % os.getuid())) + print("Setup cache directories") + setup_cache_dirs() + + print(f"Removing cached TSFC kernels from {os.environ.get('FIREDRAKE_TSFC_KERNEL_CACHE_DIR', '???')}") + tsfc_clear_cache() + + print(f"Removing cached PyOP2 code from {os.environ.get('FIREDRAKE_TSFC_KERNEL_CACHE_DIR', '???')}") + pyop2_clear_cache() + pytools_cache = appdirs.user_cache_dir("pytools", "pytools") - print('Removing cached TSFC kernels from %s' % tsfc_cache) - print('Removing cached PyOP2 code from %s' % pyop2_cache) - print('Removing cached pytools files from %s' % pytools_cache) - for cache in [tsfc_cache, pyop2_cache, pytools_cache]: - if os.path.exists(cache): - shutil.rmtree(cache, ignore_errors=True) + print(f"Removing cached pytools files from {pytools_cache}") + if os.path.exists(pytools_cache): + shutil.rmtree(pytools_cache, ignore_errors=True) if __name__ == '__main__': diff --git a/firedrake_configuration/__init__.py b/firedrake_configuration/__init__.py deleted file mode 100644 index b7970ab0b7..0000000000 --- a/firedrake_configuration/__init__.py +++ /dev/null @@ -1,58 +0,0 @@ -"""The :mod:`firedrake_configuration` module records the configuration -with which Firedrake was last installed or updated. It is a separate -package from Firedrake in order to ensure that `firedrake-update` can -always access the configuration, even if the :mod:`.firedrake` module -itself is broken.""" - -import json -import os -import sys -import petsc4py - -# Attempt to read configuration from file. -try: - with open(os.path.join(sys.prefix, - ".configuration.json"), "r") as f: - _config = json.load(f) - -except IOError: - # Fall back to old location. - try: - with open(os.path.join(os.path.dirname(__file__), - "configuration.json"), "r") as f: - _config = json.load(f) - - except IOError: - _config = {} - - -def petsc_packages(): - conf = petsc4py.get_config() - with open(os.path.join(conf["PETSC_DIR"], conf["PETSC_ARCH"], "include", "petscconf.h"), "r") as f: - *_, packages = next(line for line in f if line.startswith("#define PETSC_HAVE_PACKAGES")).split() - return set(packages[2:-2].split(":")) - - -options = _config.get("options", {}) -options["with_parmetis"] = "parmetis" in petsc_packages() -_config["options"] = options - - -def get_config(): - """Return the current configuration dictionary""" - return _config - - -def get_config_json(): - """Return a json serialisation of the current configuration. This - could be output by a Firedrake application to assist in the - reproduction of results.""" - return json.dumps(_config) - - -def setup_cache_dirs(): - config = get_config() - if "PYOP2_CACHE_DIR" not in os.environ: - os.environ["PYOP2_CACHE_DIR"] = os.path.join(config["options"]["cache_dir"], "pyop2") - if 'FIREDRAKE_TSFC_KERNEL_CACHE_DIR' not in os.environ: - os.environ["FIREDRAKE_TSFC_KERNEL_CACHE_DIR"] = os.path.join(config["options"]["cache_dir"], "tsfc")