New file(1) port

This commit is contained in:
Dirk Vogt
2012-06-18 10:53:30 +00:00
committed by Thomas Veerman
parent 991c41db8f
commit 835f680238
307 changed files with 28432 additions and 32769 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
EXTRA_DIST = README example.py py_magic.c py_magic.h setup.py
EXTRA_DIST = README example.py magic.py setup.py
+64 -28
View File
@@ -1,8 +1,9 @@
# Makefile.in generated by automake 1.10 from Makefile.am.
# Makefile.in generated by automake 1.11 from Makefile.am.
# @configure_input@
# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
# 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation,
# Inc.
# This Makefile.in is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
@@ -14,8 +15,9 @@
@SET_MAKE@
VPATH = @srcdir@
pkglibdir = $(libdir)/@PACKAGE@
pkgincludedir = $(includedir)/@PACKAGE@
pkglibdir = $(libdir)/@PACKAGE@
pkglibexecdir = $(libexecdir)/@PACKAGE@
am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
install_sh_DATA = $(install_sh) -c -m 644
install_sh_PROGRAM = $(install_sh) -c
@@ -33,19 +35,29 @@ host_triplet = @host@
subdir = python
DIST_COMMON = README $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/acinclude.m4 \
$(top_srcdir)/configure.ac
am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \
$(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \
$(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \
$(top_srcdir)/acinclude.m4 $(top_srcdir)/configure.ac
am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
$(ACLOCAL_M4)
mkinstalldirs = $(install_sh) -d
CONFIG_HEADER = $(top_builddir)/config.h
CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
AM_V_GEN = $(am__v_GEN_$(V))
am__v_GEN_ = $(am__v_GEN_$(AM_DEFAULT_VERBOSITY))
am__v_GEN_0 = @echo " GEN " $@;
AM_V_at = $(am__v_at_$(V))
am__v_at_ = $(am__v_at_$(AM_DEFAULT_VERBOSITY))
am__v_at_0 = @
SOURCES =
DIST_SOURCES =
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
pkgdatadir = @pkgdatadir@
ACLOCAL = @ACLOCAL@
AMTAR = @AMTAR@
AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@
AR = @AR@
AUTOCONF = @AUTOCONF@
AUTOHEADER = @AUTOHEADER@
@@ -56,43 +68,47 @@ CCDEPMODE = @CCDEPMODE@
CFLAGS = @CFLAGS@
CPP = @CPP@
CPPFLAGS = @CPPFLAGS@
CXX = @CXX@
CXXCPP = @CXXCPP@
CXXDEPMODE = @CXXDEPMODE@
CXXFLAGS = @CXXFLAGS@
CYGPATH_W = @CYGPATH_W@
DEFS = @DEFS@
DEPDIR = @DEPDIR@
DSYMUTIL = @DSYMUTIL@
ECHO = @ECHO@
DUMPBIN = @DUMPBIN@
ECHO_C = @ECHO_C@
ECHO_N = @ECHO_N@
ECHO_T = @ECHO_T@
EGREP = @EGREP@
EXEEXT = @EXEEXT@
F77 = @F77@
FFLAGS = @FFLAGS@
FGREP = @FGREP@
GREP = @GREP@
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
LD = @LD@
LDFLAGS = @LDFLAGS@
LIBOBJS = @LIBOBJS@
LIBS = @LIBS@
LIBTOOL = @LIBTOOL@
LIPO = @LIPO@
LN_S = @LN_S@
LTLIBOBJS = @LTLIBOBJS@
MAINT = @MAINT@
MAKEINFO = @MAKEINFO@
MINGW = @MINGW@
MKDIR_P = @MKDIR_P@
NM = @NM@
NMEDIT = @NMEDIT@
OBJDUMP = @OBJDUMP@
OBJEXT = @OBJEXT@
OTOOL = @OTOOL@
OTOOL64 = @OTOOL64@
PACKAGE = @PACKAGE@
PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
PACKAGE_NAME = @PACKAGE_NAME@
PACKAGE_STRING = @PACKAGE_STRING@
PACKAGE_TARNAME = @PACKAGE_TARNAME@
PACKAGE_URL = @PACKAGE_URL@
PACKAGE_VERSION = @PACKAGE_VERSION@
PATH_SEPARATOR = @PATH_SEPARATOR@
RANLIB = @RANLIB@
@@ -107,8 +123,7 @@ abs_srcdir = @abs_srcdir@
abs_top_builddir = @abs_top_builddir@
abs_top_srcdir = @abs_top_srcdir@
ac_ct_CC = @ac_ct_CC@
ac_ct_CXX = @ac_ct_CXX@
ac_ct_F77 = @ac_ct_F77@
ac_ct_DUMPBIN = @ac_ct_DUMPBIN@
am__include = @am__include@
am__leading_dot = @am__leading_dot@
am__quote = @am__quote@
@@ -140,6 +155,7 @@ libdir = @libdir@
libexecdir = @libexecdir@
localedir = @localedir@
localstatedir = @localstatedir@
lt_ECHO = @lt_ECHO@
mandir = @mandir@
mkdir_p = @mkdir_p@
oldincludedir = @oldincludedir@
@@ -152,24 +168,25 @@ sharedstatedir = @sharedstatedir@
srcdir = @srcdir@
sysconfdir = @sysconfdir@
target_alias = @target_alias@
top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
EXTRA_DIST = README example.py py_magic.c py_magic.h setup.py
EXTRA_DIST = README example.py magic.py setup.py
all: all-am
.SUFFIXES:
$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps)
@for dep in $?; do \
case '$(am__configure_deps)' in \
*$$dep*) \
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \
&& exit 0; \
( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \
&& { if test -f $@; then exit 0; else break; fi; }; \
exit 1;; \
esac; \
done; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu python/Makefile'; \
cd $(top_srcdir) && \
$(AUTOMAKE) --gnu python/Makefile
echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu python/Makefile'; \
$(am__cd) $(top_srcdir) && \
$(AUTOMAKE) --gnu python/Makefile
.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
@case '$?' in \
@@ -183,10 +200,11 @@ Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES)
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
$(top_srcdir)/configure: $(am__configure_deps)
$(top_srcdir)/configure: @MAINTAINER_MODE_TRUE@ $(am__configure_deps)
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
$(ACLOCAL_M4): $(am__aclocal_m4_deps)
$(ACLOCAL_M4): @MAINTAINER_MODE_TRUE@ $(am__aclocal_m4_deps)
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
$(am__aclocal_m4_deps):
mostlyclean-libtool:
-rm -f *.lo
@@ -216,13 +234,17 @@ distdir: $(DISTFILES)
if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
if test -d $$d/$$file; then \
dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \
if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \
if test -d "$(distdir)/$$file"; then \
find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
fi; \
cp -pR $$d/$$file $(distdir)$$dir || exit 1; \
if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \
find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \
fi; \
cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \
else \
test -f $(distdir)/$$file \
|| cp -p $$d/$$file $(distdir)/$$file \
test -f "$(distdir)/$$file" \
|| cp -p $$d/$$file "$(distdir)/$$file" \
|| exit 1; \
fi; \
done
@@ -250,6 +272,7 @@ clean-generic:
distclean-generic:
-test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
-test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)
maintainer-clean-generic:
@echo "This command is intended for maintainers to use"
@@ -268,6 +291,8 @@ dvi-am:
html: html-am
html-am:
info: info-am
info-am:
@@ -276,18 +301,28 @@ install-data-am:
install-dvi: install-dvi-am
install-dvi-am:
install-exec-am:
install-html: install-html-am
install-html-am:
install-info: install-info-am
install-info-am:
install-man:
install-pdf: install-pdf-am
install-pdf-am:
install-ps: install-ps-am
install-ps-am:
installcheck-am:
maintainer-clean: maintainer-clean-am
@@ -321,6 +356,7 @@ uninstall-am:
maintainer-clean-generic mostlyclean mostlyclean-generic \
mostlyclean-libtool pdf pdf-am ps ps-am uninstall uninstall-am
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
+5 -18
View File
@@ -1,26 +1,13 @@
This directory contains Python bindings to allow you to access the
libmagic api. At the moment their status is "experimental" and
they are not built by default. You may need to edit include and library
search paths in setup.py so that python's distutils build process can
find the magic header and lib files.
libmagic api. At the moment their status is "experimental".
In order to be able to compile magic-python you need to have python
and the python-dev packages installed.
Python libraries are always built for a particular version of Python
(2.2, 2.3, etc), and libraries built for one version will not be seen
by another.
To build:
You can install the modules either with:
$ python setup.py build
$ python setup.py install
Now, you can install the modules:
or, if you have easy_install:
$ cp build/lib.*/magic.so /usr/lib/python2.3/lib-dynload/
(the directory /usr/lib/python2.3 may vary, depending on your installation)
$ easy_install .
magic-python should work now!
+9 -8
View File
@@ -1,16 +1,17 @@
#! /usr/bin/python
import magic
ms = magic.open(magic.MAGIC_NONE)
ms = magic.open(magic.NONE)
ms.load()
type = ms.file("/path/to/some/file")
print type
tp = ms.file("/bin/ls")
print (tp)
f = file("/path/to/some/file", "r")
buffer = f.read(4096)
f = open("/bin/ls", "rb")
buf = f.read(4096)
f.close()
type = ms.buffer(buffer)
print type
tp = ms.buffer(buf)
print (tp)
ms.close()
+204
View File
@@ -0,0 +1,204 @@
#!/usr/bin/env python
'''
Python bindings for libmagic
'''
import ctypes
from ctypes import *
from ctypes.util import find_library
def _init():
"""
Loads the shared library through ctypes and returns a library
L{ctypes.CDLL} instance
"""
return ctypes.cdll.LoadLibrary(find_library('magic'))
_libraries = {}
_libraries['magic'] = _init()
# Flag constants for open and setflags
MAGIC_NONE = NONE = 0
MAGIC_DEBUG = DEBUG = 1
MAGIC_SYMLINK = SYMLINK = 2
MAGIC_COMPRESS = COMPRESS = 4
MAGIC_DEVICES = DEVICES = 8
MAGIC_MIME_TYPE = MIME_TYPE = 16
MAGIC_CONTINUE = CONTINUE = 32
MAGIC_CHECK = CHECK = 64
MAGIC_PRESERVE_ATIME = PRESERVE_ATIME = 128
MAGIC_RAW = RAW = 256
MAGIC_ERROR = ERROR = 512
MAGIC_MIME_ENCODING = MIME_ENCODING = 1024
MAGIC_MIME = MIME = 1040
MAGIC_APPLE = APPLE = 2048
MAGIC_NO_CHECK_COMPRESS = NO_CHECK_COMPRESS = 4096
MAGIC_NO_CHECK_TAR = NO_CHECK_TAR = 8192
MAGIC_NO_CHECK_SOFT = NO_CHECK_SOFT = 16384
MAGIC_NO_CHECK_APPTYPE = NO_CHECK_APPTYPE = 32768
MAGIC_NO_CHECK_ELF = NO_CHECK_ELF = 65536
MAGIC_NO_CHECK_TEXT = NO_CHECK_TEXT = 131072
MAGIC_NO_CHECK_CDF = NO_CHECK_CDF = 262144
MAGIC_NO_CHECK_TOKENS = NO_CHECK_TOKENS = 1048576
MAGIC_NO_CHECK_ENCODING = NO_CHECK_ENCODING = 2097152
MAGIC_NO_CHECK_BUILTIN = NO_CHECK_BUILTIN = 4173824
class magic_set(Structure):
pass
magic_set._fields_ = []
magic_t = POINTER(magic_set)
_open = _libraries['magic'].magic_open
_open.restype = magic_t
_open.argtypes = [c_int]
_close = _libraries['magic'].magic_close
_close.restype = None
_close.argtypes = [magic_t]
_file = _libraries['magic'].magic_file
_file.restype = c_char_p
_file.argtypes = [magic_t, c_char_p]
_descriptor = _libraries['magic'].magic_descriptor
_descriptor.restype = c_char_p
_descriptor.argtypes = [magic_t, c_int]
_buffer = _libraries['magic'].magic_buffer
_buffer.restype = c_char_p
_buffer.argtypes = [magic_t, c_void_p, c_size_t]
_error = _libraries['magic'].magic_error
_error.restype = c_char_p
_error.argtypes = [magic_t]
_setflags = _libraries['magic'].magic_setflags
_setflags.restype = c_int
_setflags.argtypes = [magic_t, c_int]
_load = _libraries['magic'].magic_load
_load.restype = c_int
_load.argtypes = [magic_t, c_char_p]
_compile = _libraries['magic'].magic_compile
_compile.restype = c_int
_compile.argtypes = [magic_t, c_char_p]
_check = _libraries['magic'].magic_check
_check.restype = c_int
_check.argtypes = [magic_t, c_char_p]
_list = _libraries['magic'].magic_list
_list.restype = c_int
_list.argtypes = [magic_t, c_char_p]
_errno = _libraries['magic'].magic_errno
_errno.restype = c_int
_errno.argtypes = [magic_t]
class Magic(object):
def __init__(self, ms):
self._magic_t = ms
def close(self):
"""
Closes the magic database and deallocates any resources used.
"""
_close(self._magic_t)
def file(self, file):
"""
Returns a textual description of the contents of the argument passed
as a filename or None if an error occurred and the MAGIC_ERROR flag
is set. A call to errno() will return the numeric error code.
"""
return _file(self._magic_t, file)
def descriptor(self, fd):
"""
Like the file method, but the argument is a file descriptor.
"""
return _descriptor(self._magic_t, fd)
def buffer(self, buf):
"""
Returns a textual description of the contents of the argument passed
as a buffer or None if an error occurred and the MAGIC_ERROR flag
is set. A call to errno() will return the numeric error code.
"""
return _buffer(self._magic_t, buf, len(buf))
def error(self):
"""
Returns a textual explanation of the last error or None
if there was no error.
"""
return _error(self._magic_t)
def setflags(self, flags):
"""
Set flags on the magic object which determine how magic checking behaves;
a bitwise OR of the flags described in libmagic(3), but without the MAGIC_
prefix.
Returns -1 on systems that don't support utime(2) or utimes(2)
when PRESERVE_ATIME is set.
"""
return _setflags(self._magic_t, flags)
def load(self, file=None):
"""
Must be called to load entries in the colon separated list of database files
passed as argument or the default database file if no argument before
any magic queries can be performed.
Returns 0 on success and -1 on failure.
"""
return _load(self._magic_t, file)
def compile(self, dbs):
"""
Compile entries in the colon separated list of database files
passed as argument or the default database file if no argument.
Returns 0 on success and -1 on failure.
The compiled files created are named from the basename(1) of each file
argument with ".mgc" appended to it.
"""
return _compile(self._magic_t, dbs)
def check(self, dbs):
"""
Check the validity of entries in the colon separated list of
database files passed as argument or the default database file
if no argument.
Returns 0 on success and -1 on failure.
"""
return _check(self._magic_t, dbs)
def list(self, dbs):
"""
Check the validity of entries in the colon separated list of
database files passed as argument or the default database file
if no argument.
Returns 0 on success and -1 on failure.
"""
return _list(self._magic_t, dbs)
def errno(self):
"""
Returns a numeric error code. If return value is 0, an internal
magic error occurred. If return value is non-zero, the value is
an OS error code. Use the errno module or os.strerror() can be used
to provide detailed error information.
"""
return _errno(self._magic_t)
def open(flags):
"""
Returns a magic object on success and None on failure.
Flags argument as for setflags.
"""
return Magic(_open(flags))
-353
View File
@@ -1,353 +0,0 @@
/* $NetBSD: py_magic.c,v 1.1.1.1 2009/05/08 16:35:10 christos Exp $ */
/*
Python wrappers for magic functions.
Copyright (C) Brett Funderburg, Deepfile Corp. Austin, TX, US 2003
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice immediately at the beginning of the file, without modification,
this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include <Python.h>
#include <magic.h>
#include "py_magic.h"
/* Exceptions raised by this module */
PyObject* magic_error_obj;
/* Create a new magic_cookie_hnd object */
PyObject* new_magic_cookie_handle(magic_t cookie)
{
magic_cookie_hnd* mch;
mch = PyObject_New(magic_cookie_hnd, &magic_cookie_type);
mch->cookie = cookie;
return (PyObject*)mch;
}
static char _magic_open__doc__[] =
"Returns a magic cookie on success and None on failure.\n";
static PyObject* py_magic_open(PyObject* self, PyObject* args)
{
int flags = 0;
magic_t cookie;
if(!PyArg_ParseTuple(args, "i", &flags))
return NULL;
if(!(cookie = magic_open(flags))) {
PyErr_SetString(magic_error_obj, "failure initializing magic cookie");
return NULL;
}
return new_magic_cookie_handle(cookie);
}
static char _magic_close__doc__[] =
"Closes the magic database and deallocates any resources used.\n";
static PyObject* py_magic_close(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
magic_close(hnd->cookie);
Py_INCREF(Py_None);
return Py_None;
}
static char _magic_error__doc__[] =
"Returns a textual explanation of the last error or None \
if there was no error.\n";
static PyObject* py_magic_error(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
const char* message = NULL;
PyObject* result = Py_None;
message = magic_error(hnd->cookie);
if(message != NULL)
result = PyString_FromString(message);
else
Py_INCREF(Py_None);
return result;
}
static char _magic_errno__doc__[] =
"Returns a numeric error code. If return value is 0, an internal \
magic error occurred. If return value is non-zero, the value is \
an OS error code. Use the errno module or os.strerror() can be used \
to provide detailed error information.\n";
static PyObject* py_magic_errno(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
return PyInt_FromLong(magic_errno(hnd->cookie));
}
static char _magic_file__doc__[] =
"Returns a textual description of the contents of the argument passed \
as a filename or None if an error occurred and the MAGIC_ERROR flag \
is set. A call to errno() will return the numeric error code.\n";
static PyObject* py_magic_file(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
char* filename = NULL;
const char* message = NULL;
PyObject* result = Py_None;
if(!(PyArg_ParseTuple(args, "s", &filename)))
return NULL;
message = magic_file(hnd->cookie, filename);
if(message != NULL)
result = PyString_FromString(message);
else
Py_INCREF(Py_None);
return result;
}
static char _magic_buffer__doc__[] =
"Returns a textual description of the contents of the argument passed \
as a buffer or None if an error occurred and the MAGIC_ERROR flag \
is set. A call to errno() will return the numeric error code.\n";
static PyObject* py_magic_buffer(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
void* buffer = NULL;
int buffer_length = 0;
const char* message = NULL;
PyObject* result = Py_None;
if(!(PyArg_ParseTuple(args, "s#", (char**)&buffer, &buffer_length)))
return NULL;
message = magic_buffer(hnd->cookie, buffer, buffer_length);
if(message != NULL)
result = PyString_FromString(message);
else
Py_INCREF(Py_None);
return result;
}
static char _magic_setflags__doc__[] =
"Set flags on the cookie object.\n \
Returns -1 on systems that don't support utime(2) or utimes(2) \
when MAGIC_PRESERVE_ATIME is set.\n";
static PyObject* py_magic_setflags(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
int flags;
int result;
if(!(PyArg_ParseTuple(args, "i", &flags)))
return NULL;
result = magic_setflags(hnd->cookie, flags);
return PyInt_FromLong(result);
}
static char _magic_check__doc__[] =
"Check the validity of entries in the colon separated list of \
database files passed as argument or the default database file \
if no argument.\n Returns 0 on success and -1 on failure.\n";
static PyObject* py_magic_check(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
char* filename = NULL;
int result;
if(!(PyArg_ParseTuple(args, "|s", &filename)))
return NULL;
result = magic_check(hnd->cookie, filename);
return PyInt_FromLong(result);
}
static char _magic_compile__doc__[] =
"Compile entries in the colon separated list of database files \
passed as argument or the default database file if no argument.\n \
Returns 0 on success and -1 on failure.\n \
The compiled files created are named from the basename(1) of each file \
argument with \".mgc\" appended to it.\n";
static PyObject* py_magic_compile(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
char* filename = NULL;
int result;
if(!(PyArg_ParseTuple(args, "|s", &filename)))
return NULL;
result = magic_compile(hnd->cookie, filename);
return PyInt_FromLong(result);
}
static char _magic_load__doc__[] =
"Must be called to load entries in the colon separated list of database files \
passed as argument or the default database file if no argument before \
any magic queries can be performed.\n \
Returns 0 on success and -1 on failure.\n";
static PyObject* py_magic_load(PyObject* self, PyObject* args)
{
magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
char* filename = NULL;
int result;
if(!(PyArg_ParseTuple(args, "|s", &filename)))
return NULL;
result = magic_load(hnd->cookie, filename);
return PyInt_FromLong(result);
}
/* object methods */
static PyMethodDef magic_cookie_hnd_methods[] = {
{ "close", (PyCFunction)py_magic_close,
METH_NOARGS, _magic_close__doc__ },
{ "error", (PyCFunction)py_magic_error,
METH_NOARGS, _magic_error__doc__ },
{ "file", (PyCFunction)py_magic_file,
METH_VARARGS, _magic_file__doc__ },
{ "buffer", (PyCFunction)py_magic_buffer,
METH_VARARGS, _magic_buffer__doc__ },
{ "setflags", (PyCFunction)py_magic_setflags,
METH_VARARGS, _magic_setflags__doc__ },
{ "check", (PyCFunction)py_magic_check,
METH_VARARGS, _magic_check__doc__ },
{ "compile", (PyCFunction)py_magic_compile,
METH_VARARGS, _magic_compile__doc__ },
{ "load", (PyCFunction)py_magic_load,
METH_VARARGS, _magic_load__doc__ },
{ "errno", (PyCFunction)py_magic_errno,
METH_NOARGS, _magic_errno__doc__ },
{ NULL, NULL }
};
/* module level methods */
static PyMethodDef magic_methods[] = {
{ "open", (PyCFunction)py_magic_open,
METH_VARARGS, _magic_open__doc__ },
{ NULL, NULL }
};
static void py_magic_dealloc(PyObject* self)
{
PyObject_Del(self);
}
static PyObject* py_magic_getattr(PyObject* self, char* attrname)
{
return Py_FindMethod(magic_cookie_hnd_methods, self, attrname);
}
PyTypeObject magic_cookie_type = {
PyObject_HEAD_INIT(NULL)
0,
"Magic cookie",
sizeof(magic_cookie_hnd),
0,
py_magic_dealloc, /* tp_dealloc */
0, /* tp_print */
py_magic_getattr, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
};
/* Initialize constants */
static struct const_vals {
const char* const name;
unsigned int value;
} module_const_vals[] = {
{ "MAGIC_NONE", MAGIC_NONE },
{ "MAGIC_DEBUG", MAGIC_DEBUG },
{ "MAGIC_SYMLINK", MAGIC_SYMLINK },
{ "MAGIC_COMPRESS", MAGIC_COMPRESS },
{ "MAGIC_DEVICES", MAGIC_DEVICES },
{ "MAGIC_MIME", MAGIC_MIME },
{ "MAGIC_CONTINUE", MAGIC_CONTINUE },
{ "MAGIC_CHECK", MAGIC_CHECK },
{ "MAGIC_PRESERVE_ATIME", MAGIC_PRESERVE_ATIME },
{ "MAGIC_ERROR", MAGIC_ERROR},
{ NULL }
};
static void const_init(PyObject* dict)
{
struct const_vals* tmp;
PyObject *obj;
for(tmp = module_const_vals; tmp->name; ++tmp) {
obj = PyInt_FromLong(tmp->value);
PyDict_SetItemString(dict, tmp->name, obj);
Py_DECREF(obj);
}
}
/*
* Module initialization
*/
void initmagic(void)
{
PyObject* module;
PyObject* dict;
/* Initialize module */
module = Py_InitModule("magic", magic_methods);
dict = PyModule_GetDict(module);
magic_error_obj = PyErr_NewException("magic.error", NULL, NULL);
PyDict_SetItemString(dict, "error", magic_error_obj);
magic_cookie_type.ob_type = &PyType_Type;
/* Initialize constants */
const_init(dict);
if(PyErr_Occurred())
Py_FatalError("can't initialize module magic");
}
-43
View File
@@ -1,43 +0,0 @@
/* $NetBSD: py_magic.h,v 1.1.1.1 2009/05/08 16:35:10 christos Exp $ */
/*
Python wrappers for magic functions.
Copyright (C) Brett Funderburg, Deepfile Corp. Austin, TX, US 2003
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice immediately at the beginning of the file, without modification,
this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#ifndef _PY_MAGIC_H
#define _PY_MAGIC_H
typedef struct {
PyObject_HEAD
magic_t cookie;
} magic_cookie_hnd;
extern PyTypeObject magic_cookie_type;
#endif /* _PY_MAGIC_H */
+7 -13
View File
@@ -1,16 +1,10 @@
# Python distutils build script for magic extension
from distutils.core import setup, Extension
from distutils.core import setup
magic_module = Extension('magic',
libraries = ['magic'],
library_dirs = ['./','../','../src','/usr/lib/'],
include_dirs = ['./','../','../src','/usr/include/'],
sources = ['py_magic.c'])
setup (name = 'Magic file extensions',
version = '0.1',
author = 'Brett Funderburg',
author_email = 'brettf@deepfile.com',
setup(name = 'Magic file extensions',
version = '0.2',
author = 'Reuben Thomas',
author_email = 'rrt@sc3d.org',
license = 'BSD',
description = 'libmagic python bindings',
ext_modules = [magic_module])
description = 'libmagic Python bindings',
py_modules = ['magic'])