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

Downloads were failing on me, simpified and removed progress indicator #530

Open
wants to merge 3 commits into
base: Stino-Dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
120 changes: 27 additions & 93 deletions libs/base_utils/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,102 +58,36 @@ def get_remote_etag(url):

def download(url, target_dir,
message_consumer=sys.stdout.write, mode='resume'):
"""."""
is_done = False
trunk_size = 1024
done_size = 0
"""Download a file from an URL into a target directory."""
is_msg_quiet = True

file_name = os.path.basename(url)
target_file_path = os.path.join(target_dir, file_name)
tmp_file_path = target_file_path + '.stino-down'

remote_info = get_remote_file_info(url)
remote_size = int(remote_info.get('Content-Length', '0'))

if remote_size > 0:
if mode == 'resume':
if os.path.isfile(target_file_path):
if os.path.getsize(target_file_path) == remote_size:
is_done = True

if not is_done:
if url.startswith('https'):
opener = urllib.request.build_opener(HTTPSHandlerV3())
urllib.request.install_opener(opener)
req = urllib.request.Request(url)
if os.path.isfile(tmp_file_path):
if mode == 'resume':
done_size = os.path.getsize(tmp_file_path)
req.add_header('Range', 'bytes=%d-' % done_size)
else:
os.remove(tmp_file_path)

try:
remote_f = urllib.request.urlopen(req)
except (ValueError, urllib.error.HTTPError, urllib.error.URLError):
message_consumer('[Error] Can not fetch %s\n' % url,
is_msg_quiet)
else:
message_consumer('[%s] Download started.\n' % url,
is_msg_quiet)
block = b''
retry_counter = 0

if not os.path.isdir(target_dir):
os.makedirs(target_dir)
f = open(tmp_file_path, 'ab')
while True:
try:
trunk = remote_f.read(trunk_size)
except (urllib.error.HTTPError, urllib.error.URLError):
retry_counter += 1
if retry_counter < 20:
continue
else:
break
else:
if not trunk:
is_done = True
break

block += trunk
if len(block) > remote_size / 10:
done_size += len(block)
f.write(block)
block = b''

percent = done_size / remote_size * 100
text = '[%s] %.0f%%' % (url, percent)
text += ' ('
text += '%.2f' % (done_size / 1024 / 1024)
text += ' M / '
text += '%.2f' % (remote_size / 1024 / 1024)
text += ' M)\n'
message_consumer(text, is_msg_quiet)

if done_size < remote_size:
f.write(block)

remote_f.close()
f.close()

if is_done:
if os.path.isfile(target_file_path):
os.remove(target_file_path)
try:
os.rename(tmp_file_path, target_file_path)
except IOError:
msg = 'Can not rename %s to %s.' % (tmp_file_path,
target_file_path)
message_consumer(msg, is_msg_quiet)
else:
message_consumer('[%s] Download completed.\n' % url,
is_msg_quiet)
else:
message_consumer('[%s] Download failed.\n' % url,
is_msg_quiet)
return is_done
try:
import urllib.request
response = urllib.request.urlopen(url)
response_info = dict(response.headers)
message_consumer(
"Downloading %s Bytes into %s" % (
response_info.get('Content-Length', '0'),
target_file_path,
),
is_msg_quiet
)
open(target_file_path, 'wb').write(response.read())
except Exception as e:
message_consumer(
'[Error] Can not fetch %s: %s\n' % (
url,
str(e)
),
is_msg_quiet
)
return False
message_consumer(
'Download finished!\n',
is_msg_quiet
)
return True


class HTTPSConnectionV3(http.client.HTTPSConnection):
Expand Down
4 changes: 2 additions & 2 deletions libs/stino_runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ def get_h_path_info(project):
ext_app_path = arduino_info['ext_app_path']
platform_path = selected.get_sel_platform_path(arduino_info)

platform_paths = [platform_path, sketchbook_path, ext_app_path]
platform_paths = [sketchbook_path, platform_path, ext_app_path]
for path in platform_paths:
libraries_path = os.path.join(path, 'libraries')
paths = glob.glob(libraries_path + '/*')
Expand Down Expand Up @@ -1825,7 +1825,7 @@ def run_build_commands(cmds, msgs):
"""."""
is_ok = True
n = 0

percent = 0
non_blank_msgs = [m for m in msgs if m]
total = len(non_blank_msgs)
for cmd, msg in zip(cmds, msgs):
Expand Down