Skip to content

Commit

Permalink
Merge pull request #99 from pedohorse/fix-hip-script-input-validation
Browse files Browse the repository at this point in the history
Fix hip script input validation
  • Loading branch information
pedohorse authored Sep 1, 2024
2 parents 35efe2e + 507d9ba commit 713d47c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/lifeblood/stock_nodes/houdini/nodes/hip_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def process_task(self, context) -> ProcessingResult:
script = 'import os, hou\n'

source_hip = context.param_value('hip path')
if not source_hip:
raise ProcessingError('hip path is empty')
dest_hip = source_hip
if context.param_value('save different hip'):
dest_hip = context.param_value('save hip path')
Expand All @@ -69,7 +71,12 @@ def process_task(self, context) -> ProcessingResult:
'hou.hipFile.addEventCallback(__fix_hip_env__)\n'

script += 'def __main_body__():\n'
script += '\n'.join(f' {line}' for line in context.param_value('script').splitlines())

code_lines = '\n'.join(f' {line}' for line in context.param_value('script').splitlines())
if code_lines.strip() == '':
script += ' pass'
else:
script += code_lines

script += '\n\n' \
f'hou.hipFile.load({repr(source_hip)}, ignore_load_warnings=True)\n' \
Expand Down
60 changes: 60 additions & 0 deletions tests/nodes/test_hip_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import random
import ast
from asyncio import Event
from lifeblood.scheduler import Scheduler
from lifeblood.worker import Worker
from lifeblood.nodethings import ProcessingError
from lifeblood_testing_common.nodes_common import TestCaseBase, PseudoContext

from typing import List


class TestHipScript(TestCaseBase):
async def test_trivial(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({})

node = context.create_node('hip_script', 'footest')

node.set_param_value('hip path', '/tmp/some/path/stuff.hip')
node.set_param_value('script', 'do_things()')

res = context.process_task(node, task)
script = res.invocation_job.extra_files()['work_to_do.py']
ast.parse(script)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_empty_script(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({})

node = context.create_node('hip_script', 'footest')

node.set_param_value('hip path', '/tmp/some/path/stuff.hip')
node.set_param_value('script', '')

res = context.process_task(node, task)
script = res.invocation_job.extra_files()['work_to_do.py']
ast.parse(script)

await self._helper_test_node_with_arg_update(
_logic
)

async def test_empty_hip_path(self):
async def _logic(sched: Scheduler, workers: List[Worker], done_waiter: Event, context: PseudoContext):
task = context.create_pseudo_task_with_attrs({})

node = context.create_node('hip_script', 'footest')

node.set_param_value('hip path', '')
node.set_param_value('script', '')

self.assertRaises(ProcessingError, context.process_task, node, task)

await self._helper_test_node_with_arg_update(
_logic
)

0 comments on commit 713d47c

Please sign in to comment.