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 642a9dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
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

0 comments on commit 642a9dc

Please sign in to comment.