Python: Add checksum module.
authorTomas Mlcoch <tmlcoch@redhat.com>
Mon, 10 Jun 2013 12:51:02 +0000 (14:51 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Mon, 10 Jun 2013 12:51:02 +0000 (14:51 +0200)
src/python/CMakeLists.txt
src/python/__init__.py
src/python/checksum-py.c [new file with mode: 0644]
src/python/checksum-py.h [new file with mode: 0644]
src/python/createrepo_cmodule.c
tests/python/tests/test_checksum.py [new file with mode: 0644]

index 00aef576a1359a50a337fdf33883cd8de76851cd..50b6143bda518b9908aa5467c4e72eb49992aa93 100644 (file)
@@ -13,6 +13,7 @@ SET (createrepo_c_COPIES __init__.py)
 FILE(COPY ${createrepo_c_COPIES} DESTINATION ./createrepo_c/)
 
 SET (craeterepo_cmodule_SRCS
+     checksum-py.c
      contentstat-py.c
      createrepo_cmodule.c
      exception-py.c
index afbae43b8b9627f236561b3992f22d93cf7c26a4..9dfc1f032b8c735a995a658b91293cdb505511c9 100644 (file)
@@ -154,3 +154,6 @@ def xml_parse_other(path, newpkgcb=None, pkgcb=None, warningcb=None):
 
 def xml_parse_repomd(path, repomdobj, warning_cb=None):
     return _createrepo_c.xml_parse_repomd(path, repomdobj, warning_cb)
+
+checksum_name_str   = _createrepo_c.checksum_name_str
+checksum_type       = _createrepo_c.checksum_type
diff --git a/src/python/checksum-py.c b/src/python/checksum-py.c
new file mode 100644 (file)
index 0000000..ed2dd37
--- /dev/null
@@ -0,0 +1,53 @@
+/* createrepo_c - Library of routines for manipulation with repodata
+ * Copyright (C) 2013  Tomas Mlcoch
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+ * USA.
+ */
+
+#include <Python.h>
+#include <assert.h>
+#include <stddef.h>
+
+#include "src/createrepo_c.h"
+
+#include "typeconversion.h"
+#include "exception-py.h"
+
+PyObject *
+py_checksum_name_str(PyObject *self, PyObject *args)
+{
+    int type;
+
+    CR_UNUSED(self);
+
+    if (!PyArg_ParseTuple(args, "i:py_checksum_name_Str", &type))
+        return NULL;
+
+    return PyStringOrNone_FromString(cr_checksum_name_str(type));
+}
+
+PyObject *
+py_checksum_type(PyObject *self, PyObject *args)
+{
+    char *type;
+
+    CR_UNUSED(self);
+
+    if (!PyArg_ParseTuple(args, "s:py_checksum_type", &type))
+        return NULL;
+
+    return PyLong_FromLong((long) cr_checksum_type(type));
+}
diff --git a/src/python/checksum-py.h b/src/python/checksum-py.h
new file mode 100644 (file)
index 0000000..933c0e0
--- /dev/null
@@ -0,0 +1,28 @@
+/* createrepo_c - Library of routines for manipulation with repodata
+ * Copyright (C) 2013  Tomas Mlcoch
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+ * USA.
+ */
+
+#ifndef CR_CHECKSUM_PY_H
+#define CR_CHECKSUM_PY_H
+
+#include "src/createrepo_c.h"
+
+PyObject *py_checksum_name_str(PyObject *self, PyObject *args);
+PyObject *py_checksum_type(PyObject *self, PyObject *args);
+
+#endif
index 8fa9e0dab26e01e5076c9ca3e98c74d805e0d249..d2f3d1400aa91db51adb2433a6049398a3ac5366 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "src/createrepo_c.h"
 
+#include "checksum-py.h"
 #include "contentstat-py.h"
 #include "exception-py.h"
 #include "load_metadata-py.h"
@@ -55,6 +56,10 @@ static struct PyMethodDef createrepo_c_methods[] = {
      METH_VARARGS, NULL},
     {"xml_parse_repomd",        (PyCFunction)py_xml_parse_repomd,
      METH_VARARGS, NULL},
+    {"checksum_name_str",       (PyCFunction)py_checksum_name_str,
+     METH_VARARGS, NULL},
+    {"checksum_type",           (PyCFunction)py_checksum_type,
+     METH_VARARGS, NULL},
     { NULL }
 };
 
diff --git a/tests/python/tests/test_checksum.py b/tests/python/tests/test_checksum.py
new file mode 100644 (file)
index 0000000..f1fc669
--- /dev/null
@@ -0,0 +1,35 @@
+import unittest
+import shutil
+import tempfile
+import os.path
+import createrepo_c as cr
+
+from fixtures import *
+
+class TestCaseChecksum(unittest.TestCase):
+
+    def test_checksum_name_str(self):
+        self.assertEqual(cr.checksum_name_str(cr.MD5), "md5")
+        self.assertEqual(cr.checksum_name_str(cr.SHA), "sha")
+        self.assertEqual(cr.checksum_name_str(cr.SHA1), "sha1")
+        self.assertEqual(cr.checksum_name_str(cr.SHA224), "sha224")
+        self.assertEqual(cr.checksum_name_str(cr.SHA256), "sha256")
+        self.assertEqual(cr.checksum_name_str(cr.SHA384), "sha384")
+        self.assertEqual(cr.checksum_name_str(cr.SHA512), "sha512")
+        self.assertEqual(cr.checksum_name_str(65), None)
+
+    def test_checksum_type(self):
+        self.assertEqual(cr.checksum_type("sha256"), cr.SHA256)
+        self.assertEqual(cr.checksum_type("SHA256"), cr.SHA256)
+        self.assertEqual(cr.checksum_type("Sha256"), cr.SHA256)
+        self.assertEqual(cr.checksum_type("sHa256"), cr.SHA256)
+        self.assertEqual(cr.checksum_type("ShA256"), cr.SHA256)
+
+        self.assertEqual(cr.checksum_type("md5"), cr.MD5)
+        self.assertEqual(cr.checksum_type("sha"), cr.SHA)
+        self.assertEqual(cr.checksum_type("sha1"), cr.SHA1)
+        self.assertEqual(cr.checksum_type("sha224"), cr.SHA224)
+        self.assertEqual(cr.checksum_type("sha256"), cr.SHA256)
+        self.assertEqual(cr.checksum_type("sha384"), cr.SHA384)
+        self.assertEqual(cr.checksum_type("sha512"), cr.SHA512)
+