summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorEitan Adler <eadler@FreeBSD.org>2018-07-29 05:53:49 +0000
committerEitan Adler <eadler@FreeBSD.org>2018-07-29 05:53:49 +0000
commitad1ba6e1e032f79d03b9a6e2c714de84d1911b3a (patch)
tree24e9595e175209972236019508c9ac6b34ec83b9 /python
parentfd86496b2232c1f79946dc2d21515012a33b6274 (diff)
Notes
Diffstat (limited to 'python')
-rw-r--r--python/CHANGELOG.md16
-rw-r--r--python/LICENSE25
-rw-r--r--python/Makefile.am3
-rw-r--r--python/Makefile.in82
-rw-r--r--python/README0
-rw-r--r--python/README.md31
-rw-r--r--python/magic.py5
-rw-r--r--python/setup.py7
-rw-r--r--python/tests.py32
9 files changed, 179 insertions, 22 deletions
diff --git a/python/CHANGELOG.md b/python/CHANGELOG.md
new file mode 100644
index 000000000000..ac3c0c0e9455
--- /dev/null
+++ b/python/CHANGELOG.md
@@ -0,0 +1,16 @@
+# Python `file-magic` Log of Changes
+
+## `0.4.0`
+
+- Sync with current version of file:
+ * Retain python 2 compatibility, factoring out the conversion functions.
+ * Avoid double encoding with python3
+ * Restore python-2 compatibility.
+
+
+## `0.3.0`
+
+- Fix `setup.py` so we can upload to PyPI
+- Add function `detect_from_filename`
+- Add function `detect_from_fobj`
+- Add function `detect_from_content`
diff --git a/python/LICENSE b/python/LICENSE
new file mode 100644
index 000000000000..46cfc24317ac
--- /dev/null
+++ b/python/LICENSE
@@ -0,0 +1,25 @@
+Copyright (c) Ian F. Darwin 1986-1995.
+Software written by Ian F. Darwin and others;
+maintained 1995-present by Christos Zoulas and others.
+
+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.
+
+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.
diff --git a/python/Makefile.am b/python/Makefile.am
index e0c469528e96..f2d3412b8658 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -1,3 +1,4 @@
-EXTRA_DIST = README example.py magic.py setup.py
+EXTRA_DIST = LICENSE CHANGELOG.md README.md example.py magic.py setup.py \
+ tests.py
diff --git a/python/Makefile.in b/python/Makefile.in
index aeee6dca7324..40fc01ab3931 100644
--- a/python/Makefile.in
+++ b/python/Makefile.in
@@ -1,7 +1,7 @@
-# Makefile.in generated by automake 1.13.1 from Makefile.am.
+# Makefile.in generated by automake 1.15 from Makefile.am.
# @configure_input@
-# Copyright (C) 1994-2012 Free Software Foundation, Inc.
+# Copyright (C) 1994-2014 Free Software Foundation, Inc.
# This Makefile.in is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
@@ -14,23 +14,61 @@
@SET_MAKE@
VPATH = @srcdir@
-am__make_dryrun = \
- { \
- am__dry=no; \
+am__is_gnu_make = { \
+ if test -z '$(MAKELEVEL)'; then \
+ false; \
+ elif test -n '$(MAKE_HOST)'; then \
+ true; \
+ elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \
+ true; \
+ else \
+ false; \
+ fi; \
+}
+am__make_running_with_option = \
+ case $${target_option-} in \
+ ?) ;; \
+ *) echo "am__make_running_with_option: internal error: invalid" \
+ "target option '$${target_option-}' specified" >&2; \
+ exit 1;; \
+ esac; \
+ has_opt=no; \
+ sane_makeflags=$$MAKEFLAGS; \
+ if $(am__is_gnu_make); then \
+ sane_makeflags=$$MFLAGS; \
+ else \
case $$MAKEFLAGS in \
*\\[\ \ ]*) \
- echo 'am--echo: ; @echo "AM" OK' | $(MAKE) -f - 2>/dev/null \
- | grep '^AM OK$$' >/dev/null || am__dry=yes;; \
- *) \
- for am__flg in $$MAKEFLAGS; do \
- case $$am__flg in \
- *=*|--*) ;; \
- *n*) am__dry=yes; break;; \
- esac; \
- done;; \
+ bs=\\; \
+ sane_makeflags=`printf '%s\n' "$$MAKEFLAGS" \
+ | sed "s/$$bs$$bs[$$bs $$bs ]*//g"`;; \
+ esac; \
+ fi; \
+ skip_next=no; \
+ strip_trailopt () \
+ { \
+ flg=`printf '%s\n' "$$flg" | sed "s/$$1.*$$//"`; \
+ }; \
+ for flg in $$sane_makeflags; do \
+ test $$skip_next = yes && { skip_next=no; continue; }; \
+ case $$flg in \
+ *=*|--*) continue;; \
+ -*I) strip_trailopt 'I'; skip_next=yes;; \
+ -*I?*) strip_trailopt 'I';; \
+ -*O) strip_trailopt 'O'; skip_next=yes;; \
+ -*O?*) strip_trailopt 'O';; \
+ -*l) strip_trailopt 'l'; skip_next=yes;; \
+ -*l?*) strip_trailopt 'l';; \
+ -[dEDm]) skip_next=yes;; \
+ -[JT]) skip_next=yes;; \
+ esac; \
+ case $$flg in \
+ *$$target_option*) has_opt=yes; break;; \
esac; \
- test $$am__dry = yes; \
- }
+ done; \
+ test $$has_opt = yes
+am__make_dryrun = (target_option=n; $(am__make_running_with_option))
+am__make_keepgoing = (target_option=k; $(am__make_running_with_option))
pkgincludedir = $(includedir)/@PACKAGE@
pkglibdir = $(libdir)/@PACKAGE@
pkglibexecdir = $(libexecdir)/@PACKAGE@
@@ -49,7 +87,6 @@ POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
subdir = python
-DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am README
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \
$(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \
@@ -57,6 +94,7 @@ am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \
$(top_srcdir)/acinclude.m4 $(top_srcdir)/configure.ac
am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
$(ACLOCAL_M4)
+DIST_COMMON = $(srcdir)/Makefile.am $(am__DIST_COMMON)
mkinstalldirs = $(install_sh) -d
CONFIG_HEADER = $(top_builddir)/config.h
CONFIG_CLEAN_FILES =
@@ -81,6 +119,7 @@ am__can_run_installinfo = \
*) (install-info --version) >/dev/null 2>&1;; \
esac
am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP)
+am__DIST_COMMON = $(srcdir)/Makefile.in
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
pkgdatadir = @pkgdatadir@
ACLOCAL = @ACLOCAL@
@@ -94,6 +133,7 @@ AWK = @AWK@
CC = @CC@
CCDEPMODE = @CCDEPMODE@
CFLAGS = @CFLAGS@
+CFLAG_VISIBILITY = @CFLAG_VISIBILITY@
CPP = @CPP@
CPPFLAGS = @CPPFLAGS@
CYGPATH_W = @CYGPATH_W@
@@ -109,6 +149,7 @@ EGREP = @EGREP@
EXEEXT = @EXEEXT@
FGREP = @FGREP@
GREP = @GREP@
+HAVE_VISIBILITY = @HAVE_VISIBILITY@
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
@@ -200,7 +241,9 @@ target_alias = @target_alias@
top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
-EXTRA_DIST = README example.py magic.py setup.py
+EXTRA_DIST = LICENSE CHANGELOG.md README.md example.py magic.py setup.py \
+ tests.py
+
all: all-am
.SUFFIXES:
@@ -216,7 +259,6 @@ $(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign python/Makefile'; \
$(am__cd) $(top_srcdir) && \
$(AUTOMAKE) --foreign python/Makefile
-.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
@case '$?' in \
*config.status*) \
@@ -391,6 +433,8 @@ uninstall-am:
mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \
tags-am uninstall uninstall-am
+.PRECIOUS: Makefile
+
# 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.
diff --git a/python/README b/python/README
deleted file mode 100644
index e69de29bb2d1..000000000000
--- a/python/README
+++ /dev/null
diff --git a/python/README.md b/python/README.md
new file mode 100644
index 000000000000..d626e31882aa
--- /dev/null
+++ b/python/README.md
@@ -0,0 +1,31 @@
+# `file-magic`: Python Bindings
+
+This library is a Python ctypes interface to `libmagic`.
+
+
+## Installing
+
+You can install `file-magic` either with:
+
+ python setup.py install
+ # or
+ easy_install .
+ # or
+ pip install file-magic
+
+
+## Using
+
+ import magic
+
+ detected = magic.detect_from_filename('magic.py')
+ print 'Detected MIME type: {}'.format(detected.mime_type)
+ print 'Detected encoding: {}'.format(detected.encoding)
+ print 'Detected file type name: {}'.format(detected.name)
+
+
+## Developing/Contributing
+
+To run the tests:
+
+ python setup.py test
diff --git a/python/magic.py b/python/magic.py
index 662569e889d0..4c1ff7a7de7a 100644
--- a/python/magic.py
+++ b/python/magic.py
@@ -248,7 +248,10 @@ none_magic.load()
def _create_filemagic(mime_detected, type_detected):
- mime_type, mime_encoding = mime_detected.split('; ')
+ try:
+ mime_type, mime_encoding = mime_detected.split('; ')
+ except ValueError:
+ raise ValueError(mime_detected)
return FileMagic(name=type_detected, mime_type=mime_type,
encoding=mime_encoding.replace('charset=', ''))
diff --git a/python/setup.py b/python/setup.py
index 24ae182fff6b..a864b1e18ac2 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -4,14 +4,19 @@ from __future__ import unicode_literals
from setuptools import setup
+with open('README.md', 'r') as fh:
+ long_description = fh.read()
+
setup(name='file-magic',
- version='0.3.0',
+ version='0.4.0',
author='Reuben Thomas, Álvaro Justen',
author_email='rrt@sc3d.org, alvarojusten@gmail.com',
url='https://github.com/file/file',
license='BSD',
description='(official) libmagic Python bindings',
+ long_description=long_description,
+ long_description_content_type='text/markdown',
py_modules=['magic'],
test_suite='tests',
classifiers = [
diff --git a/python/tests.py b/python/tests.py
new file mode 100644
index 000000000000..197a8fc4b519
--- /dev/null
+++ b/python/tests.py
@@ -0,0 +1,32 @@
+# coding: utf-8
+
+import unittest
+
+import magic
+
+
+class MagicTestCase(unittest.TestCase):
+
+ filename = 'magic.py'
+ expected_mime_type = 'text/x-python'
+ expected_encoding = 'us-ascii'
+ expected_name = 'Python script, ASCII text executable'
+
+ def assert_result(self, result):
+ self.assertEqual(result.mime_type, self.expected_mime_type)
+ self.assertEqual(result.encoding, self.expected_encoding)
+ self.assertEqual(result.name, self.expected_name)
+
+ def test_detect_from_filename(self):
+ result = magic.detect_from_filename(self.filename)
+ self.assert_result(result)
+
+ def test_detect_from_fobj(self):
+ with open(self.filename) as fobj:
+ result = magic.detect_from_fobj(fobj)
+ self.assert_result(result)
+
+ def test_detect_from_content(self):
+ with open(self.filename) as fobj:
+ result = magic.detect_from_content(fobj.read(4096))
+ self.assert_result(result)