Skip to content

Commit ceaa07f

Browse files
authored
Update process for Discarding Javascript (#2178)
* Update prepare_live.py * Update prepare_live.py * 24.2.1
1 parent 34cd4dc commit ceaa07f

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ boto3==1.28.43
2222
GitPython==3.1.41
2323
cryptography==43.0.1
2424
apscheduler==3.10.4
25-
pyopenssl==24.0.0
25+
pyopenssl==24.2.1
2626
pytest-xdist==3.5.0
2727
parameterized==0.9.0

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ GitPython==3.1.41
99
cryptography==43.0.1
1010
apscheduler==3.10.4
1111
vidua==0.4.5
12-
pyopenssl==24.0.0
12+
pyopenssl==24.2.1

tools/prepare_live.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import glob
44
import os
5+
import re
56
import shutil
67
import subprocess
78
from hashlib import md5
@@ -11,6 +12,13 @@
1112
import requests
1213
from bs4 import BeautifulSoup
1314

15+
# Load version from version.py
16+
version = None
17+
with open("version.py", "r") as version_file:
18+
for line in version_file:
19+
if line.startswith("version"):
20+
version = line.split("=")[1].strip().strip('"')
21+
1422

1523
def get_files(path, extension, recursive=False):
1624
"""Generate filepaths for each file into path with the target extension."""
@@ -25,25 +33,28 @@ def get_files(path, extension, recursive=False):
2533

2634
def find_list_resources(tag, attribute, soup):
2735
"""Find resources based off of their tag and attributes."""
28-
list = []
36+
resources = []
2937
for x in soup.findAll(tag):
3038
try:
31-
list.append(x[attribute])
39+
resources.append(x[attribute])
3240
except KeyError:
3341
pass
34-
return list
42+
return resources
3543

3644

3745
for f in [*get_files(os.getcwd(), "html.jinja2", recursive=True), *get_files(os.getcwd(), "html", recursive=True)]:
3846
with open(f, "r") as reader:
39-
html: str = reader.read()
47+
html = reader.read()
4048
soup = BeautifulSoup(html, features="html.parser")
4149

4250
image_src = find_list_resources("img", "src", soup)
4351
script_src = find_list_resources("script", "src", soup)
4452
css_link = find_list_resources("link", "href", soup)
53+
54+
# Update HTML file links
4555
for link in [*css_link, *script_src, *image_src]:
4656
if "http://" in link or "https://" in link:
57+
# External link
4758
pre = ""
4859
if "wiki/" in f:
4960
pre = "."
@@ -52,19 +63,20 @@ def find_list_resources(tag, attribute, soup):
5263
req = requests.get(link, allow_redirects=False)
5364
open(f".{file_name}", "wb").write(req.content)
5465
html = html.replace(link, f".{pre}{file_name}")
55-
with open(f, "w") as writer:
56-
writer.write(html)
66+
elif link.endswith(".js"):
67+
# Local JavaScript file - append version query parameter
68+
html = html.replace(link, f"{link}?v={version}")
69+
70+
# Update JavaScript blocks
71+
pattern = r'(\{ src: [\'"])(\.\/static\/js\/.*?\.js)([\'"], defer: true \})'
72+
updated_html = re.sub(pattern, rf"\1\2?v={version}\3", html)
73+
74+
# Write the modified HTML content back to the file
75+
with open(f, "w") as writer:
76+
writer.write(updated_html)
5777

58-
# subprocess.run(["css-html-js-minify", "static/styles/", "--overwrite"])
59-
# subprocess.run(["pyminify", "-i", "."])
6078
subprocess.run(["python3", "setup.py", "bdist_wheel"])
6179
shutil.copyfile("dist/dk64rando-1.0.0-py3-none-any.whl", "static/py_libraries/dk64rando-1.0.0-py3-none-any.whl")
62-
with open("./static/py_libraries/dk64rando-1.0.0-py3-none-any.whl", "rb") as file:
63-
wheel = file.read()
64-
hash = md5(wheel).hexdigest()
65-
with open("version.py", "a") as version:
66-
version.write(f'\nwhl_hash = "{hash}"')
67-
6880

6981
# Create the file Gemfile
7082
with open("Gemfile", "w") as file:

0 commit comments

Comments
 (0)