def write_json_schemas()

in schemas/generate_json_schema.py [0:0]


def write_json_schemas(json_schemas_path: Path, python_package_dir: Path):
    json_schemas_path.mkdir(exist_ok=True)

    models = {}

    for package in JSON_SCHEMA_PACKAGES:
        models.update(
            {
                model_name: getattr(package, model_name)
                for model_name in package.__all__
                if issubclass(getattr(package, model_name), BaseModel)
            }
        )

    written_paths = set()

    for model_name, model in models.items():
        model_schema_path = json_schemas_path / f"{model_name}.schema.json"
        written_paths.add(model_schema_path)

        json_schema = prettify_json_schema(model.model_json_schema())
        with model_schema_path.open("w") as f:
            json.dump(json_schema, f, indent=2)
            f.write("\n")

    # Ensure we don't include any files in schemas/ that we did not generate (e.g., if a
    # model gets removed).
    for path in list(json_schemas_path.iterdir()):
        if path not in written_paths:
            path.unlink()

    # Copy schemas into the python package.
    schemas_dist_dir = python_package_dir / "schemas"
    if schemas_dist_dir.exists():
        shutil.rmtree(schemas_dist_dir)

    shutil.copytree(json_schemas_path, schemas_dist_dir)