xref: /minix3/external/bsd/file/dist/python/magic.py (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1835f6802SDirk Vogt#!/usr/bin/env python
2835f6802SDirk Vogt'''
3835f6802SDirk VogtPython bindings for libmagic
4835f6802SDirk Vogt'''
5835f6802SDirk Vogt
6835f6802SDirk Vogtimport ctypes
7835f6802SDirk Vogt
8835f6802SDirk Vogtfrom ctypes import *
9835f6802SDirk Vogtfrom ctypes.util import find_library
10835f6802SDirk Vogt
11*0a6a1f1dSLionel Sambuc
12835f6802SDirk Vogtdef _init():
13835f6802SDirk Vogt    """
14835f6802SDirk Vogt    Loads the shared library through ctypes and returns a library
15835f6802SDirk Vogt    L{ctypes.CDLL} instance
16835f6802SDirk Vogt    """
17835f6802SDirk Vogt    return ctypes.cdll.LoadLibrary(find_library('magic'))
18835f6802SDirk Vogt
19835f6802SDirk Vogt_libraries = {}
20835f6802SDirk Vogt_libraries['magic'] = _init()
21835f6802SDirk Vogt
22835f6802SDirk Vogt# Flag constants for open and setflags
23835f6802SDirk VogtMAGIC_NONE = NONE = 0
24835f6802SDirk VogtMAGIC_DEBUG = DEBUG = 1
25835f6802SDirk VogtMAGIC_SYMLINK = SYMLINK = 2
26835f6802SDirk VogtMAGIC_COMPRESS = COMPRESS = 4
27835f6802SDirk VogtMAGIC_DEVICES = DEVICES = 8
28835f6802SDirk VogtMAGIC_MIME_TYPE = MIME_TYPE = 16
29835f6802SDirk VogtMAGIC_CONTINUE = CONTINUE = 32
30835f6802SDirk VogtMAGIC_CHECK = CHECK = 64
31835f6802SDirk VogtMAGIC_PRESERVE_ATIME = PRESERVE_ATIME = 128
32835f6802SDirk VogtMAGIC_RAW = RAW = 256
33835f6802SDirk VogtMAGIC_ERROR = ERROR = 512
34835f6802SDirk VogtMAGIC_MIME_ENCODING = MIME_ENCODING = 1024
35835f6802SDirk VogtMAGIC_MIME = MIME = 1040
36835f6802SDirk VogtMAGIC_APPLE = APPLE = 2048
37835f6802SDirk Vogt
38835f6802SDirk VogtMAGIC_NO_CHECK_COMPRESS = NO_CHECK_COMPRESS = 4096
39835f6802SDirk VogtMAGIC_NO_CHECK_TAR = NO_CHECK_TAR = 8192
40835f6802SDirk VogtMAGIC_NO_CHECK_SOFT = NO_CHECK_SOFT = 16384
41835f6802SDirk VogtMAGIC_NO_CHECK_APPTYPE = NO_CHECK_APPTYPE = 32768
42835f6802SDirk VogtMAGIC_NO_CHECK_ELF = NO_CHECK_ELF = 65536
43835f6802SDirk VogtMAGIC_NO_CHECK_TEXT = NO_CHECK_TEXT = 131072
44835f6802SDirk VogtMAGIC_NO_CHECK_CDF = NO_CHECK_CDF = 262144
45835f6802SDirk VogtMAGIC_NO_CHECK_TOKENS = NO_CHECK_TOKENS = 1048576
46835f6802SDirk VogtMAGIC_NO_CHECK_ENCODING = NO_CHECK_ENCODING = 2097152
47835f6802SDirk Vogt
48835f6802SDirk VogtMAGIC_NO_CHECK_BUILTIN = NO_CHECK_BUILTIN = 4173824
49835f6802SDirk Vogt
50*0a6a1f1dSLionel Sambuc
51835f6802SDirk Vogtclass magic_set(Structure):
52835f6802SDirk Vogt    pass
53835f6802SDirk Vogtmagic_set._fields_ = []
54835f6802SDirk Vogtmagic_t = POINTER(magic_set)
55835f6802SDirk Vogt
56835f6802SDirk Vogt_open = _libraries['magic'].magic_open
57835f6802SDirk Vogt_open.restype = magic_t
58835f6802SDirk Vogt_open.argtypes = [c_int]
59835f6802SDirk Vogt
60835f6802SDirk Vogt_close = _libraries['magic'].magic_close
61835f6802SDirk Vogt_close.restype = None
62835f6802SDirk Vogt_close.argtypes = [magic_t]
63835f6802SDirk Vogt
64835f6802SDirk Vogt_file = _libraries['magic'].magic_file
65835f6802SDirk Vogt_file.restype = c_char_p
66835f6802SDirk Vogt_file.argtypes = [magic_t, c_char_p]
67835f6802SDirk Vogt
68835f6802SDirk Vogt_descriptor = _libraries['magic'].magic_descriptor
69835f6802SDirk Vogt_descriptor.restype = c_char_p
70835f6802SDirk Vogt_descriptor.argtypes = [magic_t, c_int]
71835f6802SDirk Vogt
72835f6802SDirk Vogt_buffer = _libraries['magic'].magic_buffer
73835f6802SDirk Vogt_buffer.restype = c_char_p
74835f6802SDirk Vogt_buffer.argtypes = [magic_t, c_void_p, c_size_t]
75835f6802SDirk Vogt
76835f6802SDirk Vogt_error = _libraries['magic'].magic_error
77835f6802SDirk Vogt_error.restype = c_char_p
78835f6802SDirk Vogt_error.argtypes = [magic_t]
79835f6802SDirk Vogt
80835f6802SDirk Vogt_setflags = _libraries['magic'].magic_setflags
81835f6802SDirk Vogt_setflags.restype = c_int
82835f6802SDirk Vogt_setflags.argtypes = [magic_t, c_int]
83835f6802SDirk Vogt
84835f6802SDirk Vogt_load = _libraries['magic'].magic_load
85835f6802SDirk Vogt_load.restype = c_int
86835f6802SDirk Vogt_load.argtypes = [magic_t, c_char_p]
87835f6802SDirk Vogt
88835f6802SDirk Vogt_compile = _libraries['magic'].magic_compile
89835f6802SDirk Vogt_compile.restype = c_int
90835f6802SDirk Vogt_compile.argtypes = [magic_t, c_char_p]
91835f6802SDirk Vogt
92835f6802SDirk Vogt_check = _libraries['magic'].magic_check
93835f6802SDirk Vogt_check.restype = c_int
94835f6802SDirk Vogt_check.argtypes = [magic_t, c_char_p]
95835f6802SDirk Vogt
96835f6802SDirk Vogt_list = _libraries['magic'].magic_list
97835f6802SDirk Vogt_list.restype = c_int
98835f6802SDirk Vogt_list.argtypes = [magic_t, c_char_p]
99835f6802SDirk Vogt
100835f6802SDirk Vogt_errno = _libraries['magic'].magic_errno
101835f6802SDirk Vogt_errno.restype = c_int
102835f6802SDirk Vogt_errno.argtypes = [magic_t]
103835f6802SDirk Vogt
104*0a6a1f1dSLionel Sambuc
105835f6802SDirk Vogtclass Magic(object):
106835f6802SDirk Vogt    def __init__(self, ms):
107835f6802SDirk Vogt        self._magic_t = ms
108835f6802SDirk Vogt
109835f6802SDirk Vogt    def close(self):
110835f6802SDirk Vogt        """
111835f6802SDirk Vogt        Closes the magic database and deallocates any resources used.
112835f6802SDirk Vogt        """
113835f6802SDirk Vogt        _close(self._magic_t)
114835f6802SDirk Vogt
11508ff44c4SLionel Sambuc    def file(self, filename):
116835f6802SDirk Vogt        """
117835f6802SDirk Vogt        Returns a textual description of the contents of the argument passed
118835f6802SDirk Vogt        as a filename or None if an error occurred and the MAGIC_ERROR flag
119835f6802SDirk Vogt        is set.  A call to errno() will return the numeric error code.
120835f6802SDirk Vogt        """
12108ff44c4SLionel Sambuc        try:  # attempt python3 approach first
122*0a6a1f1dSLionel Sambuc            if isinstance(filename, bytes):
123*0a6a1f1dSLionel Sambuc                bi = filename
124*0a6a1f1dSLionel Sambuc            else:
12508ff44c4SLionel Sambuc                bi = bytes(filename, 'utf-8')
12608ff44c4SLionel Sambuc            return str(_file(self._magic_t, bi), 'utf-8')
12708ff44c4SLionel Sambuc        except:
128*0a6a1f1dSLionel Sambuc            return _file(self._magic_t, filename.encode('utf-8'))
129835f6802SDirk Vogt
130835f6802SDirk Vogt    def descriptor(self, fd):
131835f6802SDirk Vogt        """
132835f6802SDirk Vogt        Like the file method, but the argument is a file descriptor.
133835f6802SDirk Vogt        """
134835f6802SDirk Vogt        return _descriptor(self._magic_t, fd)
135835f6802SDirk Vogt
136835f6802SDirk Vogt    def buffer(self, buf):
137835f6802SDirk Vogt        """
138835f6802SDirk Vogt        Returns a textual description of the contents of the argument passed
139835f6802SDirk Vogt        as a buffer or None if an error occurred and the MAGIC_ERROR flag
140835f6802SDirk Vogt        is set. A call to errno() will return the numeric error code.
141835f6802SDirk Vogt        """
14208ff44c4SLionel Sambuc        try:  # attempt python3 approach first
14308ff44c4SLionel Sambuc            return str(_buffer(self._magic_t, buf, len(buf)), 'utf-8')
14408ff44c4SLionel Sambuc        except:
145835f6802SDirk Vogt            return _buffer(self._magic_t, buf, len(buf))
146835f6802SDirk Vogt
147835f6802SDirk Vogt    def error(self):
148835f6802SDirk Vogt        """
149835f6802SDirk Vogt        Returns a textual explanation of the last error or None
150835f6802SDirk Vogt        if there was no error.
151835f6802SDirk Vogt        """
15208ff44c4SLionel Sambuc        try:  # attempt python3 approach first
15308ff44c4SLionel Sambuc            return str(_error(self._magic_t), 'utf-8')
15408ff44c4SLionel Sambuc        except:
155835f6802SDirk Vogt            return _error(self._magic_t)
156835f6802SDirk Vogt
157835f6802SDirk Vogt    def setflags(self, flags):
158835f6802SDirk Vogt        """
159*0a6a1f1dSLionel Sambuc        Set flags on the magic object which determine how magic checking
160*0a6a1f1dSLionel Sambuc        behaves; a bitwise OR of the flags described in libmagic(3), but
161*0a6a1f1dSLionel Sambuc        without the MAGIC_ prefix.
162835f6802SDirk Vogt
163835f6802SDirk Vogt        Returns -1 on systems that don't support utime(2) or utimes(2)
164835f6802SDirk Vogt        when PRESERVE_ATIME is set.
165835f6802SDirk Vogt        """
166835f6802SDirk Vogt        return _setflags(self._magic_t, flags)
167835f6802SDirk Vogt
16808ff44c4SLionel Sambuc    def load(self, filename=None):
169835f6802SDirk Vogt        """
170*0a6a1f1dSLionel Sambuc        Must be called to load entries in the colon separated list of database
171*0a6a1f1dSLionel Sambuc        files passed as argument or the default database file if no argument
172*0a6a1f1dSLionel Sambuc        before any magic queries can be performed.
173835f6802SDirk Vogt
174835f6802SDirk Vogt        Returns 0 on success and -1 on failure.
175835f6802SDirk Vogt        """
17608ff44c4SLionel Sambuc        return _load(self._magic_t, filename)
177835f6802SDirk Vogt
178835f6802SDirk Vogt    def compile(self, dbs):
179835f6802SDirk Vogt        """
180835f6802SDirk Vogt        Compile entries in the colon separated list of database files
181835f6802SDirk Vogt        passed as argument or the default database file if no argument.
182835f6802SDirk Vogt        Returns 0 on success and -1 on failure.
183835f6802SDirk Vogt        The compiled files created are named from the basename(1) of each file
184835f6802SDirk Vogt        argument with ".mgc" appended to it.
185835f6802SDirk Vogt        """
186835f6802SDirk Vogt        return _compile(self._magic_t, dbs)
187835f6802SDirk Vogt
188835f6802SDirk Vogt    def check(self, dbs):
189835f6802SDirk Vogt        """
190835f6802SDirk Vogt        Check the validity of entries in the colon separated list of
191835f6802SDirk Vogt        database files passed as argument or the default database file
192835f6802SDirk Vogt        if no argument.
193835f6802SDirk Vogt        Returns 0 on success and -1 on failure.
194835f6802SDirk Vogt        """
195835f6802SDirk Vogt        return _check(self._magic_t, dbs)
196835f6802SDirk Vogt
197835f6802SDirk Vogt    def list(self, dbs):
198835f6802SDirk Vogt        """
199835f6802SDirk Vogt        Check the validity of entries in the colon separated list of
200835f6802SDirk Vogt        database files passed as argument or the default database file
201835f6802SDirk Vogt        if no argument.
202835f6802SDirk Vogt        Returns 0 on success and -1 on failure.
203835f6802SDirk Vogt        """
204835f6802SDirk Vogt        return _list(self._magic_t, dbs)
205835f6802SDirk Vogt
206835f6802SDirk Vogt    def errno(self):
207835f6802SDirk Vogt        """
208835f6802SDirk Vogt        Returns a numeric error code. If return value is 0, an internal
209835f6802SDirk Vogt        magic error occurred. If return value is non-zero, the value is
210835f6802SDirk Vogt        an OS error code. Use the errno module or os.strerror() can be used
211835f6802SDirk Vogt        to provide detailed error information.
212835f6802SDirk Vogt        """
213835f6802SDirk Vogt        return _errno(self._magic_t)
214835f6802SDirk Vogt
215*0a6a1f1dSLionel Sambuc
216835f6802SDirk Vogtdef open(flags):
217835f6802SDirk Vogt    """
218835f6802SDirk Vogt    Returns a magic object on success and None on failure.
219835f6802SDirk Vogt    Flags argument as for setflags.
220835f6802SDirk Vogt    """
221835f6802SDirk Vogt    return Magic(_open(flags))
222