Command table: Sorted subcommands (#9951)

Sort the sub-commands so that every time we execute the script it generates the exact same results.
This will case less merge conflicts if two PRs edit different json files.

also:
* make the script agnostic to where it is executed (more flexible).
* add documentation about commands.c and the json files in the readme.

Co-authored-by: Oran Agra <oran@redislabs.com>
This commit is contained in:
guybe7
2021-12-16 12:54:40 +02:00
committed by GitHub
co-authored by Oran Agra
parent 70ff26b454
commit ffbe36fc3e
3 changed files with 98 additions and 90 deletions
+8 -6
View File
@@ -4,8 +4,6 @@ import os
import glob
import json
# Note: This script should be run from the src/ dir: ../utils/generate-command-code.py
ARG_TYPES = {
"string": "ARG_TYPE_STRING",
"integer": "ARG_TYPE_INTEGER",
@@ -305,12 +303,13 @@ class Command(object):
def write_internal_structs(self, f):
if self.subcommands:
for subcommand in sorted(self.subcommands, key=lambda cmd: cmd.name):
subcommand_list = sorted(self.subcommands, key=lambda cmd: cmd.name)
for subcommand in subcommand_list:
subcommand.write_internal_structs(f)
f.write("/* %s command table */\n" % self.fullname())
f.write("struct redisCommand %s[] = {\n" % self.subcommand_table_name())
for subcommand in self.subcommands:
for subcommand in subcommand_list:
f.write("{%s},\n" % subcommand.struct_code())
f.write("{0}\n")
f.write("};\n\n")
@@ -367,9 +366,12 @@ def create_command(name, desc):
# MAIN
# Figure out where the sources are
srcdir = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + "/../src")
# Create all command objects
print("Processing json files...")
for filename in glob.glob('commands/*.json'):
for filename in glob.glob('%s/commands/*.json' % srcdir):
with open(filename,"r") as f:
d = json.load(f)
for name, desc in d.items():
@@ -387,7 +389,7 @@ for command in commands.values():
command.subcommands.append(subcommand)
print("Generating commands.c...")
with open("commands.c","w") as f:
with open("%s/commands.c" % srcdir,"w") as f:
f.write("/* Automatically generated by %s, do not edit. */\n\n" % os.path.basename(__file__))
f.write("#include \"server.h\"\n")
f.write(