3 Python bindings for libmagic
9 from ctypes
.util
import find_library
13 Loads the shared library through ctypes and returns a library
14 L{ctypes.CDLL} instance
16 return ctypes
.cdll
.LoadLibrary(find_library('magic'))
19 _libraries
['magic'] = _init()
21 # Flag constants for open and setflags
23 MAGIC_DEBUG
= DEBUG
= 1
24 MAGIC_SYMLINK
= SYMLINK
= 2
25 MAGIC_COMPRESS
= COMPRESS
= 4
26 MAGIC_DEVICES
= DEVICES
= 8
27 MAGIC_MIME_TYPE
= MIME_TYPE
= 16
28 MAGIC_CONTINUE
= CONTINUE
= 32
29 MAGIC_CHECK
= CHECK
= 64
30 MAGIC_PRESERVE_ATIME
= PRESERVE_ATIME
= 128
32 MAGIC_ERROR
= ERROR
= 512
33 MAGIC_MIME_ENCODING
= MIME_ENCODING
= 1024
34 MAGIC_MIME
= MIME
= 1040
35 MAGIC_APPLE
= APPLE
= 2048
37 MAGIC_NO_CHECK_COMPRESS
= NO_CHECK_COMPRESS
= 4096
38 MAGIC_NO_CHECK_TAR
= NO_CHECK_TAR
= 8192
39 MAGIC_NO_CHECK_SOFT
= NO_CHECK_SOFT
= 16384
40 MAGIC_NO_CHECK_APPTYPE
= NO_CHECK_APPTYPE
= 32768
41 MAGIC_NO_CHECK_ELF
= NO_CHECK_ELF
= 65536
42 MAGIC_NO_CHECK_TEXT
= NO_CHECK_TEXT
= 131072
43 MAGIC_NO_CHECK_CDF
= NO_CHECK_CDF
= 262144
44 MAGIC_NO_CHECK_TOKENS
= NO_CHECK_TOKENS
= 1048576
45 MAGIC_NO_CHECK_ENCODING
= NO_CHECK_ENCODING
= 2097152
47 MAGIC_NO_CHECK_BUILTIN
= NO_CHECK_BUILTIN
= 4173824
49 class magic_set(Structure
):
51 magic_set
._fields
_ = []
52 magic_t
= POINTER(magic_set
)
54 _open
= _libraries
['magic'].magic_open
55 _open
.restype
= magic_t
56 _open
.argtypes
= [c_int
]
58 _close
= _libraries
['magic'].magic_close
60 _close
.argtypes
= [magic_t
]
62 _file
= _libraries
['magic'].magic_file
63 _file
.restype
= c_char_p
64 _file
.argtypes
= [magic_t
, c_char_p
]
66 _descriptor
= _libraries
['magic'].magic_descriptor
67 _descriptor
.restype
= c_char_p
68 _descriptor
.argtypes
= [magic_t
, c_int
]
70 _buffer
= _libraries
['magic'].magic_buffer
71 _buffer
.restype
= c_char_p
72 _buffer
.argtypes
= [magic_t
, c_void_p
, c_size_t
]
74 _error
= _libraries
['magic'].magic_error
75 _error
.restype
= c_char_p
76 _error
.argtypes
= [magic_t
]
78 _setflags
= _libraries
['magic'].magic_setflags
79 _setflags
.restype
= c_int
80 _setflags
.argtypes
= [magic_t
, c_int
]
82 _load
= _libraries
['magic'].magic_load
84 _load
.argtypes
= [magic_t
, c_char_p
]
86 _compile
= _libraries
['magic'].magic_compile
87 _compile
.restype
= c_int
88 _compile
.argtypes
= [magic_t
, c_char_p
]
90 _check
= _libraries
['magic'].magic_check
91 _check
.restype
= c_int
92 _check
.argtypes
= [magic_t
, c_char_p
]
94 _list
= _libraries
['magic'].magic_list
96 _list
.argtypes
= [magic_t
, c_char_p
]
98 _errno
= _libraries
['magic'].magic_errno
99 _errno
.restype
= c_int
100 _errno
.argtypes
= [magic_t
]
103 def __init__(self
, ms
):
108 Closes the magic database and deallocates any resources used.
110 _close(self
._magic
_t
)
112 def file(self
, file):
114 Returns a textual description of the contents of the argument passed
115 as a filename or None if an error occurred and the MAGIC_ERROR flag
116 is set. A call to errno() will return the numeric error code.
118 return _file(self
._magic
_t
, file)
120 def descriptor(self
, fd
):
122 Like the file method, but the argument is a file descriptor.
124 return _descriptor(self
._magic
_t
, fd
)
126 def buffer(self
, buf
):
128 Returns a textual description of the contents of the argument passed
129 as a buffer or None if an error occurred and the MAGIC_ERROR flag
130 is set. A call to errno() will return the numeric error code.
132 return _buffer(self
._magic
_t
, buf
, len(buf
))
136 Returns a textual explanation of the last error or None
137 if there was no error.
139 return _error(self
._magic
_t
)
141 def setflags(self
, flags
):
143 Set flags on the magic object which determine how magic checking behaves;
144 a bitwise OR of the flags described in libmagic(3), but without the MAGIC_
147 Returns -1 on systems that don't support utime(2) or utimes(2)
148 when PRESERVE_ATIME is set.
150 return _setflags(self
._magic
_t
, flags
)
152 def load(self
, file=None):
154 Must be called to load entries in the colon separated list of database files
155 passed as argument or the default database file if no argument before
156 any magic queries can be performed.
158 Returns 0 on success and -1 on failure.
160 return _load(self
._magic
_t
, file)
162 def compile(self
, dbs
):
164 Compile entries in the colon separated list of database files
165 passed as argument or the default database file if no argument.
166 Returns 0 on success and -1 on failure.
167 The compiled files created are named from the basename(1) of each file
168 argument with ".mgc" appended to it.
170 return _compile(self
._magic
_t
, dbs
)
172 def check(self
, dbs
):
174 Check the validity of entries in the colon separated list of
175 database files passed as argument or the default database file
177 Returns 0 on success and -1 on failure.
179 return _check(self
._magic
_t
, dbs
)
183 Check the validity of entries in the colon separated list of
184 database files passed as argument or the default database file
186 Returns 0 on success and -1 on failure.
188 return _list(self
._magic
_t
, dbs
)
192 Returns a numeric error code. If return value is 0, an internal
193 magic error occurred. If return value is non-zero, the value is
194 an OS error code. Use the errno module or os.strerror() can be used
195 to provide detailed error information.
197 return _errno(self
._magic
_t
)
201 Returns a magic object on success and None on failure.
202 Flags argument as for setflags.
204 return Magic(_open(flags
))