Skip to content

Commit

Permalink
Address ruff PTH (ansible#1773)
Browse files Browse the repository at this point in the history
* Address ruff PTH101 exceptions

* Address ruff PTH112 exceptions

* Address ruff PTH115 exceptions

* Address ruff PTH107 exceptions

* Address ruff PTH114 exceptions

* Address ruff PTH204 exceptions
  • Loading branch information
shatakshiiii authored May 27, 2024
1 parent 0211ce6 commit b15e552
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 17 deletions.
6 changes: 0 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -305,22 +305,16 @@ ignore = [
'PT019', # Fixture `_mocked_func` without value is injected as parameter, use `@pytest.mark.usefixtures` instead
'PT022', # [*] No teardown in fixture `cmd_in_tty`, use `return` instead of `yield`
'PTH100', # `os.path.abspath()` should be replaced by `Path.resolve()`
'PTH101', # `os.chmod()` should be replaced by `Path.chmod()`
'PTH103', # `os.makedirs()` should be replaced by `Path.mkdir(parents=True)`
'PTH107', # `os.remove()` should be replaced by `Path.unlink()`
'PTH109', # `os.getcwd()` should be replaced by `Path.cwd()`
'PTH110', # `os.path.exists()` should be replaced by `Path.exists()`
'PTH111', # `os.path.expanduser()` should be replaced by `Path.expanduser()`
'PTH112', # `os.path.isdir()` should be replaced by `Path.is_dir()`
'PTH113', # `os.path.isfile()` should be replaced by `Path.is_file()`
'PTH114', # `os.path.islink()` should be replaced by `Path.is_symlink()`
'PTH115', # `os.readlink()` should be replaced by `Path.readlink()`
'PTH116', # `os.stat()` should be replaced by `Path.stat()`, `Path.owner()`, or `Path.group()`
'PTH118', # `os.path.join()` should be replaced by `Path` with `/` operator
'PTH119', # `os.path.basename()` should be replaced by `Path.name`
'PTH120', # `os.path.dirname()` should be replaced by `Path.parent`
'PTH122', # `os.path.splitext()` should be replaced by `Path.suffix`
'PTH204', # `os.path.getmtime` should be replaced by `Path.stat().st_mtime`
'PTH207', # Replace `glob` with `Path.glob` or `Path.rglob`
'RET505', # Unnecessary `else` after `return` statement
'RUF005', # [*] Consider `[self._name, *shlex.split(self._interaction.action.match.groupdict()["params"] or "")]` instead of concatenation
Expand Down
6 changes: 3 additions & 3 deletions src/ansible_navigator/actions/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ def _set_inventories_mtime(self) -> None:
"""Record the inventory modification time."""
modification_times = []
for inventory in self._inventories:
if os.path.isdir(inventory):
if Path(inventory).is_dir():
modification_times.append(
max(
os.path.getmtime(e)
Path(e).stat().st_mtime
for e in glob.glob(os.path.join(inventory, "**"), recursive=True)
),
)
elif os.path.isfile(inventory):
modification_times.append(os.path.getmtime(inventory))
modification_times.append(Path(inventory).stat().st_mtime)
if modification_times:
self._inventories_mtime = max(modification_times)
else:
Expand Down
3 changes: 2 additions & 1 deletion src/ansible_navigator/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import sys

from pathlib import Path
from typing import TYPE_CHECKING
from typing import NoReturn

Expand Down Expand Up @@ -135,7 +136,7 @@ def get_and_check_collection_doc_cache(
message = "Collection doc cache: version was empty or incorrect, rebuilding"
messages.append(LogMessage(level=logging.INFO, message=message))
collection_cache.close()
os.remove(collection_doc_cache_path)
Path(collection_doc_cache_path).unlink()
collection_cache = KeyValueStore(collection_doc_cache_path)
collection_cache["version"] = VERSION_CDC
cache_version = collection_cache["version"]
Expand Down
3 changes: 2 additions & 1 deletion src/ansible_navigator/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import zoneinfo

from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any

Expand Down Expand Up @@ -57,7 +58,7 @@ def setup_logger(args: ApplicationConfiguration) -> None:
:param args: The CLI args
"""
if os.path.exists(args.log_file) and args.log_append is False:
os.remove(args.log_file)
Path(args.log_file).unlink()
handler = logging.FileHandler(args.log_file)

time_zone = args.entry("time_zone").value.current
Expand Down
2 changes: 1 addition & 1 deletion src/ansible_navigator/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def time_stamp_for_file(path: str, time_zone: str) -> tuple[float | None, str |
:returns: The UNIX timestamp and an ISO 8601 string
"""
try:
modified = os.path.getmtime(path)
modified = Path(path).stat().st_mtime
except FileNotFoundError:
# It may have been mounted to a different location in the execution environment
modified = None
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def locked_directory(tmpdir: str) -> Generator[str, None, None]:
:param tmpdir: Fixture for temporary directory
:yields: The temporary directory
"""
os.chmod(tmpdir, 0o000)
Path(tmpdir).chmod(0o000)
yield tmpdir
os.chmod(tmpdir, 0o777)
Path(tmpdir).chmod(0o777)


@pytest.fixture(scope="session")
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ def copytree(
source_path = os.path.join(src, name)
destination_path = os.path.join(dst, name)
try:
if symlinks and os.path.islink(source_path):
source_link = os.readlink(source_path)
if symlinks and Path(source_path).is_symlink():
source_link = Path(source_path).readlink()
os.symlink(source_link, destination_path)
elif os.path.isdir(source_path):
elif Path(source_path).is_dir():
copytree(
source_path,
destination_path,
Expand Down

0 comments on commit b15e552

Please sign in to comment.