Skip to content

Commit 5dcfeb6

Browse files
committed
base: Drop python-magic usage, just check file extension
python-magic is overkill, bugzilla attachment autodetect doesn't even inspect files and just checks file extensions. Simplify by switching to that method. Plus it's a bit of a pain to manage the conditional dependency
1 parent 584e191 commit 5dcfeb6

3 files changed

Lines changed: 8 additions & 35 deletions

File tree

bugzilla/base.py

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import getpass
1414
import locale
1515
from logging import getLogger
16+
import mimetypes
1617
import os
1718
import sys
1819

@@ -40,33 +41,6 @@
4041

4142
log = getLogger(__name__)
4243

43-
mimemagic = None
44-
45-
46-
def _detect_filetype(fname):
47-
global mimemagic
48-
49-
if mimemagic is None:
50-
try:
51-
# pylint: disable=import-error
52-
import magic
53-
mimemagic = magic.open(getattr(magic, "MAGIC_MIME_TYPE", 16))
54-
mimemagic.load()
55-
except ImportError as e:
56-
log.debug("Could not load python-magic: %s", e)
57-
mimemagic = None
58-
if not mimemagic:
59-
return None
60-
61-
if not os.path.isabs(fname):
62-
return None
63-
64-
try:
65-
return mimemagic.file(fname)
66-
except Exception as e:
67-
log.debug("Could not detect content_type: %s", e)
68-
return None
69-
7044

7145
def _nested_update(d, u):
7246
# Helper for nested dict update()
@@ -1556,10 +1530,11 @@ def attachfile(self, idlist, attachfile, description, **kwargs):
15561530
if 'file_name' not in kwargs and hasattr(f, "name"):
15571531
kwargs['file_name'] = os.path.basename(f.name)
15581532
if 'content_type' not in kwargs:
1559-
ctype = _detect_filetype(getattr(f, "name", None))
1560-
if not ctype:
1561-
ctype = 'application/octet-stream'
1562-
kwargs['content_type'] = ctype
1533+
ctype = None
1534+
if kwargs['file_name']:
1535+
ctype = mimetypes.guess_type(
1536+
kwargs['file_name'], strict=False)[0]
1537+
kwargs['content_type'] = ctype or 'application/octet-stream'
15631538

15641539
ret = self._proxy.Bug.add_attachment(kwargs)
15651540

python-bugzilla.spec

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ over XML-RPC.\
4949
%package -n python2-bugzilla
5050
Summary: %summary
5151
Requires: python2-requests
52-
Requires: python2-magic
5352
# This dep is for back compat, so that installing python-bugzilla continues
5453
# to give the cli tool
5554
Requires: python-bugzilla-cli
@@ -64,7 +63,6 @@ Requires: python-bugzilla-cli
6463
%package -n python3-bugzilla
6564
Summary: %summary
6665
Requires: python3-requests
67-
Requires: python3-magic
6866
%{?python_provide:%python_provide python3-bugzilla}
6967

7068
%if %{without python2}

tests/test_rw_functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,12 @@ def _test8Attachments(self):
530530
attachid = att1["id"]
531531
assert att1["summary"] == desc1
532532
assert att1["id"] == int(out1.splitlines()[2].split()[2])
533-
assert att1["content_type"] == "application/octet-stream"
533+
assert att1["content_type"] == "text/plain"
534534

535535
att2 = setbug.attachments[-1]
536536
assert att2["summary"] == desc2
537537
assert att2["id"] == int(out2.splitlines()[2].split()[2])
538-
assert att2["content_type"] == "text/x-diff"
538+
assert att2["content_type"] == "application/octet-stream"
539539

540540
# Set attachment flags
541541
assert att1["flags"] == []

0 commit comments

Comments
 (0)