Skip to content

Commit

Permalink
add user-specific/local environment variable setting with python-dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
“Dafydd committed Nov 8, 2024
1 parent af605eb commit e99e6cb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cstar/base/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def handle_config_status(self) -> None:
+ "you will need to set it up.\n"
+ "It is recommended that you install this base model in \n"
+ f"{ext_dir}"
+ "\nThis will also modify your `cstar_local_config.py` file."
+ "\nThis will also modify your `~/.cstar.env` file."
+ "\n#######################################################"
)
while True:
Expand Down
6 changes: 1 addition & 5 deletions cstar/base/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ def load_lmod_modules(self):
def compiler(self) -> str:
pass

# @property
# @abstractmethod
# def environment_variables(self) -> dict:
# pass

# Scheduler/MPI related
@property
@abstractmethod
Expand Down Expand Up @@ -271,3 +266,4 @@ def set_environment() -> CStarEnvironment:


environment = set_environment()
os.environ.update(environment.environment_variables)
31 changes: 13 additions & 18 deletions cstar/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@
from math import ceil
from typing import Tuple
from pathlib import Path
# from cstar.base.environment import _CSTAR_CONFIG_FILE


# def _write_to_config_file(config_file_str: str) -> None:
# """Write config_file_str to C-Star config file to configure environment on
# import."""

# if not Path(_CSTAR_CONFIG_FILE).exists():
# print(f"Updating environment in C-Star configuration file {_CSTAR_CONFIG_FILE}")
# base_conf_str = (
# "# This file was generated by C-Star and is specific to your machine. "
# + "# It contains environment information related to your cases & their dependencies. "
# + "# You can safely delete this file, but C-Star may prompt you to re-install things if so."
# )

# base_conf_str += "\nimport os\nfrom cstar.base.environment import _CSTAR_ENVIRONMENT_VARIABLES\n"
# base_conf_str += "def get_user_environment():\n"
# config_file_str = base_conf_str + config_file_str
def _update_user_dotenv(env_file_str) -> None:
print("Updating environment in C-Star configuration file ~/.cstar.env")
env_file = Path("~/.cstar.env").expanduser()
if not env_file.exists():
print(f"Updating environment in C-Star configuration file {env_file}")
base_env_str = (
"# This file was generated by C-Star and is specific to your machine. "
+ "# It contains environment information related to your cases & their dependencies. "
+ "# You can safely delete this file, but C-Star may prompt you to re-install things if so."
)
env_file_str = base_env_str + "\n" + env_file_str

# with open(_CSTAR_CONFIG_FILE, "a") as f:
# f.write(config_file_str)
with open(Path("~/.cstar.env").expanduser(), "a") as F:
F.write(env_file_str)


def _clone_and_checkout(
Expand Down
15 changes: 11 additions & 4 deletions cstar/marbl/base_model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import subprocess

from pathlib import Path
from cstar.base import BaseModel
from cstar.base.utils import (
_clone_and_checkout,
)
from cstar.base.utils import _clone_and_checkout, _update_user_dotenv
from cstar.base.environment import environment


Expand Down Expand Up @@ -51,6 +49,15 @@ def get(self, target: str | Path) -> None:
local_path=target,
checkout_target=self.checkout_target,
)
# Set environment variables for this session:
os.environ["MARBL_ROOT"] = str(target)
environment.environment_variables["MARBL_ROOT"] = os.environ["MARBL_ROOT"]
env_file_str = f'MARBL_ROOT="{target}"\n'
_update_user_dotenv(env_file_str)

# f'\n _CSTAR_ENVIRONMENT_VARIABLES["MARBL_ROOT"]="{target}"\n'
# )
# _write_to_config_file(config_file_str)

# TODO
################################################################################
Expand Down
13 changes: 9 additions & 4 deletions cstar/roms/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import subprocess
from pathlib import Path
from cstar.base.base_model import BaseModel
from cstar.base.utils import (
_clone_and_checkout,
)
from cstar.base.utils import _clone_and_checkout, _update_user_dotenv
from cstar.base.environment import environment


Expand Down Expand Up @@ -76,11 +74,18 @@ def get(self, target: str | Path) -> None:

# TODO
################################################################################
os.environ["ROMS_ROOT"] = str(target)
environment.environment_variables["ROMS_ROOT"] = os.environ["ROMS_ROOT"]
# os.environ["ROMS_ROOT"] = str(target)
# _CSTAR_ENVIRONMENT_VARIABLES["ROMS_ROOT"] = os.environ["ROMS_ROOT"]
os.environ["PATH"] += f":{target}/Tools-Roms/"
environment.environment_variables["PATH"] = os.environ["PATH"]
# os.environ["PATH"] += f":{target}/Tools-Roms/"
# _CSTAR_ENVIRONMENT_VARIABLES["PATH"] = os.environ["PATH"]

env_file_str = (
f"ROMS_ROOT={target}" + "\nPATH=${PATH}:" + f"{target}/Tools-Roms\n"
)
_update_user_dotenv(env_file_str)
# Set the configuration file to be read by __init__.py for future sessions:
# config_file_str = (
# f' _CSTAR_ENVIRONMENT_VARIABLES["ROMS_ROOT"]="{target}"'
Expand Down

0 comments on commit e99e6cb

Please sign in to comment.