From 7dcae31c1eaa45626718a2cef5984b200ba59af0 Mon Sep 17 00:00:00 2001 From: Joseph Brill <48932340+jcbrill@users.noreply.github.com> Date: Sun, 15 May 2022 11:56:22 -0400 Subject: Change cache key to tuple and read/write list of dictionaries to json --- SCons/Tool/MSCommon/common.py | 6 ++++-- SCons/Tool/MSCommon/vc.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/SCons/Tool/MSCommon/common.py b/SCons/Tool/MSCommon/common.py index bc7cad3..591359d 100644 --- a/SCons/Tool/MSCommon/common.py +++ b/SCons/Tool/MSCommon/common.py @@ -106,7 +106,8 @@ def read_script_env_cache(): try: p = Path(CONFIG_CACHE) with p.open('r') as f: - envcache = json.load(f) + envcache_list = json.load(f) + envcache = {tuple(d['key']): d['data'] for d in envcache_list} except FileNotFoundError: # don't fail if no cache file, just proceed without it pass @@ -119,7 +120,8 @@ def write_script_env_cache(cache): try: p = Path(CONFIG_CACHE) with p.open('w') as f: - json.dump(cache, f, indent=2) + envcache_list = [{'key': list(key), 'data': data} for key, data in cache.items()] + json.dump(envcache_list, f, indent=2) except TypeError: # data can't serialize to json, don't leave partial file with suppress(FileNotFoundError): diff --git a/SCons/Tool/MSCommon/vc.py b/SCons/Tool/MSCommon/vc.py index 525051c..7f758f5 100644 --- a/SCons/Tool/MSCommon/vc.py +++ b/SCons/Tool/MSCommon/vc.py @@ -986,7 +986,7 @@ def script_env(script, args=None): if script_env_cache is None: script_env_cache = common.read_script_env_cache() - cache_key = f"{script}--{args}" if args else f"{script}" + cache_key = (script, args if args else None) cache_data = script_env_cache.get(cache_key, None) # Brief sanity check: if we got a value for the key, -- cgit v0.12 From e7e365ed20be7e7a2e0a079ca47a33416d725eca Mon Sep 17 00:00:00 2001 From: Joseph Brill <48932340+jcbrill@users.noreply.github.com> Date: Sun, 15 May 2022 14:24:42 -0400 Subject: Add comments describing reason for data structure conversion to/from json --- SCons/Tool/MSCommon/common.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SCons/Tool/MSCommon/common.py b/SCons/Tool/MSCommon/common.py index 591359d..e009fd5 100644 --- a/SCons/Tool/MSCommon/common.py +++ b/SCons/Tool/MSCommon/common.py @@ -106,6 +106,9 @@ def read_script_env_cache(): try: p = Path(CONFIG_CACHE) with p.open('r') as f: + # Convert the list of cache entry dictionaries read from + # json to the cache dictionary. Reconstruct the cache key + # tuple from the key list written to json. envcache_list = json.load(f) envcache = {tuple(d['key']): d['data'] for d in envcache_list} except FileNotFoundError: @@ -120,6 +123,9 @@ def write_script_env_cache(cache): try: p = Path(CONFIG_CACHE) with p.open('w') as f: + # Convert the cache dictionary to a list of cache entry + # dictionaries. The cache key is converted from a tuple to + # a list for compatibility with json. envcache_list = [{'key': list(key), 'data': data} for key, data in cache.items()] json.dump(envcache_list, f, indent=2) except TypeError: -- cgit v0.12