scanner: replace deprecated imghdr with libmagic/puremagic

libmagic's Python bindings detect more types, puremagic is pure Python.

imghdr was dropped in Python 3.13.

This reverts commit 3bc246ccad.
This commit is contained in:
Jochen Sprickerhof 2024-10-07 20:50:37 +02:00 committed by Hans-Christoph Steiner
parent 432618eb03
commit 8a5359ab3f
2 changed files with 12 additions and 3 deletions

View file

@ -16,7 +16,6 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import imghdr
import itertools import itertools
import json import json
import logging import logging
@ -35,6 +34,11 @@ from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from typing import Union from typing import Union
try:
import magic
except ImportError:
import puremagic as magic
if sys.version_info >= (3, 11): if sys.version_info >= (3, 11):
import tomllib import tomllib
else: else:
@ -895,8 +899,12 @@ def scan_source(build_dir, build=metadata.Build(), json_per_build=None):
] ]
def is_image_file(path): def is_image_file(path):
if imghdr.what(path) is not None: try:
mimetype = magic.from_file(path, mime=True)
if mimetype and mimetype.startswith('image/'):
return True return True
except Exception as e:
logging.info(e)
def safe_path(path_in_build_dir): def safe_path(path_in_build_dir):
for sp in safe_paths: for sp in safe_paths:

View file

@ -100,6 +100,7 @@ setup(
'paramiko', 'paramiko',
'Pillow', 'Pillow',
'apache-libcloud >= 0.14.1', 'apache-libcloud >= 0.14.1',
'puremagic',
'python-vagrant', 'python-vagrant',
'PyYAML', 'PyYAML',
'qrcode', 'qrcode',