From b5d2f9b5ae563f46989b974c67237a746d51a19e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= <lorenz.huedepohl@rzg.mpg.de>
Date: Tue, 24 Jul 2018 16:49:53 +0200
Subject: [PATCH] Better error handling for unset attributes

---
 mpcdf_common.py | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/mpcdf_common.py b/mpcdf_common.py
index 9153446..04c70d4 100644
--- a/mpcdf_common.py
+++ b/mpcdf_common.py
@@ -150,28 +150,28 @@ def mpcdf_enable_repositories(api_url, project, package, verbose=False):
         for enable in build.findall("./enable"):
             build.remove(enable)
 
-    compilers = get_attribute_values(api_url, project, package, "MPCDF:compiler_modules", with_project=True)
-    all_compilers = get_attribute_values(api_url, project, None, "MPCDF:compiler_modules")
-    mpis = get_attribute_values(api_url, project, package, "MPCDF:mpi_modules", with_project=True)
-    cudas = get_attribute_values(api_url, project, package, "MPCDF:cuda_modules", with_project=True)
-
     try:
-        default_compilers = get_attribute_values(api_url, project, None, "MPCDF:default_compiler")
-    except UnsetAttributeException:
-        print("ERROR: Attribute MPCDF:default_compiler not set for project", file=sys.stderr)
-        raise SystemExit(1)
+        enable_repos = get_attribute_values(api_url, project, package, "MPCDF:enable_repositories")
+    except Exception:
+        if verbose:
+            print("Warning: Could not get attribute MPCDF:enable_repositories for package {0}, skipping".format(package))
+        return False
 
-    try:
-        default_mpis = get_attribute_values(api_url, project, None, "MPCDF:default_mpi")
-    except UnsetAttributeException:
-        print("ERROR: Attribute MPCDF:default_mpi not set for project", file=sys.stderr)
-        raise SystemExit(1)
+    def try_get_attribute(attribute, package, with_project=False):
+        try:
+            return get_attribute_values(api_url, project, package, "MPCDF:" + attribute, with_project=with_project)
+        except UnsetAttributeException:
+            print("ERROR: Attribute MPCDF:" + attribute + " is not set, aborting here", file=sys.stderr)
+            raise SystemExit(1)
 
-    try:
-        default_cudas = get_attribute_values(api_url, project, None, "MPCDF:default_cuda")
-    except UnsetAttributeException:
-        print("ERROR: Attribute MPCDF:default_cuda not set for project", file=sys.stderr)
-        raise SystemExit(1)
+    compilers = try_get_attribute(package, "compiler_modules", with_project=True)
+    all_compilers = try_get_attribute(None, "compiler_modules")
+    mpis = try_get_attribute(package, "mpi_modules", with_project=True)
+    cudas = try_get_attribute(package, "cuda_modules", with_project=True)
+
+    default_compilers = try_get_attribute(None, "default_compiler")
+    default_mpis = try_get_attribute(None, "default_mpi")
+    default_cudas = try_get_attribute(None, "default_cuda")
 
     def enable(name):
         node = ElementTree.Element("enable")
@@ -181,13 +181,6 @@ def mpcdf_enable_repositories(api_url, project, package, verbose=False):
         if verbose:
             print("Enabling", name)
 
-    try:
-        enable_repos = get_attribute_values(api_url, project, package, "MPCDF:enable_repositories")
-    except Exception:
-        if verbose:
-            print("Warning: Could not get attribute MPCDF:enable_repositories for package {0}".format(package))
-        return False
-
     def actual_compilers():
         for compiler in compilers:
             if compiler == "intel":
-- 
GitLab