2121 UiPathTraceContext ,
2222)
2323from ._runtime ._runtime import UiPathRuntime
24+ from ._utils ._common import serialize_object
2425from ._utils ._console import ConsoleLogger
2526from .middlewares import MiddlewareResult , Middlewares
2627
@@ -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,14 @@ 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 ()
90+ return await runtime .execute ()
8991
90- asyncio .run (execute ())
92+ result = asyncio .run (execute ())
9193
9294 # Return success
93- return MiddlewareResult (should_continue = False )
95+ return MiddlewareResult (
96+ should_continue = False , output = serialize_object (result .output )
97+ )
9498
9599 except UiPathRuntimeError as e :
96100 return MiddlewareResult (
@@ -118,6 +122,18 @@ async def execute():
118122 type = click .Path (exists = True ),
119123 help = "File path for the .json input" ,
120124)
125+ @click .option (
126+ "--input-file" ,
127+ required = False ,
128+ type = click .Path (exists = True ),
129+ help = "Alias for '-f/--file' arguments" ,
130+ )
131+ @click .option (
132+ "--output-file" ,
133+ required = False ,
134+ type = click .Path (exists = False ),
135+ help = "File path where the output will be written" ,
136+ )
121137@click .option (
122138 "--debug" ,
123139 is_flag = True ,
@@ -135,30 +151,37 @@ def run(
135151 input : Optional [str ],
136152 resume : bool ,
137153 file : Optional [str ],
154+ input_file : Optional [str ],
155+ output_file : Optional [str ],
138156 debug : bool ,
139157 debug_port : int ,
140158) -> None :
141159 """Execute the project."""
142- if file :
160+ if file := ( file or input_file ):
143161 _ , file_extension = os .path .splitext (file )
144162 if file_extension != ".json" :
145163 console .error ("Input file extension must be '.json'." )
146164 with open (file ) as f :
147165 input = f .read ()
148- # Setup debugging if requested
149166
167+ # Setup debugging if requested
150168 if not setup_debugging (debug , debug_port ):
151169 console .error (f"Failed to start debug server on port { debug_port } " )
152170
153171 # Process through middleware chain
154- result = Middlewares .next ("run" , entrypoint , input , resume )
172+ result = Middlewares .next (
173+ "run" , entrypoint , input , resume , debug = debug , debug_port = debug_port
174+ )
155175
156176 if result .should_continue :
157177 result = python_run_middleware (
158178 entrypoint = entrypoint ,
159179 input = input ,
160180 resume = resume ,
161181 )
182+ if output_file :
183+ with open (output_file , "w" ) as f :
184+ f .write (str (result .output ))
162185
163186 # Handle result from middleware
164187 if result .error_message :
0 commit comments