FILE(COPY ${createrepo_c_COPIES} DESTINATION ./createrepo_c/)
SET (craeterepo_cmodule_SRCS
+ contentstat-py.c
createrepo_cmodule.c
exception-py.c
load_metadata-py.c
locate_metadata-py.c
- #metadata_hashtable.c
package-py.c
parsepkg-py.c
repomd-py.c
CreaterepoCError = _createrepo_c.CreaterepoCError
+# ContentStat class
+
+ContentStat = _createrepo_c.ContentStat
+
# Metadata class
Metadata = _createrepo_c.Metadata
XmlFile = _createrepo_c.XmlFile
class PrimaryXmlFile(XmlFile):
- def __init__(self, filename, compressiontype=GZ_COMPRESSION):
- XmlFile.__init__(self, filename, XMLFILE_PRIMARY, compressiontype)
+ def __init__(self, filename, compressiontype=GZ_COMPRESSION,
+ contentstat=None):
+ XmlFile.__init__(self, filename, XMLFILE_PRIMARY,
+ compressiontype, contentstat)
class FilelistsXmlFile(XmlFile):
- def __init__(self, filename, compressiontype=GZ_COMPRESSION):
- XmlFile.__init__(self, filename, XMLFILE_FILELISTS, compressiontype)
+ def __init__(self, filename, compressiontype=GZ_COMPRESSION,
+ contentstat=None):
+ XmlFile.__init__(self, filename, XMLFILE_FILELISTS,
+ compressiontype, contentstat)
class OtherXmlFile(XmlFile):
- def __init__(self, filename, compressiontype=GZ_COMPRESSION):
- XmlFile.__init__(self, filename, XMLFILE_OTHER, compressiontype)
+ def __init__(self, filename, compressiontype=GZ_COMPRESSION,
+ contentstat=None):
+ XmlFile.__init__(self, filename, XMLFILE_OTHER,
+ compressiontype, contentstat)
# Functions
#include "src/createrepo_c.h"
+#include "contentstat-py.h"
#include "exception-py.h"
#include "load_metadata-py.h"
#include "locate_metadata-py.h"
/* Objects */
+ /* _createrepo_c.ContentStat */
+ if (PyType_Ready(&ContentStat_Type) < 0)
+ return;
+ Py_INCREF(&ContentStat_Type);
+ PyModule_AddObject(m, "ContentStat", (PyObject *)&ContentStat_Type);
+
/* _createrepo_c.Package */
if (PyType_Ready(&Package_Type) < 0)
return;
*((int *) ((size_t) rec + (size_t) member_offset)) = (int) val;
return 0;
}
+
static int
set_str(_RepomdRecordObject *self, PyObject *value, void *member_offset)
{
#include "xml_file-py.h"
#include "package-py.h"
#include "exception-py.h"
+#include "contentstat-py.h"
#include "typeconversion.h"
typedef struct {
PyObject_HEAD
cr_XmlFile *xmlfile;
+ cr_ContentStat *stat;
} _XmlFileObject;
static PyObject * xmlfile_close(_XmlFileObject *self, void *nothing);
CR_UNUSED(args);
CR_UNUSED(kwds);
_XmlFileObject *self = (_XmlFileObject *)type->tp_alloc(type, 0);
- if (self)
+ if (self) {
self->xmlfile = NULL;
+ self->stat = NULL;
+ }
return (PyObject *)self;
}
char *path;
int type, comtype;
GError *err = NULL;
- PyObject *ret;
+ PyObject *py_stat, *ret;
+ cr_ContentStat *stat;
CR_UNUSED(kwds);
- if (!PyArg_ParseTuple(args, "sii|:xmlfile_init", &path, &type, &comtype))
+ if (!PyArg_ParseTuple(args, "siiO|:xmlfile_init",
+ &path, &type, &comtype, &py_stat))
return -1;
/* Check arguments */
return -1;
}
+ if (py_stat == Py_None) {
+ stat = NULL;
+ } else if (ContentStatObject_Check(py_stat)) {
+ stat = ContentStat_FromPyObject(py_stat);
+ } else {
+ PyErr_SetString(PyExc_ValueError, "Use ContentStat or None");
+ return -1;
+ }
+
/* Free all previous resources when reinitialization */
ret = xmlfile_close(self, NULL);
Py_XDECREF(ret);
+ Py_XDECREF(self->stat);
+ self->stat = NULL;
if (ret == NULL) {
// Error encountered!
return -1;
}
/* Init */
- self->xmlfile = cr_xmlfile_open(path, type, comtype, &err);
+ self->xmlfile = cr_xmlfile_sopen(path, type, comtype, stat, &err);
if (err) {
PyErr_Format(CrErr_Exception, "XmlFile initialization failed: %s", err->message);
g_clear_error(&err);
return -1;
}
+ self->stat = stat;
+ Py_XINCREF(stat);
return 0;
}
xmlfile_dealloc(_XmlFileObject *self)
{
cr_xmlfile_close(self->xmlfile, NULL);
+ Py_XDECREF(self->stat);
Py_TYPE(self)->tp_free(self);
}
CR_UNUSED(nothing);
+ Py_XDECREF(self->stat);
+ self->stat = NULL;
+
if (self->xmlfile) {
cr_xmlfile_close(self->xmlfile, &err);
self->xmlfile = NULL;
return NULL;
}
}
+
Py_RETURN_NONE;
}
--- /dev/null
+import unittest
+import shutil
+import tempfile
+import os.path
+import createrepo_c as cr
+
+from fixtures import *
+
+class TestCaseContentStat(unittest.TestCase):
+
+ # TODO:
+ # - Test rename_file() for files with checksum
+
+ def setUp(self):
+ self.tmpdir = tempfile.mkdtemp(prefix="createrepo_ctest-")
+
+ def tearDown(self):
+ shutil.rmtree(self.tmpdir)
+
+ def test_contentstat(self):
+ pkg = cr.package_from_rpm(PKG_ARCHER_PATH)
+ self.assertTrue(pkg)
+
+ cs = cr.ContentStat(cr.SHA256)
+ self.assertEqual(cs.size, 0)
+ self.assertEqual(cs.checksum_type, cr.SHA256)
+ self.assertEqual(cs.checksum, None)
+
+ path = os.path.join(self.tmpdir, "primary.xml.gz")
+ f = cr.PrimaryXmlFile(path, cr.GZ_COMPRESSION, cs)
+ self.assertTrue(f)
+ self.assertTrue(os.path.isfile(path))
+ f.add_pkg(pkg)
+ f.close()
+
+ self.assertTrue(os.path.isfile(path))
+
+ self.assertEqual(cs.size, 2686)
+ self.assertEqual(cs.checksum_type, cr.SHA256)
+ self.assertEqual(cs.checksum, "05509ef37239eb123d41c077626b788352ef94"\
+ "5200231a901802ef59e565978f")
+
def test_xmlfile_basic_operations(self):
pri = cr.XmlFile(self.tmpdir+"/primary.xml.gz",
cr.XMLFILE_PRIMARY,
- cr.GZ_COMPRESSION)
+ cr.GZ_COMPRESSION,
+ None)
self.assertTrue(pri)
self.assertTrue(os.path.isfile(self.tmpdir+"/primary.xml.gz"))
fil = cr.XmlFile(self.tmpdir+"/filelists.xml.gz",
cr.XMLFILE_FILELISTS,
- cr.GZ_COMPRESSION)
+ cr.GZ_COMPRESSION,
+ None)
self.assertTrue(fil)
self.assertTrue(os.path.isfile(self.tmpdir+"/filelists.xml.gz"))
oth = cr.XmlFile(self.tmpdir+"/other.xml.gz",
cr.XMLFILE_OTHER,
- cr.GZ_COMPRESSION)
+ cr.GZ_COMPRESSION,
+ None)
self.assertTrue(oth)
self.assertTrue(os.path.isfile(self.tmpdir+"/other.xml.gz"))
self.assertFalse(os.path.exists(path))
# Bad file type
- self.assertRaises(ValueError, cr.XmlFile, path, 86, cr.GZ_COMPRESSION)
+ self.assertRaises(ValueError, cr.XmlFile, path, 86,
+ cr.GZ_COMPRESSION, None)
self.assertFalse(os.path.exists(path))
# Bad compression type
- self.assertRaises(ValueError, cr.XmlFile, path, cr.XMLFILE_PRIMARY, 678)
+ self.assertRaises(ValueError, cr.XmlFile, path,
+ cr.XMLFILE_PRIMARY, 678, None)
+ self.assertFalse(os.path.exists(path))
+
+ # Bad contentstat object
+ self.assertRaises(ValueError, cr.XmlFile, path,
+ cr.XMLFILE_PRIMARY, 678, "foo")
self.assertFalse(os.path.exists(path))
# Non existing path
self.assertRaises(cr.CreaterepoCError, cr.PrimaryXmlFile,
- "foobar/foo/xxx/cvydmaticxuiowe")
+ "foobar/foo/xxx/cvydmaticxuiowe")
# Already existing file
open(path, "w").write("foobar")