23
23
import hashlib
24
24
import json
25
25
from pathlib import Path
26
+ from io import BytesIO
26
27
from typing import Dict , List , Tuple
27
28
from dotenv import load_dotenv
28
29
import os
@@ -65,7 +66,8 @@ def build_tree(directory: Path, parent_path) -> Tuple[Dict, List[str]]:
65
66
directory_contents = {}
66
67
all_hashes = []
67
68
68
- for item in directory .iterdir ():
69
+ sorted_items = sorted (directory .iterdir (), key = lambda x : x .name )
70
+ for item in sorted_items :
69
71
relative_path = f"{ parent_path } /{ item .name } " if parent_path else item .name
70
72
71
73
if item .is_dir ():
@@ -100,24 +102,9 @@ def build_tree(directory: Path, parent_path) -> Tuple[Dict, List[str]]:
100
102
json .dump (fixtures_tree , file , indent = 4 )
101
103
102
104
103
- def download_github_artifact ( download_url : str , output_path : str , headers : dict ):
105
+ def write_artifact_fixtures_tree_json ( commit : str = "" , develop : bool = False ):
104
106
"""
105
- Helper function to download github zip artifacts and extract them to the output path.
106
- """
107
- temp_zip_path = Path (output_path ) / "temp_artifact.zip"
108
- with requests .get (download_url , headers = headers , stream = True ) as download_response :
109
- download_response .raise_for_status ()
110
- with open (temp_zip_path , "wb" ) as f :
111
- for chunk in download_response .iter_content (chunk_size = 8192 ):
112
- f .write (chunk )
113
- with zipfile .ZipFile (temp_zip_path , "r" ) as zip_ref :
114
- zip_ref .extractall (output_path )
115
- temp_zip_path .unlink ()
116
-
117
-
118
- def get_artifact_fixtures_tree (output_path : str = "." , commit : str = "" , develop : bool = False ):
119
- """
120
- Retrieves a fixtures tree artifact from EEST. By default, it will download the latest
107
+ Retrieves a fixtures tree artifact json data from EEST. By default, it will download the latest
121
108
main branch base artifact. If the develop flag is set, it will download the latest development
122
109
artifact. The commit flag can be used to download a specific artifact on the main branch based.
123
110
"""
@@ -126,7 +113,7 @@ def get_artifact_fixtures_tree(output_path: str = ".", commit: str = "", develop
126
113
if not github_token :
127
114
raise ValueError ("GitHub PAT not found. Ensure GITHUB_PAT is set within your .env file." )
128
115
129
- api_url = "https://api.github.com/repos/ethereum /execution-spec-tests/actions/artifacts"
116
+ api_url = "https://api.github.com/repos/spencer-tb /execution-spec-tests/actions/artifacts"
130
117
headers = {"Authorization" : f"token { github_token } " }
131
118
response = requests .get (api_url , headers = headers )
132
119
response .raise_for_status ()
@@ -141,12 +128,28 @@ def get_artifact_fixtures_tree(output_path: str = ".", commit: str = "", develop
141
128
and (commit == "" or artifact_commit .startswith (commit )) # latest or specific
142
129
):
143
130
download_url = artifact ["archive_download_url" ]
144
- download_github_artifact (download_url , output_path , headers )
145
- return
146
-
147
- raise ValueError (
148
- f"No active artifact named '{ artifact_name } ' found or matching commit '{ commit } '."
149
- )
131
+ response = requests .get (download_url , headers = headers )
132
+ response .raise_for_status ()
133
+ with zipfile .ZipFile (BytesIO (response .content )) as zip_ref :
134
+ json_file_info = next (
135
+ (
136
+ file_info
137
+ for file_info in zip_ref .infolist ()
138
+ if file_info .filename .endswith ("_hash_tree.json" )
139
+ ),
140
+ None ,
141
+ )
142
+ if json_file_info is None :
143
+ raise FileNotFoundError ("No fixtures tree hash file found in the artifact." )
144
+ with zip_ref .open (json_file_info ) as json_file :
145
+ json_data = json .load (json_file )
146
+ with open (f"./{ artifact_name } _artifact.json" , "w" ) as file :
147
+ json .dump (json_data , file , indent = 4 )
148
+ return
149
+ else :
150
+ raise ValueError (
151
+ f"No active artifact named { artifact_name } found or matching commit { commit } ."
152
+ )
150
153
151
154
152
155
def main ():
@@ -205,13 +208,18 @@ def main():
205
208
)
206
209
return
207
210
208
- # Download the latest artifact from the main branch
209
- get_artifact_fixtures_tree (
210
- output_path = "./" ,
211
+ # Download the latest fixtures tree hash json artifact from the main branch
212
+ write_artifact_fixtures_tree_json (
211
213
commit = args .commit ,
212
214
develop = args .develop
213
215
)
214
216
217
+ # Generate the fixtures tree hash from the local input directory
218
+ generate_fixtures_tree_json (
219
+ fixtures_directory = input_path ,
220
+ output_file = "fixtures_hash_tree_local.json"
221
+ )
222
+
215
223
216
224
if __name__ == "__main__" :
217
225
main ()
0 commit comments