python: Add compression_suffix and detect_compression functions.
authorTomas Mlcoch <tmlcoch@redhat.com>
Thu, 13 Jun 2013 11:39:44 +0000 (13:39 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Thu, 13 Jun 2013 11:39:44 +0000 (13:39 +0200)
src/python/__init__.py
src/python/compression_wrapper-py.c
src/python/compression_wrapper-py.h
src/python/createrepo_cmodule.c
tests/python/tests/test_compression_wrapper.py [new file with mode: 0644]

index dd08405..0b872d4 100644 (file)
@@ -172,3 +172,6 @@ checksum_type       = _createrepo_c.checksum_type
 
 def compress_file(src, dst, comtype, stat=None):
     return _createrepo_c.compress_file(src, dst, comtype, stat)
+
+compression_suffix  = _createrepo_c.compression_suffix
+detect_compression  = _createrepo_c.detect_compression
index 02807e1..4a8a823 100644 (file)
 #include "contentstat-py.h"
 #include "typeconversion.h"
 
+/*
+ * Module functions
+ */
+
+PyObject *
+py_compression_suffix(PyObject *self, PyObject *args)
+{
+    int type;
+
+    CR_UNUSED(self);
+
+    if (!PyArg_ParseTuple(args, "i:py_compression_suffix", &type))
+        return NULL;
+
+    return PyStringOrNone_FromString(cr_compression_suffix(type));
+}
+
+PyObject *
+py_detect_compression(PyObject *self, PyObject *args)
+{
+    long type;
+    char *filename;
+    GError *tmp_err = NULL;
+
+    CR_UNUSED(self);
+
+    if (!PyArg_ParseTuple(args, "s:py_detect_compression", &filename))
+        return NULL;
+
+    type = cr_detect_compression(filename, &tmp_err);
+    if (tmp_err) {
+        PyErr_Format(CrErr_Exception, "%s", tmp_err->message);
+        g_clear_error(&tmp_err);
+        return NULL;
+    }
+
+    return PyLong_FromLong(type);
+}
+
+/*
+ * CrFile object
+ */
+
 typedef struct {
     PyObject_HEAD
     CR_FILE *f;
index 81ebe2f..7d75cab 100644 (file)
@@ -26,4 +26,7 @@ extern PyTypeObject CrFile_Type;
 
 #define CrFileObject_Check(o)   PyObject_TypeCheck(o, &CrFile_Type)
 
+PyObject *py_compression_suffix(PyObject *self, PyObject *args);
+PyObject *py_detect_compression(PyObject *self, PyObject *args);
+
 #endif
index 24d62a8..6e5e426 100644 (file)
@@ -64,6 +64,10 @@ static struct PyMethodDef createrepo_c_methods[] = {
      METH_VARARGS, NULL},
     {"compress_file",           (PyCFunction)py_compress_file_with_stat,
      METH_VARARGS, NULL},
+    {"compression_suffix",      (PyCFunction)py_compression_suffix,
+     METH_VARARGS, NULL},
+    {"detect_compression",      (PyCFunction)py_detect_compression,
+     METH_VARARGS, NULL},
     { NULL }
 };
 
diff --git a/tests/python/tests/test_compression_wrapper.py b/tests/python/tests/test_compression_wrapper.py
new file mode 100644 (file)
index 0000000..d19569c
--- /dev/null
@@ -0,0 +1,59 @@
+import unittest
+import os.path
+import createrepo_c as cr
+
+from fixtures import *
+
+class TestCaseCompressionWrapper(unittest.TestCase):
+    def test_compression_suffix(self):
+        self.assertEqual(cr.compression_suffix(cr.AUTO_DETECT_COMPRESSION), None)
+        self.assertEqual(cr.compression_suffix(cr.UNKNOWN_COMPRESSION), None)
+        self.assertEqual(cr.compression_suffix(cr.NO_COMPRESSION), None)
+        self.assertEqual(cr.compression_suffix(123), None)
+
+        self.assertEqual(cr.compression_suffix(cr.GZ), ".gz")
+        self.assertEqual(cr.compression_suffix(cr.BZ2), ".bz2")
+        self.assertEqual(cr.compression_suffix(cr.XZ), ".xz")
+
+    def test_detect_compression(self):
+
+        # no compression
+        path = os.path.join(COMPRESSED_FILES_PATH, "01_plain.txt")
+        comtype = cr.detect_compression(path)
+        self.assertEqual(comtype, cr.NO_COMPRESSION)
+
+        # gz compression
+        path = os.path.join(COMPRESSED_FILES_PATH, "01_plain.txt.gz")
+        comtype = cr.detect_compression(path)
+        self.assertEqual(comtype, cr.GZ)
+
+        # bz2 compression
+        path = os.path.join(COMPRESSED_FILES_PATH, "01_plain.txt.bz2")
+        comtype = cr.detect_compression(path)
+        self.assertEqual(comtype, cr.BZ2)
+
+        # xz compression
+        path = os.path.join(COMPRESSED_FILES_PATH, "01_plain.txt.xz")
+        comtype = cr.detect_compression(path)
+        self.assertEqual(comtype, cr.XZ)
+
+        # Bad suffix - no compression
+        path = os.path.join(COMPRESSED_FILES_PATH, "01_plain.foo0")
+        comtype = cr.detect_compression(path)
+        self.assertEqual(comtype, cr.NO_COMPRESSION)
+
+        # Bad suffix - gz compression
+        path = os.path.join(COMPRESSED_FILES_PATH, "01_plain.foo1")
+        comtype = cr.detect_compression(path)
+        self.assertEqual(comtype, cr.GZ)
+
+        # Bad suffix - bz2 compression
+        path = os.path.join(COMPRESSED_FILES_PATH, "01_plain.foo2")
+        comtype = cr.detect_compression(path)
+        self.assertEqual(comtype, cr.BZ2)
+
+        # Bad suffix - xz compression
+        path = os.path.join(COMPRESSED_FILES_PATH, "01_plain.foo3")
+        comtype = cr.detect_compression(path)
+        self.assertEqual(comtype, cr.XZ)
+