Repository navigation
Expand file tree
/
Copy pathrun.py
More file actions
77 lines (65 loc) · 2.72 KB
/
Copy pathrun.py
File metadata and controls
77 lines (65 loc) · 2.72 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sys
import yaml
import subprocess
from src.common.path_utils import resolve_path
def read_yaml(file_path: str) -> dict:
"""Read and parse a YAML pipeline configuration file.
Args:
file_path (str): Path to the YAML file.
Returns:
dict: Parsed dictionary from the YAML configuration.
"""
with open(file_path, 'r') as file:
return yaml.safe_load(file)
def execute_steps(steps: list, single_step: str = None) -> None:
"""Execute configured pipeline steps sequentially or individually.
Args:
steps (list): List of step configuration dictionaries from YAML.
single_step (str, optional): Target step key to execute if mode is 'single'.
"""
for step in steps:
for key, details in step.items():
name = details.get('name')
enabled = details.get('enabled', False)
script = details.get('script')
arguments = details.get('arguments', {})
if single_step is not None and key != single_step:
continue
script_path = script.replace("/", ".").lstrip(".").rstrip(".py")
print(f"[{name}] Running...")
if enabled or key == single_step:
cmd = [sys.executable, "-m", script_path]
for arg_key, arg_value in arguments.items():
if isinstance(arg_value, dict):
for sub_key, sub_value in arg_value.items():
cmd.append(f"--{sub_key}")
cmd.append(str(sub_value))
else:
cmd.append(f"--{arg_key}")
cmd.append(str(arg_value))
try:
cmd_str = " ".join(cmd)
print(f"Executing command: {cmd_str}")
result = subprocess.run(cmd, check=True, text=True, stderr=subprocess.PIPE)
print(f"[{name}] Finished successfully.")
except subprocess.CalledProcessError as e:
print(f"[{name}] Error occurred :(\n{e.stderr}")
return
else:
print(f"[{name}] Step is disabled. Skipping....")
if __name__ == "__main__":
yaml_file_path = "pipeline.yml"
try:
yaml_content = read_yaml(yaml_file_path)
mode = yaml_content.get('mode', 'sequential')
steps = yaml_content.get('steps', [])
if mode == 'single':
step = yaml_content.get('step', None)
if step:
execute_steps(steps, step)
else:
print("Error: 'step' key is missing in the YAML file.")
else:
execute_steps(steps)
except Exception as e:
print(f"Error: {e}")