-
Notifications
You must be signed in to change notification settings - Fork 30
/
launch_test_locally.py
181 lines (133 loc) · 4.67 KB
/
launch_test_locally.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
"""
Description
:Example:
"""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import logging
from pathlib import Path
import yaml
def parse_yaml_file(yaml_file):
# Load the YAML file
with open(yaml_file, "r") as stream:
workflow = yaml.safe_load(stream)
return workflow
def create_symbolic_links(source_dir, target_dir):
# Convert paths to Path objects
source_dir = Path(source_dir)
target_dir = Path(target_dir)
# Ensure target directory exists
target_dir.mkdir(parents=True, exist_ok=True)
# Loop through each item in the source directory
for item in source_dir.iterdir():
target_item_path = target_dir / item.name
# Create symbolic link for each item
try:
target_item_path.symlink_to(item.resolve())
except FileExistsError:
pass
def parse_arguments(default_ci_yaml, testing_datadir):
"""Parse script arguments."""
parser = ArgumentParser(
description="...", formatter_class=ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--ci_yaml",
help="increase output verbosity",
default=default_ci_yaml,
type=Path,
)
parser.add_argument(
"--data_dir",
help="Directory where dataset files are located",
default=testing_datadir,
type=Path,
)
parser.add_argument(
"-o",
"--outdir",
help="increase output verbosity",
default="local_CI",
type=Path,
)
parser.add_argument(
"-c",
"--cpu",
type=int,
default=4,
help="Use this amount of cpu when number of cpu is specified in the command.",
)
parser.add_argument(
"-v", "--verbose", help="increase output verbosity", action="store_true"
)
parser.add_argument(
"--skip_msa",
action="store_true",
help="Skip msa command as it takes quite some time to complete.",
)
parser.add_argument(
"-f",
"--force",
action="store_true",
help="Force writing in output directory if exists.",
)
args = parser.parse_args()
return args
def main():
script_path = Path(__file__).resolve()
ppanggolin_main_dir = script_path.parent.parent
default_ci_yaml = ppanggolin_main_dir / ".github/workflows/main.yml"
testing_datadir = ppanggolin_main_dir / "testingDataset"
args = parse_arguments(default_ci_yaml, testing_datadir)
if args.verbose:
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG)
logging.info("Mode verbose ON")
else:
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
if not args.outdir.is_dir():
logging.debug(f"Create output directory {args.outdir.absolute().as_posix()}")
Path.mkdir(args.outdir)
elif not args.force:
raise FileExistsError(
f"{args.outdir} already exists. Use -f if you want to overwrite the files in the directory"
)
# setup test dir
# execution_dir = args.outdir # / args.data_dir.name
create_symbolic_links(args.data_dir, args.outdir)
workflow = parse_yaml_file(args.ci_yaml)
excluded_steps = [
"Install ppanggolin",
"Get core number on linux",
"Get core number on macos",
]
test_script = args.outdir / "launch_test_command.sh"
with open(test_script, "w") as fl:
fl.write("#!/bin/bash\nset -e\n")
# Iterate over jobs and steps
for job in workflow["jobs"].values():
if "steps" not in job:
continue
for step in job["steps"]:
if "run" not in step:
continue
if step["name"] in excluded_steps:
logging.info(f"Ignoring: {step}")
continue
# Execute the command line
command = step["run"].strip()
# process the command
command = command.replace("$NUM_CPUS", f"{args.cpu}")
command = command.replace("cd ", "# cd ")
if args.skip_msa:
command = command.replace("ppanggolin msa", "# ppanggolin msa")
if command == "pytest":
command = f"pytest {ppanggolin_main_dir}"
# log the step name
logging.info(f'Executing: {step["name"]}')
logging.debug(f" {command}\n")
# write the command in the script
fl.write(f'\n# {step["name"]}\n\n')
fl.write(command)
print(f"(cd {args.outdir}; bash {test_script.name})")
if __name__ == "__main__":
main()