Changeset View
Changeset View
Standalone View
Standalone View
test/lint/check-rpc-mappings.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2017-2018 The Bitcoin Core developers | # Copyright (c) 2017-2018 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Check RPC argument consistency.""" | """Check RPC argument consistency.""" | ||||
from collections import defaultdict | from collections import defaultdict | ||||
import glob | |||||
import os | import os | ||||
import re | import re | ||||
import sys | import sys | ||||
# Source files (relative to root) to scan for dispatch tables | # Source files (relative to root) to scan for dispatch tables | ||||
SOURCES = [ | SOURCE_PATTERNS = [ | ||||
"src/rpc/abc.cpp", | "src/rpc/*.cpp", | ||||
"src/rpc/blockchain.cpp", | "src/wallet/rpc*.cpp", | ||||
"src/rpc/mining.cpp", | |||||
"src/rpc/misc.cpp", | |||||
"src/rpc/net.cpp", | |||||
"src/rpc/rawtransaction.cpp", | |||||
"src/rpc/server.cpp", | |||||
"src/wallet/rpcdump.cpp", | |||||
"src/wallet/rpcwallet.cpp", | |||||
] | ] | ||||
# Source file (relative to root) containing conversion mapping | # Source file (relative to root) containing conversion mapping | ||||
SOURCE_CLIENT = 'src/rpc/client.cpp' | SOURCE_CLIENT = 'src/rpc/client.cpp' | ||||
# Argument names that should be ignored in consistency checks | # Argument names that should be ignored in consistency checks | ||||
IGNORE_DUMMY_ARGS = {'dummy', 'arg0', 'arg1', 'arg2', | IGNORE_DUMMY_ARGS = {'dummy', 'arg0', 'arg1', 'arg2', | ||||
'arg3', 'arg4', 'arg5', 'arg6', 'arg7', 'arg8', 'arg9'} | 'arg3', 'arg4', 'arg5', 'arg6', 'arg7', 'arg8', 'arg9'} | ||||
Show All 36 Lines | with open(fname, "r", encoding="utf8") as f: | ||||
name = parse_string(m.group(2)) | name = parse_string(m.group(2)) | ||||
args_str = m.group(4).strip() | args_str = m.group(4).strip() | ||||
if args_str: | if args_str: | ||||
args = [RPCArgument(parse_string(x.strip()).split( | args = [RPCArgument(parse_string(x.strip()).split( | ||||
'|'), idx) for idx, x in enumerate(args_str.split(','))] | '|'), idx) for idx, x in enumerate(args_str.split(','))] | ||||
else: | else: | ||||
args = [] | args = [] | ||||
cmds.append(RPCCommand(name, args)) | cmds.append(RPCCommand(name, args)) | ||||
assert not in_rpcs and cmds, "Something went wrong with parsing the C++ file: update the regexps" | assert not in_rpcs, "Something went wrong with parsing the C++ file: update the regexps" | ||||
return cmds | return cmds | ||||
def process_mapping(fname): | def process_mapping(fname): | ||||
"""Find and parse conversion table in implementation file `fname`.""" | """Find and parse conversion table in implementation file `fname`.""" | ||||
cmds = [] | cmds = [] | ||||
in_rpcs = False | in_rpcs = False | ||||
with open(fname, "r", encoding="utf8") as f: | with open(fname, "r", encoding="utf8") as f: | ||||
Show All 19 Lines | |||||
def main(): | def main(): | ||||
if len(sys.argv) != 2: | if len(sys.argv) != 2: | ||||
print('Usage: {} ROOT-DIR'.format(sys.argv[0]), file=sys.stderr) | print('Usage: {} ROOT-DIR'.format(sys.argv[0]), file=sys.stderr) | ||||
sys.exit(1) | sys.exit(1) | ||||
root = sys.argv[1] | root = sys.argv[1] | ||||
# Find the sources files | |||||
sources = [] | |||||
for glob_regex in SOURCE_PATTERNS: | |||||
sources.extend(glob.glob(os.path.join(root, glob_regex))) | |||||
# Get all commands from dispatch tables | # Get all commands from dispatch tables | ||||
cmds = [] | cmds = [] | ||||
for fname in SOURCES: | for fname in set(sources): | ||||
cmds += process_commands(os.path.join(root, fname)) | cmds += process_commands(fname) | ||||
cmds_by_name = {} | cmds_by_name = {} | ||||
for cmd in cmds: | for cmd in cmds: | ||||
cmds_by_name[cmd.name] = cmd | cmds_by_name[cmd.name] = cmd | ||||
# Get current convert mapping for client | # Get current convert mapping for client | ||||
client = SOURCE_CLIENT | client = SOURCE_CLIENT | ||||
mapping = set(process_mapping(os.path.join(root, client))) | mapping = set(process_mapping(os.path.join(root, client))) | ||||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |