Skip to content
This repository has been archived by the owner on Jul 3, 2022. It is now read-only.

Commit

Permalink
ft: show sizes with SI prefixes for downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattwmaster58 committed Aug 15, 2021
1 parent 6f157d9 commit 495be1e
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions pyppeteer/chromium_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@

"""Chromium download module."""

from io import BytesIO
import logging
import os
from pathlib import Path
import stat
import sys
from io import BytesIO
from pathlib import Path
from zipfile import ZipFile

import urllib3
from tqdm import tqdm

from pyppeteer import __chromium_revision__, __pyppeteer_home__
from tqdm import tqdm

logger = logging.getLogger(__name__)

DOWNLOADS_FOLDER = Path(__pyppeteer_home__) / 'local-chromium'
DEFAULT_DOWNLOAD_HOST = 'https://storage.googleapis.com'
DOWNLOAD_HOST = os.environ.get(
'PYPPETEER_DOWNLOAD_HOST', DEFAULT_DOWNLOAD_HOST)
DOWNLOAD_HOST = os.environ.get('PYPPETEER_DOWNLOAD_HOST', DEFAULT_DOWNLOAD_HOST)
BASE_URL = f'{DOWNLOAD_HOST}/chromium-browser-snapshots'

REVISION = os.environ.get(
'PYPPETEER_CHROMIUM_REVISION', __chromium_revision__)
REVISION = os.environ.get('PYPPETEER_CHROMIUM_REVISION', __chromium_revision__)

NO_PROGRESS_BAR = os.environ.get('PYPPETEER_NO_PROGRESS_BAR', '')
if NO_PROGRESS_BAR.lower() in ('1', 'true'):
Expand All @@ -43,8 +40,7 @@

chromiumExecutable = {
'linux': DOWNLOADS_FOLDER / REVISION / 'chrome-linux' / 'chrome',
'mac': (DOWNLOADS_FOLDER / REVISION / 'chrome-mac' / 'Chromium.app' /
'Contents' / 'MacOS' / 'Chromium'),
'mac': (DOWNLOADS_FOLDER / REVISION / 'chrome-mac' / 'Chromium.app' / 'Contents' / 'MacOS' / 'Chromium'),
'win32': DOWNLOADS_FOLDER / REVISION / windowsArchive / 'chrome.exe',
'win64': DOWNLOADS_FOLDER / REVISION / windowsArchive / 'chrome.exe',
}
Expand All @@ -56,9 +52,7 @@ def current_platform() -> str:
return 'linux'
elif sys.platform.startswith('darwin'):
return 'mac'
elif (sys.platform.startswith('win') or
sys.platform.startswith('msys') or
sys.platform.startswith('cyg')):
elif sys.platform.startswith('win') or sys.platform.startswith('msys') or sys.platform.startswith('cyg'):
if sys.maxsize > 2 ** 31 - 1:
return 'win64'
return 'win32'
Expand All @@ -72,8 +66,7 @@ def get_url() -> str:

def download_zip(url: str) -> BytesIO:
"""Download data from url."""
logger.warning('Starting Chromium download. '
'Download may take a few minutes.')
logger.warning('Starting Chromium download. ' 'Download may take a few minutes.')

# Uncomment the statement below to disable HTTPS warnings and allow
# download without certificate verification. This is *strongly* as it
Expand All @@ -87,8 +80,7 @@ def download_zip(url: str) -> BytesIO:
# set preload_content=False means using stream later.
r = http.request('GET', url, preload_content=False)
if r.status >= 400:
raise OSError(f'Chromium downloadable not found at {url}: '
f'Received {r.data.decode()}.\n')
raise OSError(f'Chromium downloadable not found at {url}: ' f'Received {r.data.decode()}.\n')

# 10 * 1024
_data = BytesIO()
Expand All @@ -100,7 +92,7 @@ def download_zip(url: str) -> BytesIO:
total_length = int(r.headers['content-length'])
except (KeyError, ValueError, AttributeError):
total_length = 0
process_bar = tqdm(total=total_length)
process_bar = tqdm(total=total_length, unit_scale=True, unit='b')
for chunk in r.stream(10240):
_data.write(chunk)
process_bar.update(len(chunk))
Expand All @@ -116,19 +108,16 @@ def extract_zip(data: BytesIO, path: Path) -> None:
if current_platform() == 'mac':
import subprocess
import shutil

zip_path = path / 'chrome.zip'
if not path.exists():
path.mkdir(parents=True)
with zip_path.open('wb') as f:
f.write(data.getvalue())
if not shutil.which('unzip'):
raise OSError('Failed to automatically extract chromium.'
f'Please unzip {zip_path} manually.')
raise OSError('Failed to automatically extract chromium.' f'Please unzip {zip_path} manually.')
proc = subprocess.run(
['unzip', str(zip_path)],
cwd=str(path),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
['unzip', str(zip_path)], cwd=str(path), stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
)
if proc.returncode != 0:
logger.error(proc.stdout.decode())
Expand All @@ -141,8 +130,7 @@ def extract_zip(data: BytesIO, path: Path) -> None:
exec_path = chromium_executable()
if not exec_path.exists():
raise IOError('Failed to extract chromium.')
exec_path.chmod(exec_path.stat().st_mode | stat.S_IXOTH | stat.S_IXGRP |
stat.S_IXUSR)
exec_path.chmod(exec_path.stat().st_mode | stat.S_IXOTH | stat.S_IXGRP | stat.S_IXUSR)
logger.warning(f'chromium extracted to: {path}')


Expand All @@ -155,10 +143,7 @@ def chromium_excutable() -> Path:
"""[Deprecated] miss-spelled function.
Use `chromium_executable` instead.
"""
logger.warning(
'`chromium_excutable` function is deprecated. '
'Use `chromium_executable instead.'
)
logger.warning('`chromium_excutable` function is deprecated. ' 'Use `chromium_executable instead.')
return chromium_executable()


Expand Down

0 comments on commit 495be1e

Please sign in to comment.