From 22f740cca5df4382f31a63db66e8a11ed7f48459 Mon Sep 17 00:00:00 2001
From: chausman <chausman@nrao.edu>
Date: Mon, 17 Jun 2024 12:27:08 -0600
Subject: [PATCH] fix default determination to handle symlinked casa installs

---
 .../system/services/casa_matrix_service.py    | 41 ++++++++++++-------
 1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/shared/workspaces/workspaces/system/services/casa_matrix_service.py b/shared/workspaces/workspaces/system/services/casa_matrix_service.py
index 2148d3827..50e2c50e4 100644
--- a/shared/workspaces/workspaces/system/services/casa_matrix_service.py
+++ b/shared/workspaces/workspaces/system/services/casa_matrix_service.py
@@ -17,6 +17,7 @@
 #
 import logging
 import os
+import pathlib
 import re
 import shutil
 
@@ -57,11 +58,29 @@ def casa_version_from_path(path: str) -> str:
     # Defaults
     casa_version, pipeline_version = "unknown", "default"
 
-    cv_search = re.search(CASA_VERSION_REGEX, path)
-    pv_search = re.search(PIPELINE_VERSION_REGEX, path)
+    def check_regex(regex: str, path_str: str) -> re.Match[str] | None:
+        """
+        check casa paths against version and pipeline regexes
+        :param regex:
+        :param path_str:
+        :return:
+        """
+        return re.search(regex, path_str)
+
+    cv_search = check_regex(CASA_VERSION_REGEX, path)
+
+    if cv_search is None:
+        # this is probably a default link, make sure we use the actual version path
+        path2 = pathlib.Path(path)
+        if path2.is_symlink():
+            path = str(path2.readlink())
+            cv_search = check_regex(CASA_VERSION_REGEX, path)
+
+    pv_search = check_regex(PIPELINE_VERSION_REGEX, path)
+
     if cv_search:
         # Replace last '-' in the directory name with '.' to match versions stored in the database
-        casa_version = '.'.join(cv_search.group(1).rsplit('-', 1))
+        casa_version = ".".join(cv_search.group(1).rsplit("-", 1))
     if pv_search:
         # Strip el* from the release name (used to be a thing)
         pipeline_version = re.sub(EL_SUFFIX_REGEX, "", pv_search.group(1))
@@ -80,7 +99,7 @@ def natural_sort(in_list: list, reverse: bool = False) -> list:
     :return: A naturally sorted list
     """
     convert = lambda text: int(text) if text.isdigit() else text.lower()
-    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
+    alphanum_key = lambda key: [convert(c) for c in re.split("([0-9]+)", key)]
     return sorted(in_list, key=alphanum_key, reverse=reverse)
 
 
@@ -93,7 +112,7 @@ class CasaMatrixService(CasaMatrixServiceIF):
         self.session = session
 
         # Used for linking and looking up linked CASA versions
-        self.env_dir = str(CapoConfig().profile).replace('dsoc-', '')
+        self.env_dir = str(CapoConfig().profile).replace("dsoc-", "")
 
         # A valid version will cross-check installed versions in the casa_root against allowed versions in the database
         self.installed_versions = []
@@ -199,10 +218,7 @@ class CasaMatrixService(CasaMatrixServiceIF):
         return ret
 
     def get_version(
-        self,
-        version: str | None = None,
-        capability: str | None = None,
-        telescope: str | None = None
+        self, version: str | None = None, capability: str | None = None, telescope: str | None = None
     ) -> dict[str, str]:
         """
         Returns the CASA version that is valid for processing.
@@ -219,10 +235,7 @@ class CasaMatrixService(CasaMatrixServiceIF):
         return versions[0] if versions else {}
 
     def get_versions(
-        self,
-        version: str | None = None,
-        capability: str | None = None,
-        telescope: str | None = None
+        self, version: str | None = None, capability: str | None = None, telescope: str | None = None
     ) -> list[dict[str, str]]:
         """
         Returns the CASA versions that are valid for processing as a dict.
@@ -283,7 +296,7 @@ class CasaMatrixService(CasaMatrixServiceIF):
             return None
 
         # Some capabilities have different paths per telescope (looking at you ALMA)
-        if len(keys) > 1 and telescope and telescope.lower() == 'alma':
+        if len(keys) > 1 and telescope and telescope.lower() == "alma":
             key = keys[1]
         else:
             key = keys[0]
-- 
GitLab