|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +ROOT_DIR = Path(__file__).parent |
| 10 | + |
| 11 | +if os.environ.get('GITHUB_ACTIONS', '') == '': |
| 12 | + print('Not running when not in GitHub Actions.') |
| 13 | + sys.exit() |
| 14 | +summary_file = os.environ.get('GITHUB_STEP_SUMMARY') |
| 15 | +if summary_file is None: |
| 16 | + sys.exit('$GITHUB_STEP_SUMMARY is not set') |
| 17 | + |
| 18 | +gh_pages = ROOT_DIR.parent / 'pages' |
| 19 | +subprocess.run(['git', 'fetch', 'https://github.com/matplotlib/cheatsheets.git', |
| 20 | + 'gh-pages:upstream-gh-pages'], check=True) |
| 21 | +subprocess.run(['git', 'worktree', 'add', gh_pages, 'upstream-gh-pages'], |
| 22 | + check=True) |
| 23 | + |
| 24 | +diff_dir = ROOT_DIR / 'diffs' |
| 25 | +diff_dir.mkdir(exist_ok=True) |
| 26 | + |
| 27 | +hashes = {} |
| 28 | +for original in gh_pages.glob('*.png'): |
| 29 | + result = subprocess.run( |
| 30 | + ['compare', '-metric', 'PHASH', |
| 31 | + original, |
| 32 | + ROOT_DIR / 'docs/_build/html' / original.name, |
| 33 | + diff_dir / f'{original.stem}-diff.png'], |
| 34 | + text=True, stderr=subprocess.PIPE) |
| 35 | + if result.returncode == 2: # Some kind of IO or similar error. |
| 36 | + hashes[original] = (float('nan'), result.stderr) |
| 37 | + elif result.stderr: # Images were different. |
| 38 | + hashes[original] = (float(result.stderr), '') |
| 39 | + else: # No differences. |
| 40 | + hashes[original] = (0.0, '') |
| 41 | + |
| 42 | +with open(summary_file, 'w+') as summary: |
| 43 | + print('# Cheatsheet image comparison', file=summary) |
| 44 | + print('| Filename | Perceptual Hash Difference | Error message |', file=summary) |
| 45 | + print('| -------- | -------------------------- | ------------- |', file=summary) |
| 46 | + for filename, (hash, message) in hashes.items(): |
| 47 | + message = message.replace('\n', ' ').replace('|', '\\|') |
| 48 | + print(f'| {filename.name} | {hash:.05f} | {message}', file=summary) |
| 49 | + print(file=summary) |
| 50 | + |
| 51 | +subprocess.run(['git', 'worktree', 'remove', gh_pages]) |
0 commit comments