Skip to content

Commit f65f305

Browse files
Jiri Pirkodavem330
authored andcommitted
tools: ynl-gen: use temporary file for rendering
Currently any error during render leads to output an empty file. That is quite annoying when using tools/net/ynl/ynl-regen.sh which git greps files with content of "YNL-GEN.." and therefore ignores empty files. So once you fail to regen, you have to checkout the file. Avoid that by rendering to a temporary file first, only at the end copy the content to the actual destination. Signed-off-by: Jiri Pirko <[email protected]> Reviewed-by: Jakub Kicinski <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 58f2ffd commit f65f305

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import collections
66
import os
77
import re
8+
import shutil
9+
import tempfile
810
import yaml
911

1012
from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, SpecEnumEntry
@@ -2304,7 +2306,7 @@ def main():
23042306
parser.add_argument('-o', dest='out_file', type=str)
23052307
args = parser.parse_args()
23062308

2307-
out_file = open(args.out_file, 'w+') if args.out_file else os.sys.stdout
2309+
tmp_file = tempfile.TemporaryFile('w+') if args.out_file else os.sys.stdout
23082310

23092311
if args.header is None:
23102312
parser.error("--header or --source is required")
@@ -2329,7 +2331,7 @@ def main():
23292331
print(f'Message enum-model {parsed.msg_id_model} not supported for {args.mode} generation')
23302332
os.sys.exit(1)
23312333

2332-
cw = CodeWriter(BaseNlLib(), out_file)
2334+
cw = CodeWriter(BaseNlLib(), tmp_file)
23332335

23342336
_, spec_kernel = find_kernel_root(args.spec)
23352337
if args.mode == 'uapi' or args.header:
@@ -2578,6 +2580,10 @@ def main():
25782580
if args.header:
25792581
cw.p(f'#endif /* {hdr_prot} */')
25802582

2583+
if args.out_file:
2584+
out_file = open(args.out_file, 'w+')
2585+
tmp_file.seek(0)
2586+
shutil.copyfileobj(tmp_file, out_file)
25812587

25822588
if __name__ == "__main__":
25832589
main()

0 commit comments

Comments
 (0)