Skip to content

Commit

Permalink
Do multiple attempt to download the nodejs archive (IncompleteRead er…
Browse files Browse the repository at this point in the history
…ror) (#329)

* Do multiple attempt to download the nodejs archive (fix for IncompleteRead error)

* use logger instead of print

* Fix linters errors

* fix incomaptible py2.7 imports
  • Loading branch information
bagerard committed May 7, 2023
1 parent a5d94e5 commit 9f2f0c4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
23 changes: 17 additions & 6 deletions nodeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,17 +582,28 @@ def tarfile_open(*args, **kwargs):
tf.close()


def _download_node_file(node_url, n_attempt=3):
"""Do multiple attempts to avoid incomplete data in case
of unstable network"""
while n_attempt > 0:
try:
return io.BytesIO(urlopen(node_url).read())
except IncompleteRead as e:
logger.warning(
'Incomplete read while reading'
'from {} - {}'.format(node_url, e)
)
n_attempt -= 1
if n_attempt == 0:
raise e


def download_node_src(node_url, src_dir, args):
"""
Download source code
"""
logger.info('.', extra=dict(continued=True))
try:
dl_contents = io.BytesIO(urlopen(node_url).read())
except IncompleteRead as e:
logger.warning('Incomplete read while reading'
'from {}'.format(node_url))
dl_contents = e.partial
dl_contents = _download_node_file(node_url)
logger.info('.', extra=dict(continued=True))

if is_WIN or is_CYGWIN:
Expand Down
13 changes: 12 additions & 1 deletion tests/nodeenv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytest

import nodeenv

from nodeenv import IncompleteRead

HERE = os.path.abspath(os.path.dirname(__file__))

Expand Down Expand Up @@ -146,3 +146,14 @@ def test_get_latest_node_version():
@pytest.mark.usefixtures('mock_index_json')
def test_get_lts_node_version():
assert nodeenv.get_last_lts_node_version() == '12.14.0'


def test__download_node_file():
with mock.patch.object(nodeenv, 'urlopen') as m_urlopen:
m_urlopen.side_effect = IncompleteRead("dummy")
with pytest.raises(IncompleteRead):
nodeenv._download_node_file(
"https://dummy/nodejs.tar.gz",
n_attempt=5
)
assert m_urlopen.call_count == 5

0 comments on commit 9f2f0c4

Please sign in to comment.