Fix sim output
This commit is contained in:
@@ -5,6 +5,7 @@ from typing import Optional
|
|||||||
from pydantic import BaseModel, ConfigDict
|
from pydantic import BaseModel, ConfigDict
|
||||||
|
|
||||||
from primaite import PRIMAITE_PATHS
|
from primaite import PRIMAITE_PATHS
|
||||||
|
from primaite.simulator import SIM_OUTPUT
|
||||||
|
|
||||||
|
|
||||||
class SessionIOSettings(BaseModel):
|
class SessionIOSettings(BaseModel):
|
||||||
@@ -36,6 +37,10 @@ class SessionIO:
|
|||||||
def __init__(self, settings: SessionIOSettings = SessionIOSettings()) -> None:
|
def __init__(self, settings: SessionIOSettings = SessionIOSettings()) -> None:
|
||||||
self.settings: SessionIOSettings = settings
|
self.settings: SessionIOSettings = settings
|
||||||
self.session_path: Path = self.generate_session_path()
|
self.session_path: Path = self.generate_session_path()
|
||||||
|
|
||||||
|
# set global SIM_OUTPUT path
|
||||||
|
SIM_OUTPUT.path = self.session_path / "simulation_output"
|
||||||
|
|
||||||
# warning TODO: must be careful not to re-initialise sessionIO because it will create a new path each time it's
|
# warning TODO: must be careful not to re-initialise sessionIO because it will create a new path each time it's
|
||||||
# possible refactor needed
|
# possible refactor needed
|
||||||
|
|
||||||
|
|||||||
@@ -324,6 +324,11 @@ class PrimaiteSession:
|
|||||||
protocols=cfg["game_config"]["protocols"],
|
protocols=cfg["game_config"]["protocols"],
|
||||||
)
|
)
|
||||||
sess.training_options = TrainingOptions(**cfg["training_config"])
|
sess.training_options = TrainingOptions(**cfg["training_config"])
|
||||||
|
|
||||||
|
# READ IO SETTINGS (this sets the global session path as well) # TODO: GLOBAL SIDE EFFECTS...
|
||||||
|
io_settings = cfg.get("io_settings", {})
|
||||||
|
sess.io_manager.settings = SessionIOSettings(**io_settings)
|
||||||
|
|
||||||
sim = sess.simulation
|
sim = sess.simulation
|
||||||
net = sim.network
|
net = sim.network
|
||||||
|
|
||||||
@@ -524,8 +529,4 @@ class PrimaiteSession:
|
|||||||
if agent_load_path:
|
if agent_load_path:
|
||||||
sess.policy.load(Path(agent_load_path))
|
sess.policy.load(Path(agent_load_path))
|
||||||
|
|
||||||
# READ IO SETTINGS
|
|
||||||
io_settings = cfg.get("io_settings", {})
|
|
||||||
sess.io_manager.settings = SessionIOSettings(**io_settings)
|
|
||||||
|
|
||||||
return sess
|
return sess
|
||||||
|
|||||||
@@ -1,14 +1,26 @@
|
|||||||
|
"""Warning: SIM_OUTPUT is a mutable global variable for the simulation output directory."""
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from primaite import _PRIMAITE_ROOT
|
from primaite import _PRIMAITE_ROOT
|
||||||
|
|
||||||
SIM_OUTPUT = None
|
__all__ = ["SIM_OUTPUT"]
|
||||||
"A path at the repo root dir to use temporarily for sim output testing while in dev."
|
|
||||||
# TODO: Remove once we integrate the simulation into PrimAITE and it uses the primaite session path
|
|
||||||
|
|
||||||
if not SIM_OUTPUT:
|
|
||||||
session_timestamp = datetime.now()
|
class __SimOutput:
|
||||||
date_dir = session_timestamp.strftime("%Y-%m-%d")
|
def __init__(self):
|
||||||
sim_path = session_timestamp.strftime("%Y-%m-%d_%H-%M-%S")
|
self._path: Path = (
|
||||||
SIM_OUTPUT = _PRIMAITE_ROOT.parent.parent / "simulation_output" / date_dir / sim_path
|
_PRIMAITE_ROOT.parent.parent / "simulation_output" / datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
SIM_OUTPUT.mkdir(exist_ok=True, parents=True)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def path(self) -> Path:
|
||||||
|
return self._path
|
||||||
|
|
||||||
|
@path.setter
|
||||||
|
def path(self, new_path: Path) -> None:
|
||||||
|
self._path = new_path
|
||||||
|
self._path.mkdir(exist_ok=True, parents=True)
|
||||||
|
|
||||||
|
|
||||||
|
SIM_OUTPUT = __SimOutput()
|
||||||
|
|||||||
@@ -957,7 +957,7 @@ class Node(SimComponent):
|
|||||||
if not kwargs.get("session_manager"):
|
if not kwargs.get("session_manager"):
|
||||||
kwargs["session_manager"] = SessionManager(sys_log=kwargs.get("sys_log"), arp_cache=kwargs.get("arp"))
|
kwargs["session_manager"] = SessionManager(sys_log=kwargs.get("sys_log"), arp_cache=kwargs.get("arp"))
|
||||||
if not kwargs.get("root"):
|
if not kwargs.get("root"):
|
||||||
kwargs["root"] = SIM_OUTPUT / kwargs["hostname"]
|
kwargs["root"] = SIM_OUTPUT.path / kwargs["hostname"]
|
||||||
if not kwargs.get("file_system"):
|
if not kwargs.get("file_system"):
|
||||||
kwargs["file_system"] = FileSystem(sys_log=kwargs["sys_log"], sim_root=kwargs["root"] / "fs")
|
kwargs["file_system"] = FileSystem(sys_log=kwargs["sys_log"], sim_root=kwargs["root"] / "fs")
|
||||||
if not kwargs.get("software_manager"):
|
if not kwargs.get("software_manager"):
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ class PacketCapture:
|
|||||||
|
|
||||||
def _get_log_path(self) -> Path:
|
def _get_log_path(self) -> Path:
|
||||||
"""Get the path for the log file."""
|
"""Get the path for the log file."""
|
||||||
root = SIM_OUTPUT / self.hostname
|
root = SIM_OUTPUT.path / self.hostname
|
||||||
root.mkdir(exist_ok=True, parents=True)
|
root.mkdir(exist_ok=True, parents=True)
|
||||||
return root / f"{self._logger_name}.log"
|
return root / f"{self._logger_name}.log"
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ class SysLog:
|
|||||||
JSON-like messages.
|
JSON-like messages.
|
||||||
"""
|
"""
|
||||||
log_path = self._get_log_path()
|
log_path = self._get_log_path()
|
||||||
|
|
||||||
file_handler = logging.FileHandler(filename=log_path)
|
file_handler = logging.FileHandler(filename=log_path)
|
||||||
file_handler.setLevel(logging.DEBUG)
|
file_handler.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
@@ -81,7 +80,7 @@ class SysLog:
|
|||||||
|
|
||||||
:return: Path object representing the location of the log file.
|
:return: Path object representing the location of the log file.
|
||||||
"""
|
"""
|
||||||
root = SIM_OUTPUT / self.hostname
|
root = SIM_OUTPUT.path / self.hostname
|
||||||
root.mkdir(exist_ok=True, parents=True)
|
root.mkdir(exist_ok=True, parents=True)
|
||||||
return root / f"{self.hostname}_sys.log"
|
return root / f"{self.hostname}_sys.log"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user