forked from hcientist/OnlinePythonTutor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_cache_busting_query_strings.py
More file actions
30 lines (24 loc) · 925 Bytes
/
add_cache_busting_query_strings.py
File metadata and controls
30 lines (24 loc) · 925 Bytes
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
# add cache-busting query strings for build/*.js files referenced by all html
# files in the current directory
import os, re, hashlib
PREFIX = 'src="'
SUFFIX = '"'
STUFF_RE = re.compile(PREFIX + '(build/.*[.]js).*?' + SUFFIX)
print 'Added cache-busting hashes to these HTML files:'
for f in os.listdir('.'):
if f.endswith('.html'):
new_f = f + '.NEW'
with open(new_f, 'w') as out:
for line in open(f):
m = STUFF_RE.search(line)
if m:
js_filename = m.group(1)
assert os.path.isfile(js_filename)
md5_hash = hashlib.md5(open(js_filename, 'rb').read()).hexdigest()
md5_hash = md5_hash[:10] # truncate to prettify
modified_line = re.sub(STUFF_RE, 'src="' + js_filename + '?' + md5_hash + SUFFIX, line)
print f + ':' + modified_line,
out.write(modified_line)
else:
out.write(line)
os.rename(new_f, f)