Update, refactor and extend file hash-related logic

This commit is contained in:
codeking 2024-09-20 18:25:11 +02:00
parent fe92bbf351
commit 7235438a4a
5 changed files with 20 additions and 15 deletions

View file

@ -31,15 +31,15 @@ class ApplicationController:
persistent_state_path = f'{profile.get_data_path()}/persistent-state'
if not os.path.isdir(persistent_state_path) or len(os.listdir(persistent_state_path)) == 0:
shutil.copytree(f'{version.installation_path}/resources/initial-state', persistent_state_path)
shutil.copytree(f'{version.get_installation_path()}/resources/initial-state', persistent_state_path)
display = ApplicationController.__find_unused_display()
base_initialization_file_template = open(f'/{Constants.SP_DATA_HOME}/.init.ptpl', 'r').read()
base_initialization_file_contents = base_initialization_file_template.format(display=display, time_zone=profile.location.time_zone, sp_data_home=Constants.SP_DATA_HOME)
application_initialization_file_template = open(f'/{version.installation_path}/.init.ptpl', 'r').read()
application_initialization_file_contents = application_initialization_file_template.format(application_version_home=version.installation_path, port_number=port_number or -1, home=Constants.HOME, profile_data_path=profile.get_data_path(), config_home=Constants.CONFIG_HOME, application_version_number=version.version_number)
application_initialization_file_template = open(f'/{version.get_installation_path()}/.init.ptpl', 'r').read()
application_initialization_file_contents = application_initialization_file_template.format(application_version_home=version.get_installation_path(), port_number=port_number or -1, home=Constants.HOME, profile_data_path=profile.get_data_path(), config_home=Constants.CONFIG_HOME, application_version_number=version.version_number)
session_state = SessionStateController.get_or_new(profile.id)
SessionStateController.update_or_create(session_state)

View file

@ -29,7 +29,7 @@ class ApplicationVersionController:
@staticmethod
def uninstall(application_version: ApplicationVersion):
shutil.rmtree(application_version.installation_path, ignore_errors=True)
shutil.rmtree(application_version.get_installation_path(), ignore_errors=True)
@staticmethod
def _sync(proxies: Optional[dict] = None):
@ -67,10 +67,10 @@ class ApplicationVersionController:
with tarfile.open(fileobj=BytesIO(response.content), mode = 'r:gz') as tar_file:
tar_file.extractall(application_version.installation_path)
tar_file.extractall(application_version.get_installation_path())
tar_file.close()
with open(f'{application_version.installation_path}/.sha3-512', 'w') as hash_file:
with open(f'{application_version.get_installation_path()}/.sha3-512', 'w') as hash_file:
hash_file.write(f'{file_hash}\n')
hash_file.close()

View file

@ -322,6 +322,5 @@ class ConnectionController:
try:
requests.get(f'{Constants.SP_API_BASE_URL}/health', timeout=timeout, proxies=ConnectionController.get_proxies(port_number))
except requests.exceptions.RequestException:
raise ConnectionError('The connection could not be established.')

View file

@ -51,20 +51,26 @@ class ApplicationVersion(Model):
default=None,
metadata=config(exclude=Exclude.ALWAYS)
)
installation_path: Optional[str] = field(
default=None,
metadata=config(exclude=Exclude.ALWAYS)
)
def __post_init__(self):
self.installation_path = f'{Constants.SP_APPLICATION_DATA_HOME}/{self.application_code}/{self.version_number}'
def get_installation_path(self):
return f'{Constants.SP_APPLICATION_DATA_HOME}/{self.application_code}/{self.version_number}'
def is_installed(self):
return os.path.isdir(self.installation_path) and len(os.listdir(self.installation_path)) > 0
return os.path.isdir(self.get_installation_path()) and len(os.listdir(self.get_installation_path())) > 0
def is_supported(self):
return self.exists(self.application_code, self.version_number) and self.format_revision == 1
def get_installed_file_hash(self):
try:
return open(f'{self.get_installation_path()}/.sha3-512').readline().strip()
except FileNotFoundError:
return None
def is_fresh(self):
return self.is_installed() and (not self.is_supported() or self.file_hash == self.get_installed_file_hash())
@staticmethod
def find_by_id(id: int):
Model._create_table_if_not_exists(table_name=_table_name, table_definition=_table_definition)

View file

@ -170,7 +170,7 @@ if __name__ == '__main__':
if type(exception).__name__ == 'InvalidSubscriptionError':
print('The profile\'s subscription appears to be invalid.\n')
if type(exception).__name__ == 'MissingSubscriptionError':
elif type(exception).__name__ == 'MissingSubscriptionError':
print('The profile is not tied to a subscription.\n')
manage_subscription_input = None