From cb2d61583dd37318f561e0672a386c0e7422790b Mon Sep 17 00:00:00 2001 From: Matej Matuska Date: Thu, 11 Jul 2024 15:31:24 +0200 Subject: [PATCH] Replace deprecated shutil.rmtree onerror kwarg 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. --- .../system_upgrade/common/libraries/overlaygen.py | 6 +++++- repos/system_upgrade/common/libraries/utils.py | 15 +++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/repos/system_upgrade/common/libraries/overlaygen.py b/repos/system_upgrade/common/libraries/overlaygen.py index 1132cde1ce..98b9d43d80 100644 --- a/repos/system_upgrade/common/libraries/overlaygen.py +++ b/repos/system_upgrade/common/libraries/overlaygen.py @@ -1,6 +1,7 @@ import contextlib import os import shutil +import sys from collections import namedtuple from leapp.exceptions import StopActorExecutionError @@ -343,7 +344,10 @@ 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): + shutil.rmtree(scratch_dir, onexc=utils.report_and_ignore_shutil_rmtree_error) + else: + shutil.rmtree(scratch_dir, onerror=utils.report_and_ignore_shutil_rmtree_error) api.current_logger().debug('Recursively removed scratch directory %s.', scratch_dir) diff --git a/repos/system_upgrade/common/libraries/utils.py b/repos/system_upgrade/common/libraries/utils.py index 38b9bb1a53..b7aa9c743d 100644 --- a/repos/system_upgrade/common/libraries/utils.py +++ b/repos/system_upgrade/common/libraries/utils.py @@ -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):