From: Tomas Mlcoch Date: Mon, 10 Jun 2013 12:51:02 +0000 (+0200) Subject: Python: Add checksum module. X-Git-Tag: upstream/0.2.1~93 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f66390e2073ec2398e46f383abd825a89e7b55e7;p=services%2Fcreaterepo_c.git Python: Add checksum module. --- diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index 00aef57..50b6143 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -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 diff --git a/src/python/__init__.py b/src/python/__init__.py index afbae43..9dfc1f0 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -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 index 0000000..ed2dd37 --- /dev/null +++ b/src/python/checksum-py.c @@ -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 +#include +#include + +#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 index 0000000..933c0e0 --- /dev/null +++ b/src/python/checksum-py.h @@ -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 diff --git a/src/python/createrepo_cmodule.c b/src/python/createrepo_cmodule.c index 8fa9e0d..d2f3d14 100644 --- a/src/python/createrepo_cmodule.c +++ b/src/python/createrepo_cmodule.c @@ -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 index 0000000..f1fc669 --- /dev/null +++ b/tests/python/tests/test_checksum.py @@ -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) +