2 * Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; version 2
7 * of the License or (at your option) a later version of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
26 static PyObject *sha_to_pyhex(const unsigned char *sha)
30 for (i = 0; i < 20; i++) {
31 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
32 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
35 return PyString_FromStringAndSize(hexsha, 40);
38 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
42 PyObject *ret, *item, *name;
44 if (!PyArg_ParseTuple(args, "s#", &text, &len))
47 /* TODO: currently this returns a list; if memory usage is a concern,
48 * consider rewriting as a custom iterator object */
59 mode = strtol(text, &text, 8);
62 PyErr_SetString(PyExc_RuntimeError, "Expected space");
69 namelen = strlen(text);
71 name = PyString_FromStringAndSize(text, namelen);
77 if (text + namelen + 20 >= end) {
78 PyErr_SetString(PyExc_RuntimeError, "SHA truncated");
84 item = Py_BuildValue("(NlN)", name, mode,
85 sha_to_pyhex((unsigned char *)text+namelen+1));
91 if (PyList_Append(ret, item) == -1) {
110 int cmp_tree_item(const void *_a, const void *_b)
112 const struct tree_item *a = _a, *b = _b;
113 const char *remain_a, *remain_b;
115 if (strlen(a->name) > strlen(b->name)) {
116 common = strlen(b->name);
117 remain_a = a->name + common;
118 remain_b = (S_ISDIR(b->mode)?"/":"");
119 } else if (strlen(b->name) > strlen(a->name)) {
120 common = strlen(a->name);
121 remain_a = (S_ISDIR(a->mode)?"/":"");
122 remain_b = b->name + common;
123 } else { /* strlen(a->name) == strlen(b->name) */
128 ret = strncmp(a->name, b->name, common);
131 return strcmp(remain_a, remain_b);
134 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
136 struct tree_item *qsort_entries;
140 PyObject *key, *value;
142 if (!PyDict_Check(entries)) {
143 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
147 num = PyDict_Size(entries);
148 qsort_entries = malloc(num * sizeof(struct tree_item));
149 if (qsort_entries == NULL) {
155 while (PyDict_Next(entries, &pos, &key, &value)) {
156 PyObject *py_mode, *py_sha;
158 if (PyTuple_Size(value) != 2) {
159 PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
164 py_mode = PyTuple_GET_ITEM(value, 0);
165 py_sha = PyTuple_GET_ITEM(value, 1);
166 qsort_entries[i].tuple = Py_BuildValue("(OOO)", key, py_mode, py_sha);
167 if (!PyString_CheckExact(key)) {
168 PyErr_SetString(PyExc_TypeError, "Name is not a string");
172 qsort_entries[i].name = PyString_AS_STRING(key);
173 if (!PyInt_CheckExact(py_mode)) {
174 PyErr_SetString(PyExc_TypeError, "Mode is not an int");
178 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
182 qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
184 ret = PyList_New(num);
191 for (i = 0; i < num; i++) {
192 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
200 static PyMethodDef py_objects_methods[] = {
201 { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
202 { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
203 { NULL, NULL, 0, NULL }
206 void init_objects(void)
210 m = Py_InitModule3("_objects", py_objects_methods, NULL);