31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
"""Build-time native dependencies; never use unrelated tools from PATH."""
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
def python_ssl_binaries():
|
|
import _ssl
|
|
import ssl
|
|
import pefile
|
|
|
|
extension = Path(_ssl.__file__).resolve()
|
|
roots = (extension.parent, Path(sys.base_prefix), Path(sys.base_prefix) / "DLLs")
|
|
with pefile.PE(str(extension), fast_load=True) as image:
|
|
image.parse_data_directories(directories=[pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_IMPORT"]])
|
|
names = [entry.dll.decode("ascii") for entry in image.DIRECTORY_ENTRY_IMPORT]
|
|
binaries = []
|
|
for name in names:
|
|
if not name.lower().startswith(("libssl", "libcrypto")):
|
|
continue
|
|
source = next((root / name for root in roots if (root / name).is_file()), None)
|
|
if source is None:
|
|
raise RuntimeError(f"Python OpenSSL dependency is missing: {name}")
|
|
binaries.append((name, str(source.resolve()), "BINARY"))
|
|
return binaries
|
|
|
|
|
|
def pin_ssl_binaries(collected, verified):
|
|
"""Replace any PATH-selected DLL with the one belonging to build Python."""
|
|
identities = {name.casefold() for name, _, _ in verified}
|
|
return [item for item in collected if item[0].casefold() not in identities] + list(verified)
|