Skip to content

Commit c26dc8d

Browse files
committed
src/entry_points/diff_fixtures.py: Refactor artifact download function.
1 parent 2881da3 commit c26dc8d

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

src/entry_points/diff_fixtures.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import hashlib
2424
import json
2525
from pathlib import Path
26+
from io import BytesIO
2627
from typing import Dict, List, Tuple
2728
from dotenv import load_dotenv
2829
import os
@@ -65,7 +66,8 @@ def build_tree(directory: Path, parent_path) -> Tuple[Dict, List[str]]:
6566
directory_contents = {}
6667
all_hashes = []
6768

68-
for item in directory.iterdir():
69+
sorted_items = sorted(directory.iterdir(), key=lambda x: x.name)
70+
for item in sorted_items:
6971
relative_path = f"{parent_path}/{item.name}" if parent_path else item.name
7072

7173
if item.is_dir():
@@ -100,24 +102,9 @@ def build_tree(directory: Path, parent_path) -> Tuple[Dict, List[str]]:
100102
json.dump(fixtures_tree, file, indent=4)
101103

102104

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):
104106
"""
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
121108
main branch base artifact. If the develop flag is set, it will download the latest development
122109
artifact. The commit flag can be used to download a specific artifact on the main branch based.
123110
"""
@@ -126,7 +113,7 @@ def get_artifact_fixtures_tree(output_path: str = ".", commit: str = "", develop
126113
if not github_token:
127114
raise ValueError("GitHub PAT not found. Ensure GITHUB_PAT is set within your .env file.")
128115

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"
130117
headers = {"Authorization": f"token {github_token}"}
131118
response = requests.get(api_url, headers=headers)
132119
response.raise_for_status()
@@ -141,12 +128,28 @@ def get_artifact_fixtures_tree(output_path: str = ".", commit: str = "", develop
141128
and (commit == "" or artifact_commit.startswith(commit)) # latest or specific
142129
):
143130
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+
)
150153

151154

152155
def main():
@@ -205,13 +208,18 @@ def main():
205208
)
206209
return
207210

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(
211213
commit=args.commit,
212214
develop=args.develop
213215
)
214216

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+
215223

216224
if __name__ == "__main__":
217225
main()

whitelist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ changelog
4747
chfast
4848
classdict
4949
cli2
50+
cmd
5051
codeAddr
5152
codecopy
5253
codesize
@@ -139,6 +140,7 @@ hyperledger
139140
ignoreRevsFile
140141
img
141142
incrementing
143+
infolist
142144
init
143145
initcode
144146
instantiation

0 commit comments

Comments
 (0)