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