Skip to content

Commit

Permalink
implement tile version handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Noahnc committed Oct 25, 2023
1 parent 28193e0 commit 96ec220
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion infrapatch/core/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_all_terraform_resources(self, project_root: Path) -> list[VersionedTerra
for terraform_file in progress.track(terraform_files, description="Parsing .tf files..."):
resources.extend(self.hcl_handler.get_terraform_resources_from_file(terraform_file))
for resource in progress.track(resources, description="Getting newest resource versions..."):
resource.set_newest_version(self.registry_handler.get_newest_version(resource))
resource.newest_version = self.registry_handler.get_newest_version(resource)
return resources

def print_resource_table(self, resources: list[VersionedTerraformResource], only_upgradable: bool = False):
Expand Down
50 changes: 44 additions & 6 deletions infrapatch/core/models/versioned_terraform_resources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging as log
import re
import semantic_version
from dataclasses import dataclass
from distutils.version import StrictVersion
from pathlib import Path
from typing import Optional

Expand All @@ -17,7 +17,7 @@ class VersionedTerraformResource:
name: str
current_version: str
source_file: Path
newest_version: Optional[str] = None
_newest_version: Optional[str] = None
_status: str = ResourceStatus.UNPATCHED
_base_domain: str = None
_identifier: str = None
Expand All @@ -43,21 +43,59 @@ def resource_name(self):
def identifier(self) -> str:
return self._identifier

def set_newest_version(self, version: str):
self.newest_version = version
@property
def newest_version(self) -> Optional[str]:
return self._newest_version

@property
def newest_version_base(self):
if self.has_tile_constraint():
return self.newest_version.strip("~>")
return self.newest_version

@newest_version.setter
def newest_version(self, version: str):
if self.has_tile_constraint():
self._newest_version = f"~>{version}"
return
self._newest_version = version

def set_patched(self):
self._status = ResourceStatus.PATCHED

def has_tile_constraint(self):
return re.match(r"^~>[0-9]+\.[0-9]+\.[0-9]+$", self.current_version)

def set_patch_error(self):
self._status = ResourceStatus.PATCH_ERROR

def installed_version_equal_or_newer_than_new_version(self):

if self.newest_version is None:
raise Exception(f"Newest version of resource '{self.name}' is not set.")
if StrictVersion(self.newest_version) > StrictVersion(self.current_version):

newest = semantic_version.Version(self.newest_version_base)

# check if the current version has the following format: "1.2.3"
if re.match(r"^[0-9]+\.[0-9]+\.[0-9]+$", self.current_version):
current = semantic_version.Version(self.current_version)
if current >= newest:
return True
return False

# chech if the current version has the following format: "~>3.76.0"
if self.has_tile_constraint():
current = semantic_version.Version(self.current_version.strip("~>"))
if current.major > newest.major:
return True
if current.minor >= newest.minor:
return True
return False
return True

current_constraint = semantic_version.NpmSpec(self.current_version)
if newest in current_constraint:
return True
return False

def check_if_up_to_date(self):
if self.status == ResourceStatus.PATCH_ERROR:
Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ click~=8.1.7
rich~=13.6.0
pygohcl~=1.0.7
GitPython~=3.1.40
setuptools~=65.5.1
setuptools~=65.5.1
pygit2~=1.13.1
semantic_version~=2.10.0
```
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"rich~=13.6.0",
"pygohcl~=1.0.7",
"GitPython~=3.1.40",
"setuptools~=65.5.1"
"setuptools~=65.5.1",
"semantic_version~=2.10.0"
],
python_requires='>=3.11',
entry_points='''
Expand Down

0 comments on commit 96ec220

Please sign in to comment.