Commit f65f305a authored by Jiri Pirko's avatar Jiri Pirko Committed by David S. Miller

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: default avatarJiri Pirko <jiri@nvidia.com>
Reviewed-by: default avatarJakub Kicinski <kuba@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 58f2ffde
...@@ -5,6 +5,8 @@ import argparse ...@@ -5,6 +5,8 @@ import argparse
import collections import collections
import os import os
import re import re
import shutil
import tempfile
import yaml import yaml
from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, SpecEnumEntry from lib import SpecFamily, SpecAttrSet, SpecAttr, SpecOperation, SpecEnumSet, SpecEnumEntry
...@@ -2304,7 +2306,7 @@ def main(): ...@@ -2304,7 +2306,7 @@ def main():
parser.add_argument('-o', dest='out_file', type=str) parser.add_argument('-o', dest='out_file', type=str)
args = parser.parse_args() args = parser.parse_args()
out_file = open(args.out_file, 'w+') if args.out_file else os.sys.stdout tmp_file = tempfile.TemporaryFile('w+') if args.out_file else os.sys.stdout
if args.header is None: if args.header is None:
parser.error("--header or --source is required") parser.error("--header or --source is required")
...@@ -2329,7 +2331,7 @@ def main(): ...@@ -2329,7 +2331,7 @@ def main():
print(f'Message enum-model {parsed.msg_id_model} not supported for {args.mode} generation') print(f'Message enum-model {parsed.msg_id_model} not supported for {args.mode} generation')
os.sys.exit(1) os.sys.exit(1)
cw = CodeWriter(BaseNlLib(), out_file) cw = CodeWriter(BaseNlLib(), tmp_file)
_, spec_kernel = find_kernel_root(args.spec) _, spec_kernel = find_kernel_root(args.spec)
if args.mode == 'uapi' or args.header: if args.mode == 'uapi' or args.header:
...@@ -2578,6 +2580,10 @@ def main(): ...@@ -2578,6 +2580,10 @@ def main():
if args.header: if args.header:
cw.p(f'#endif /* {hdr_prot} */') cw.p(f'#endif /* {hdr_prot} */')
if args.out_file:
out_file = open(args.out_file, 'w+')
tmp_file.seek(0)
shutil.copyfileobj(tmp_file, out_file)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment