Skip to content

Commit

Permalink
refactor(api, shared-data): update command-schema make target to not …
Browse files Browse the repository at this point in the history
…require version (#17555)

make command-schema no longer takes a version number argument but instead automatically updates the most recent command schema in shared-data
  • Loading branch information
jbleon95 authored Feb 20, 2025
1 parent 15d4965 commit 98e385a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
4 changes: 2 additions & 2 deletions api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ term:
plot-session:
$(python) util/plot_session.py $(plot_type) $(plot_type).pdf

PHONY: command-schema
.PHONY: command-schema
command-schema:
$(python) src/opentrons/protocol_engine/commands/generate_command_schema.py $(COMMAND_SCHEMA_VERSION) > ../shared-data/command/schemas/$(COMMAND_SCHEMA_VERSION).json
$(python) src/opentrons/protocol_engine/commands/generate_command_schema.py --overwrite-shared-data
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import sys
from opentrons.protocol_engine.commands.command_unions import CommandCreateAdapter

from opentrons_shared_data.command import get_newest_schema_version
from opentrons_shared_data.load import get_shared_data_root


def generate_command_schema(version: str) -> str:
"""Generate a JSON Schema that all valid create commands can validate against."""
Expand All @@ -14,6 +17,13 @@ def generate_command_schema(version: str) -> str:
return json.dumps(schema_as_dict, indent=2, sort_keys=True)


def write_command_schema(json_string: str, version: str) -> None:
"""Write a JSON command schema to the shared-data command schema directory."""
path = get_shared_data_root() / "command" / "schemas" / f"{version}.json"
with open(path, "w") as schema_file:
schema_file.write(json_string)


if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="generate_command_schema",
Expand All @@ -22,10 +32,29 @@ def generate_command_schema(version: str) -> str:
parser.add_argument(
"version",
type=str,
help="The command schema version. This is a single integer (e.g. 7) that will be used to name the generated schema file",
nargs="?",
help="The command schema version. This is a single integer (e.g. 7) that will be used to name the generated"
" schema file. If not included, it will automatically use the latest version in shared-data.",
)
parser.add_argument(
"--overwrite-shared-data",
action="store_true",
help="If used, overwrites the specified or automatically chosen command schema version in shared-data."
" If not included, the generated schema will be printed to stdout.",
)
args = parser.parse_args()
print(generate_command_schema(args.version))

if args.version is None:
version_string = get_newest_schema_version()
else:
version_string = args.version

command_schema = generate_command_schema(version_string)

if args.overwrite_shared_data:
write_command_schema(command_schema, version_string)
else:
print(command_schema)

sys.exit()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Test that the command schema is in sync with it's source models."""
"""Test that the command schema is in sync with its source models."""
from opentrons_shared_data.command import load_schema_string, get_newest_schema_version
from opentrons.protocol_engine.commands import generate_command_schema

Expand All @@ -10,7 +10,7 @@
Or, if this change is intentional, update the shared JSON schema by running this from the monorepo root:
make -C api command-schema COMMAND_SCHEMA_VERSION=<version number>
make -C api command-schema
make format
...and include the updated JSON schema file in your pull request.
Expand Down

0 comments on commit 98e385a

Please sign in to comment.