Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CGetURLOpener with urlretrieve #105

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions cget/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import click, os, sys, shutil, json, six, hashlib, ssl
import click, os, sys, shutil, json, six, hashlib

if sys.version_info[0] < 3:
try:
Expand All @@ -16,7 +16,7 @@
else:
import subprocess

from six.moves.urllib import request
from six.moves.urllib import request, error

def to_bool(value):
x = str(value).lower()
Expand Down Expand Up @@ -209,12 +209,6 @@ def symlink_to(src, dst_dir):
os.symlink(src, target)
return target

class CGetURLOpener(request.FancyURLopener):
def http_error_default(self, url, fp, errcode, errmsg, headers):
if errcode >= 400:
raise BuildError("Download failed with error {0} for: {1}".format(errcode, url))
return request.FancyURLopener.http_error_default(self, url, fp, errcode, errmsg, headers)

def download_to(url, download_dir, insecure=False):
name = url.split('/')[-1]
file = os.path.join(download_dir, name)
Expand All @@ -227,9 +221,12 @@ def hook(count, block_size, total_size):
# Hack because we can't set the position
bar.pos = percent
bar.update(0)
context = None
if insecure: context = ssl._create_unverified_context()
CGetURLOpener(context=context).retrieve(url, filename=file, reporthook=hook, data=None)
try:
request.urlretrieve(url, filename=file, reporthook=hook, data=None)
except error.HTTPError as e:
if e.status >= 400:
raise BuildError("Download failed with error {0} for: {1}".format(e.status, url))
raise
bar.update(bar_len)
if not os.path.exists(file):
raise BuildError("Download failed for: {0}".format(url))
Expand Down