From d37cf8d0d5564a0b225bebfa40da16b6402c4f22 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 20 Feb 2019 14:14:12 -0800 Subject: [PATCH] Add get_file_magic to tests/lib.py Related: rhbz#1673744 --- tests/lib.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/lib.py b/tests/lib.py index cadb86fe..44155712 100644 --- a/tests/lib.py +++ b/tests/lib.py @@ -16,6 +16,7 @@ # import sys from contextlib import contextmanager +import magic from io import StringIO @contextmanager @@ -27,3 +28,17 @@ def captured_output(): yield sys.stdout, sys.stderr finally: 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