Skip to content

Commit 75a9b40

Browse files
committed
xdrgen: Generalize/harden pathname construction
Use Python's built-in Path constructor to find the Jinja templates. This provides better error checking, proper use of path component separators, and more reliable location of the template files. Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent 898f944 commit 75a9b40

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

tools/net/sunrpc/xdrgen/generators/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""Define a base code generator class"""
44

5-
import sys
5+
from pathlib import Path
66
from jinja2 import Environment, FileSystemLoader, Template
77

88
from xdr_ast import _XdrAst, Specification, _RpcProgram, _XdrTypeSpecifier
@@ -14,8 +14,11 @@ def create_jinja2_environment(language: str, xdr_type: str) -> Environment:
1414
"""Open a set of templates based on output language"""
1515
match language:
1616
case "C":
17+
templates_dir = (
18+
Path(__file__).parent.parent / "templates" / language / xdr_type
19+
)
1720
environment = Environment(
18-
loader=FileSystemLoader(sys.path[0] + "/templates/C/" + xdr_type + "/"),
21+
loader=FileSystemLoader(templates_dir),
1922
trim_blocks=True,
2023
lstrip_blocks=True,
2124
)
@@ -48,9 +51,7 @@ def find_xdr_program_name(root: Specification) -> str:
4851

4952
def header_guard_infix(filename: str) -> str:
5053
"""Extract the header guard infix from the specification filename"""
51-
basename = filename.split("/")[-1]
52-
program = basename.replace(".x", "")
53-
return program.upper()
54+
return Path(filename).stem.upper()
5455

5556

5657
def kernel_c_type(spec: _XdrTypeSpecifier) -> str:

0 commit comments

Comments
 (0)