Skip to content

Commit

Permalink
debug filepath structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesus Pequeno committed Jul 9, 2024
1 parent f95feba commit 68fa968
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 54 deletions.
36 changes: 23 additions & 13 deletions src/prepare_dlc_dev_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,29 +271,35 @@ def handle_currency_option(currency_paths):
files are copied from the inferred previous version files, with the version and
short_version values updated accordingly.
"""
buildspec_pattern = r"^(\w+)/(training|inference)/buildspec-(\d+)-(\d+)(?:-(.+))?\.yml$"

buildspec_pattern = (
r"^(\w+)/(training|inference)/buildspec(?:-(\w+))?-(\d+)-(\d+)(?:-(.+))?\.yml$"
)
for currency_path in currency_paths:
if not validate_currency_path(currency_path, buildspec_pattern):
continue

framework, job_type, major_version, minor_version, extra = extract_path_components(
currency_path, buildspec_pattern
)
(
framework,
job_type,
optional_suffix,
major_version,
minor_version,
extra_suffix,
) = extract_path_components(currency_path, buildspec_pattern)
previous_minor_version = str(int(minor_version) - 1)

previous_version_path = os.path.join(
get_cloned_folder_path(),
framework,
job_type,
f"buildspec-{major_version}-{previous_minor_version}{'-' + extra if extra else ''}.yml",
f"buildspec{'-' + optional_suffix if optional_suffix else ''}-{major_version}-{previous_minor_version}{'-' + extra_suffix if extra_suffix else ''}.yml",
)

if os.path.isfile(previous_version_path):
updated_content = generate_new_file_content(
previous_version_path, major_version, minor_version
previous_version_path, major_version, minor_version, optional_suffix, extra_suffix
)
create_new_file_with_updated_version(
currency_path, updated_content, optional_suffix, extra_suffix
)
create_new_file_with_updated_version(currency_path, updated_content)
else:
LOGGER.warning(f"Previous version file not found: {previous_version_path}")

Expand All @@ -320,7 +326,9 @@ def extract_path_components(currency_path, pattern):
return match.groups()


def generate_new_file_content(previous_version_path, major_version, minor_version):
def generate_new_file_content(
previous_version_path, major_version, minor_version, optional_suffix, extra_suffix
):
"""
Generates the content for the new buildspec file with the updated version and short_version values.
"""
Expand All @@ -334,7 +342,9 @@ def generate_new_file_content(previous_version_path, major_version, minor_versio
return content


def create_new_file_with_updated_version(currency_path, updated_content):
def create_new_file_with_updated_version(
currency_path, updated_content, optional_suffix, extra_suffix
):
"""
Creates a new buildspec file with the updated content.
"""
Expand All @@ -344,7 +354,7 @@ def create_new_file_with_updated_version(currency_path, updated_content):
with open(new_file_path, "w") as new_file:
new_file.writelines(updated_content)

LOGGER.info(f"Created {currency_path}")
LOGGER.info(f"Created {new_file_path}")


def main():
Expand Down
41 changes: 0 additions & 41 deletions test/dlc_tests/sanity/quick_checks/test_prepare_dev_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
import os
import re

from unittest.mock import patch, mock_open
from src import prepare_dlc_dev_environment
Expand Down Expand Up @@ -243,43 +242,3 @@ def test_handle_currency_option_invalid_path(tmp_path, caplog):
prepare_dlc_dev_environment.handle_currency_option([invalid_currency_path])

assert "Invalid currency path format: invalid/file/path-1-2-hello.yml" in caplog.text


@pytest.mark.quick_checks
@pytest.mark.model("N/A")
@pytest.mark.integration("generate_new_file_content")
@pytest.mark.parametrize(
"file_path",
[
"pytorch/inference/buildspec-1-14.yml",
"pytorch/training/buildspec-2-4-sm",
],
)
def test_generate_new_file_content(file_paths):
for file_path in file_paths:
# Extract major_version and minor_version from the file_path
# Assuming the file_path follows the pattern: <framework>/<job_type>/buildspec-<major_version>-<minor_version>.yml
match = re.match(r"^(\w+)/(\w+)/buildspec-(\d+)-(\d+)(?:-.+)?.yml$", file_path)
if not match:
raise ValueError(f"Invalid file path format: {file_path}")

framework, job_type, major_version, minor_version = match.groups()
previous_minor_version = str(int(minor_version) - 1)

previous_version_path = os.path.join(
framework, job_type, f"buildspec-{major_version}-{previous_minor_version}.yml"
)
mock_file_content = f'version: &VERSION {major_version}.{previous_minor_version}.0\nshort_version: &SHORT_VERSION "{major_version}.{previous_minor_version}"\n'
expected_content = [
f"version: &VERSION {major_version}.{minor_version}.0\n",
f'short_version: &SHORT_VERSION "{major_version}.{minor_version}"\n',
]

@patch("builtins.open", new_callable=mock_open, read_data=mock_file_content)
def mock_generate_new_file_content(mock_file):
result = prepare_dlc_dev_environment.generate_new_file_content(
previous_version_path, major_version, minor_version
)
assert result == expected_content

mock_generate_new_file_content()

0 comments on commit 68fa968

Please sign in to comment.