Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable-2.14] ansible-test - Avoid direct use of errno. #79972

Merged
merged 1 commit into from Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/ansible-test-errno.yml
@@ -0,0 +1,2 @@
minor_changes:
- ansible-test - Update error handling code to use Python 3.x constructs, avoiding direct use of ``errno``.
8 changes: 2 additions & 6 deletions test/lib/ansible_test/_internal/commands/coverage/__init__.py
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

import collections.abc as c
import errno
import json
import os
import re
Expand Down Expand Up @@ -135,11 +134,8 @@ def get_coverage_files(language: str, path: t.Optional[str] = None) -> list[str]
try:
coverage_files = [os.path.join(coverage_dir, f) for f in os.listdir(coverage_dir)
if '=coverage.' in f and '=%s' % language in f]
except IOError as ex:
if ex.errno == errno.ENOENT:
return []

raise
except FileNotFoundError:
return []

return coverage_files

Expand Down
11 changes: 3 additions & 8 deletions test/lib/ansible_test/_internal/core_ci.py
Expand Up @@ -8,7 +8,6 @@
import re
import traceback
import uuid
import errno
import time
import typing as t

Expand Down Expand Up @@ -347,18 +346,14 @@ def _clear(self):
try:
self.connection = None
os.remove(self.path)
except OSError as ex:
if ex.errno != errno.ENOENT:
raise
except FileNotFoundError:
pass

def _load(self):
"""Load instance information."""
try:
data = read_text_file(self.path)
except IOError as ex:
if ex.errno != errno.ENOENT:
raise

except FileNotFoundError:
return False

if not data.startswith('{'):
Expand Down
7 changes: 1 addition & 6 deletions test/lib/ansible_test/_internal/io.py
@@ -1,7 +1,6 @@
"""Functions for disk IO."""
from __future__ import annotations

import errno
import io
import json
import os
Expand Down Expand Up @@ -32,11 +31,7 @@ def read_binary_file(path: str) -> bytes:

def make_dirs(path: str) -> None:
"""Create a directory at path, including any necessary parent directories."""
try:
os.makedirs(to_bytes(path))
except OSError as ex:
if ex.errno != errno.EEXIST:
raise
os.makedirs(to_bytes(path), exist_ok=True)


def write_json_file(path: str,
Expand Down
14 changes: 5 additions & 9 deletions test/lib/ansible_test/_internal/util.py
Expand Up @@ -3,7 +3,6 @@

import abc
import collections.abc as c
import errno
import enum
import fcntl
import importlib.util
Expand Down Expand Up @@ -467,10 +466,8 @@ def raw_command(
cmd_bytes = [to_bytes(arg) for arg in cmd]
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
except OSError as ex:
if ex.errno == errno.ENOENT:
raise ApplicationError('Required program "%s" not found.' % cmd[0])
raise
except FileNotFoundError as ex:
raise ApplicationError('Required program "%s" not found.' % cmd[0]) from ex

if communicate:
data_bytes = to_optional_bytes(data)
Expand Down Expand Up @@ -694,12 +691,11 @@ def verified_chmod(path: str, mode: int) -> 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:
shutil.rmtree(to_bytes(path))
except OSError as ex:
if ex.errno != errno.ENOENT:
raise
except FileNotFoundError:
pass


def is_binary_file(path: str) -> bool:
Expand Down
Expand Up @@ -22,7 +22,6 @@
import ast
import datetime
import json
import errno
import os
import re
import subprocess
Expand Down Expand Up @@ -2467,12 +2466,9 @@ def __init__(self, base_branch, plugin_type):
self.head_tree = self._get_module_files()
else:
raise
except OSError as ex:
if ex.errno == errno.ENOENT:
# fallback when git is not installed
self.head_tree = self._get_module_files()
else:
raise
except FileNotFoundError:
# fallback when git is not installed
self.head_tree = self._get_module_files()

allowed_exts = ('.py', '.ps1')
if plugin_type != 'module':
Expand Down