Skip to content

Commit

Permalink
remove version number from leaflet filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-boyu authored and rht committed Apr 24, 2022
1 parent 048d0dd commit 7345d87
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion mesa_geo/visualization/modules/MapVisualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class MapModule(VisualizationElement):
"""A MapModule for Leaflet maps."""

package_includes = ["external/leaflet-1.8.0.js", "MapModule.js"]
package_includes = ["external/leaflet.js", "MapModule.js"]
local_includes = []

def __init__(
Expand Down
2 changes: 1 addition & 1 deletion mesa_geo/visualization/templates/modular_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link href="/static/css/bootstrap-switch.min.css" type="text/css" rel="stylesheet" />
<link href="/static/css/bootstrap-slider.min.css" type="text/css" rel="stylesheet" />
<link href="/static/css/visualization.css" type="text/css" rel="stylesheet" />
<link href="/static/css/external/leaflet-1.8.0.css" type="text/css" rel="stylesheet" />
<link href="/static/css/external/leaflet.css" type="text/css" rel="stylesheet" />

<!-- This is the Tornado template for the Modular Visualization. The Javascript code opens a WebSocket connection to
the server (the port is set via the template). On every step, it receives inputs, one per module, and sends
Expand Down
34 changes: 18 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,16 @@ def get_mesa_templates(package, template_dir):


def get_frontend_dep():
# Important: Make sure to update package_includes with the new version number in
# mesa_geo/visualization/modules/MapVisualization.py,
# and the hardcoded css file in mesa_geo/visualization/templates/modular_template.html.
# Important: Make sure to update the integrity_hash together with the new version number,
# otherwise the previous file is going to be kept and used.
leaflet_version = "1.8.0"
ensure_frontend_dep_single(
f"https://unpkg.com/leaflet@{leaflet_version}/dist/leaflet.js",
out_name=f"leaflet-{leaflet_version}.js",
external_dir_single="mesa_geo/visualization/templates/js/external",
integrity_hash="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ==",
)
ensure_frontend_dep_single(
f"https://unpkg.com/leaflet@{leaflet_version}/dist/leaflet.css",
out_name=f"leaflet-{leaflet_version}.css",
external_dir_single="mesa_geo/visualization/templates/css/external",
integrity_hash="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ==",
)
Expand All @@ -87,24 +84,29 @@ def get_frontend_dep():
def ensure_frontend_dep_single(
url, external_dir_single, out_name=None, integrity_hash=None
):
def _hash(filepath):
with open(filepath, "rb") as f:
file_as_bytes = f.read()
file_hash = base64.b64encode(hashlib.sha512(file_as_bytes).digest())
return "sha512-" + file_hash.decode()

os.makedirs(external_dir_single, exist_ok=True)
# Used for downloading e.g. Leaflet single file
if out_name is None:
out_name = url.split("/")[-1]
dst_path = os.path.join(external_dir_single, out_name)
if os.path.isfile(dst_path):
return
if integrity_hash and (_hash(dst_path) == integrity_hash):
return
else:
return
print(f"Downloading the {out_name} dependency from the internet...")
urllib.request.urlretrieve(url, out_name)
if integrity_hash:
with open(out_name, "rb") as f:
bytes = f.read()
actual_hash = base64.b64encode(hashlib.sha512(bytes).digest())
actual_hash = "sha512-" + actual_hash.decode()
if actual_hash != integrity_hash:
os.remove(out_name)
raise ValueError(
f"Integrity check failed for {out_name}. Expected {integrity_hash}, received {actual_hash}."
)
if integrity_hash and ((actual_hash := _hash(out_name)) != integrity_hash):
os.remove(out_name)
raise ValueError(
f"Integrity check failed for {out_name}. Expected {integrity_hash}, received {actual_hash}."
)
shutil.move(out_name, dst_path)


Expand Down

0 comments on commit 7345d87

Please sign in to comment.