From 884b0e6e89bb1eb1c11bb155121918cae76eda32 Mon Sep 17 00:00:00 2001
From: Charlotte Hausman <chausman@nrao.edu>
Date: Wed, 21 Jul 2021 15:14:38 +0000
Subject: [PATCH] Post Apocalypse Testing Round 4 - Restores

---
 .../casa_envoy/casa_envoy/foundation.py       |   6 +-
 .../casa_envoy/casa_envoy/launchers.py        |   4 +
 .../pexable/delivery/delivery/products.py     |   2 +-
 .../7b284596590c_correcting_restore_ppr.py    | 126 ++++++++++++++++++
 4 files changed, 136 insertions(+), 2 deletions(-)
 create mode 100644 schema/versions/7b284596590c_correcting_restore_ppr.py

diff --git a/apps/cli/executables/pexable/casa_envoy/casa_envoy/foundation.py b/apps/cli/executables/pexable/casa_envoy/casa_envoy/foundation.py
index b09b3cf89..674c5f7d4 100644
--- a/apps/cli/executables/pexable/casa_envoy/casa_envoy/foundation.py
+++ b/apps/cli/executables/pexable/casa_envoy/casa_envoy/foundation.py
@@ -7,6 +7,8 @@ import sys
 import tarfile
 from pathlib import Path
 
+from distutils.dir_util import copy_tree
+
 from casa_envoy.schema import AbstractTextFile
 
 from casa_envoy.interfaces import FoundationIF
@@ -42,8 +44,10 @@ class RestoreFoundation(FoundationIF):
         if Path(cal_path).exists():
             calibration = tarfile.open(cal_path)
             # extract to products
-            calibration.extractall(path="./products")
+            calibration.extractall("./rawdata")
             calibration.close()
+            src = "./rawdata/products"
+            copy_tree(src, "./products/")
         else:
             self.logger.error(f"ERROR: calibration tar file {cal_name} not found in rawdata!")
             # abort since required data not found
diff --git a/apps/cli/executables/pexable/casa_envoy/casa_envoy/launchers.py b/apps/cli/executables/pexable/casa_envoy/casa_envoy/launchers.py
index e31bc581b..3f333db98 100644
--- a/apps/cli/executables/pexable/casa_envoy/casa_envoy/launchers.py
+++ b/apps/cli/executables/pexable/casa_envoy/casa_envoy/launchers.py
@@ -93,6 +93,10 @@ class CasaLauncher:
         for file in casa_logs:
             if re.match("^.*SEVERE\sflagmanager.*$", open(file).read()):
                 self.logger.error("CASA ERROR!")
+                sys.exit(1)
+            elif re.match("Traceback (most recent call last):", open(file).read()):
+                self.logger.error("CASA ERROR!")
+                sys.exit(1)
             else:
                 self.logger.info("CASA Success!")
 
diff --git a/apps/cli/executables/pexable/delivery/delivery/products.py b/apps/cli/executables/pexable/delivery/delivery/products.py
index c2b6c533e..7fdfb59d1 100644
--- a/apps/cli/executables/pexable/delivery/delivery/products.py
+++ b/apps/cli/executables/pexable/delivery/delivery/products.py
@@ -71,7 +71,7 @@ class CalibratedMS(SpooledProduct):
         return self.path.absolute().name
 
     def deliver_to(self, destination: Destination):
-        destination.add_directory(self.path, self.cms_name[:-3])
+        destination.add_directory(self.path, self.cms_name)
 
     def __str__(self):
         return f"calibrated MS {self.cms_name}"
diff --git a/schema/versions/7b284596590c_correcting_restore_ppr.py b/schema/versions/7b284596590c_correcting_restore_ppr.py
new file mode 100644
index 000000000..9de941985
--- /dev/null
+++ b/schema/versions/7b284596590c_correcting_restore_ppr.py
@@ -0,0 +1,126 @@
+"""correcting restore PPR
+
+Revision ID: 7b284596590c
+Revises: fd68d858d354
+Create Date: 2021-07-20 16:18:25.909424
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = "7b284596590c"
+down_revision = "fd68d858d354"
+branch_labels = None
+depends_on = None
+
+ppr_content = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<ns2:SciPipeRequest xmlns:ns2="Common/pipelinescience/SciPipeRequest">
+    <ProjectSummary>
+        <ProposalCode>VLA/{{projectCode}}</ProposalCode>
+        <Observatory>NRAO</Observatory>
+        <Telescope>VLA</Telescope>
+        <ProcessingSite>Socorro</ProcessingSite>
+        <Operator>vlapipe</Operator>
+        <Mode>SCIENCE</Mode>
+        <Version>NGRH-ALMA-10_8</Version>
+        <CreationTime>{{created_at}}</CreationTime>
+    </ProjectSummary>
+    <ProjectStructure>TBD</ProjectStructure>
+    <ProcessingRequests>
+        <RootDirectory>{{root_directory}}</RootDirectory>
+        <ProcessingRequest>
+            <ProcessingIntents/>
+            <ProcessingProcedure>
+                <ProcedureTitle>Workspaces Restore</ProcedureTitle>
+                <ProcessingCommand>
+                    <Command>hifv_restoredata</Command>
+                    <ParameterSet/>
+                </ProcessingCommand>
+                <ProcessingCommand>
+                    <Command>hifv_statwt</Command>
+                    <ParameterSet/>
+                </ProcessingCommand>
+            </ProcessingProcedure>
+            <DataSet>
+                <RelativePath>{{relative_path}}</RelativePath>
+                <SdmIdentifier>{{sdmId}}</SdmIdentifier>
+                <DataType>asdm</DataType>
+            </DataSet>
+        </ProcessingRequest>0
+    </ProcessingRequests>
+</ns2:SciPipeRequest>
+
+"""
+
+old_ppr_content = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<ns2:SciPipeRequest xmlns:ns2="Common/pipelinescience/SciPipeRequest">
+    <ProjectSummary>
+        <ProposalCode>VLA/null</ProposalCode>
+        <Observatory>NRAO</Observatory>
+        <Telescope>VLA</Telescope>
+        <ProcessingSite>Socorro</ProcessingSite>
+        <Operator>vlapipe</Operator>
+        <Mode>SCIENCE</Mode>
+        <Version>NGRH-ALMA-10_8</Version>
+        <CreationTime>{{created_at}}</CreationTime>
+    </ProjectSummary>
+    <ProjectStructure>TBD</ProjectStructure>
+    <ProcessingRequests>
+        <RootDirectory>{{root_directory}}</RootDirectory>
+        <ProcessingRequest>
+            <ProcessingIntents>
+                <Intents>
+                    <Keyword>VLA_INTERFEROMETRY_STANDARD_OBSERVING_MODE</Keyword>
+                    <Value>Undefined</Value>
+                </Intents>
+            </ProcessingIntents>
+            <ProcessingProcedure>
+                <ProcedureTitle>Workspaces Restore</ProcedureTitle>
+                <ProcessingCommand>
+                    <Command>hifv_restoredata</Command>
+                    <ParameterSet/>
+                </ProcessingCommand>
+                <ProcessingCommand>
+                    <Command>hifv_statwt</Command>
+                    <ParameterSet/>
+                </ProcessingCommand>
+            </ProcessingProcedure>
+            <DataSet>
+                <RelativePath>{{relative_path}}</RelativePath>
+                <SdmIdentifier>{{sdmId}}</SdmIdentifier>
+                <DataType>asdm</DataType>
+            </DataSet>
+        </ProcessingRequest>0
+    </ProcessingRequests>
+    <ResultsProcessing>
+        <ArchiveResults>false</ArchiveResults>
+        <CleanUpDisk>false</CleanUpDisk>
+        <UpdateProjectLifeCycle>false</UpdateProjectLifeCycle>
+        <NotifyOperatorWhenDone>false</NotifyOperatorWhenDone>
+        <SDMall>false</SDMall>
+        <SDMonly>false</SDMonly>
+        <PipelineOperatorAddress>Unknown</PipelineOperatorAddress>
+    </ResultsProcessing>
+</ns2:SciPipeRequest>
+
+"""
+
+
+def upgrade():
+    op.execute(
+        f"""
+        UPDATE workflow_templates
+        SET content=E'{ppr_content}' WHERE filename='PPR.xml' AND workflow_name='restore_cms'
+        """
+    )
+
+
+def downgrade():
+    op.execute(
+        f"""
+        UPDATE workflow_templates
+        SET content=E'{old_ppr_content}' WHERE filename='PPR.xml' AND workflow_name='restore_cms'
+        """
+    )
-- 
GitLab