Remove more Python 2.x compat code

pull/83089/head
Matt Clay 1 month ago
parent 5187061939
commit cd365057d3

@ -0,0 +1,3 @@
minor_changes:
- unarchive - Remove Python 2.7 compatibility imports.
- ansible-test - Remove Python 2.7 compatibility imports.

@ -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.common.locale import get_best_parsable_locale
from ansible.module_utils.urls import fetch_file from ansible.module_utils.urls import fetch_file
try: # python 3.3+ from shlex import quote
from shlex import quote # type: ignore[attr-defined] from zipfile import BadZipFile
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
# String from tar that shows the tar contents are different from the # String from tar that shows the tar contents are different from the
# filesystem # filesystem

@ -22,27 +22,13 @@ import errno
import io import io
import json import json
import os import os
import shlex
import shutil import shutil
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import typing as t
try: import urllib.request
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
ENCODING = 'utf-8' ENCODING = 'utf-8'
@ -280,7 +266,7 @@ def devnull(): # type: () -> t.IO[bytes]
def download_file(url, path): # type: (str, str) -> None def download_file(url, path): # type: (str, str) -> None
"""Download the given URL to the specified file path.""" """Download the given URL to the specified file path."""
with open(to_bytes(path), 'wb') as saved_file: 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) shutil.copyfileobj(download, saved_file)
@ -291,7 +277,7 @@ class ApplicationError(Exception):
class SubprocessError(ApplicationError): class SubprocessError(ApplicationError):
"""A command returned a non-zero status.""" """A command returned a non-zero status."""
def __init__(self, cmd, status, stdout, stderr): # type: (t.List[str], int, str, str) -> None 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: if stderr:
message += '\n>>> Standard Error\n%s' % stderr.strip() 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 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.""" """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] cmd_bytes = [to_bytes(c) for c in cmd]

Loading…
Cancel
Save