]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
xdrgen: Generalize/harden pathname construction
authorChuck Lever <chuck.lever@oracle.com>
Mon, 27 Oct 2025 13:56:31 +0000 (09:56 -0400)
committerChuck Lever <chuck.lever@oracle.com>
Tue, 25 Nov 2025 14:09:42 +0000 (09:09 -0500)
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>
tools/net/sunrpc/xdrgen/generators/__init__.py

index b98574a36a4ac3b85fb602fd802c736f3733e872..e22632cf38fbec3cc7186776c70303853a64f248 100644 (file)
@@ -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: