ansible-test - Avoid direct use of `errno`.

Error handling on Python 3.x no longer requires the use of `errno` to identify specific errors.
pull/79510/head
Matt Clay 2 years ago
parent cda16cc5e9
commit f9715f436c

@ -0,0 +1,2 @@
minor_changes:
- ansible-test - Update error handling code to use Python 3.x constructs, avoiding direct use of ``errno``.

@ -2,7 +2,6 @@
from __future__ import annotations from __future__ import annotations
import collections.abc as c import collections.abc as c
import errno
import json import json
import os import os
import re import re
@ -135,12 +134,9 @@ def get_coverage_files(language: str, path: t.Optional[str] = None) -> list[str]
try: try:
coverage_files = [os.path.join(coverage_dir, f) for f in os.listdir(coverage_dir) coverage_files = [os.path.join(coverage_dir, f) for f in os.listdir(coverage_dir)
if '=coverage.' in f and '=%s' % language in f] if '=coverage.' in f and '=%s' % language in f]
except IOError as ex: except FileNotFoundError:
if ex.errno == errno.ENOENT:
return [] return []
raise
return coverage_files return coverage_files

@ -8,7 +8,6 @@ import os
import re import re
import traceback import traceback
import uuid import uuid
import errno
import time import time
import typing as t import typing as t
@ -347,18 +346,14 @@ class AnsibleCoreCI:
try: try:
self.connection = None self.connection = None
os.remove(self.path) os.remove(self.path)
except OSError as ex: except FileNotFoundError:
if ex.errno != errno.ENOENT: pass
raise
def _load(self): def _load(self):
"""Load instance information.""" """Load instance information."""
try: try:
data = read_text_file(self.path) data = read_text_file(self.path)
except IOError as ex: except FileNotFoundError:
if ex.errno != errno.ENOENT:
raise
return False return False
if not data.startswith('{'): if not data.startswith('{'):

@ -1,7 +1,6 @@
"""Functions for disk IO.""" """Functions for disk IO."""
from __future__ import annotations from __future__ import annotations
import errno
import io import io
import json import json
import os import os
@ -32,11 +31,7 @@ def read_binary_file(path: str) -> bytes:
def make_dirs(path: str) -> None: def make_dirs(path: str) -> None:
"""Create a directory at path, including any necessary parent directories.""" """Create a directory at path, including any necessary parent directories."""
try: os.makedirs(to_bytes(path), exist_ok=True)
os.makedirs(to_bytes(path))
except OSError as ex:
if ex.errno != errno.EEXIST:
raise
def write_json_file(path: str, def write_json_file(path: str,

@ -3,7 +3,6 @@ from __future__ import annotations
import abc import abc
import collections.abc as c import collections.abc as c
import errno
import enum import enum
import fcntl import fcntl
import importlib.util import importlib.util
@ -467,10 +466,8 @@ def raw_command(
cmd_bytes = [to_bytes(arg) for arg in cmd] cmd_bytes = [to_bytes(arg) for arg in cmd]
env_bytes = dict((to_bytes(k), to_bytes(v)) for k, v in env.items()) env_bytes = dict((to_bytes(k), to_bytes(v)) for k, v in env.items())
process = subprocess.Popen(cmd_bytes, env=env_bytes, stdin=stdin, stdout=stdout, stderr=stderr, cwd=cwd) # pylint: disable=consider-using-with process = subprocess.Popen(cmd_bytes, env=env_bytes, stdin=stdin, stdout=stdout, stderr=stderr, cwd=cwd) # pylint: disable=consider-using-with
except OSError as ex: except FileNotFoundError as ex:
if ex.errno == errno.ENOENT: raise ApplicationError('Required program "%s" not found.' % cmd[0]) from ex
raise ApplicationError('Required program "%s" not found.' % cmd[0])
raise
if communicate: if communicate:
data_bytes = to_optional_bytes(data) data_bytes = to_optional_bytes(data)
@ -694,12 +691,11 @@ def verified_chmod(path: str, mode: int) -> None:
def remove_tree(path: str) -> None: def remove_tree(path: str) -> None:
"""Remove the specified directory, siliently continuing if the directory does not exist.""" """Remove the specified directory, silently continuing if the directory does not exist."""
try: try:
shutil.rmtree(to_bytes(path)) shutil.rmtree(to_bytes(path))
except OSError as ex: except FileNotFoundError:
if ex.errno != errno.ENOENT: pass
raise
def is_binary_file(path: str) -> bool: def is_binary_file(path: str) -> bool:

@ -22,7 +22,6 @@ import argparse
import ast import ast
import datetime import datetime
import json import json
import errno
import os import os
import re import re
import subprocess import subprocess
@ -2467,12 +2466,9 @@ class GitCache:
self.head_tree = self._get_module_files() self.head_tree = self._get_module_files()
else: else:
raise raise
except OSError as ex: except FileNotFoundError:
if ex.errno == errno.ENOENT:
# fallback when git is not installed # fallback when git is not installed
self.head_tree = self._get_module_files() self.head_tree = self._get_module_files()
else:
raise
allowed_exts = ('.py', '.ps1') allowed_exts = ('.py', '.ps1')
if plugin_type != 'module': if plugin_type != 'module':

Loading…
Cancel
Save