forked from anna-is-cute/paste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources.py
executable file
·95 lines (80 loc) · 2.5 KB
/
resources.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
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
94
95
#!/usr/bin/env python3
from base64 import b64encode
from hashlib import sha256, sha384
from itertools import chain
from pathlib import Path
from shutil import which
from subprocess import run
def integrity_all(paths):
return { path: integrity(path) for path in paths }
def integrity(path):
with open(path, 'rb') as f:
return b64encode(sha384(f.read()).digest()).decode('utf-8')
def hash_str(s):
return sha256(s.encode('utf-8')).digest()
sass = which('sass')
babel = which('babel')
if sass is None:
print('warn: could not find sass in path. css will not be compiled')
if babel is None:
print('warn: could not find babel in path. js will not be compiled')
# web resources path
web_path = Path('webserver/web')
# all static js and css
files = list(chain(
web_path.glob('static/**/*.js'),
web_path.glob('static/**/*.css'),
))
src = {
'css': [
web_path / 'src/css/style.scss',
web_path / 'src/css/dark-style.scss',
],
'js': [
web_path / 'src/js/',
],
}
# template files
templates = list(web_path.glob('templates/**/*.html.tera'))
# calculate old integrity hashes to potentially replace
old_integrity = integrity_all(files)
# compile css resources
if sass is not None:
for css in src['css']:
print(f'compiling {css}')
static_css = str(css).replace('scss', 'css').replace('/src/', '/static/')
run([sass, '-s', 'compressed', f'{css}:{static_css}'])
# compile js resources (assuming directories)
if babel is not None:
for js in src['js']:
print(f'compiling {js}')
static_js = str(js).replace('/src/', '/static/')
run([babel, '-s', 'true', '-d', static_js, str(js)])
# calculate new integrity hashes
new_integrity = integrity_all(files)
# determine which files changed
changed = {}
for name, ihash in new_integrity.items():
if name not in old_integrity or ihash == old_integrity[name]:
continue
changed[name] = (old_integrity[name], ihash)
# go through every template and replace change hashes if present
for template in templates:
# read in template
with open(template, 'r') as f:
content = f.read()
# calculate sha256 hash before replacing
before = hash_str(content)
# replace any changed hashes
for name, paths in changed.items():
(old, new) = paths
content = content.replace(old, new)
# calculate sha256 hash after replacing
after = hash_str(content)
# if no change, ignore
if before == after:
continue
# update the template with the replaced hashes
print(f'updating {template}')
with open(template, 'w') as f:
f.write(content)