@@ -66,19 +66,16 @@ def get_mesa_templates(package, template_dir):
6666
6767
6868def get_frontend_dep ():
69- # Important: Make sure to update package_includes with the new version number in
70- # mesa_geo/visualization/modules/MapVisualization.py,
71- # and the hardcoded css file in mesa_geo/visualization/templates/modular_template.html.
69+ # Important: Make sure to update the integrity_hash together with the new version number,
70+ # otherwise the previous file is going to be kept and used.
7271 leaflet_version = "1.8.0"
7372 ensure_frontend_dep_single (
7473 f"https://unpkg.com/leaflet@{ leaflet_version } /dist/leaflet.js" ,
75- out_name = f"leaflet-{ leaflet_version } .js" ,
7674 external_dir_single = "mesa_geo/visualization/templates/js/external" ,
7775 integrity_hash = "sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ==" ,
7876 )
7977 ensure_frontend_dep_single (
8078 f"https://unpkg.com/leaflet@{ leaflet_version } /dist/leaflet.css" ,
81- out_name = f"leaflet-{ leaflet_version } .css" ,
8279 external_dir_single = "mesa_geo/visualization/templates/css/external" ,
8380 integrity_hash = "sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ==" ,
8481 )
@@ -87,24 +84,29 @@ def get_frontend_dep():
8784def ensure_frontend_dep_single (
8885 url , external_dir_single , out_name = None , integrity_hash = None
8986):
87+ def _hash (filepath ):
88+ with open (filepath , "rb" ) as f :
89+ file_as_bytes = f .read ()
90+ file_hash = base64 .b64encode (hashlib .sha512 (file_as_bytes ).digest ())
91+ return "sha512-" + file_hash .decode ()
92+
9093 os .makedirs (external_dir_single , exist_ok = True )
9194 # Used for downloading e.g. Leaflet single file
9295 if out_name is None :
9396 out_name = url .split ("/" )[- 1 ]
9497 dst_path = os .path .join (external_dir_single , out_name )
9598 if os .path .isfile (dst_path ):
96- return
99+ if integrity_hash and (_hash (dst_path ) == integrity_hash ):
100+ return
101+ else :
102+ return
103+ print (f"Downloading the { out_name } dependency from the internet..." )
97104 urllib .request .urlretrieve (url , out_name )
98- if integrity_hash :
99- with open (out_name , "rb" ) as f :
100- bytes = f .read ()
101- actual_hash = base64 .b64encode (hashlib .sha512 (bytes ).digest ())
102- actual_hash = "sha512-" + actual_hash .decode ()
103- if actual_hash != integrity_hash :
104- os .remove (out_name )
105- raise ValueError (
106- f"Integrity check failed for { out_name } . Expected { integrity_hash } , received { actual_hash } ."
107- )
105+ if integrity_hash and ((actual_hash := _hash (out_name )) != integrity_hash ):
106+ os .remove (out_name )
107+ raise ValueError (
108+ f"Integrity check failed for { out_name } . Expected { integrity_hash } , received { actual_hash } ."
109+ )
108110 shutil .move (out_name , dst_path )
109111
110112
0 commit comments