Skip to content

Commit b157705

Browse files
committed
refactor: fix cantera_file NameError, unify archive relpath, derive completion flags
run/input.py: - cantera_file was only assigned in the chemistry=T branch; the error message on line 69 would raise NameError when chemistry=F and all candidates (h2o2.yaml) fail to load — set cantera_file='h2o2.yaml' before the branch so the error message is always valid run/archive.py: - Extract _relpath_safe(path, dirpath) to replace three inline try/except blocks scattered across __build_manifest, __copy_dir, __write_tar - Fix manifest fallback: was falling back to the full absolute path on cross-device paths; now uses basename like copy/tar already did — manifest and archive contents now agree cli/completion_gen.py: - Bash and zsh completion lists for MFC config flags were hardcoded, already missing --reldebug/--no-reldebug; derive dynamically from MFCConfig dataclass fields (same source argparse_gen.py already uses) - New MFCConfig fields are now automatically reflected in completions
1 parent 1dbcdd6 commit b157705

3 files changed

Lines changed: 49 additions & 61 deletions

File tree

toolchain/mfc/cli/completion_gen.py

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,42 @@
55
in sync with the CLI schema definitions.
66
"""
77

8+
import dataclasses
89
from typing import List, Set
910

1011
from .schema import CLISchema, Command, CompletionType
1112

13+
14+
def _mfc_config_bash_flags() -> List[str]:
15+
"""Return bash completion flag strings derived from MFCConfig fields."""
16+
from ..state import MFCConfig
17+
18+
flags = []
19+
for f in dataclasses.fields(MFCConfig):
20+
cli = f.name.replace("_", "-")
21+
flags.append(f"--{cli}")
22+
flags.append(f"--no-{cli}")
23+
return flags
24+
25+
26+
def _mfc_config_zsh_flags() -> List[str]:
27+
"""Return zsh completion spec strings derived from MFCConfig fields."""
28+
from ..state import MFCConfig, gpuConfigOptions
29+
30+
specs = []
31+
modes = ":".join(e.value for e in gpuConfigOptions if e.value != gpuConfigOptions.NONE.value)
32+
for f in dataclasses.fields(MFCConfig):
33+
cli = f.name.replace("_", "-")
34+
label = cli.replace("-", " ").title()
35+
if f.name == "gpu":
36+
specs.append(f"'--{cli}[Enable GPU]:mode:({modes})'")
37+
specs.append(f"'--no-{cli}[Disable GPU]'")
38+
else:
39+
specs.append(f"'--{cli}[Enable {label}]'")
40+
specs.append(f"'--no-{cli}[Disable {label}]'")
41+
return specs
42+
43+
1244
# Mapping of completion types to bash completion expressions
1345
_BASH_COMPLETION_MAP = {
1446
CompletionType.FILES_PY: 'COMPREPLY=( $(compgen -f -X "!*.py" -- "${cur}") $(compgen -d -- "${cur}") )',
@@ -31,26 +63,7 @@ def _collect_all_options(cmd: Command, schema: CLISchema) -> List[str]:
3163

3264
# MFC config flags
3365
if common_set.mfc_config_flags:
34-
options.update(
35-
[
36-
"--mpi",
37-
"--no-mpi",
38-
"--gpu",
39-
"--no-gpu",
40-
"--debug",
41-
"--no-debug",
42-
"--gcov",
43-
"--no-gcov",
44-
"--unified",
45-
"--no-unified",
46-
"--single",
47-
"--no-single",
48-
"--mixed",
49-
"--no-mixed",
50-
"--fastmath",
51-
"--no-fastmath",
52-
]
53-
)
66+
options.update(_mfc_config_bash_flags())
5467
else:
5568
for arg in common_set.arguments:
5669
if arg.short:
@@ -324,26 +337,7 @@ def _generate_zsh_command_args(cmd: Command, schema: CLISchema) -> List[str]:
324337
continue
325338

326339
if common_set.mfc_config_flags:
327-
arg_lines.extend(
328-
[
329-
"'--mpi[Enable MPI]'",
330-
"'--no-mpi[Disable MPI]'",
331-
"'--gpu[Enable GPU]:mode:(acc mp)'",
332-
"'--no-gpu[Disable GPU]'",
333-
"'--debug[Build with debug compiler flags (for MFC code)]'",
334-
"'--no-debug[Build without debug flags]'",
335-
"'--gcov[Enable gcov coverage]'",
336-
"'--no-gcov[Disable gcov coverage]'",
337-
"'--unified[Enable unified memory]'",
338-
"'--no-unified[Disable unified memory]'",
339-
"'--single[Enable single precision]'",
340-
"'--no-single[Disable single precision]'",
341-
"'--mixed[Enable mixed precision]'",
342-
"'--no-mixed[Disable mixed precision]'",
343-
"'--fastmath[Enable fast math]'",
344-
"'--no-fastmath[Disable fast math]'",
345-
]
346-
)
340+
arg_lines.extend(_mfc_config_zsh_flags())
347341
else:
348342
for arg in common_set.arguments:
349343
desc = arg.help.replace("'", "").replace("[", "").replace("]", "")[:120]

toolchain/mfc/run/archive.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
from ..state import ARG, CFG
1212
from . import input
1313

14+
15+
def _relpath_safe(path: str, dirpath: str) -> str:
16+
"""Return relpath(path, dirpath), falling back to basename on cross-device paths."""
17+
try:
18+
return os.path.relpath(path, dirpath)
19+
except ValueError:
20+
return os.path.basename(path)
21+
22+
1423
ARTIFACT_FILENAMES = [
1524
"equations.dat",
1625
"run_time.inf",
@@ -74,13 +83,7 @@ def __collect_sources(case: input.MFCInputFile, targets) -> list:
7483

7584
def __build_manifest(case: input.MFCInputFile, targets, sources: list, archive_path: str, archive_format: str) -> dict:
7685
dirpath = case.dirpath
77-
relative_sources = []
78-
for src in sources:
79-
try:
80-
rel = os.path.relpath(src, dirpath)
81-
except ValueError:
82-
rel = src
83-
relative_sources.append(rel)
86+
relative_sources = [_relpath_safe(src, dirpath) for src in sources]
8487

8588
return {
8689
"timestamp": datetime.datetime.now().astimezone().isoformat(),
@@ -101,11 +104,7 @@ def __copy_dir(sources: list, case: input.MFCInputFile, dest: str) -> None:
101104
dirpath = case.dirpath
102105

103106
for src in sources:
104-
try:
105-
rel = os.path.relpath(src, dirpath)
106-
except ValueError:
107-
rel = os.path.basename(src)
108-
107+
rel = _relpath_safe(src, dirpath)
109108
target_path = os.path.join(dest, rel)
110109
os.makedirs(os.path.dirname(target_path), exist_ok=True)
111110

@@ -120,10 +119,7 @@ def __write_tar(sources: list, case: input.MFCInputFile, dest: str, compressed:
120119
arcroot = os.path.basename(dest).removesuffix(".tar.zst").removesuffix(".tar")
121120

122121
def rel_for(path: str) -> str:
123-
try:
124-
return os.path.relpath(path, dirpath)
125-
except ValueError:
126-
return os.path.basename(path)
122+
return _relpath_safe(path, dirpath)
127123

128124
if compressed:
129125
if not does_command_exist("tar"):

toolchain/mfc/run/input.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,15 @@ def get_cantera_solution(self):
4747

4848
if self.params.get("chemistry", "F") == "T":
4949
cantera_file = self.params["cantera_file"]
50-
5150
candidates = [
5251
cantera_file,
5352
os.path.join(self.dirpath, cantera_file),
5453
os.path.join(common.MFC_MECHANISMS_DIR, cantera_file),
5554
]
5655
else:
57-
# If Chemistry is turned off, we return a default (dummy) solution
58-
# that will not be used in the simulation, so that MFC can still
59-
# be compiled.
60-
candidates = ["h2o2.yaml"]
56+
# Chemistry is off — return a dummy solution so MFC still compiles.
57+
cantera_file = "h2o2.yaml"
58+
candidates = [cantera_file]
6159

6260
for candidate in candidates:
6361
try:

0 commit comments

Comments
 (0)