-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcarc_conda_setup.py
More file actions
348 lines (291 loc) · 13.6 KB
/
carc_conda_setup.py
File metadata and controls
348 lines (291 loc) · 13.6 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3
"""
CARC Conda Environment Setup Script
Based on CARC documentation: https://www.carc.usc.edu/user-guides/hpc-systems/software/conda
"""
import subprocess
import sys
import os
import platform
import shutil
import argparse
from pathlib import Path
import time
import getpass
import json
class CARCCondaSetup:
def __init__(self):
self.current_step = 0
self.total_steps = 6
self.status = {}
self.is_first_time = True
self.env_name = "torch-env"
self.python_version = "3.12" # Using 3.12
self.home_dir = os.path.expanduser("~")
self.config_file = os.path.join(self.home_dir, ".carc_conda_config.json")
def print_status(self, message, status="info"):
"""Print status message with color indicators."""
if status == "success":
print(f"✓ {message}")
elif status == "error":
print(f"✗ {message}")
elif status == "warning":
print(f"! {message}")
elif status == "info":
print(f" {message}")
elif status == "highlight":
print(f"→ {message}")
def get_user_input(self, prompt, default=None, required=False):
"""Get user input with a prompt."""
while True:
print(f"{prompt} ", end='')
if default:
print(f"[{default}] ", end='')
user_input = input().strip()
if not user_input and default:
return default
elif not user_input and required:
self.print_status("This field is required. Please try again.", "warning")
continue
return user_input
def check_bash_configs(self):
"""Check if user's bash configuration files exist and copy from /etc/skel if they don't."""
home_dir = os.path.expanduser("~")
bash_profile = os.path.join(home_dir, ".bash_profile")
bashrc = os.path.join(home_dir, ".bashrc")
print("\nChecking bash configuration files...")
# Check .bash_profile
print(f"\nChecking .bash_profile at: {bash_profile}")
if os.path.exists(bash_profile):
print("✓ .bash_profile exists")
else:
print("✗ .bash_profile not found")
print("Creating .bash_profile...")
try:
shutil.copy("/etc/skel/.bash_profile", bash_profile)
print("✓ Successfully created .bash_profile")
except Exception as e:
print(f"✗ Error creating .bash_profile: {e}")
return False
# Check .bashrc
print(f"\nChecking .bashrc at: {bashrc}")
if os.path.exists(bashrc):
print("✓ .bashrc exists")
else:
print("✗ .bashrc not found")
print("Creating .bashrc...")
try:
shutil.copy("/etc/skel/.bashrc", bashrc)
print("✓ Successfully created .bashrc")
except Exception as e:
print(f"✗ Error creating .bashrc: {e}")
return False
print("\n✓ Bash configuration check completed successfully")
return True
def load_conda_module(self):
"""Load conda module as per CARC documentation."""
print("\nStep 1: Loading conda module...")
try:
# Module purge and load conda as per CARC docs
module_cmd = "module purge && module load conda"
subprocess.run(module_cmd, shell=True, check=True, executable='/bin/bash')
self.print_status("Successfully loaded conda module", "success")
return True
except subprocess.CalledProcessError as e:
self.print_status(f"Error loading conda module: {e}", "error")
return False
def initialize_conda(self):
"""Initialize conda in bash shell as per CARC documentation."""
print("\nStep 2: Initializing conda...")
try:
# Initialize conda in bash as per CARC docs
init_cmd = "conda init bash"
subprocess.run(init_cmd, shell=True, check=True, executable='/bin/bash')
# Source bashrc to make conda available
source_cmd = "source ~/.bashrc"
subprocess.run(source_cmd, shell=True, check=True, executable='/bin/bash')
self.print_status("Successfully initialized conda", "success")
return True
except subprocess.CalledProcessError as e:
self.print_status(f"Error initializing conda: {e}", "error")
return False
def create_conda_environment(self):
"""Create conda environment as per CARC documentation."""
print(f"\nStep 3: Creating conda environment '{self.env_name}'...")
try:
# Create environment with Python as per CARC docs
create_cmd = f"conda create --name {self.env_name} python=3.12 -y"
subprocess.run(create_cmd, shell=True, check=True, executable='/bin/bash')
self.print_status(f"Successfully created environment '{self.env_name}'", "success")
return True
except subprocess.CalledProcessError as e:
self.print_status(f"Error creating conda environment: {e}", "error")
return False
def install_pytorch(self):
"""Install PyTorch as per CARC PyTorch installation guide."""
print(f"\nStep 4: Installing PyTorch in environment '{self.env_name}'...")
try:
# Initialize conda shell hook
init_cmd = "eval \"$(conda shell.bash hook)\""
activate_cmd = f"conda activate {self.env_name}"
# Install PyTorch using pip as recommended
install_cmd = f"{init_cmd} && {activate_cmd} && pip install torch torchvision torchaudio"
subprocess.run(install_cmd, shell=True, check=True, executable='/bin/bash')
self.print_status("Successfully installed PyTorch", "success")
return True
except subprocess.CalledProcessError as e:
self.print_status(f"Error installing PyTorch: {e}", "error")
return False
def install_additional_packages(self):
"""Install additional data science packages."""
print(f"\nStep 5: Installing additional packages in environment '{self.env_name}'...")
packages = ["numpy", "pandas", "scikit-learn", "matplotlib", "line_profiler","tensorboard"]
try:
init_cmd = "eval \"$(conda shell.bash hook)\""
activate_cmd = f"conda activate {self.env_name}"
for package in packages:
print(f"Installing {package}...")
install_cmd = f"{init_cmd} && {activate_cmd} && conda install -y {package}"
subprocess.run(install_cmd, shell=True, check=True, executable='/bin/bash')
self.print_status(f"Successfully installed {package}", "success")
return True
except subprocess.CalledProcessError as e:
self.print_status(f"Error installing packages: {e}", "error")
return False
def install_jupyter_kernel(self):
"""Install Jupyter kernel for the conda environment."""
print(f"\nStep 6: Installing Jupyter kernel for '{self.env_name}'...")
try:
init_cmd = "eval \"$(conda shell.bash hook)\""
activate_cmd = f"conda activate {self.env_name}"
# Install ipykernel
install_cmd = f"{init_cmd} && {activate_cmd} && conda install -c conda-forge ipykernel -y"
subprocess.run(install_cmd, shell=True, check=True, executable='/bin/bash')
# Install the kernel
kernel_cmd = f"{init_cmd} && {activate_cmd} && python -m ipykernel install --user --name {self.env_name} --display-name \"{self.env_name}\""
subprocess.run(kernel_cmd, shell=True, check=True, executable='/bin/bash')
self.print_status(f"Successfully installed Jupyter kernel for '{self.env_name}'", "success")
return True
except subprocess.CalledProcessError as e:
self.print_status(f"Error installing Jupyter kernel: {e}", "error")
return False
def save_config(self):
"""Save configuration to mark setup as complete."""
config = {
"setup_completed": True,
"env_name": self.env_name,
"python_version": "3.12",
"setup_date": time.strftime("%Y-%m-%d %H:%M:%S")
}
try:
with open(self.config_file, 'w') as f:
json.dump(config, f, indent=2)
self.print_status("Configuration saved", "success")
except Exception as e:
self.print_status(f"Error saving configuration: {e}", "warning")
def show_usage_instructions(self):
"""Show instructions for using the conda environment."""
print("\n" + "="*60)
print("CARC CONDA SETUP COMPLETED SUCCESSFULLY!")
print("="*60)
print(f"\nYour conda environment '{self.env_name}' is ready to use!")
print("\nTo activate your environment:")
print(f" conda activate {self.env_name}")
print("\nTo test PyTorch installation:")
print(f" conda activate {self.env_name}")
print(" python -c 'import torch; print(torch.__version__)'")
print("\nTo clean up conda cache and free up space:")
print(" conda clean --all")
print("\nFor more information:")
print(" https://www.carc.usc.edu/user-guides/hpc-systems/software/conda")
print(" https://www.carc.usc.edu/user-guides/data-science/pytorch-installation")
print("\n" + "="*60)
def run_interactive_setup(self):
"""Run the interactive setup process."""
print("Welcome to CARC Conda Environment Setup!")
print("This script will set up conda and install PyTorch according to CARC documentation.\n")
# Get user preferences
self.env_name = self.get_user_input("Enter environment name", self.env_name)
print(f"\nSetup configuration:")
print(f" Environment name: {self.env_name}")
print(f" Python version: 3.12")
proceed = self.get_user_input("Proceed with setup? (y/n)", "y")
if proceed.lower() not in ['y', 'yes']:
print("Setup cancelled.")
return False
# Run setup steps
steps = [
("Checking bash configuration files", self.check_bash_configs),
("Loading conda module", self.load_conda_module),
("Initializing conda", self.initialize_conda),
("Creating conda environment", self.create_conda_environment),
("Installing PyTorch", self.install_pytorch),
("Installing additional packages", self.install_additional_packages),
("Installing Jupyter kernel", self.install_jupyter_kernel)
]
for step_name, step_func in steps:
print(f"\n{step_name}...")
if not step_func():
self.print_status(f"Setup failed at: {step_name}", "error")
return False
# Save configuration and show instructions
self.save_config()
self.show_usage_instructions()
return True
def run_automated_setup(self):
"""Run the automated setup process with default values."""
print("Running automated CARC conda setup...")
# Run setup steps
steps = [
("Checking bash configuration files", self.check_bash_configs),
("Loading conda module", self.load_conda_module),
("Initializing conda", self.initialize_conda),
("Creating conda environment", self.create_conda_environment),
("Installing PyTorch", self.install_pytorch),
("Installing additional packages", self.install_additional_packages),
("Installing Jupyter kernel", self.install_jupyter_kernel)
]
for step_name, step_func in steps:
print(f"\n{step_name}...")
if not step_func():
self.print_status(f"Setup failed at: {step_name}", "error")
return False
# Save configuration and show instructions
self.save_config()
self.show_usage_instructions()
return True
def main():
parser = argparse.ArgumentParser(
description='CARC Conda Environment Setup Script',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python carc_conda_setup_improved.py # Automated setup with defaults
python carc_conda_setup_improved.py --interactive # Interactive setup
python carc_conda_setup_improved.py --env-name myenv # Custom environment name
"""
)
parser.add_argument('--env-name',
help='Name of the conda environment (default: pytorch_env)')
parser.add_argument('--interactive', action='store_true',
help='Run in interactive mode (requires user input)')
args = parser.parse_args()
setup = CARCCondaSetup()
# Override defaults with command line arguments
if args.env_name:
setup.env_name = args.env_name
try:
if args.interactive:
success = setup.run_interactive_setup()
else:
success = setup.run_automated_setup()
if not success:
sys.exit(1)
except KeyboardInterrupt:
print("\nSetup interrupted by user.")
sys.exit(1)
except Exception as e:
print(f"\nUnexpected error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()