summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Clay <matt@mystile.com>2022-06-24 10:17:03 -0700
committerMatt Clay <matt@mystile.com>2022-06-24 12:23:57 -0700
commite1ff7798440b3aa1809ca3ab266714dd442128c3 (patch)
treec2a0e4c23cda853261a9196a5b1530f8bb3366c6
parentea451113cf5d6dfe9b66bb457241827661c88c68 (diff)
downloadansible-e1ff7798440b3aa1809ca3ab266714dd442128c3.tar.gz
[stable-2.12] ansible-test - Improve pip bootstrap download.
(cherry picked from commit b9d13d222cfe4ebd9675d5bbf529e929daaded6f) Co-authored-by: Matt Clay <matt@mystile.com>
-rw-r--r--changelogs/fragments/ansible-test-pip-bootstrap.yml3
-rw-r--r--test/lib/ansible_test/_util/target/setup/requirements.py26
2 files changed, 24 insertions, 5 deletions
diff --git a/changelogs/fragments/ansible-test-pip-bootstrap.yml b/changelogs/fragments/ansible-test-pip-bootstrap.yml
new file mode 100644
index 0000000000..fb169dfec1
--- /dev/null
+++ b/changelogs/fragments/ansible-test-pip-bootstrap.yml
@@ -0,0 +1,3 @@
+minor_changes:
+ - ansible-test - An improved error message is shown when the download of a pip bootstrap script fails.
+ The download now uses ``urllib2`` instead of ``urllib`` on Python 2.
diff --git a/test/lib/ansible_test/_util/target/setup/requirements.py b/test/lib/ansible_test/_util/target/setup/requirements.py
index f460c5c541..8bac926724 100644
--- a/test/lib/ansible_test/_util/target/setup/requirements.py
+++ b/test/lib/ansible_test/_util/target/setup/requirements.py
@@ -18,6 +18,7 @@ if DESIRED_RLIMIT_NOFILE < CURRENT_RLIMIT_NOFILE:
CURRENT_RLIMIT_NOFILE = DESIRED_RLIMIT_NOFILE
import base64
+import contextlib
import errno
import io
import json
@@ -41,10 +42,10 @@ except ImportError:
try:
from urllib.request import urlopen
except ImportError:
- from urllib import urlopen
+ # noinspection PyCompatibility,PyUnresolvedReferences
+ from urllib2 import urlopen # pylint: disable=ansible-bad-import-from
ENCODING = 'utf-8'
-PAYLOAD = b'{payload}' # base-64 encoded JSON payload which will be populated before this script is executed
Text = type(u'')
@@ -91,7 +92,20 @@ def bootstrap(pip, options): # type: (str, t.Dict[str, t.Any]) -> None
log('Downloading pip %s bootstrap script: %s' % (pip_version, url))
make_dirs(os.path.dirname(cache_path))
- download_file(url, temp_path)
+
+ try:
+ download_file(url, temp_path)
+ except Exception as ex:
+ raise ApplicationError(('''
+Download failed: %s
+
+The bootstrap script can be manually downloaded and saved to: %s
+
+If you're behind a proxy, consider commenting on the following GitHub issue:
+
+https://github.com/ansible/ansible/issues/77304
+''' % (ex, cache_path)).strip())
+
shutil.move(temp_path, cache_path)
log('Cached pip %s bootstrap script: %s' % (pip_version, cache_path))
@@ -196,8 +210,8 @@ def devnull(): # type: () -> t.IO[bytes]
def download_file(url, path): # type: (str, str) -> None
"""Download the given URL to the specified file path."""
with open(to_bytes(path), 'wb') as saved_file:
- download = urlopen(url)
- shutil.copyfileobj(download, saved_file)
+ with contextlib.closing(urlopen(url)) as download:
+ shutil.copyfileobj(download, saved_file)
class ApplicationError(Exception):
@@ -318,5 +332,7 @@ def to_text(value, errors='strict'): # type: (t.AnyStr, str) -> t.Text
raise Exception('value is not bytes or text: %s' % type(value))
+PAYLOAD = b'{payload}' # base-64 encoded JSON payload which will be populated before this script is executed
+
if __name__ == '__main__':
main()