Skip to content

Commit

Permalink
Use python-magic API when available
Browse files Browse the repository at this point in the history
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 #1083

[1] https://github.com/ahupp/python-magic/blob/master/COMPAT.md
  • Loading branch information
danigm committed Aug 3, 2023
1 parent fa98be1 commit c6ff3e5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rpmlint/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ''


Expand Down

0 comments on commit c6ff3e5

Please sign in to comment.