Add get_file_magic to tests/lib.py
Related: rhbz#1673744
This commit is contained in:
parent
1f716641cf
commit
d37cf8d0d5
15
tests/lib.py
15
tests/lib.py
@ -16,6 +16,7 @@
|
|||||||
#
|
#
|
||||||
import sys
|
import sys
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
import magic
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
@ -27,3 +28,17 @@ def captured_output():
|
|||||||
yield sys.stdout, sys.stderr
|
yield sys.stdout, sys.stderr
|
||||||
finally:
|
finally:
|
||||||
sys.stdout, sys.stderr = old_out, old_err
|
sys.stdout, sys.stderr = old_out, old_err
|
||||||
|
|
||||||
|
def get_file_magic(filename):
|
||||||
|
"""Get the file type details using libmagic
|
||||||
|
|
||||||
|
Returns "" on failure or a string containing the description of the file
|
||||||
|
"""
|
||||||
|
details = ""
|
||||||
|
try:
|
||||||
|
ms = magic.open(magic.NONE)
|
||||||
|
ms.load()
|
||||||
|
details = ms.file(filename)
|
||||||
|
finally:
|
||||||
|
ms.close()
|
||||||
|
return details
|
||||||
|
Loading…
Reference in New Issue
Block a user