From c6741751432a65f840a77538adc988dcfef482ed Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Tue, 11 Jun 2013 12:21:32 +0200 Subject: [PATCH] python: Add misc module. --- src/python/CMakeLists.txt | 1 + src/python/__init__.py | 3 ++ src/python/createrepo_cmodule.c | 3 ++ src/python/misc-py.c | 62 +++++++++++++++++++++++++++++++++++++++++ src/python/misc-py.h | 27 ++++++++++++++++++ tests/python/tests/test_misc.py | 47 +++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+) create mode 100644 src/python/misc-py.c create mode 100644 src/python/misc-py.h create mode 100644 tests/python/tests/test_misc.py diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index 50b6143..16f9eb8 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -19,6 +19,7 @@ SET (craeterepo_cmodule_SRCS exception-py.c load_metadata-py.c locate_metadata-py.c + misc-py.c package-py.c parsepkg-py.c repomd-py.c diff --git a/src/python/__init__.py b/src/python/__init__.py index 9dfc1f0..660edba 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -157,3 +157,6 @@ def xml_parse_repomd(path, repomdobj, warning_cb=None): checksum_name_str = _createrepo_c.checksum_name_str checksum_type = _createrepo_c.checksum_type + +def compress_file(src, dst, comtype, stat=None): + return _createrepo_c.compress_file(src, dst, comtype, stat) diff --git a/src/python/createrepo_cmodule.c b/src/python/createrepo_cmodule.c index d2f3d14..a41fae7 100644 --- a/src/python/createrepo_cmodule.c +++ b/src/python/createrepo_cmodule.c @@ -26,6 +26,7 @@ #include "exception-py.h" #include "load_metadata-py.h" #include "locate_metadata-py.h" +#include "misc-py.h" #include "package-py.h" #include "parsepkg-py.h" #include "repomd-py.h" @@ -60,6 +61,8 @@ static struct PyMethodDef createrepo_c_methods[] = { METH_VARARGS, NULL}, {"checksum_type", (PyCFunction)py_checksum_type, METH_VARARGS, NULL}, + {"compress_file", (PyCFunction)py_compress_file_with_stat, + METH_VARARGS, NULL}, { NULL } }; diff --git a/src/python/misc-py.c b/src/python/misc-py.c new file mode 100644 index 0000000..fcd9ba6 --- /dev/null +++ b/src/python/misc-py.c @@ -0,0 +1,62 @@ +/* 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" +#include "misc-py.h" +#include "contentstat-py.h" + +PyObject * +py_compress_file_with_stat(PyObject *self, PyObject *args) +{ + int type; + char *src, *dst; + PyObject *py_contentstat = NULL; + cr_ContentStat *contentstat; + GError *tmp_err = NULL; + + CR_UNUSED(self); + + if (!PyArg_ParseTuple(args, "sziO:py_compress_file", &src, &dst, &type, + &py_contentstat)) + return NULL; + + if (!py_contentstat || py_contentstat == Py_None) { + contentstat = NULL; + } else { + contentstat = ContentStat_FromPyObject(py_contentstat); + if (!contentstat) + return NULL; + } + + cr_compress_file_with_stat(src, dst, type, contentstat, &tmp_err); + if (tmp_err) { + PyErr_SetString(CrErr_Exception, tmp_err->message); + g_clear_error(&tmp_err); + return NULL; + } + + Py_RETURN_NONE; +} diff --git a/src/python/misc-py.h b/src/python/misc-py.h new file mode 100644 index 0000000..78ebca5 --- /dev/null +++ b/src/python/misc-py.h @@ -0,0 +1,27 @@ +/* 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_MISC_PY_H +#define CR_MISC_PY_H + +#include "src/createrepo_c.h" + +PyObject *py_compress_file_with_stat(PyObject *self, PyObject *args); + +#endif diff --git a/tests/python/tests/test_misc.py b/tests/python/tests/test_misc.py new file mode 100644 index 0000000..9a217c4 --- /dev/null +++ b/tests/python/tests/test_misc.py @@ -0,0 +1,47 @@ +import os.path +import tempfile +import unittest +import shutil +import createrepo_c as cr + +from fixtures import * + +class TestCaseMisc(unittest.TestCase): + + def setUp(self): + self.tmpdir = tempfile.mkdtemp(prefix="createrepo_ctest-") + self.nofile = os.path.join(self.tmpdir, "this_file_should_not_exists") + self.tmpfile = os.path.join(self.tmpdir, "file") + self.content = "some\nfoo\ncontent\n" + open(self.tmpfile, "w").write(self.content) + + def tearDown(self): + shutil.rmtree(self.tmpdir) + + def test_compress_file(self): + # Non exist file + self.assertRaises(cr.CreaterepoCError, cr.compress_file, + self.nofile, None, cr.BZ2) + + # Compression - use the same name+suffix + cr.compress_file(self.tmpfile, None, cr.BZ2) + self.assertTrue(os.path.isfile(self.tmpfile+".bz2")) + + # Compression - new name + new_name = os.path.join(self.tmpdir, "foobar.gz") + cr.compress_file(self.tmpfile, new_name, cr.GZ) + self.assertTrue(os.path.isfile(new_name)) + + # Compression - with stat + stat = cr.ContentStat(cr.SHA256) + cr.compress_file(self.tmpfile, None, cr.XZ, stat) + self.assertTrue(os.path.isfile(self.tmpfile+".xz")) + self.assertEqual(stat.checksum, "e61ebaa6241e335c779194ce7af98c590f1"\ + "b26a749f219b997a0d7d5a773063b") + self.assertEqual(stat.checksum_type, cr.SHA256) + self.assertEqual(stat.size, len(self.content)) + + # Check directory for unexpected files + self.assertEqual(set(os.listdir(self.tmpdir)), + set(['file.bz2', 'file.xz', 'file', 'foobar.gz'])) + -- 2.7.4