Skip to content

Commit

Permalink
handle directory renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
stavros-k committed Aug 27, 2024
1 parent 4c0d007 commit e605c02
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
37 changes: 8 additions & 29 deletions catalog_reader/scripts/apps_hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,10 @@
from catalog_reader.dev_directory import get_ci_development_directory
from catalog_reader.library import get_hashes_of_base_lib_versions
from catalog_reader.names import get_library_path, get_library_hashes_path, get_base_library_dir_name_from_version
from catalog_reader.scripts.version_bump import is_valid_bump_type, bump_version, rename_versioned_dir


def is_valid_bump_type(bump: str) -> bool:
if bump in ('patch', 'minor', 'major'):
return True

return False


def bump_version(version: str, bump: str) -> str:
parts = version.split('.')
if len(parts) != 3:
raise ValueError(f'Invalid version {version!r}')
if bump == 'patch':
parts[2] = str(int(parts[2]) + 1)
elif bump == 'minor':
parts[1] = str(int(parts[1]) + 1)
parts[2] = '0'
elif bump == 'major':
parts[0] = str(int(parts[0]) + 1)
parts[1] = '0'
parts[2] = '0'

return '.'.join(parts)


def update_catalog_hashes(catalog_path: str, bump: str | None = None) -> None:
def update_catalog_hashes(catalog_path: str, bump_type: str | None = None) -> None:
if not os.path.exists(catalog_path):
raise CatalogDoesNotExist(catalog_path)

Expand Down Expand Up @@ -91,14 +68,16 @@ def update_catalog_hashes(catalog_path: str, bump: str | None = None) -> None:
catalog_base_lib_dir_path = os.path.join(library_dir.as_posix(), lib_version)
shutil.copytree(catalog_base_lib_dir_path, app_base_lib_dir.as_posix())

if bump:
if not is_valid_bump_type(bump):
if bump_type:
if not is_valid_bump_type(bump_type):
print(
f'[\033[91mERROR\x1B[0m]\tInvalid bump type {bump!r}, '
f'[\033[91mERROR\x1B[0m]\tInvalid bump type {bump_type!r}, '
f'skipping version bumping for {app_dir.name!r} in {train_dir.name}'
)
else:
app_config['version'] = bump_version(app_config['version'], bump)
new_version = bump_version(app_config["version"], bump_type)
rename_versioned_dir(app_config['version'], new_version, train_dir.name, app_dir)
app_config['version'] = new_version

app_config['lib_version_hash'] = hashes[lib_version]
with open(str(app_metadata_file), 'w') as f:
Expand Down
59 changes: 59 additions & 0 deletions catalog_reader/scripts/version_bump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import shutil

from pathlib import Path
from apps_exceptions import ValidationErrors


def is_valid_bump_type(bump: str) -> bool:
if bump in ('patch', 'minor', 'major'):
return True

return False


def is_valid_version(version: str) -> bool:
if not version:
return False

return version.count('.') == 2


def bump_version(version: str, bump: str) -> str:
parts = version.split('.')
if len(parts) != 3:
raise ValueError(f'Invalid version {version!r}')
if bump == 'patch':
parts[2] = str(int(parts[2]) + 1)
elif bump == 'minor':
parts[1] = str(int(parts[1]) + 1)
parts[2] = '0'
elif bump == 'major':
parts[0] = str(int(parts[0]) + 1)
parts[1] = '0'
parts[2] = '0'

return '.'.join(parts)


def rename_versioned_dir(version: str, new_version: str, train_name: str, app_dir: Path) -> None:
verrors = ValidationErrors()

if not is_valid_version(version) or not is_valid_version(new_version):
verrors.add('version_bump', f'Invalid version {version!r} or {new_version!r}')

if not app_dir.is_dir():
verrors.add('version_bump', f'{app_dir.name!r} is not a directory')

verrors.check()

dir_base = app_dir / 'templates/library' / train_name / app_dir.name
curr_versioned_dir = dir_base / f'v{version}'
new_versioned_dir = dir_base / f'v{new_version}'
if not curr_versioned_dir.is_dir():
return

if new_versioned_dir.is_dir():
verrors.add('version_bump', f'App {app_dir.name!r} library with version {new_version!r} already exists')
shutil.move(curr_versioned_dir, new_versioned_dir)

print(f'[\033[92mOK\x1B[0m]\tUpdated app {app_dir.name!r} library from {version!r} to {new_version!r}')

0 comments on commit e605c02

Please sign in to comment.