From 0a1858c3772216e7d05add9d1eda376779746c15 Mon Sep 17 00:00:00 2001
From: Eleni Mathioulaki <emathioulaki@athenarc.gr>
Date: Tue, 4 Jul 2023 13:33:19 +0200
Subject: [PATCH] don't modify spack.yaml if no site-specific spack.yaml exists

---
 install_spack_env.sh  |  2 --
 site-config/ymerge.py | 14 ++++++++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/install_spack_env.sh b/install_spack_env.sh
index ea3b131f..a4dce99e 100644
--- a/install_spack_env.sh
+++ b/install_spack_env.sh
@@ -57,8 +57,6 @@ spack env activate $EBRAINS_SPACK_ENV
 # update environment site-configs
 rm -rf $SPACK_ENV/site-config && cp -r $EBRAINS_REPO/site-config $SPACK_ENV
 # update spack.yaml: merge top-level and site-specific spack.yaml files
-# TODO: find a more robust way to do this (maybe with yq? but we need to download the binary)
-# TODO: or add pyyaml to dependencies and use python script
 spack-python $EBRAINS_REPO/site-config/ymerge.py $EBRAINS_REPO/spack.yaml $EBRAINS_REPO/site-config/$SYSTEMNAME/spack.yaml > /tmp/spack.yaml
 cp /tmp/spack.yaml $SPACK_ENV/
 
diff --git a/site-config/ymerge.py b/site-config/ymerge.py
index 23a1f0a6..accb8779 100644
--- a/site-config/ymerge.py
+++ b/site-config/ymerge.py
@@ -1,4 +1,14 @@
-import sys
+# spack-python script that merges two environment configuration files (spack.yaml) into one
+# Usage: spack-python /path/to/first/spack.yaml /path/to/second/spack.yaml
+# (note: if the second file does not exist, the output is the first file
+
+import sys, os
 from spack.config import merge_yaml, read_config_file, syaml
 
-print(syaml.dump(merge_yaml(read_config_file(sys.argv[1]), read_config_file(sys.argv[2]))))
+if not os.path.exists(sys.argv[2]):
+    merged = syaml.dump(read_config_file(sys.argv[1]))
+else:
+    merged = syaml.dump(merge_yaml(read_config_file(sys.argv[1]), read_config_file(sys.argv[2])))
+
+print(merged)
+
-- 
GitLab