On 9/15/20 11:44 AM, Vladimir Sementsov-Ogievskiy wrote:
We have a very frequent pattern of creating coroutine from function with several arguments:
+++ b/scripts/block-coroutine-wrapper.py @@ -0,0 +1,187 @@ +#!/usr/bin/env python3 +"""Generate coroutine wrappers for block subsystem.
Looking at the generated file after patch 5 is applied,...
+ +def gen_header(): + copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL) + copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE) + copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE) + return f"""\
This generated comment...
+ + +def gen_wrappers_file(input_code: str) -> str: + res = gen_header()
...is getting inserted into the generated file...
+ for func in func_decl_iter(input_code): + res += '\n\n\n' + res += gen_wrapper(func) + + return prettify(res) # prettify to wrap long lines + + +if __name__ == '__main__': + if len(sys.argv) < 3: + exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...') + + with open(sys.argv[1], 'w') as f_out: + for fname in sys.argv[2:]: + with open(fname) as f_in: + f_out.write(gen_wrappers_file(f_in.read()))
multiple times. You'll want to hoist the call to gen_header outside the loop over fname in sys.argv[2:].
-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org