From 2c3043c633b86d061e15a746ffbe41c5bc3d4d4b Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Wed, 15 Dec 2021 00:07:59 +1000 Subject: [PATCH] Allow custom config files. - Needed to switch from a timestamp to a checksum. - Added a --config command line option. --- eris/eris/__main__.py | 46 ++++++++++++++++++++++++++++--------- eris/eris/tools.py | 9 ++++++-- eris/tests/__main___test.py | 4 +++- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/eris/eris/__main__.py b/eris/eris/__main__.py index 618e1d8..2426b40 100755 --- a/eris/eris/__main__.py +++ b/eris/eris/__main__.py @@ -18,6 +18,7 @@ import asyncio import contextlib import functools import gzip +import hashlib import importlib import importlib.resources import itertools @@ -39,7 +40,6 @@ import pyinotify import termstr import eris -import eris.tools as tools import eris.worker as worker import eris.paged_list as paged_list import sorted_collection @@ -65,6 +65,7 @@ Options: highlighting. Defaults to "native". -c TYPE, --compression=TYPE The type of compression used in the cache: gzip, lzma, bz2, or none. Defaults to gzip. + --config=FILE_NAME Use a custom config file instead of tools.toml. """ @@ -1068,21 +1069,36 @@ def chdir(path): os.chdir(old_cwd) +def source_checksum(): + resources = ["__main__.py", "tools.py"] + if "config" not in os.environ: + resources.append("tools.toml") + checksum_paths = [] + for resource in resources: + with importlib.resources.path(eris, resource) as resource_path: + checksum_paths.append(resource_path) + if "config" in os.environ: + checksum_paths.append(os.environ["config"]) + sha256 = hashlib.sha256() + for path in checksum_paths: + with open(path, "rb") as source_file: + sha256.update(source_file.read()) + return sha256.hexdigest() + + def manage_cache(root_path): cache_path = os.path.join(root_path, tools.CACHE_PATH) - timestamp_path = os.path.join(cache_path, "creation_time") + checksum_path = os.path.join(cache_path, "source_checksum") if os.path.exists(cache_path): - timestamp = os.stat(timestamp_path).st_mtime - for resource_path in ["__main__.py", "tools.py", "tools.toml"]: - with importlib.resources.path(eris, resource_path) as resource: - if resource.stat().st_mtime > timestamp: - print("Eris has been updated, so clearing the cache and" - " recalculating all results…") - shutil.rmtree(cache_path) - break + with open(checksum_path, "r") as checksum_file: + cache_checksum = checksum_file.read() + if source_checksum() != cache_checksum: + print("Eris has been changed, so clearing the cache and recalculating all results…") + shutil.rmtree(cache_path) if not os.path.exists(cache_path): os.mkdir(cache_path) - open(timestamp_path, "w").close() + with open(checksum_path, "w") as checksum_file: + checksum_file.write(source_checksum()) def print_tool_info(): @@ -1105,11 +1121,19 @@ def print_tool_info(): def check_arguments(): + global tools cmdline_help = __doc__ + USAGE.replace("*", "") arguments = docopt.docopt(cmdline_help, help=False) if arguments["--help"]: print(cmdline_help) sys.exit(0) + if arguments["--config"] is not None: + config_path = arguments["--config"] + if not os.path.exists(config_path): + print("File does not exist:", config_path) + sys.exit(1) + os.environ["ERIS_CONFIG"] = config_path + import eris.tools as tools if arguments["--info"]: print_tool_info() sys.exit(0) diff --git a/eris/eris/tools.py b/eris/eris/tools.py index d9424df..38f79ab 100755 --- a/eris/eris/tools.py +++ b/eris/eris/tools.py @@ -460,8 +460,13 @@ def make_tool_function(dependencies, command, url=None, error_status=None, elinks, git_diff, git_blame = None, None, None # For linters. -with importlib.resources.open_text(eris, "tools.toml") as tools_toml_file: - tools_toml = toml.load(tools_toml_file) + + +if "ERIS_CONFIG" in os.environ: + tools_toml = toml.load(os.environ["ERIS_CONFIG"]) +else: + with importlib.resources.open_text(eris, "tools.toml") as tools_toml_file: + tools_toml = toml.load(tools_toml_file) tools_for_extensions = tools_toml["tools_for_extensions"] del tools_toml["tools_for_extensions"] for tool_name, tool_toml in tools_toml.items(): diff --git a/eris/tests/__main___test.py b/eris/tests/__main___test.py index e1e8e94..5643dee 100755 --- a/eris/tests/__main___test.py +++ b/eris/tests/__main___test.py @@ -13,6 +13,8 @@ import fill3 import golden import eris.__main__ as __main__ +import eris.tools as tools +__main__.tools = tools os.environ["TERM"] = "xterm-256color" @@ -204,7 +206,7 @@ class MainTestCase(unittest.TestCase): with contextlib.redirect_stdout(io.StringIO()): loop.run_until_complete(__main__.main("test", root_path, worker_count=2, is_being_tested=True)) - for file_name in ["summary.pickle", "creation_time", + for file_name in ["summary.pickle", "source_checksum", "foo-metadata", "foo-contents"]: self.assertTrue(os.path.exists(".eris/" + file_name)) self.assertEqual(_mount_total(), mount_total)