Skip to content

Commit 0e01046

Browse files
committed
Add pip install requirements.txt step to NeMoLauncherSlurmInstallStrategy
1 parent b7694aa commit 0e01046

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/cloudai/schema/test_template/nemo_launcher/slurm_install_strategy.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def install(self) -> InstallStatusResult:
157157

158158
try:
159159
self._clone_repository(subdir_path)
160+
self._install_requirements(subdir_path)
160161
docker_image_result = self.docker_image_cache_manager.ensure_docker_image(
161162
self.docker_image_url, self.SUBDIR_PATH, self.DOCKER_IMAGE_FILENAME
162163
)
@@ -293,3 +294,22 @@ def _clone_repository(self, subdir_path: str) -> None:
293294
result = subprocess.run(checkout_cmd, cwd=repo_path, capture_output=True, text=True)
294295
if result.returncode != 0:
295296
raise RuntimeError(f"Failed to checkout commit: {result.stderr}")
297+
298+
def _install_requirements(self, subdir_path: str) -> None:
299+
"""
300+
Installs the required Python packages from the requirements.txt file in the cloned repository.
301+
302+
Args:
303+
subdir_path (str): Subdirectory path for installation.
304+
"""
305+
repo_path = os.path.join(subdir_path, self.REPOSITORY_NAME)
306+
requirements_file = os.path.join(repo_path, "requirements.txt")
307+
308+
if os.path.isfile(requirements_file):
309+
logging.debug("Installing requirements from %s", requirements_file)
310+
install_cmd = ["pip", "install", "-r", requirements_file]
311+
result = subprocess.run(install_cmd, capture_output=True, text=True)
312+
if result.returncode != 0:
313+
raise RuntimeError(f"Failed to install requirements: {result.stderr}")
314+
else:
315+
logging.warning("requirements.txt not found in %s", repo_path)

tests/test_slurm_install_strategy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def test_clone_repository_when_path_does_not_exist(self, strategy: NeMoLauncherS
155155
mock_run.return_value.returncode = 0
156156
mock_exists.side_effect = lambda path: path == str(subdir_path)
157157
strategy._clone_repository(str(subdir_path))
158+
strategy._install_requirements(str(subdir_path))
158159

159160
mock_run.assert_any_call(
160161
["git", "clone", strategy.repository_url, str(repo_path)], capture_output=True, text=True
@@ -166,6 +167,20 @@ def test_clone_repository_when_path_does_not_exist(self, strategy: NeMoLauncherS
166167
text=True,
167168
)
168169

170+
def test_install_requirements(self, strategy: NeMoLauncherSlurmInstallStrategy):
171+
subdir_path = Path(strategy.slurm_system.install_path) / strategy.SUBDIR_PATH
172+
repo_path = subdir_path / strategy.REPOSITORY_NAME
173+
requirements_file = repo_path / "requirements.txt"
174+
repo_path.mkdir(parents=True, exist_ok=True)
175+
requirements_file.touch()
176+
177+
with patch("subprocess.run") as mock_run:
178+
mock_run.return_value.returncode = 0
179+
strategy._install_requirements(str(subdir_path))
180+
mock_run.assert_called_with(
181+
["pip", "install", "-r", str(requirements_file)], capture_output=True, text=True
182+
)
183+
169184
def test_clone_repository_when_path_exists(self, strategy: NeMoLauncherSlurmInstallStrategy):
170185
subdir_path = Path(strategy.slurm_system.install_path) / strategy.SUBDIR_PATH
171186
repo_path = subdir_path / strategy.REPOSITORY_NAME

0 commit comments

Comments
 (0)