Commit 75a9b40f authored by Chuck Lever's avatar Chuck Lever
Browse files

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: default avatarChuck Lever <chuck.lever@oracle.com>
parent 898f9446
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

"""Define a base code generator class"""

import sys
from pathlib import Path
from jinja2 import Environment, FileSystemLoader, Template

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

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


def kernel_c_type(spec: _XdrTypeSpecifier) -> str: