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))
56 mode = strtol(text, &text, 8);
59 PyErr_SetString(PyExc_RuntimeError, "Expected space");
66 namelen = strlen(text);
68 name = PyString_FromStringAndSize(text, namelen);
74 item = Py_BuildValue("(lN)", mode,
75 sha_to_pyhex((unsigned char *)text+namelen+1));
81 if (PyDict_SetItem(ret, name, item) == -1) {
101 int cmp_tree_item(const void *_a, const void *_b)
103 const struct tree_item *a = _a, *b = _b;
104 const char *remain_a, *remain_b;
106 if (strlen(a->name) > strlen(b->name)) {
107 common = strlen(b->name);
108 remain_a = a->name + common;
109 remain_b = (S_ISDIR(b->mode)?"/":"");
110 } else if (strlen(b->name) > strlen(a->name)) {
111 common = strlen(a->name);
112 remain_a = (S_ISDIR(a->mode)?"/":"");
113 remain_b = b->name + common;
114 } else { /* strlen(a->name) == strlen(b->name) */
119 ret = strncmp(a->name, b->name, common);
122 return strcmp(remain_a, remain_b);
125 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
127 struct tree_item *qsort_entries;
131 PyObject *key, *value;
133 if (!PyDict_Check(entries)) {
134 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
138 num = PyDict_Size(entries);
139 qsort_entries = malloc(num * sizeof(struct tree_item));
140 if (qsort_entries == NULL) {
146 while (PyDict_Next(entries, &pos, &key, &value)) {
147 PyObject *py_mode, *py_sha;
149 if (PyTuple_Size(value) != 2) {
150 PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
155 py_mode = PyTuple_GET_ITEM(value, 0);
156 py_sha = PyTuple_GET_ITEM(value, 1);
157 qsort_entries[i].tuple = Py_BuildValue("(OOO)", key, py_mode, py_sha);
158 if (!PyString_CheckExact(key)) {
159 PyErr_SetString(PyExc_TypeError, "Name is not a string");
163 qsort_entries[i].name = PyString_AS_STRING(key);
164 if (!PyInt_CheckExact(py_mode)) {
165 PyErr_SetString(PyExc_TypeError, "Mode is not an int");
169 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
173 qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
175 ret = PyList_New(num);
182 for (i = 0; i < num; i++) {
183 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
191 static PyMethodDef py_objects_methods[] = {
192 { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
193 { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
194 { NULL, NULL, 0, NULL }
197 void init_objects(void)
201 m = Py_InitModule3("_objects", py_objects_methods, NULL);