From c6ff3e5513a9b525ac8ec926eaf123026dc102c4 Mon Sep 17 00:00:00 2001 From: Daniel Garcia Moreno Date: Thu, 3 Aug 2023 11:47:08 +0200 Subject: [PATCH] Use python-magic API when available The usage of the python-magic -> libmagic API comatibility [1] raises a deprecation warning. This patch uses the python-magic default API when it's available and in other case it uses the libmagic default API. Fix https://github.com/rpm-software-management/rpmlint/issues/1083 [1] https://github.com/ahupp/python-magic/blob/master/COMPAT.md --- rpmlint/pkg.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/rpmlint/pkg.py b/rpmlint/pkg.py index cb5f93fe1..092199e8d 100644 --- a/rpmlint/pkg.py +++ b/rpmlint/pkg.py @@ -354,10 +354,25 @@ def parse_deps(line): return prcos +def _get_magic_libmagic(path): + return magic.detect_from_filename(path).name + + +def _get_magic_python_magic(path): + return magic.from_file(path) + + def get_magic(path): + # python-magic & libmagic compatibility code + # https://github.com/ahupp/python-magic/blob/master/COMPAT.md + detect_magic = _get_magic_python_magic + if not hasattr(magic, 'from_file'): + # libmagic python bindings + detect_magic = _get_magic_libmagic + try: - return magic.detect_from_filename(path).name - except ValueError: + return detect_magic(path) + except (ValueError, FileNotFoundError): return ''