From faead66c4ea9dcfa7ca6c5f0e9f00b71a396c077 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Wed, 23 Feb 2022 23:55:36 +0100 Subject: [PATCH] generate_packets.py: Replace positional arguments with optional, named arguments Note: Empty or invalid paths will no longer be skipped, but result in an error. See osdn #43972 Signed-off-by: Alina Lenk --- common/Makefile.am | 8 +++++- common/generate_packets.py | 50 +++++++++++++++++++++++--------------- meson.build | 9 ++++--- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/common/Makefile.am b/common/Makefile.am index 834900d53c..1c4fede216 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -163,7 +163,13 @@ BUILT_SOURCES = packets_gen.c packets_gen.h packets_gen.h packets_gen.c: packets_generate .INTERMEDIATE: packets_generate packets_generate: networking/packets.def generate_packets.py - cd $(srcdir) && ./generate_packets.py packets_gen.h packets_gen.c ../client/packhand_gen.h ../client/packhand_gen.c ../server/hand_gen.h ../server/hand_gen.c + cd $(srcdir) && ./generate_packets.py \ + --common-h packets_gen.h \ + --common-c packets_gen.c \ + --client-h ../client/packhand_gen.h \ + --client-c ../client/packhand_gen.c \ + --server-h ../server/hand_gen.h \ + --server-c ../server/hand_gen.c touch packets_generate # These files are not generated to builddir, but to srcdir */ diff --git a/common/generate_packets.py b/common/generate_packets.py index 90e0b87248..c61e42d6ec 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -50,27 +50,39 @@ from functools import partial is_verbose = False +def file_path(s): + """Parse the given path and check basic validity.""" + path = Path(s) + + if path.is_reserved() or not path.name: + raise ValueError("not a valid file path: %r" % s) + if path.exists() and not path.is_file(): + raise ValueError("not a file: %r" % s) + + return path + def get_argparser(): parser = argparse.ArgumentParser( description = "Generate packet-related code from packets.def", ) - parser.add_argument("common_header_path", type = Path, - help = "output path for common/packets_gen.h") - parser.add_argument("common_impl_path", type = Path, - help = "output path for common/packets_gen.c") - parser.add_argument("client_header_path", type = Path, - help = "output path for client/packhand_gen.h") - parser.add_argument("client_impl_path", type = Path, - help = "output path for client/packhand_gen.c") - parser.add_argument("server_header_path", type = Path, - help = "output path for server/hand_gen.h") - parser.add_argument("server_impl_path", type = Path, - help = "output path for server/hand_gen.c") - parser.add_argument("-v", "--verbose", action = "store_true", help = "enable log messages during code generation") + path_args = ( + # (dest, option, canonical path) + ("common_header_path", "--common-h", "common/packets_gen.h"), + ("common_impl_path", "--common-c", "common/packets_gen.c"), + ("client_header_path", "--client-h", "client/packhand_gen.h"), + ("client_impl_path", "--client-c", "client/packhand_gen.c"), + ("server_header_path", "--server-h", "server/hand_gen.h"), + ("server_impl_path", "--server-c", "server/hand_gen.c"), + ) + + for dest, option, canonical in path_args: + parser.add_argument(option, dest = dest, type = file_path, + help = "output path for %s" % canonical) + return parser def verbose(s): @@ -1977,7 +1989,7 @@ def parse_packets_def(def_text): def write_common_header(path, packets): """Write contents for common/packets_gen.h to the given path""" - if not path or not path.name: + if path is None: return with fc_open(path) as output_h: output_h.write(''' @@ -2016,7 +2028,7 @@ void delta_stats_reset(void); def write_common_impl(path, packets): """Write contents for common/packets_gen.c to the given path""" - if not path or not path.name: + if path is None: return with fc_open(path) as output_c: output_c.write(''' @@ -2087,7 +2099,7 @@ static int stats_total_sent; def write_server_header(path, packets): """Write contents for server/hand_gen.h to the given path""" - if not path or not path.name: + if path is None: return with fc_open(path) as f: f.write(''' @@ -2133,7 +2145,7 @@ bool server_handle_packet(enum packet_type type, const void *packet, def write_client_header(path, packets): """Write contents for client/packhand_gen.h to the given path""" - if not path or not path.name: + if path is None: return with fc_open(path) as f: f.write(''' @@ -2178,7 +2190,7 @@ bool client_handle_packet(enum packet_type type, const void *packet); def write_server_impl(path, packets): """Write contents for server/hand_gen.c to the given path""" - if not path or not path.name: + if path is None: return with fc_open(path) as f: f.write(''' @@ -2237,7 +2249,7 @@ bool server_handle_packet(enum packet_type type, const void *packet, def write_client_impl(path, packets): """Write contents for client/packhand_gen.c to the given path""" - if not path or not path.name: + if path is None: return with fc_open(path) as f: f.write(''' diff --git a/meson.build b/meson.build index 080305f80a..786cf3baec 100644 --- a/meson.build +++ b/meson.build @@ -467,18 +467,21 @@ specenum = custom_target('specenum_gen.h', output: 'specenum_gen.h', pack_common = custom_target('packets_common', output: ['packets_gen.h', 'packets_gen.c'], command: [python_exe, files('common/generate_packets.py'), - '@OUTPUT0@', '@OUTPUT1@', '', '', '', ''], + '--common-h', '@OUTPUT0@', + '--common-c', '@OUTPUT1@'], depend_files: files('common/networking/packets.def')) pack_server = custom_target('packets_server', output: ['hand_gen.h', 'hand_gen.c'], command: [python_exe, files('common/generate_packets.py'), - '', '', '', '', '@OUTPUT0@', '@OUTPUT1@'], + '--server-h', '@OUTPUT0@', + '--server-c', '@OUTPUT1@'], depend_files: files('common/networking/packets.def')) pack_client = custom_target('packets_client', output: ['packhand_gen.h', 'packhand_gen.c'], command: [python_exe, files('common/generate_packets.py'), - '', '', '@OUTPUT0@', '@OUTPUT1@', '', ''], + '--client-h', '@OUTPUT0@', + '--client-c', '@OUTPUT1@'], depend_files: files('common/networking/packets.def')) gitrev = custom_target('gitrev', output: 'fc_gitrev_gen.h', -- 2.17.1