generate-model-dependency-fixtures 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. from __future__ import annotations
  3. from sentry.runner import configure
  4. configure()
  5. import click
  6. from sentry.backup.dependencies import DependenciesJSONEncoder, dependencies, sorted_dependencies
  7. from sentry.testutils.factories import get_fixture_path # noqa
  8. encoder = DependenciesJSONEncoder(
  9. sort_keys=True,
  10. ensure_ascii=True,
  11. check_circular=True,
  12. allow_nan=True,
  13. indent=2,
  14. encoding="utf-8",
  15. )
  16. @click.command()
  17. def main():
  18. """Used for generating fixtures for the model dependency map."""
  19. click.echo("\nThis script can take up to 30 seconds, please be patient...\n")
  20. # Do all of the calculation before any file writes, so that we don't end up with partial
  21. # overwrites.
  22. detailed = {str(k): v for k, v in dependencies().items()}
  23. flat = {k: v.flatten() for k, v in detailed.items()}
  24. sorted = sorted_dependencies()
  25. det_path = get_fixture_path("backup", "model_dependencies", "detailed.json")
  26. with open(det_path, "w+") as fixture:
  27. fixture.write(encoder.encode(detailed))
  28. flat_path = get_fixture_path("backup", "model_dependencies", "flat.json")
  29. with open(flat_path, "w+") as fixture:
  30. fixture.write(encoder.encode(flat))
  31. det_path = get_fixture_path("backup", "model_dependencies", "sorted.json")
  32. with open(det_path, "w+") as fixture:
  33. fixture.write(encoder.encode(sorted))
  34. click.echo(
  35. f"\nSuccess! The dependency mapping fixtures at {[det_path, flat_path]} were updated.\n"
  36. )
  37. if __name__ == "__main__":
  38. main()