Skip to content

Commit

Permalink
Add support for env variables in .f files. #35
Browse files Browse the repository at this point in the history
  • Loading branch information
amykyta3 committed Oct 26, 2023
1 parent bdec003 commit ca1a768
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/peakrdl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import sys
import os
import shlex
from typing import TYPE_CHECKING, List, Dict, Optional, Set
import re
from typing import TYPE_CHECKING, List, Dict, Optional, Set, Match

from systemrdl import RDLCompileError

Expand Down Expand Up @@ -89,9 +90,26 @@ def expand_file_args(argv: List[str], _pathlist: Optional[Set[str]] = None) -> L
return new_argv


def expand_arg_vars(argv: List[str]) -> List[str]:
"""
Expand environment variables in args
"""
pattern = re.compile(r"\$(\w+|\{[^}]*\})")
def repl(m: Match) -> str:
k = m.group(1)
if k.startswith("{") and k.endswith("}"):
k = k[1:-1]

v = os.environ.get(k, m.group(0))
return v

return [pattern.sub(repl, arg) for arg in argv]


def main() -> None:
# manually expand any -f argfiles first
argv = expand_file_args(sys.argv[1:])
argv = expand_arg_vars(argv)

cfg = load_cfg(argv)

Expand Down
1 change: 1 addition & 0 deletions test/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_input_dne(self):
], expects_error=True)

def test_f_argfile(self):
os.environ["PEAKRDL_TOP_TEST_ENVVAR"] = "nested"
self.run_commandline([
'-f', os.path.join(self.testdata_dir, "dump_nested.f"),
])
Expand Down
2 changes: 1 addition & 1 deletion test/testdata/dump_nested.f
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dump testdata/parameters.rdl --top nested
dump testdata/parameters.rdl --top ${PEAKRDL_TOP_TEST_ENVVAR}

0 comments on commit ca1a768

Please sign in to comment.