Repository navigation
Expand file tree
/
Copy pathverify-migration.py
More file actions
93 lines (82 loc) · 4.71 KB
/
Copy pathverify-migration.py
File metadata and controls
93 lines (82 loc) · 4.71 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Verify copied originals, published body, URLs, assets and backup integrity."""
import hashlib
import json
import re
from pathlib import Path
from urllib.parse import urljoin, urlsplit, unquote
from html import unescape
from lxml import html
ROOT = Path(__file__).resolve().parents[1]
OLD = ROOT.parent / 'SpriCoder-Blog'
baseline = json.loads((ROOT / 'migration/baseline.json').read_text())
draft_file = ROOT / 'migration/drafts-local.json'
if draft_file.exists():
baseline += json.loads(draft_file.read_text())
posts = json.loads((ROOT / 'src/data/posts.json').read_text())
by_id = {post['id']: post for post in posts}
path_moves = json.loads((ROOT / 'migration/path-moves.json').read_text())['moves']
physical_by_logical = {logical: physical for physical, logical in path_moves.items()}
reference_repairs = json.loads((ROOT / 'content/reference-repairs.json').read_text())
failures = []
def sha(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def check(condition, message):
if not condition:
failures.append(message)
def inner(node):
return (node.text or '') + ''.join(html.tostring(child, encoding='unicode', with_tail=True) for child in node)
for item in baseline:
physical_id = physical_by_logical.get(item['id'], item['id'])
copied = ROOT / 'content' / ('posts' if item['published'] else 'drafts') / physical_id
if item['published'] and item.get('markdown_body_sha256'):
match = re.match(r'^\ufeff?---\r?\n[\s\S]*?\r?\n---(?:\r?\n|$)([\s\S]*)$', copied.read_text())
digest = hashlib.sha256(match.group(1).encode()).hexdigest() if match else None
check(digest == item['markdown_body_sha256'], f"Original article body changed: {item['id']}")
else:
check(copied.exists() and sha(copied) == item['markdown_sha256'], f"Original Markdown changed: {item['id']}")
if not item['published']:
check(item['id'] not in by_id, f"Draft published: {item['id']}")
continue
post = by_id.get(item['id'])
check(post is not None, f"Article missing: {item['id']}")
output = ROOT / 'dist' / post['url'].lstrip('/') / 'index.html'
check(output.is_file(), f"URL missing: {post['url']}")
if output.is_file():
document = html.fromstring(output.read_bytes())
body = document.xpath('//*[@data-pagefind-body]')
snapshot = html.fragment_fromstring((ROOT / item['snapshot']).read_text(), create_parent='div')
# Preserve the publication snapshot except for individually documented link repairs.
for repair in reference_repairs['repairs']:
if repair['id'] == item['id']:
for node in snapshot.iter(repair['tag']):
if node.get(repair['attribute']) == unescape(repair['url']):
node.set(repair['attribute'], repair['replacement'])
check(len(body) == 1 and inner(body[0]) == inner(snapshot), f"Rendered body changed: {post['url']}")
check(document.xpath('string(//h1)') == post['title'], f"Article title mismatch: {post['url']}")
for url in item['legacyUrls']:
check((ROOT / 'dist' / url.lstrip('/') / 'index.html').is_file(), f"Legacy compatibility URL missing: {url}")
assets = json.loads((ROOT / 'migration/assets.json').read_text())
for asset in assets:
output = ROOT / 'dist' / asset['relative']
check(output.exists() and sha(output) == asset['sha256'], f"Asset changed: {asset['relative']}")
routes = json.loads((ROOT / 'migration/legacy-routes.json').read_text())
for route in routes:
check((ROOT / 'dist' / route['url'].lstrip('/') / 'index.html').exists(), f"Listing route missing: {route['url']}")
backup = json.loads((ROOT / 'migration/hexo-backup-manifest.json').read_text())
for file, digest in backup.items():
original = OLD / file
check(original.is_file() and sha(original) == digest, f"Hexo backup changed: {file}")
check((ROOT / 'public/js/duration.js').read_bytes() == (OLD / '.deploy_git/js/duration.js').read_bytes(), 'Founding time script changed')
check((ROOT / 'dist/.nojekyll').is_file(), '.nojekyll missing')
check((ROOT / 'dist/pagefind/pagefind.js').is_file(), 'Search index missing')
inherited = json.loads((ROOT / 'migration/inherited-broken-references.json').read_text())
summary = {
'publishedArticles': len(posts), 'preservedMarkdown': len(baseline),
'draftsExcluded': sum(not post['published'] for post in baseline),
'verifiedLegacyListingRoutes': len(routes), 'verifiedLocalImageAssets': len(assets),
'unchangedHexoBackupFiles': len(backup), 'inheritedBrokenReferences': len(inherited),
'failures': failures,
}
(ROOT / 'migration/verification.json').write_text(json.dumps(summary, ensure_ascii=False, indent=2) + '\n')
print(json.dumps(summary, ensure_ascii=False))
raise SystemExit(1 if failures else 0)