update_snapshots.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. import argparse
  4. import hashlib
  5. import json
  6. import shutil
  7. from pathlib import Path
  8. parser = argparse.ArgumentParser(description="Update Galata Snapshot images.")
  9. parser.add_argument("report", help="Path to the galata-report directory")
  10. args = parser.parse_args()
  11. # Calculate hashes of all png files in the test/directory
  12. def sha1(path):
  13. with open(path, "rb") as f:
  14. return hashlib.sha1(f.read()).hexdigest()
  15. filehashes = {sha1(p): p for p in Path(".").glob("**/*-snapshots/*-linux.png")}
  16. # For every json file in data directory except report.json
  17. data_dir = Path(args.report).expanduser().resolve() / "data"
  18. for p in data_dir.glob("*.json"):
  19. if p.name == "report.json":
  20. continue
  21. with open(p, "rb") as f:
  22. z = json.load(f)
  23. for t in z["tests"]:
  24. if t["outcome"] != "unexpected":
  25. continue
  26. for r in t["results"]:
  27. for attachment in r["attachments"]:
  28. if attachment["name"] == "expected":
  29. expected = Path(attachment["path"]).stem
  30. elif attachment["name"] == "actual":
  31. actual = data_dir / Path(attachment["path"]).name
  32. if expected and attachment and expected in filehashes:
  33. shutil.copyfile(actual, filehashes[expected])
  34. print(f"{actual} -> {filehashes[expected]}")