11import json
2+ import hashlib
23import logging
34from conf import *
45from tqdm import tqdm
@@ -12,6 +13,12 @@ def get_saved_hash():
1213 return {}
1314
1415
16+ def save_hash (saved_hash : dict ):
17+ saved_hash = dict (sorted (saved_hash .items (), key = lambda x : x [0 ].lower ()))
18+ with open (f'{ WORKDIR } /whl/data/sha256sums.json' , 'w' , encoding = 'utf-8' ) as json_file :
19+ json .dump (saved_hash , json_file , indent = 2 )
20+
21+
1522def get_whl_sha256 (name : str , saved_hash : dict ):
1623 if name in saved_hash :
1724 return saved_hash [name ]['sha256' ]
@@ -70,3 +77,36 @@ def get_assets_from_html() -> list:
7077 pkgs .append ({'name' : pkg_filename , 'url' : pkg_url })
7178
7279 return pkgs
80+
81+
82+ def calculate_hash (file : str , algorithm : str = 'sha256' ) -> str :
83+ with open (file , 'rb' ) as f :
84+ hash_obj = hashlib .new (algorithm )
85+ hash_obj .update (f .read ())
86+ return hash_obj .hexdigest ()
87+
88+
89+ def get_local_whl () -> list [tuple [str , str ]]:
90+ """
91+ Get all .whl files in LOCAL_WHL_DIR
92+ :return: list of tuples (filename, path)
93+ """
94+ whl_files = []
95+ for root , dirs , files in os .walk (LOCAL_WHL_DIR ):
96+ for file in files :
97+ if file .endswith ('.whl' ):
98+ whl_files .append ((file , os .path .join (root , file )))
99+ return whl_files
100+
101+
102+ def extend_hash_dict (saved_hash : dict , whl_files : list [tuple [str , str ]]) -> dict :
103+ saved_wheels = saved_hash .keys ()
104+ assert not any (name in saved_wheels for name , _ in whl_files )
105+
106+ print ('Calculating hash for local wheels...' )
107+ for name , path in tqdm (whl_files ):
108+ saved_hash [name ] = {
109+ 'sha256' : calculate_hash (path ),
110+ 'verify' : False
111+ }
112+ return saved_hash
0 commit comments