magic.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # coding: utf-8
  2. '''
  3. Python bindings for libmagic
  4. '''
  5. import ctypes
  6. from collections import namedtuple
  7. from ctypes import *
  8. from ctypes.util import find_library
  9. def _init():
  10. """
  11. Loads the shared library through ctypes and returns a library
  12. L{ctypes.CDLL} instance
  13. """
  14. return ctypes.cdll.LoadLibrary(find_library('magic'))
  15. _libraries = {}
  16. _libraries['magic'] = _init()
  17. # Flag constants for open and setflags
  18. MAGIC_NONE = NONE = 0
  19. MAGIC_DEBUG = DEBUG = 1
  20. MAGIC_SYMLINK = SYMLINK = 2
  21. MAGIC_COMPRESS = COMPRESS = 4
  22. MAGIC_DEVICES = DEVICES = 8
  23. MAGIC_MIME_TYPE = MIME_TYPE = 16
  24. MAGIC_CONTINUE = CONTINUE = 32
  25. MAGIC_CHECK = CHECK = 64
  26. MAGIC_PRESERVE_ATIME = PRESERVE_ATIME = 128
  27. MAGIC_RAW = RAW = 256
  28. MAGIC_ERROR = ERROR = 512
  29. MAGIC_MIME_ENCODING = MIME_ENCODING = 1024
  30. MAGIC_MIME = MIME = 1040 # MIME_TYPE + MIME_ENCODING
  31. MAGIC_APPLE = APPLE = 2048
  32. MAGIC_NO_CHECK_COMPRESS = NO_CHECK_COMPRESS = 4096
  33. MAGIC_NO_CHECK_TAR = NO_CHECK_TAR = 8192
  34. MAGIC_NO_CHECK_SOFT = NO_CHECK_SOFT = 16384
  35. MAGIC_NO_CHECK_APPTYPE = NO_CHECK_APPTYPE = 32768
  36. MAGIC_NO_CHECK_ELF = NO_CHECK_ELF = 65536
  37. MAGIC_NO_CHECK_TEXT = NO_CHECK_TEXT = 131072
  38. MAGIC_NO_CHECK_CDF = NO_CHECK_CDF = 262144
  39. MAGIC_NO_CHECK_TOKENS = NO_CHECK_TOKENS = 1048576
  40. MAGIC_NO_CHECK_ENCODING = NO_CHECK_ENCODING = 2097152
  41. MAGIC_NO_CHECK_BUILTIN = NO_CHECK_BUILTIN = 4173824
  42. FileMagic = namedtuple('FileMagic', ('mime_type', 'encoding', 'name'))
  43. class magic_set(Structure):
  44. pass
  45. magic_set._fields_ = []
  46. magic_t = POINTER(magic_set)
  47. _open = _libraries['magic'].magic_open
  48. _open.restype = magic_t
  49. _open.argtypes = [c_int]
  50. _close = _libraries['magic'].magic_close
  51. _close.restype = None
  52. _close.argtypes = [magic_t]
  53. _file = _libraries['magic'].magic_file
  54. _file.restype = c_char_p
  55. _file.argtypes = [magic_t, c_char_p]
  56. _descriptor = _libraries['magic'].magic_descriptor
  57. _descriptor.restype = c_char_p
  58. _descriptor.argtypes = [magic_t, c_int]
  59. _buffer = _libraries['magic'].magic_buffer
  60. _buffer.restype = c_char_p
  61. _buffer.argtypes = [magic_t, c_void_p, c_size_t]
  62. _error = _libraries['magic'].magic_error
  63. _error.restype = c_char_p
  64. _error.argtypes = [magic_t]
  65. _setflags = _libraries['magic'].magic_setflags
  66. _setflags.restype = c_int
  67. _setflags.argtypes = [magic_t, c_int]
  68. _load = _libraries['magic'].magic_load
  69. _load.restype = c_int
  70. _load.argtypes = [magic_t, c_char_p]
  71. _compile = _libraries['magic'].magic_compile
  72. _compile.restype = c_int
  73. _compile.argtypes = [magic_t, c_char_p]
  74. _check = _libraries['magic'].magic_check
  75. _check.restype = c_int
  76. _check.argtypes = [magic_t, c_char_p]
  77. _list = _libraries['magic'].magic_list
  78. _list.restype = c_int
  79. _list.argtypes = [magic_t, c_char_p]
  80. _errno = _libraries['magic'].magic_errno
  81. _errno.restype = c_int
  82. _errno.argtypes = [magic_t]
  83. class Magic(object):
  84. def __init__(self, ms):
  85. self._magic_t = ms
  86. def close(self):
  87. """
  88. Closes the magic database and deallocates any resources used.
  89. """
  90. _close(self._magic_t)
  91. def file(self, filename):
  92. """
  93. Returns a textual description of the contents of the argument passed
  94. as a filename or None if an error occurred and the MAGIC_ERROR flag
  95. is set. A call to errno() will return the numeric error code.
  96. """
  97. if isinstance(filename, bytes):
  98. bi = filename
  99. else:
  100. try: # keep Python 2 compatibility
  101. bi = bytes(filename, 'utf-8')
  102. except TypeError:
  103. bi = bytes(filename)
  104. r = _file(self._magic_t, bi)
  105. if isinstance(r, str):
  106. return r
  107. else:
  108. return str(r, 'utf-8')
  109. def descriptor(self, fd):
  110. """
  111. Like the file method, but the argument is a file descriptor.
  112. """
  113. return _descriptor(self._magic_t, fd)
  114. def buffer(self, buf):
  115. """
  116. Returns a textual description of the contents of the argument passed
  117. as a buffer or None if an error occurred and the MAGIC_ERROR flag
  118. is set. A call to errno() will return the numeric error code.
  119. """
  120. r = _buffer(self._magic_t, buf, len(buf))
  121. if isinstance(r, str):
  122. return r
  123. else:
  124. return str(r, 'utf-8')
  125. def error(self):
  126. """
  127. Returns a textual explanation of the last error or None
  128. if there was no error.
  129. """
  130. e = _error(self._magic_t)
  131. if isinstance(e, str):
  132. return e
  133. else:
  134. return str(e, 'utf-8')
  135. def setflags(self, flags):
  136. """
  137. Set flags on the magic object which determine how magic checking
  138. behaves; a bitwise OR of the flags described in libmagic(3), but
  139. without the MAGIC_ prefix.
  140. Returns -1 on systems that don't support utime(2) or utimes(2)
  141. when PRESERVE_ATIME is set.
  142. """
  143. return _setflags(self._magic_t, flags)
  144. def load(self, filename=None):
  145. """
  146. Must be called to load entries in the colon separated list of database
  147. files passed as argument or the default database file if no argument
  148. before any magic queries can be performed.
  149. Returns 0 on success and -1 on failure.
  150. """
  151. return _load(self._magic_t, filename)
  152. def compile(self, dbs):
  153. """
  154. Compile entries in the colon separated list of database files
  155. passed as argument or the default database file if no argument.
  156. Returns 0 on success and -1 on failure.
  157. The compiled files created are named from the basename(1) of each file
  158. argument with ".mgc" appended to it.
  159. """
  160. return _compile(self._magic_t, dbs)
  161. def check(self, dbs):
  162. """
  163. Check the validity of entries in the colon separated list of
  164. database files passed as argument or the default database file
  165. if no argument.
  166. Returns 0 on success and -1 on failure.
  167. """
  168. return _check(self._magic_t, dbs)
  169. def list(self, dbs):
  170. """
  171. Check the validity of entries in the colon separated list of
  172. database files passed as argument or the default database file
  173. if no argument.
  174. Returns 0 on success and -1 on failure.
  175. """
  176. return _list(self._magic_t, dbs)
  177. def errno(self):
  178. """
  179. Returns a numeric error code. If return value is 0, an internal
  180. magic error occurred. If return value is non-zero, the value is
  181. an OS error code. Use the errno module or os.strerror() can be used
  182. to provide detailed error information.
  183. """
  184. return _errno(self._magic_t)
  185. def open(flags):
  186. """
  187. Returns a magic object on success and None on failure.
  188. Flags argument as for setflags.
  189. """
  190. return Magic(_open(flags))
  191. # Objects used by `detect_from_` functions
  192. mime_magic = Magic(_open(MAGIC_MIME))
  193. mime_magic.load()
  194. none_magic = Magic(_open(MAGIC_NONE))
  195. none_magic.load()
  196. def _create_filemagic(mime_detected, type_detected):
  197. mime_type, mime_encoding = mime_detected.split('; ')
  198. return FileMagic(name=type_detected, mime_type=mime_type,
  199. encoding=mime_encoding.replace('charset=', ''))
  200. def detect_from_filename(filename):
  201. '''Detect mime type, encoding and file type from a filename
  202. Returns a `FileMagic` namedtuple.
  203. '''
  204. return _create_filemagic(mime_magic.file(filename),
  205. none_magic.file(filename))
  206. def detect_from_fobj(fobj):
  207. '''Detect mime type, encoding and file type from file-like object
  208. Returns a `FileMagic` namedtuple.
  209. '''
  210. file_descriptor = fobj.fileno()
  211. return _create_filemagic(mime_magic.descriptor(file_descriptor),
  212. none_magic.descriptor(file_descriptor))
  213. def detect_from_content(byte_content):
  214. '''Detect mime type, encoding and file type from bytes
  215. Returns a `FileMagic` namedtuple.
  216. '''
  217. return _create_filemagic(mime_magic.buffer(byte_content),
  218. none_magic.buffer(byte_content))