Skip to content

Commit

Permalink
Replace deprecated shutil.rmtree onerror kwarg
Browse files Browse the repository at this point in the history
The `onerror` argument of shutil.rmtree is deprecated and replaced by
`onexc` since Python 3.12. It accepts callback with the same arguments
except for the last one which is now a subclass of `BaseException`. Our
callback `libraries.utils.report_and_ignore_shutil_rmtree_error` is thus
modified accordingly.
  • Loading branch information
matejmatuska authored and Rezney committed Jul 18, 2024
1 parent 1176441 commit 3f7377b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 7 additions & 1 deletion repos/system_upgrade/common/libraries/overlaygen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import os
import shutil
import sys
from collections import namedtuple

from leapp.exceptions import StopActorExecutionError
Expand Down Expand Up @@ -343,7 +344,12 @@ def cleanup_scratch(scratch_dir, mounts_dir):
# NOTE(pstodulk): From time to time, it helps me with some experiments
return
api.current_logger().debug('Recursively removing scratch directory %s.', scratch_dir)
shutil.rmtree(scratch_dir, onerror=utils.report_and_ignore_shutil_rmtree_error)
if sys.version_info >= (3, 12):
# NOTE(mmatuska): The pylint suppressions are required because of a bug in pylint:
# (https://github.com/pylint-dev/pylint/issues/9622)
shutil.rmtree(scratch_dir, onexc=utils.report_and_ignore_shutil_rmtree_error) # noqa: E501; pylint: disable=unexpected-keyword-arg
else:
shutil.rmtree(scratch_dir, onerror=utils.report_and_ignore_shutil_rmtree_error) # noqa: E501; pylint: disable=deprecated-argument
api.current_logger().debug('Recursively removed scratch directory %s.', scratch_dir)


Expand Down
15 changes: 11 additions & 4 deletions repos/system_upgrade/common/libraries/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,17 @@ def report_and_ignore_shutil_rmtree_error(func, path, exc_info):
"""
Helper function for shutil.rmtree to only report errors but don't fail.
"""
api.current_logger().warning(
'While trying to remove directories: %s failed at %s with an exception %s message: %s',
func.__name__, path, exc_info[0].__name__, exc_info[1]
)
# third parameter has changed in Python 3.12
if sys.version_info >= (3, 12):
api.current_logger().warning(
'While trying to remove directories: %s failed at %s with an exception %s message: %s',
func.__name__, path, exc_info[0].__name__, exc_info[1]
)
else:
api.current_logger().warning(
'While trying to remove directories: %s failed at %s with an exception %s message: %s',
func.__name__, path, type(exc_info).__name__, str(exc_info)
)


def call_with_oserror_handled(cmd):
Expand Down

0 comments on commit 3f7377b

Please sign in to comment.