test: move-only download_from_url to stand-alone util file
What changed, and why it matters
This commit simply moves an existing download helper function from one test script into its own small utility file. There are no functional code changes, no bug fixes, and no security-related modifications.
No security action needed; this is a routine test-code refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change is a pure refactor: the download_from_url function is relocated from test/get_previous_releases.py to a new file test/download_utils.py, and the original script now imports it. The function body is identical, and the commit message explicitly states it is move-only and suggests --color-moved=dimmed-zebra for review.
Changed components
test/get_previous_releases.pytest/download_utils.pyInspect captured patch +50 / −40
diff --git a/test/download_utils.py b/test/download_utils.py
new file mode 100644
index 00000000..de7de82c
--- /dev/null
+++ b/test/download_utils.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+#
+# Copyright (c) The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or https://opensource.org/license/mit/.
+
+import time
+import urllib.request
+
+
+def download_from_url(url, archive):
+ last_print_time = time.time()
+
+ def progress_hook(progress_bytes, total_size):
+ nonlocal last_print_time
+ now = time.time()
+ percent = min(100, (progress_bytes * 100) / total_size)
+ bar_length = 40
+ filled_length = int(bar_length * percent / 100)
+ bar = '#' * filled_length + '-' * (bar_length - filled_length)
+ if now - last_print_time >= 1 or percent >= 100:
+ print(f'\rDownloading: [{bar}] {percent:.1f}%', flush=True, end="")
+ last_print_time = now
+
+ with urllib.request.urlopen(url) as response:
+ if response.status != 200:
+ raise RuntimeError(f"HTTP request failed with status code: {response.status}")
+
+ sock_info = response.fp.raw._sock.getpeername()
+ print(f"Connected to {sock_info[0]}")
+
+ total_size = int(response.getheader("Content-Length"))
+ progress_bytes = 0
+
+ with open(archive, 'wb') as file:
+ while True:
+ chunk = response.read(8192)
+ if not chunk:
+ break
+ file.write(chunk)
+ progress_bytes += len(chunk)
+ progress_hook(progress_bytes, total_size)
+
+ if progress_bytes < total_size:
+ raise RuntimeError(f"Download incomplete: expected {total_size} bytes, got {progress_bytes} bytes")
+
+ print('\n', flush=True, end="") # Flush to avoid error output on the same line.
diff --git a/test/get_previous_releases.py b/test/get_previous_releases.py
index cab4c6e8..bd83c787 100755
--- a/test/get_previous_releases.py
+++ b/test/get_previous_releases.py
@@ -16,9 +16,11 @@ import shutil
import subprocess
import sys
import time
-import urllib.request
import zipfile
+sys.path.append(str(Path(__file__).resolve().parent))
+from download_utils import download_from_url
+
TAR = os.getenv('TAR', 'tar')
SHA256_SUMS = {
@@ -102,45 +104,6 @@ def pushd(new_dir) -> None:
os.chdir(previous_dir)
-def download_from_url(url, archive):
- last_print_time = time.time()
-
- def progress_hook(progress_bytes, total_size):
- nonlocal last_print_time
- now = time.time()
- percent = min(100, (progress_bytes * 100) / total_size)
- bar_length = 40
- filled_length = int(bar_length * percent / 100)
- bar = '#' * filled_length + '-' * (bar_length - filled_length)
- if now - last_print_time >= 1 or percent >= 100:
- print(f'\rDownloading: [{bar}] {percent:.1f}%', flush=True, end="")
- last_print_time = now
-
- with urllib.request.urlopen(url) as response:
- if response.status != 200:
- raise RuntimeError(f"HTTP request failed with status code: {response.status}")
-
- sock_info = response.fp.raw._sock.getpeername()
- print(f"Connected to {sock_info[0]}")
-
- total_size = int(response.getheader("Content-Length"))
- progress_bytes = 0
-
- with open(archive, 'wb') as file:
- while True:
- chunk = response.read(8192)
- if not chunk:
- break
- file.write(chunk)
- progress_bytes += len(chunk)
- progress_hook(progress_bytes, total_size)
-
- if progress_bytes < total_size:
- raise RuntimeError(f"Download incomplete: expected {total_size} bytes, got {progress_bytes} bytes")
-
- print('\n', flush=True, end="") # Flush to avoid error output on the same line.
-
-
def download_binary(tag, args) -> int:
if Path(tag).is_dir():
if not args.remove_dir:
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.