Skip to content
Snippets Groups Projects
Commit bc33dbb5 authored by Charlotte Hausman's avatar Charlotte Hausman
Browse files

fix PPR condor correction to keep correct format and encoding

parent 48a90c5a
No related branches found
No related tags found
2 merge requests!1571catch 2.8.2.3 up with main,!1550Fix PPR condor correction to keep correct format and encoding
Pipeline #13292 passed
......@@ -25,13 +25,34 @@ import shutil
from pathlib import Path
from typing import Dict, List, Union
import prettierfier
import lxml.etree
from bs4 import BeautifulSoup
from lxml import etree
from casa_envoy.enums import ProductType
from casa_envoy.interfaces import AuditorIF
from casa_envoy.schema import AbstractTextFile
PPR_HEADER = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
"""
PPR_FORMATTER = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- IDENTITY TRANSFORM -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- RUN normalize-space() ON ALL TEXT NODES -->
<xsl:template match="text()">
<xsl:copy-of select="normalize-space()"/>
</xsl:template>
</xsl:stylesheet>
"""
def get_fields_for(product_type: str, filename: str) -> list:
cal_metadata_list = [
......@@ -128,6 +149,12 @@ class AuditFiles(AuditorIF):
return True
def correct_for_condor(self, ppr: AbstractTextFile) -> AbstractTextFile:
"""
Correct PPR.xml for HTCondor directory and path
:param ppr: content of the waiting PPR
:return: updated content of PPR written to working area
"""
shutil.copy(ppr.filename, "./working")
os.chdir("./working")
......@@ -135,8 +162,22 @@ class AuditFiles(AuditorIF):
parsed_xml.find("RootDirectory").string = self.settings["rootDirectory"]
parsed_xml.find("RelativePath").string = self.settings["processingDirectory"]
strip_declaration = str(parsed_xml).split("\n")[1:]
fixed_declaration = "".join(strip_declaration)
corrected_xml = etree.fromstring(fixed_declaration)
formatter = etree.XSLT(etree.fromstring(PPR_FORMATTER))
corrected_content = lxml.etree.tostring(
formatter(corrected_xml),
pretty_print=True,
xml_declaration=True,
encoding="UTF-8",
standalone=True,
)
with open(ppr.filename, "w") as file:
file.write(prettierfier.prettify_xml(parsed_xml.prettify(), indent=4))
file.write(corrected_content.decode())
os.chdir("../")
return ppr
......@@ -150,7 +191,9 @@ class AuditFiles(AuditorIF):
self.logger.info("Correcting PPR.xml for condor processing...")
file = self.correct_for_condor(file)
valid = self.check_required_fields(file=file, fields=get_fields_for(self.product_type, file.filename))
valid = self.check_required_fields(
file=file, fields=get_fields_for(self.product_type, file.filename)
)
if not valid:
invalid_files.append(file.filename)
......@@ -173,7 +216,9 @@ class AuditDirectories(AuditorIF):
current = os.getcwd()
needed = self.rootDirectory + "/" + self.relative_path
if needed != current:
self.logger.error("DIRECTORY ERROR: not in correct directory for processing.")
self.logger.error(
"DIRECTORY ERROR: not in correct directory for processing."
)
return False
else:
working = Path(current + "/working").is_dir()
......@@ -190,9 +235,13 @@ class AuditDirectories(AuditorIF):
self.logger.info("Checking products/ for calibration tables...")
cal_data = os.listdir(Path(current + "/products/"))
if len(cal_data) > 0:
self.logger.info("Calibration data is available. Proceeding...")
self.logger.info(
"Calibration data is available. Proceeding..."
)
else:
self.logger.error("FAILURE: calibration data not found in products/")
self.logger.error(
"FAILURE: calibration data not found in products/"
)
return False
# if (
# self.parameters["product_type"] == ProductType.VLASS_IMG.value
......@@ -217,4 +266,6 @@ class AuditDirectories(AuditorIF):
self.logger.error("FAILURE: data not found in rawdata/")
return False
else:
self.logger.error("DIRECTORY ERROR: A directory is missing from the processing root directory.")
self.logger.error(
"DIRECTORY ERROR: A directory is missing from the processing root directory."
)
<?xml version="1.0" ?>
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<ns2:SciPipeRequest xmlns:ns2="Common/pipelinescience/SciPipeRequest">
<ProjectSummary>
<ProposalCode>VLA/null</ProposalCode>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment