diff --git a/orchestra/config.py b/orchestra/config.py index 52384a2908a2a99a8f655a419afa74f7b6fa9127..462a8430cc312b4da72d07dea1566d6f3da033ce 100644 --- a/orchestra/config.py +++ b/orchestra/config.py @@ -3,25 +3,34 @@ import yaml import os +def hash_config_dir(config_dir): + hash_script = f"""find "{config_dir}" -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum""" + config_hash = subprocess.check_output(hash_script, shell=True).decode("utf-8").strip().partition(" ")[0] + return config_hash + + def gen_yaml(config_dir, use_cache=True): # TODO: this method of obtaining the orchestra directory is a hack and is duplicated in environment.py orchestra_dir = os.path.dirname(os.path.realpath(__file__ + "/..")) - config_cache_dir = f"{orchestra_dir}/.orchestra/config_cache" + config_cache_dir = f"{orchestra_dir}/.orchestra" + config_cache_file = f"{config_cache_dir}/config_cache" if use_cache: - hash_script = f"""find "{config_dir}" -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum""" - config_hash = subprocess.check_output(hash_script, shell=True).decode("utf-8").strip().partition(" ")[0] - config_cache_file = f"{config_cache_dir}/{config_hash}.yml" + config_hash = hash_config_dir(config_dir) if os.path.exists(config_cache_file): with open(config_cache_file, "rb") as f: - return f.read() + cached_hash = f.readline().decode("utf-8").strip() + if config_hash == cached_hash: + return f.read() expanded_yaml = subprocess.check_output(f"GOGC=off ytt -f {config_dir}", shell=True) if use_cache: os.makedirs(config_cache_dir, exist_ok=True) + with open(config_cache_file, "wb") as f: + f.write(config_hash.encode("utf-8") + b"\n") f.write(expanded_yaml) return expanded_yaml