2011-08-16 03:36:04 -04:00
|
|
|
"""
|
2016-11-13 01:12:58 -05:00
|
|
|
Adam Hupp (adam@hupp.org)
|
|
|
|
http://github.com/ahupp/python-magic
|
|
|
|
|
2011-08-16 03:36:04 -04:00
|
|
|
magic is a wrapper around the libmagic file identification library.
|
|
|
|
|
|
|
|
See README for more information.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
>>> import magic
|
|
|
|
>>> magic.from_file("testdata/test.pdf")
|
|
|
|
'PDF document, version 1.2'
|
|
|
|
>>> magic.from_file("testdata/test.pdf", mime=True)
|
|
|
|
'application/pdf'
|
|
|
|
>>> magic.from_buffer(open("testdata/test.pdf").read(1024))
|
|
|
|
'PDF document, version 1.2'
|
|
|
|
>>>
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
import ctypes
|
|
|
|
import ctypes.util
|
|
|
|
|
|
|
|
from ctypes import c_char_p, c_int, c_size_t, c_void_p
|
|
|
|
|
|
|
|
class MagicException(Exception): pass
|
|
|
|
|
|
|
|
class Magic:
|
|
|
|
"""
|
|
|
|
Magic is a wrapper around the libmagic C library.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
def __init__(self, mime=False, magic_file=None, mime_encoding=False):
|
2011-08-16 03:36:04 -04:00
|
|
|
"""
|
|
|
|
Create a new libmagic wrapper.
|
|
|
|
|
|
|
|
mime - if True, mimetypes are returned instead of textual descriptions
|
|
|
|
mime_encoding - if True, codec is returned
|
|
|
|
magic_file - use a mime database other than the system default
|
2016-11-13 01:12:58 -05:00
|
|
|
|
2011-08-16 03:36:04 -04:00
|
|
|
"""
|
2016-11-13 01:12:58 -05:00
|
|
|
flags = MAGIC_NONE
|
2011-08-16 03:36:04 -04:00
|
|
|
if mime:
|
2016-11-13 01:12:58 -05:00
|
|
|
flags |= MAGIC_MIME
|
2011-08-16 03:36:04 -04:00
|
|
|
elif mime_encoding:
|
2016-11-13 01:12:58 -05:00
|
|
|
flags |= MAGIC_MIME_ENCODING
|
2011-08-16 03:36:04 -04:00
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
self.cookie = magic_open(flags)
|
2011-08-16 03:36:04 -04:00
|
|
|
|
|
|
|
magic_load(self.cookie, magic_file)
|
|
|
|
|
|
|
|
|
|
|
|
def from_buffer(self, buf):
|
|
|
|
"""
|
|
|
|
Identify the contents of `buf`
|
|
|
|
"""
|
2016-11-13 01:12:58 -05:00
|
|
|
return magic_buffer(self.cookie, buf)
|
2011-08-16 03:36:04 -04:00
|
|
|
|
|
|
|
def from_file(self, filename):
|
|
|
|
"""
|
|
|
|
Identify the contents of file `filename`
|
|
|
|
raises IOError if the file does not exist
|
|
|
|
"""
|
2016-11-13 01:12:58 -05:00
|
|
|
|
2011-08-16 03:36:04 -04:00
|
|
|
if not os.path.exists(filename):
|
|
|
|
raise IOError("File does not exist: " + filename)
|
2014-10-29 03:57:49 -04:00
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
return magic_file(self.cookie, filename)
|
2014-10-29 03:57:49 -04:00
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
def __del__(self):
|
|
|
|
if self.cookie:
|
2011-08-16 03:36:04 -04:00
|
|
|
magic_close(self.cookie)
|
|
|
|
self.cookie = None
|
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
_magic_mime = None
|
|
|
|
_magic = None
|
|
|
|
|
|
|
|
def _get_magic_mime():
|
|
|
|
global _magic_mime
|
|
|
|
if not _magic_mime:
|
|
|
|
_magic_mime = Magic(mime=True)
|
|
|
|
return _magic_mime
|
2011-08-16 03:36:04 -04:00
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
def _get_magic():
|
|
|
|
global _magic
|
|
|
|
if not _magic:
|
|
|
|
_magic = Magic()
|
|
|
|
return _magic
|
2011-08-16 03:36:04 -04:00
|
|
|
|
|
|
|
def _get_magic_type(mime):
|
2016-11-13 01:12:58 -05:00
|
|
|
if mime:
|
|
|
|
return _get_magic_mime()
|
|
|
|
else:
|
|
|
|
return _get_magic()
|
2011-08-16 03:36:04 -04:00
|
|
|
|
|
|
|
def from_file(filename, mime=False):
|
|
|
|
m = _get_magic_type(mime)
|
|
|
|
return m.from_file(filename)
|
|
|
|
|
|
|
|
def from_buffer(buffer, mime=False):
|
|
|
|
m = _get_magic_type(mime)
|
|
|
|
return m.from_buffer(buffer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
libmagic = None
|
|
|
|
# Let's try to find magic or magic1
|
2016-11-13 01:12:58 -05:00
|
|
|
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')
|
2011-08-16 03:36:04 -04:00
|
|
|
|
|
|
|
# This is necessary because find_library returns None if it doesn't find the library
|
|
|
|
if dll:
|
|
|
|
libmagic = ctypes.CDLL(dll)
|
|
|
|
|
|
|
|
if not libmagic or not libmagic._name:
|
2016-11-13 01:12:58 -05:00
|
|
|
import sys
|
|
|
|
platform_to_lib = {'darwin': '/opt/local/lib/libmagic.dylib',
|
|
|
|
'win32': 'magic1.dll'}
|
|
|
|
if sys.platform in platform_to_lib:
|
2011-08-16 03:36:04 -04:00
|
|
|
try:
|
2016-11-13 01:12:58 -05:00
|
|
|
libmagic = ctypes.CDLL(platform_to_lib[sys.platform])
|
2011-08-16 03:36:04 -04:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not libmagic or not libmagic._name:
|
|
|
|
# It is better to raise an ImportError since we are importing magic module
|
|
|
|
raise ImportError('failed to find libmagic. Check your installation')
|
|
|
|
|
|
|
|
magic_t = ctypes.c_void_p
|
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
def errorcheck(result, func, args):
|
|
|
|
err = magic_error(args[0])
|
|
|
|
if err is not None:
|
2011-08-16 03:36:04 -04:00
|
|
|
raise MagicException(err)
|
|
|
|
else:
|
|
|
|
return result
|
|
|
|
|
|
|
|
magic_open = libmagic.magic_open
|
|
|
|
magic_open.restype = magic_t
|
|
|
|
magic_open.argtypes = [c_int]
|
|
|
|
|
|
|
|
magic_close = libmagic.magic_close
|
|
|
|
magic_close.restype = None
|
|
|
|
magic_close.argtypes = [magic_t]
|
|
|
|
|
|
|
|
magic_error = libmagic.magic_error
|
|
|
|
magic_error.restype = c_char_p
|
|
|
|
magic_error.argtypes = [magic_t]
|
|
|
|
|
|
|
|
magic_errno = libmagic.magic_errno
|
|
|
|
magic_errno.restype = c_int
|
|
|
|
magic_errno.argtypes = [magic_t]
|
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
magic_file = libmagic.magic_file
|
|
|
|
magic_file.restype = c_char_p
|
|
|
|
magic_file.argtypes = [magic_t, c_char_p]
|
|
|
|
magic_file.errcheck = errorcheck
|
2011-08-16 03:36:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
_magic_buffer = libmagic.magic_buffer
|
|
|
|
_magic_buffer.restype = c_char_p
|
|
|
|
_magic_buffer.argtypes = [magic_t, c_void_p, c_size_t]
|
2016-11-13 01:12:58 -05:00
|
|
|
_magic_buffer.errcheck = errorcheck
|
|
|
|
|
2011-08-16 03:36:04 -04:00
|
|
|
|
|
|
|
def magic_buffer(cookie, buf):
|
|
|
|
return _magic_buffer(cookie, buf, len(buf))
|
|
|
|
|
|
|
|
|
2016-11-13 01:12:58 -05:00
|
|
|
magic_load = libmagic.magic_load
|
|
|
|
magic_load.restype = c_int
|
|
|
|
magic_load.argtypes = [magic_t, c_char_p]
|
|
|
|
magic_load.errcheck = errorcheck
|
2011-08-16 03:36:04 -04:00
|
|
|
|
|
|
|
magic_setflags = libmagic.magic_setflags
|
|
|
|
magic_setflags.restype = c_int
|
|
|
|
magic_setflags.argtypes = [magic_t, c_int]
|
|
|
|
|
|
|
|
magic_check = libmagic.magic_check
|
|
|
|
magic_check.restype = c_int
|
|
|
|
magic_check.argtypes = [magic_t, c_char_p]
|
|
|
|
|
|
|
|
magic_compile = libmagic.magic_compile
|
|
|
|
magic_compile.restype = c_int
|
|
|
|
magic_compile.argtypes = [magic_t, c_char_p]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MAGIC_NONE = 0x000000 # No flags
|
|
|
|
|
|
|
|
MAGIC_DEBUG = 0x000001 # Turn on debugging
|
|
|
|
|
|
|
|
MAGIC_SYMLINK = 0x000002 # Follow symlinks
|
|
|
|
|
|
|
|
MAGIC_COMPRESS = 0x000004 # Check inside compressed files
|
|
|
|
|
|
|
|
MAGIC_DEVICES = 0x000008 # Look at the contents of devices
|
|
|
|
|
|
|
|
MAGIC_MIME = 0x000010 # Return a mime string
|
|
|
|
|
|
|
|
MAGIC_MIME_ENCODING = 0x000400 # Return the MIME encoding
|
|
|
|
|
|
|
|
MAGIC_CONTINUE = 0x000020 # Return all matches
|
|
|
|
|
|
|
|
MAGIC_CHECK = 0x000040 # Print warnings to stderr
|
|
|
|
|
|
|
|
MAGIC_PRESERVE_ATIME = 0x000080 # Restore access time on exit
|
|
|
|
|
|
|
|
MAGIC_RAW = 0x000100 # Don't translate unprintable chars
|
|
|
|
|
|
|
|
MAGIC_ERROR = 0x000200 # Handle ENOENT etc as real errors
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_COMPRESS = 0x001000 # Don't check for compressed files
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_TAR = 0x002000 # Don't check for tar files
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_SOFT = 0x004000 # Don't check magic entries
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_APPTYPE = 0x008000 # Don't check application type
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_ELF = 0x010000 # Don't check for elf details
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_ASCII = 0x020000 # Don't check for ascii files
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran
|
|
|
|
|
|
|
|
MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens
|