From 4072b31d06bc93a59a5db8f0bd8dcddf347ba71e Mon Sep 17 00:00:00 2001
From: Filippo Cremonese <filippocremonese@rev.ng>
Date: Thu, 27 Aug 2020 18:50:08 +0200
Subject: [PATCH] Improved configuration cache handling

---
 orchestra/config.py | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/orchestra/config.py b/orchestra/config.py
index 52384a2..462a843 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
-- 
GitLab