From cd365057d355a6a6b7d034646cb07152b1d276bd Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Mon, 8 Apr 2024 11:50:17 -0700 Subject: [PATCH] Remove more Python 2.x compat code --- .../fragments/remove-python-2-compat.yml | 3 +++ lib/ansible/modules/unarchive.py | 11 ++------ .../_util/target/setup/requirements.py | 26 +++++-------------- 3 files changed, 11 insertions(+), 29 deletions(-) create mode 100644 changelogs/fragments/remove-python-2-compat.yml diff --git a/changelogs/fragments/remove-python-2-compat.yml b/changelogs/fragments/remove-python-2-compat.yml new file mode 100644 index 00000000000..aaf6031b8f7 --- /dev/null +++ b/changelogs/fragments/remove-python-2-compat.yml @@ -0,0 +1,3 @@ +minor_changes: + - unarchive - Remove Python 2.7 compatibility imports. + - ansible-test - Remove Python 2.7 compatibility imports. diff --git a/lib/ansible/modules/unarchive.py b/lib/ansible/modules/unarchive.py index 6c51f1d4992..75ec7f8d49c 100644 --- a/lib/ansible/modules/unarchive.py +++ b/lib/ansible/modules/unarchive.py @@ -260,15 +260,8 @@ from ansible.module_utils.common.process import get_bin_path from ansible.module_utils.common.locale import get_best_parsable_locale from ansible.module_utils.urls import fetch_file -try: # python 3.3+ - from shlex import quote # type: ignore[attr-defined] -except ImportError: # older python - from pipes import quote - -try: # python 3.2+ - from zipfile import BadZipFile # type: ignore[attr-defined] -except ImportError: # older python - from zipfile import BadZipfile as BadZipFile +from shlex import quote +from zipfile import BadZipFile # String from tar that shows the tar contents are different from the # filesystem diff --git a/test/lib/ansible_test/_util/target/setup/requirements.py b/test/lib/ansible_test/_util/target/setup/requirements.py index a320dfce19a..5847ac731e5 100644 --- a/test/lib/ansible_test/_util/target/setup/requirements.py +++ b/test/lib/ansible_test/_util/target/setup/requirements.py @@ -22,27 +22,13 @@ import errno import io import json import os +import shlex import shutil import subprocess import sys import tempfile - -try: - import typing as t -except ImportError: - t = None - -try: - from shlex import quote as cmd_quote -except ImportError: - # noinspection PyProtectedMember - from pipes import quote as cmd_quote - -try: - from urllib.request import urlopen -except ImportError: - # noinspection PyCompatibility,PyUnresolvedReferences - from urllib2 import urlopen # pylint: disable=ansible-bad-import-from +import typing as t +import urllib.request ENCODING = 'utf-8' @@ -280,7 +266,7 @@ 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: - with contextlib.closing(urlopen(url)) as download: + with contextlib.closing(urllib.request.urlopen(url)) as download: shutil.copyfileobj(download, saved_file) @@ -291,7 +277,7 @@ class ApplicationError(Exception): class SubprocessError(ApplicationError): """A command returned a non-zero status.""" def __init__(self, cmd, status, stdout, stderr): # type: (t.List[str], int, str, str) -> None - message = 'A command failed with status %d: %s' % (status, ' '.join(cmd_quote(c) for c in cmd)) + message = 'A command failed with status %d: %s' % (status, shlex.join(cmd)) if stderr: message += '\n>>> Standard Error\n%s' % stderr.strip() @@ -313,7 +299,7 @@ def log(message, verbosity=0): # type: (str, int) -> None def execute_command(cmd, cwd=None, capture=False, env=None): # type: (t.List[str], t.Optional[str], bool, t.Optional[t.Dict[str, str]]) -> None """Execute the specified command.""" - log('Execute command: %s' % ' '.join(cmd_quote(c) for c in cmd), verbosity=1) + log('Execute command: %s' % shlex.join(cmd), verbosity=1) cmd_bytes = [to_bytes(c) for c in cmd]