Skip to content

Commit 6ad268c

Browse files
added script to build docker/singularity images
1 parent 0f7b169 commit 6ad268c

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ my_*
1111
__pycache__
1212
.python-version
1313
venv/
14+
.venv/
1415

1516
# editors/IDEs
1617
.idea/

Diff for: scripts/build_images.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""
2+
This script can be used to build all/selected docker/singularity images.
3+
0. Load your virtual environment like you would when using AMLB.
4+
1. Run the script with the required mode:
5+
python build_images.py -m docker
6+
python build_images.py -m singularity
7+
8+
2. To delete files forcefully, add the --force flag. This is optional but recommended in case you change something.
9+
python build_images.py -m singularity --force
10+
11+
3. To specify frameworks, use the -f flag with a comma-separated list:
12+
python build_images.py -m docker -f autosklearn,flaml,gama --force
13+
14+
4. If no frameworks are specified, the script will act on all available frameworks.
15+
16+
"""
17+
18+
import os
19+
import argparse
20+
import pathlib
21+
from pathlib import Path
22+
from typing import List, Optional
23+
24+
25+
def delete_singularity_files(
26+
directory: pathlib.Path, frameworks: Optional[List[str]] = None
27+
) -> None:
28+
"""Deletes Singularity-related files from the specified directory."""
29+
for root, _, files in os.walk(directory):
30+
for file in files:
31+
if file == "Singularityfile" or file.endswith(".sif"):
32+
file_path = Path(root) / file
33+
34+
if frameworks and not any(
35+
framework in file_path.name.lower() for framework in frameworks
36+
):
37+
continue
38+
39+
try:
40+
file_path.unlink()
41+
print(f"Deleted: {file_path}")
42+
except Exception as e:
43+
print(f"Error deleting {file_path}: {e}")
44+
45+
46+
def parse_arguments() -> argparse.Namespace:
47+
"""Parses command-line arguments."""
48+
parser = argparse.ArgumentParser(
49+
description="Script to manage Docker and Singularity frameworks."
50+
)
51+
parser.add_argument(
52+
"-m", "--mode", help="Docker or singularity", type=str, required=True
53+
)
54+
parser.add_argument(
55+
"--force", help="Delete singularity/docker files.", action="store_true"
56+
)
57+
parser.add_argument(
58+
"-f",
59+
"--frameworks",
60+
help="Comma-separated list of frameworks to act on.",
61+
type=str,
62+
)
63+
return parser.parse_args()
64+
65+
66+
def get_frameworks(framework_dir: Path, user_input: Optional[str]) -> List[str]:
67+
"""Gets the list of frameworks based on user input or directory listing."""
68+
if user_input:
69+
frameworks = [framework.lower().strip() for framework in user_input.split(",")]
70+
print(f"Running for given frameworks - {frameworks}")
71+
else:
72+
frameworks = [
73+
framework.lower()
74+
for framework in os.listdir(framework_dir)
75+
if "__" not in framework
76+
]
77+
print(f"Running for all frameworks - {frameworks}")
78+
return frameworks
79+
80+
81+
def main() -> None:
82+
args = parse_arguments()
83+
framework_dir = Path("../frameworks/")
84+
mode = args.mode.lower().strip()
85+
frameworks = get_frameworks(framework_dir, args.frameworks)
86+
87+
if mode == "docker" and args.force:
88+
print("Deleting frameworks from Docker")
89+
for framework in frameworks:
90+
os.system(f"docker rmi $(docker images 'automlbenchmark/{framework}')")
91+
92+
elif mode == "singularity" and args.force:
93+
print("Deleting frameworks from Singularity")
94+
delete_singularity_files(directory=framework_dir, frameworks=frameworks)
95+
96+
for framework in frameworks:
97+
print(f"Setting up {framework}")
98+
os.system(
99+
f"yes | python ../runbenchmark.py {framework} openml/t/3812 --mode {mode} --setup only"
100+
)
101+
102+
103+
if __name__ == "__main__":
104+
main()

0 commit comments

Comments
 (0)