Skip to content

Commit 3709d1d

Browse files
committed
feat: add output-file flag on CLI run and execution_output_file on cloud execution
1 parent 55de34c commit 3709d1d

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/uipath/_cli/_runtime/_contracts.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class UiPathRuntimeContext(BaseModel):
153153
logs_file: Optional[str] = "execution.log"
154154
logs_min_level: Optional[str] = "INFO"
155155
output_file: str = "output.json"
156+
execution_output_file: str = "execution_output.json"
156157
state_file: str = "state.db"
157158
result: Optional[UiPathRuntimeResult] = None
158159

@@ -187,6 +188,7 @@ def from_config(cls, config_path=None):
187188
mapping = {
188189
"dir": "runtime_dir",
189190
"outputFile": "output_file",
191+
"executionOutputFile":"execution_output_file",
190192
"stateFile": "state_file",
191193
"logsFile": "logs_file",
192194
}
@@ -365,11 +367,12 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
365367
content = execution_result.to_dict()
366368
logger.debug(content)
367369

368-
# Always write output file at runtime
370+
# Always write output and execution_output files at runtime
369371
if self.context.job_id:
370372
with open(self.output_file_path, "w") as f:
371373
json.dump(content, f, indent=2, default=str)
372-
374+
with open(self.execution_output_file_path, "w") as f:
375+
json.dump(execution_result.output or "", f, indent=2, default=str)
373376
# Don't suppress exceptions
374377
return False
375378

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

430+
@cached_property
431+
def execution_output_file_path(self) -> str:
432+
if self.context.runtime_dir and self.context.execution_output_file:
433+
os.makedirs(self.context.runtime_dir, exist_ok=True)
434+
return os.path.join(self.context.runtime_dir, self.context.execution_output_file)
435+
return os.path.join("__uipath", "execution_output.json")
436+
427437
@cached_property
428438
def state_file_path(self) -> str:
429439
if self.context.runtime_dir and self.context.state_file:

src/uipath/_cli/cli_run.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Optional
77
from uuid import uuid4
88

9+
from ._utils._common import serialize_object
910
import click
1011
from dotenv import load_dotenv
1112

@@ -32,6 +33,7 @@ def python_run_middleware(
3233
entrypoint: Optional[str],
3334
input: Optional[str],
3435
resume: bool,
36+
**kwargs,
3537
) -> MiddlewareResult:
3638
"""Middleware to handle Python script execution.
3739
@@ -85,12 +87,12 @@ async def execute():
8587
)
8688
context.logs_min_level = env.get("LOG_LEVEL", "INFO")
8789
async with UiPathRuntime.from_context(context) as runtime:
88-
await runtime.execute()
89-
90-
asyncio.run(execute())
90+
return await runtime.execute()
9191

92+
result = asyncio.run(execute())
93+
output = result.output or "No output was produced."
9294
# Return success
93-
return MiddlewareResult(should_continue=False)
95+
return MiddlewareResult(should_continue=False, output=serialize_object(output))
9496

9597
except UiPathRuntimeError as e:
9698
return MiddlewareResult(
@@ -118,6 +120,13 @@ async def execute():
118120
type=click.Path(exists=True),
119121
help="File path for the .json input",
120122
)
123+
@click.option(
124+
"-o",
125+
"--output-file",
126+
required=False,
127+
type=click.Path(exists=False),
128+
help="File path where the output will be written",
129+
)
121130
@click.option(
122131
"--debug",
123132
is_flag=True,
@@ -135,6 +144,7 @@ def run(
135144
input: Optional[str],
136145
resume: bool,
137146
file: Optional[str],
147+
output_file: Optional[str],
138148
debug: bool,
139149
debug_port: int,
140150
) -> None:
@@ -159,6 +169,12 @@ def run(
159169
input=input,
160170
resume=resume,
161171
)
172+
if output_file:
173+
if not result.output:
174+
console.warning("Cannot write output to file. Please update the uipath sdk extensions.")
175+
else:
176+
with open(output_file, "w") as f:
177+
f.write(str(result.output))
162178

163179
# Handle result from middleware
164180
if result.error_message:

src/uipath/_cli/middlewares.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MiddlewareResult:
1616
info_message: Optional[str] = None
1717
error_message: Optional[str] = None
1818
should_include_stacktrace: bool = False
19+
output: Optional[str] = None
1920

2021

2122
MiddlewareFunc = Callable[..., MiddlewareResult]

0 commit comments

Comments
 (0)