-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize_project.py
More file actions
139 lines (121 loc) · 4.28 KB
/
Copy pathinitialize_project.py
File metadata and controls
139 lines (121 loc) · 4.28 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
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
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "pyyaml",
# "python-dotenv"
# ]
# ///
# initialize_project.py
import os
import yaml
import logging
import argparse
from pathlib import Path
from dotenv import load_dotenv
# Import logging setup
from logging_setup import setup_logging, get_logger
def ensure_log_directory(log_file_path):
"""
Ensures that the directory for the log file exists.
"""
log_dir = Path(log_file_path).parent
if not log_dir.exists():
log_dir.mkdir(parents=True, exist_ok=True)
def initialize_project(project_name: str, logger: logging.Logger):
"""
Initializes a new RunPod project by creating necessary directories and files.
Args:
project_name (str): The name of the project to initialize.
logger (logging.Logger): The logger instance for logging messages.
"""
# Define project directory and default files
project_dir = os.path.join("projects", project_name)
config_path = os.path.join(project_dir, "config.yaml")
script_path = os.path.join(project_dir, "run_script.sh")
python_script_path = os.path.join(project_dir, "script.py")
# Create the project directory
os.makedirs(project_dir, exist_ok=True)
logger.info(f"Project directory '{project_dir}' created or already exists.")
# Create a default config.yaml
default_config = {
"project_name": project_name,
"provider": "runpod",
"gpu": "A40",
"budget": {
"max_dollars": 10,
"max_hours": 4,
},
"image": "runpod/pytorch:2.1.0-py3.10-cuda11.8.0-devel-ubuntu22.04",
"script": {
"path": "./run_script.sh",
"env": {
"HF_HOME": "/tmp/huggingface",
"CACHE_DIR": f"/workspace/{project_name}/hf_cache"
},
},
"ssh": {
"key_path": "~/.ssh/id_ed25519",
},
"upload": {
"local_dir": ".",
"remote_dir": f"/root/workspace/{project_name}"
},
}
if not os.path.exists(config_path):
with open(config_path, "w") as config_file:
yaml.dump(default_config, config_file)
logger.info(f"Created default config.yaml at {config_path}")
else:
logger.warning(f"config.yaml already exists at {config_path}")
# Create a default run_script.sh
default_script = f"""#!/bin/bash
# run_script.sh
# Default run script for {project_name}
#
# Navigate to uploaded dir
cd /root/workspace/{project_name}
#
# Install uv
pip install uv
# Run some script
script -qec "uv run script.py" /dev/null
echo "Running script for {project_name}"
"""
if not os.path.exists(script_path):
with open(script_path, "w") as script_file:
script_file.write(default_script)
os.chmod(script_path, 0o755) # Make the script executable
logger.info(f"Created default run_script.sh at {script_path}")
else:
logger.warning(f"run_script.sh already exists at {script_path}")
# Create a default script.py
hello_world_script = f"""# script.py
# Default Python script for {project_name}
def main():
print("Hello, World! This is the default script for {project_name}.")
if __name__ == "__main__":
main()
"""
if not os.path.exists(python_script_path):
with open(python_script_path, "w") as python_script_file:
python_script_file.write(hello_world_script)
logger.info(f"Created default script.py at {python_script_path}")
else:
logger.warning(f"script.py already exists at {python_script_path}")
logger.info(f"Project '{project_name}' initialized successfully in {project_dir}.")
def main():
"""
The main function to initialize a new RunPod project.
"""
# Load environment variables from .env file if present
load_dotenv()
# Set up logging without a log file
setup_logging(log_file_path=None) # Modify setup_logging to handle None
logger = get_logger('my_logger') # Retrieve the logger
# Parse command-line arguments
parser = argparse.ArgumentParser(description="Initialize a new RunPod project.")
parser.add_argument("project_name", help="The name of the project to initialize.")
args = parser.parse_args()
initialize_project(args.project_name, logger)
if __name__ == "__main__":
main()