2021-02-05 21:03:04 -08:00
|
|
|
import json
|
2021-06-11 09:44:18 -07:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Union
|
2021-02-05 21:03:04 -08:00
|
|
|
|
|
|
|
import click
|
2021-02-11 23:14:20 -08:00
|
|
|
from avrogen import write_schema_files
|
2021-02-05 21:03:04 -08:00
|
|
|
|
2021-06-11 09:44:18 -07:00
|
|
|
autogen_header = """# flake8: noqa
|
2021-02-05 21:03:04 -08:00
|
|
|
|
2021-06-11 09:44:18 -07:00
|
|
|
# This file is autogenerated by /metadata-ingestion/scripts/avro_codegen.py
|
|
|
|
# Do not modify manually!
|
|
|
|
|
|
|
|
# fmt: off
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def suppress_checks_in_file(filepath: Union[str, Path]) -> None:
|
|
|
|
"""
|
|
|
|
Adds a couple lines to the top of an autogenerated file:
|
|
|
|
- Comments to suppress flake8 and black.
|
|
|
|
- A note stating that the file was autogenerated.
|
|
|
|
"""
|
2021-03-18 02:05:05 -04:00
|
|
|
|
|
|
|
with open(filepath, "r+") as f:
|
|
|
|
contents = f.read()
|
|
|
|
|
|
|
|
f.seek(0, 0)
|
2021-06-11 09:44:18 -07:00
|
|
|
f.write(autogen_header)
|
2021-03-18 02:05:05 -04:00
|
|
|
f.write(contents)
|
|
|
|
f.write("# fmt: on\n")
|
|
|
|
|
|
|
|
|
2021-02-05 21:03:04 -08:00
|
|
|
@click.command()
|
|
|
|
@click.argument("schema_file", type=click.Path(exists=True))
|
|
|
|
@click.argument("outdir", type=click.Path())
|
2021-04-14 13:40:24 -07:00
|
|
|
def generate(schema_file: str, outdir: str) -> None:
|
2021-02-05 21:03:04 -08:00
|
|
|
with open(schema_file) as f:
|
|
|
|
raw_schema_text = f.read()
|
|
|
|
|
|
|
|
no_spaces_schema = json.dumps(json.loads(raw_schema_text))
|
2021-02-12 10:46:28 -08:00
|
|
|
schema_json = no_spaces_schema.replace(
|
|
|
|
'{"type": "string", "avro.java.string": "String"}', '"string"'
|
|
|
|
)
|
2021-02-05 21:03:04 -08:00
|
|
|
|
|
|
|
redo_spaces = json.dumps(json.loads(schema_json), indent=2)
|
|
|
|
|
|
|
|
write_schema_files(redo_spaces, outdir)
|
2021-04-30 21:10:12 -07:00
|
|
|
with open(f"{outdir}/__init__.py", "w"):
|
|
|
|
# Truncate this file.
|
|
|
|
pass
|
2021-02-05 21:03:04 -08:00
|
|
|
|
2021-06-11 09:44:18 -07:00
|
|
|
# Add headers for all generated files
|
|
|
|
generated_files = Path(outdir).glob("**/*.py")
|
|
|
|
for file in generated_files:
|
|
|
|
suppress_checks_in_file(file)
|
|
|
|
|
2021-02-05 21:03:04 -08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
generate()
|