Skip to content

Commit 9a7a08e

Browse files
machshevhcallahan-lowrisc
authored andcommitted
refactor: add WorkspaceCfg
Reduce dependency on sim_cfg by pulling out some of the data and putting it on a new WorkspaceCfg pydantic object. Signed-off-by: James McCorrie <[email protected]>
1 parent 08b8e6d commit 9a7a08e

File tree

8 files changed

+186
-77
lines changed

8 files changed

+186
-77
lines changed

src/dvsim/job/deploy.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from pathlib import Path
1212
from typing import TYPE_CHECKING, ClassVar
1313

14+
from pydantic import BaseModel
15+
from pydantic.config import ConfigDict
1416
from tabulate import tabulate
1517

1618
from dvsim.job.time import JobTime
@@ -27,6 +29,20 @@
2729
if TYPE_CHECKING:
2830
from dvsim.flow.sim import SimCfg
2931

32+
33+
class WorkspaceConfig(BaseModel):
34+
"""Workspace configuration."""
35+
36+
model_config = ConfigDict(frozen=True, extra="forbid")
37+
38+
project: str
39+
timestamp: str
40+
41+
project_root: Path
42+
scratch_root: Path
43+
scratch_path: Path
44+
45+
3046
__all__ = (
3147
"CompileSim",
3248
"Deploy",
@@ -101,6 +117,14 @@ def __init__(self, sim_cfg: "SimCfg") -> None:
101117
# Job's wall clock time (a.k.a CPU time, or runtime).
102118
self.job_runtime = JobTime()
103119

120+
self.workspace_cfg = WorkspaceConfig(
121+
project=sim_cfg.name,
122+
project_root=sim_cfg.proj_root,
123+
scratch_root=Path(sim_cfg.scratch_root),
124+
scratch_path=Path(sim_cfg.scratch_path),
125+
timestamp=sim_cfg.args.timestamp,
126+
)
127+
104128
def _define_attrs(self) -> None:
105129
"""Define the attributes this instance needs to have.
106130

src/dvsim/launcher/base.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from dvsim.utils import clean_odirs, mk_symlink, rm_path
2020

2121
if TYPE_CHECKING:
22-
from dvsim.job.deploy import Deploy
22+
from dvsim.job.deploy import Deploy, WorkspaceConfig
2323

2424

2525
class LauncherError(Exception):
@@ -97,18 +97,18 @@ def __init__(self, deploy: "Deploy") -> None:
9797
deploy: deployment object that will be launched.
9898
9999
"""
100-
cfg = deploy.sim_cfg
100+
workspace_cfg = deploy.workspace_cfg
101101

102102
# One-time preparation of the workspace.
103103
if not Launcher.workspace_prepared:
104-
# TODO: CLI args should be processed far earlier than this
105-
self.prepare_workspace(cfg.project, cfg.proj_root, cfg.args)
104+
self.prepare_workspace(workspace_cfg)
106105
Launcher.workspace_prepared = True
107106

108107
# One-time preparation of the workspace, specific to the cfg.
109-
if cfg not in Launcher.workspace_prepared_for_cfg:
110-
self.prepare_workspace_for_cfg(cfg)
111-
Launcher.workspace_prepared_for_cfg.add(cfg)
108+
project = workspace_cfg.project
109+
if project not in Launcher.workspace_prepared_for_cfg:
110+
self.prepare_workspace_for_cfg(workspace_cfg)
111+
Launcher.workspace_prepared_for_cfg.add(project)
112112

113113
# Store the deploy object handle.
114114
self.deploy = deploy
@@ -155,34 +155,40 @@ def set_pyvenv(project: str) -> None:
155155
# The code below allows each launcher variant to set its own virtualenv
156156
# because the loading / activating mechanism could be different between
157157
# them.
158-
Launcher.pyvenv = os.environ.get(
159-
f"{project.upper()}_PYVENV_{Launcher.variant.upper()}",
160-
)
158+
common_venv = f"{project.upper()}_PYVENV"
159+
variant = Launcher.variant.upper()
160+
161+
venv_path = os.environ.get(f"{common_venv}_{variant}")
162+
163+
if not venv_path:
164+
venv_path = os.environ.get(common_venv)
161165

162166
if not Launcher.pyvenv:
163167
Launcher.pyvenv = os.environ.get(f"{project.upper()}_PYVENV")
164168

165169
@staticmethod
166170
@abstractmethod
167-
def prepare_workspace(project: str, repo_top: str, args: Mapping) -> None:
171+
def prepare_workspace(cfg: "WorkspaceConfig") -> None:
168172
"""Prepare the workspace based on the chosen launcher's needs.
169173
170174
This is done once for the entire duration for the flow run.
171175
172176
Args:
173-
project: the name of the project.
174-
repo_top: the path to the repository.
175-
args: command line args passed to dvsim.
177+
cfg: workspace configuration
176178
177179
"""
178180

179181
@staticmethod
180182
@abstractmethod
181-
def prepare_workspace_for_cfg(cfg: Mapping) -> None:
183+
def prepare_workspace_for_cfg(cfg: "WorkspaceConfig") -> None:
182184
"""Prepare the workspace for a cfg.
183185
184186
This is invoked once for each cfg.
185187
'cfg' is the flow configuration object.
188+
189+
Args:
190+
cfg: workspace configuration
191+
186192
"""
187193

188194
def __str__(self) -> str:

src/dvsim/launcher/fake.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
"""Fake Launcher that returns random results."""
66

7-
from collections.abc import Mapping
87
from random import choice, random
98
from typing import TYPE_CHECKING
109

1110
from dvsim.launcher.base import ErrorMessage, Launcher
1211

1312
if TYPE_CHECKING:
14-
from dvsim.job.deploy import CovReport, Deploy, RunTest
13+
from dvsim.job.deploy import CovReport, Deploy, RunTest, WorkspaceConfig
14+
1515

1616
__all__ = ("FakeLauncher",)
1717

@@ -75,22 +75,23 @@ def kill(self) -> None:
7575
)
7676

7777
@staticmethod
78-
def prepare_workspace(project: str, repo_top: str, args: Mapping) -> None:
78+
def prepare_workspace(cfg: "WorkspaceConfig") -> None:
7979
"""Prepare the workspace based on the chosen launcher's needs.
8080
8181
This is done once for the entire duration for the flow run.
8282
8383
Args:
84-
project: the name of the project.
85-
repo_top: the path to the repository.
86-
args: command line args passed to dvsim.
84+
cfg: workspace configuration
8785
8886
"""
8987

9088
@staticmethod
91-
def prepare_workspace_for_cfg(cfg: Mapping) -> None:
89+
def prepare_workspace_for_cfg(cfg: "WorkspaceConfig") -> None:
9290
"""Prepare the workspace for a cfg.
9391
9492
This is invoked once for each cfg.
95-
'cfg' is the flow configuration object.
93+
94+
Args:
95+
cfg: workspace configuration
96+
9697
"""

src/dvsim/launcher/local.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
import os
99
import shlex
1010
import subprocess
11-
from collections.abc import Mapping
1211
from pathlib import Path
1312
from typing import TYPE_CHECKING
1413

1514
from dvsim.launcher.base import ErrorMessage, Launcher, LauncherBusyError, LauncherError
1615

1716
if TYPE_CHECKING:
18-
from dvsim.job.deploy import Deploy
17+
from dvsim.job.deploy import Deploy, WorkspaceConfig
1918

2019

2120
class LocalLauncher(Launcher):
@@ -184,22 +183,23 @@ def _close_job_log_file(self) -> None:
184183
self._log_file.close()
185184

186185
@staticmethod
187-
def prepare_workspace(project: str, repo_top: str, args: Mapping) -> None:
186+
def prepare_workspace(cfg: "WorkspaceConfig") -> None:
188187
"""Prepare the workspace based on the chosen launcher's needs.
189188
190189
This is done once for the entire duration for the flow run.
191190
192191
Args:
193-
project: the name of the project.
194-
repo_top: the path to the repository.
195-
args: command line args passed to dvsim.
192+
cfg: workspace configuration
196193
197194
"""
198195

199196
@staticmethod
200-
def prepare_workspace_for_cfg(cfg: Mapping) -> None:
197+
def prepare_workspace_for_cfg(cfg: "WorkspaceConfig") -> None:
201198
"""Prepare the workspace for a cfg.
202199
203200
This is invoked once for each cfg.
204-
'cfg' is the flow configuration object.
201+
202+
Args:
203+
cfg: workspace configuration
204+
205205
"""

0 commit comments

Comments
 (0)