samgis-lisa-on-zero / scripts /create_folders_and_variables_if_not_exists.py
alessandro trinca tornidor
[ci] add the option to install the project in a 'bare metal' installation, see README.md for instructions
0c9e62a
raw
history blame
1.82 kB
import json
import logging
import os
from pathlib import Path
def stats_pathname(pathname: Path | str):
current_pathname = Path(pathname)
return current_pathname.is_dir()
def create_folder_if_not_exists(pathname: Path | str):
current_pathname = Path(pathname)
try:
print(f"Pathname exists? {current_pathname.exists()}, That's a folder? {current_pathname.is_dir()}...")
logging.info(f"Pathname exists? {current_pathname.exists()}, That's a folder? {current_pathname.is_dir()}...")
current_pathname.unlink(missing_ok=True)
except PermissionError as pe:
print(f"permission denied on removing pathname before folder creation:{pe}.")
logging.error(f"permission denied on removing pathname before folder creation:{pe}.")
except IsADirectoryError as errdir:
print(f"that's a directory:{errdir}.")
logging.error(f"that's a directory:{errdir}.")
print(f"Creating pathname: {current_pathname} ...")
logging.info(f"Creating pathname: {current_pathname} ...")
current_pathname.mkdir(mode=0o770, parents=True, exist_ok=True)
print(f"assertion: pathname exists and is a folder: {current_pathname} ...")
logging.info(f"assertion: pathname exists and is a folder: {current_pathname} ...")
assert current_pathname.is_dir()
if __name__ == '__main__':
folders_string = os.getenv("FOLDERS_MAP")
folders_dict = json.loads(folders_string)
for folder_env_ref, folder_env_path in folders_dict.items():
print(f"folder_env_ref:{folder_env_ref}, folder_env_path:{folder_env_path}.")
logging.info(f"folder_env_ref:{folder_env_ref}, folder_env_path:{folder_env_path}.")
create_folder_if_not_exists(folder_env_path)
print("========")
assert os.getenv(folder_env_ref) == folder_env_path