import importlib.resources
import logging
import subprocess
import sys
from pathlib import Path
[docs]
logger = logging.getLogger(__name__)
[docs]
def install_tulipa(workspace: Path | str = "./") -> None:
"""Install TulipaEnergyModel.jl by instantiating a Julia environment.
This function creates a Julia Project.toml file in the specified workspace
directory and runs `Pkg.instantiate()` to install all required Julia
dependencies, including TulipaEnergyModel.jl.
Args:
workspace: Path to the workspace directory where the Julia environment
will be created. Defaults to the current directory ("./").
Returns:
Path to the directory containing the instantiated Julia environment.
Raises:
SystemExit: If Julia package instantiation fails.
"""
env_dir = Path(workspace)
project_file = env_dir / "Project.toml"
logger.info("Setting up Julia environment in: %s", env_dir.resolve())
env_dir.mkdir(parents=True, exist_ok=True)
pkg_files = importlib.resources.files("annular.market.tulipa")
toml_content = pkg_files.joinpath("Project.toml").read_text(encoding="utf-8")
logger.debug("Writing Project.toml to: %s", project_file)
with project_file.open("w", encoding="utf-8") as f:
f.write(toml_content)
logger.info("Instantiating Julia environment (this may take a few minutes)...")
cmd = [
"julia",
f"--project={env_dir}",
"-e",
"using Pkg; Pkg.instantiate()",
]
try:
subprocess.run(cmd, check=True, capture_output=False)
logger.info("Successfully instantiated Julia environment")
logger.info("Manifest created at: %s", env_dir / "Manifest.toml")
except subprocess.CalledProcessError as e:
logger.error("Failed to instantiate Julia environment")
logger.error("Command: %s", " ".join(cmd))
logger.error("Return code: %s", e.returncode)
sys.exit(1)
except FileNotFoundError:
logger.error("Julia executable not found. Please ensure Julia is installed and in PATH.")
sys.exit(1)
return None