-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_batch_script.py
More file actions
55 lines (43 loc) · 2.34 KB
/
Copy pathtest_batch_script.py
File metadata and controls
55 lines (43 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import subprocess
import os
def run_batch_script(batch_script_path, argument, wait=True, capture_output=False):
"""Runs a batch script in a subprocess, passes an argument, and handles output writing and capturing.
Args:
batch_script_path (str): Path to the batch script.
argument (str): Argument to pass to the batch script.
wait (bool, optional): Whether to wait for the subprocess to finish before returning. Defaults to True.
capture_output (bool, optional): Whether to capture the subprocess's output. Defaults to False.
Returns:
str or None: If capture_output is True, returns the captured output. Otherwise, returns None.
Raises:
subprocess.CalledProcessError: If the subprocess exits with a non-zero status.
"""
try:
# Ensure the output file path exists to avoid errors
# os.makedirs(os.path.dirname(output_file_path), exist_ok=True)
# Execute the batch script using subprocess.Popen with appropriate shell arguments
process = subprocess.Popen(
[batch_script_path, argument, r"/World/audio2face/PlayerStreaming"], # Pass the argument as a list element
stdout=subprocess.PIPE if capture_output else subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
shell=True, # Required for proper handling of spaces and special characters in batch commands
universal_newlines=True,
creationflags=subprocess.CREATE_NEW_CONSOLE
)
# Optionally wait for the subprocess to finish
if wait:
process.wait()
# Capture output if requested
if capture_output:
output = process.stdout.read()
return output
except subprocess.CalledProcessError as e:
raise subprocess.CalledProcessError(f"Batch script exited with non-zero status: {e.returncode}")
# Example usage:
batch_script_path = r"C:\Users\llmdev\Desktop\autogen-studio\skills\send_wav_a2f.bat"
argument = "hello world" # Replace with your actual argument
# Run the batch script with argument and capture output
output = run_batch_script(batch_script_path, argument, capture_output=True)
print(output) # Print the captured output
# Run the batch script with argument without capturing output
run_batch_script(batch_script_path, argument, wait=False) # Don't wait for the script to finish