File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11"""Tool for generating Software Bill of Materials (SBOM) for Python's dependencies"""
22import os
3+ import random
34import re
45import hashlib
56import json
67import glob
78from pathlib import Path , PurePosixPath , PureWindowsPath
89import subprocess
910import sys
11+ import urllib .error
1012import urllib .request
13+ import time
1114import typing
1215
1316CPYTHON_ROOT_DIR = Path (__file__ ).parent .parent .parent
@@ -163,6 +166,23 @@ def get_externals() -> list[str]:
163166 return externals
164167
165168
169+ def download_with_retries (download_location : str ,
170+ max_retries : int = 7 ,
171+ base_delay : float = 2.25 ,
172+ max_jitter : float = 1.0 ) -> typing .Any :
173+ """Download a file with exponential backoff retry."""
174+ for attempt in range (max_retries ):
175+ try :
176+ resp = urllib .request .urlopen (download_location )
177+ except urllib .error .URLError as ex :
178+ if attempt == max_retries :
179+ msg = f"Download from { download_location } failed."
180+ raise OSError (msg ) from ex
181+ time .sleep (base_delay ** attempt + random .uniform (0 , max_jitter ))
182+ else :
183+ return resp
184+
185+
166186def check_sbom_packages (sbom_data : dict [str , typing .Any ]) -> None :
167187 """Make a bunch of assertions about the SBOM package data to ensure it's consistent."""
168188
You can’t perform that action at this time.
0 commit comments