Skip to content

Commit a02430c

Browse files
committed
tools: ynl-gen: fix uAPI generation after tempfile changes
We use a tempfile for code generation, to avoid wiping the target file out if the code generator crashes. File contents are copied from tempfile to actual destination at the end of main(). uAPI generation is relatively simple so when generating the uAPI header we return from main() early, and never reach the "copy code over" stage. Since commit under Fixes uAPI headers are not updated by ynl-gen. Move the copy/commit of the code into CodeWriter, to make it easier to call at any point in time. Hook it into the destructor to make sure we don't miss calling it. Fixes: f65f305 ("tools: ynl-gen: use temporary file for rendering") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent f5e17b4 commit a02430c

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

tools/net/ynl/ynl-gen-c.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,14 +1045,30 @@ def __init__(self, cw, family, ku_space, op, op_mode, attr_set=None):
10451045

10461046

10471047
class CodeWriter:
1048-
def __init__(self, nlib, out_file):
1048+
def __init__(self, nlib, out_file=None):
10491049
self.nlib = nlib
10501050

10511051
self._nl = False
10521052
self._block_end = False
10531053
self._silent_block = False
10541054
self._ind = 0
1055-
self._out = out_file
1055+
if out_file is None:
1056+
self._out = os.sys.stdout
1057+
else:
1058+
self._out = tempfile.TemporaryFile('w+')
1059+
self._out_file = out_file
1060+
1061+
def __del__(self):
1062+
self.close_out_file()
1063+
1064+
def close_out_file(self):
1065+
if self._out == os.sys.stdout:
1066+
return
1067+
with open(self._out_file, 'w+') as out_file:
1068+
self._out.seek(0)
1069+
shutil.copyfileobj(self._out, out_file)
1070+
self._out.close()
1071+
self._out = os.sys.stdout
10561072

10571073
@classmethod
10581074
def _is_cond(cls, line):
@@ -2313,11 +2329,9 @@ def main():
23132329
parser.add_argument('--source', dest='header', action='store_false')
23142330
parser.add_argument('--user-header', nargs='+', default=[])
23152331
parser.add_argument('--exclude-op', action='append', default=[])
2316-
parser.add_argument('-o', dest='out_file', type=str)
2332+
parser.add_argument('-o', dest='out_file', type=str, default=None)
23172333
args = parser.parse_args()
23182334

2319-
tmp_file = tempfile.TemporaryFile('w+') if args.out_file else os.sys.stdout
2320-
23212335
if args.header is None:
23222336
parser.error("--header or --source is required")
23232337

@@ -2341,7 +2355,7 @@ def main():
23412355
print(f'Message enum-model {parsed.msg_id_model} not supported for {args.mode} generation')
23422356
os.sys.exit(1)
23432357

2344-
cw = CodeWriter(BaseNlLib(), tmp_file)
2358+
cw = CodeWriter(BaseNlLib(), args.out_file)
23452359

23462360
_, spec_kernel = find_kernel_root(args.spec)
23472361
if args.mode == 'uapi' or args.header:
@@ -2590,10 +2604,6 @@ def main():
25902604
if args.header:
25912605
cw.p(f'#endif /* {hdr_prot} */')
25922606

2593-
if args.out_file:
2594-
out_file = open(args.out_file, 'w+')
2595-
tmp_file.seek(0)
2596-
shutil.copyfileobj(tmp_file, out_file)
25972607

25982608
if __name__ == "__main__":
25992609
main()

0 commit comments

Comments
 (0)