Skip to content

Commit

Permalink
use tempfile module
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Jul 8, 2024
1 parent 719eac7 commit 9e377aa
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 20 deletions.
8 changes: 2 additions & 6 deletions cmem_plugin_reason/plugin_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from datetime import UTC, datetime
from pathlib import Path
from tempfile import TemporaryDirectory
from time import time
from uuid import uuid4

import validators.url
from cmem.cmempy.dp.proxy.graph import get
Expand Down Expand Up @@ -245,7 +245,6 @@ def __init__( # noqa: PLR0913
self.reasoner = reasoner
self.validate_profile = validate_profile
self.max_ram_percentage = max_ram_percentage
self.temp = f"reason_{uuid4().hex}"

def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
"""Get graphs from CMEM"""
Expand Down Expand Up @@ -315,8 +314,5 @@ def _execute(self, context: ExecutionContext) -> None:

def execute(self, inputs: tuple, context: ExecutionContext) -> None: # noqa: ARG002
"""Remove temp files on error"""
try:
with TemporaryDirectory() as self.temp:
self._execute(context)
except Exception as exc:
remove_temp(self)
raise type(exc)(exc) from exc
14 changes: 3 additions & 11 deletions cmem_plugin_reason/plugin_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from datetime import UTC, datetime
from pathlib import Path
from tempfile import TemporaryDirectory
from time import time
from uuid import uuid4

import validators.url
from cmem.cmempy.dp.proxy.graph import get
Expand Down Expand Up @@ -33,7 +33,6 @@
get_provenance,
post_profiles,
post_provenance,
remove_temp,
robot,
send_result,
validate_profiles,
Expand Down Expand Up @@ -131,7 +130,6 @@ def __init__( # noqa: PLR0913
self.md_filename = md_filename if write_md else "mdfile.md"
self.validate_profile = validate_profile
self.max_ram_percentage = max_ram_percentage
self.temp = f"reason_{uuid4().hex}"

def get_graphs(self, graphs: dict, context: ExecutionContext) -> None:
"""Get graphs from CMEM"""
Expand Down Expand Up @@ -243,11 +241,5 @@ def _execute(self, context: ExecutionContext) -> Entities:

def execute(self, inputs: tuple, context: ExecutionContext) -> Entities: # noqa: ARG002
"""Remove temp files on error"""
try:
output = self._execute(context)
remove_temp(self)
except Exception as exc:
remove_temp(self)
raise type(exc)(exc) from exc
else:
return output
with TemporaryDirectory() as self.temp:
return self._execute(context)
4 changes: 1 addition & 3 deletions cmem_plugin_reason/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
from cmem_plugin_base.dataintegration.types import BoolParameterType, IntParameterType
from defusedxml import minidom

from . import __path__

REASONERS = OrderedDict(
{
"elk": "ELK",
Expand Down Expand Up @@ -218,7 +216,7 @@ def get_provenance(plugin: WorkflowPlugin, context: ExecutionContext) -> dict |

def robot(cmd: str, max_ram_percentage: int) -> CompletedProcess:
"""Run robot.jar"""
jar = Path(__path__[0]) / "bin" / "robot.jar"
jar = Path("cmem_plugin_reason") / "bin" / "robot.jar"
cmd = f"java -XX:MaxRAMPercentage={max_ram_percentage} -jar {jar} " + cmd
return run(shlex.split(cmd), check=False, capture_output=True) # noqa: S603

Expand Down

0 comments on commit 9e377aa

Please sign in to comment.