### ===================================================================================================================
### viia_update_version_in_script(s)
### ===================================================================================================================
# Copyright ©VIIA 2025
### ===================================================================================================================
### 1. Import modules
### ===================================================================================================================
# General imports
from __future__ import annotations
from pathlib import Path
from typing import Union, TYPE_CHECKING
# References for functions and classes in the viiaPackage
if TYPE_CHECKING:
from viiapackage.viiaStatus import ViiaProject
### ===================================================================================================================
### 2. viia_update_version_in_script function
### ===================================================================================================================
[docs]def viia_update_version_in_script(
project: ViiaProject, old_version: str, new_version: str, script: Union[str, Path]) -> bool:
"""
Updates the viiapackage version in a script
Input:
- project (obj): Project object containing collections of fem objects and project variables.
- old_version (str): The old version of the viiapackage.
- new_version (str): The new/current version of the viiapackage.
- script (str): The path of the script that should be updated from the old to the new version
Output:
- A bool to indicate the status of the update.
"""
if isinstance(script, str):
script = Path(script)
if not script.exists():
project.write_log(
f"File {script} not found. Viiapackage version cannot be updated automatically and should be updated "
f"manually in {script=}.")
return False
if old_version == new_version:
project.write_log(
f"The {old_version=} and {new_version=} are the same. Viiapackage version cannot be updated automatically "
f"and should be updated manually in {script=}.")
return False
text = script.read_text()
occurrence = text.count(f"# viiapackage version v{old_version}")
if occurrence == 1:
text = text.replace(
f"# viiapackage version v{old_version}",
f"# viiapackage version v{new_version}")
with open(script, 'w') as file:
file.write(text)
project.write_log(f"The Viiapackage version is updated from {old_version=} to {new_version=} in {script=}.")
return True
elif occurrence == 0:
project.write_log(
f"No viiapackage found in {script=}. Viiapackage version cannot be updated automatically and should be "
f"updated manually in {script=}.")
return False
else:
project.write_log(
f"{occurrence} viiapackage version occurrences found in {script=}. Viiapackage version cannot be updated "
f"automatically and should be updated manually in {script=}.")
return False
### ===================================================================================================================
### 3. viia_update_version_in_scripts function
### ===================================================================================================================
[docs]def viia_update_version_in_scripts(project: ViiaProject, old_version: str, new_version: str):
"""
Updates the viiapackage version in relevant scripts
Input:
- project (obj): Project object containing collections of fem objects and project variables.
- old_version (str): The old version of the viiapackage.
- new_version (str): The new/current version of the viiapackage.
Output:
- A dict with bools to indicate the status of the update.
"""
scripts = {
'modelscript': project.workfolder_location / f'VIIA_{project.name}_modelscript.py',
'mainscript': project.workfolder_location / f'VIIA_{project.name}_mainscript.py',
'resultscript': project.workfolder_location / f'VIIA_{project.name}_resultscript.py',
'reportscript': project.workfolder_location / f'VIIA_{project.name}_reportscript.py'
}
return_dict = {}
for key, value in scripts.items():
return_dict[key] = viia_update_version_in_script(
project=project, old_version=old_version, new_version=new_version, script=value)
return return_dict
### ===================================================================================================================
### 4. End of script
### ===================================================================================================================