Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/uipath/_cli/_runtime/_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class UiPathRuntimeContext(BaseModel):
logs_file: Optional[str] = "execution.log"
logs_min_level: Optional[str] = "INFO"
output_file: str = "output.json"
execution_output_file: str = "execution_output.json"
state_file: str = "state.db"
result: Optional[UiPathRuntimeResult] = None

Expand Down Expand Up @@ -187,6 +188,7 @@ def from_config(cls, config_path=None):
mapping = {
"dir": "runtime_dir",
"outputFile": "output_file",
"executionOutputFile":"execution_output_file",
"stateFile": "state_file",
"logsFile": "logs_file",
}
Expand Down Expand Up @@ -365,11 +367,12 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
content = execution_result.to_dict()
logger.debug(content)

# Always write output file at runtime
# Always write output and execution_output files at runtime
if self.context.job_id:
with open(self.output_file_path, "w") as f:
json.dump(content, f, indent=2, default=str)

with open(self.execution_output_file_path, "w") as f:
json.dump(execution_result.output or "", f, indent=2, default=str)
# Don't suppress exceptions
return False

Expand Down Expand Up @@ -424,6 +427,13 @@ def output_file_path(self) -> str:
return os.path.join(self.context.runtime_dir, self.context.output_file)
return os.path.join("__uipath", "output.json")

@cached_property
def execution_output_file_path(self) -> str:
if self.context.runtime_dir and self.context.execution_output_file:
os.makedirs(self.context.runtime_dir, exist_ok=True)
return os.path.join(self.context.runtime_dir, self.context.execution_output_file)
return os.path.join("__uipath", "execution_output.json")

@cached_property
def state_file_path(self) -> str:
if self.context.runtime_dir and self.context.state_file:
Expand Down
24 changes: 20 additions & 4 deletions src/uipath/_cli/cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Optional
from uuid import uuid4

from ._utils._common import serialize_object
import click
from dotenv import load_dotenv

Expand All @@ -32,6 +33,7 @@ def python_run_middleware(
entrypoint: Optional[str],
input: Optional[str],
resume: bool,
**kwargs,
) -> MiddlewareResult:
"""Middleware to handle Python script execution.

Expand Down Expand Up @@ -85,12 +87,12 @@ async def execute():
)
context.logs_min_level = env.get("LOG_LEVEL", "INFO")
async with UiPathRuntime.from_context(context) as runtime:
await runtime.execute()

asyncio.run(execute())
return await runtime.execute()

result = asyncio.run(execute())
output = result.output or "No output was produced."
# Return success
return MiddlewareResult(should_continue=False)
return MiddlewareResult(should_continue=False, output=serialize_object(output))

except UiPathRuntimeError as e:
return MiddlewareResult(
Expand Down Expand Up @@ -118,6 +120,13 @@ async def execute():
type=click.Path(exists=True),
help="File path for the .json input",
)
@click.option(
"-o",
"--output-file",
required=False,
type=click.Path(exists=False),
help="File path where the output will be written",
)
@click.option(
"--debug",
is_flag=True,
Expand All @@ -135,6 +144,7 @@ def run(
input: Optional[str],
resume: bool,
file: Optional[str],
output_file: Optional[str],
debug: bool,
debug_port: int,
) -> None:
Expand All @@ -159,6 +169,12 @@ def run(
input=input,
resume=resume,
)
if output_file:
if not result.output:
console.warning("Cannot write output to file. Please update the uipath sdk extensions.")
else:
with open(output_file, "w") as f:
f.write(str(result.output))

# Handle result from middleware
if result.error_message:
Expand Down
1 change: 1 addition & 0 deletions src/uipath/_cli/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MiddlewareResult:
info_message: Optional[str] = None
error_message: Optional[str] = None
should_include_stacktrace: bool = False
output: Optional[str] = None


MiddlewareFunc = Callable[..., MiddlewareResult]
Expand Down
Loading