Skip to content

Commit

Permalink
move download into util.files
Browse files Browse the repository at this point in the history
  • Loading branch information
MusicalNinjaDad committed Jul 7, 2024
1 parent bbf63e9 commit ea7577d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 35 deletions.
3 changes: 1 addition & 2 deletions cibuildwheel/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
call,
combine_constraints,
detect_ci_provider,
download,
find_compatible_wheel,
find_uv,
free_thread_enable_313,
Expand All @@ -46,7 +45,7 @@
unwrap,
virtualenv,
)
from .util.files import move_file
from .util.files import download, move_file


@functools.lru_cache(maxsize=None)
Expand Down
3 changes: 1 addition & 2 deletions cibuildwheel/pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
BuildSelector,
call,
combine_constraints,
download,
ensure_node,
find_compatible_wheel,
get_pip_version,
Expand All @@ -32,7 +31,7 @@
test_fail_cwd_file,
virtualenv,
)
from .util.files import extract_zip, move_file
from .util.files import download, extract_zip, move_file


@dataclass(frozen=True)
Expand Down
28 changes: 28 additions & 0 deletions cibuildwheel/util/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import contextlib
import os
import shutil
import ssl
import tarfile
import urllib.request
from dataclasses import dataclass
from pathlib import Path
from time import sleep
from typing import Generator
from zipfile import ZipFile

import certifi


def extract_zip(zip_src: Path, dest: Path) -> None:
"""Extracts a zip and correctly sets permissions on extracted files.
Expand Down Expand Up @@ -39,6 +44,29 @@ def extract_tar(tar_src: Path, dest: Path) -> None:
tar_.extractall(dest)


def download(url: str, dest: Path) -> None:
print(f"+ Download {url} to {dest}")
dest_dir = dest.parent
if not dest_dir.exists():
dest_dir.mkdir(parents=True)

# we've had issues when relying on the host OS' CA certificates on Windows,
# so we use certifi (this sounds odd but requests also does this by default)
cafile = os.environ.get("SSL_CERT_FILE", certifi.where())
context = ssl.create_default_context(cafile=cafile)
repeat_num = 3
for i in range(repeat_num):
try:
with urllib.request.urlopen(url, context=context) as response:
dest.write_bytes(response.read())
return

except OSError:
if i == repeat_num - 1:
raise
sleep(3)


def move_file(src_file: Path, dst_file: Path) -> Path:
"""Moves a file safely while avoiding potential semantic confusion:
Expand Down
29 changes: 1 addition & 28 deletions cibuildwheel/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,21 @@
import re
import shlex
import shutil
import ssl
import subprocess
import sys
import textwrap
import time
import typing
import urllib.request
from collections import defaultdict
from collections.abc import Generator, Iterable, Mapping, MutableMapping, Sequence
from dataclasses import dataclass
from enum import Enum
from functools import lru_cache
from pathlib import Path, PurePath
from tempfile import TemporaryDirectory
from time import sleep
from typing import Any, ClassVar, Final, Literal, TextIO, TypeVar

import bracex
import certifi
from filelock import FileLock
from packaging.requirements import InvalidRequirement, Requirement
from packaging.specifiers import SpecifierSet
Expand All @@ -36,7 +32,7 @@
from .._compat import tomllib
from ..architecture import Architecture
from ..typing import PathOrStr, PlatformName
from .files import FileReport, extract_tar, extract_zip
from .files import FileReport, download, extract_tar, extract_zip

resources_dir: Final[Path] = Path(__file__).parents[1] / "resources"

Expand Down Expand Up @@ -302,29 +298,6 @@ def __getattr__(self, attr: str) -> Any:
return getattr(self.stream, attr)


def download(url: str, dest: Path) -> None:
print(f"+ Download {url} to {dest}")
dest_dir = dest.parent
if not dest_dir.exists():
dest_dir.mkdir(parents=True)

# we've had issues when relying on the host OS' CA certificates on Windows,
# so we use certifi (this sounds odd but requests also does this by default)
cafile = os.environ.get("SSL_CERT_FILE", certifi.where())
context = ssl.create_default_context(cafile=cafile)
repeat_num = 3
for i in range(repeat_num):
try:
with urllib.request.urlopen(url, context=context) as response:
dest.write_bytes(response.read())
return

except OSError:
if i == repeat_num - 1:
raise
sleep(3)


class DependencyConstraints:
def __init__(self, base_file_path: Path):
assert base_file_path.exists()
Expand Down
3 changes: 1 addition & 2 deletions cibuildwheel/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
BuildSelector,
call,
combine_constraints,
download,
find_compatible_wheel,
find_uv,
get_build_verbosity_extra_flags,
Expand All @@ -40,7 +39,7 @@
unwrap,
virtualenv,
)
from .util.files import extract_zip, move_file
from .util.files import download, extract_zip, move_file


def get_nuget_args(
Expand Down
2 changes: 1 addition & 1 deletion unit_test/download_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import certifi
import pytest

from cibuildwheel.util import download
from cibuildwheel.util.files import download

DOWNLOAD_URL = "https://raw.githubusercontent.com/pypa/cibuildwheel/v1.6.3/requirements-dev.txt"

Expand Down

0 comments on commit ea7577d

Please sign in to comment.