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 #if (PY_VERSION_HEX < 0x02050000)
25 typedef int Py_ssize_t;
28 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
30 static PyObject *sha_to_pyhex(const unsigned char *sha)
34 for (i = 0; i < 20; i++) {
35 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
36 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
39 return PyString_FromStringAndSize(hexsha, 40);
42 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
44 char *text, *start, *end;
46 PyObject *ret, *item, *name;
48 if (!PyArg_ParseTuple(args, "s#", &text, &len))
51 /* TODO: currently this returns a list; if memory usage is a concern,
52 * consider rewriting as a custom iterator object */
64 mode = strtol(text, &text, 8);
67 PyErr_SetString(PyExc_ValueError, "Expected space");
74 namelen = strnlen(text, len - (text - start));
76 name = PyString_FromStringAndSize(text, namelen);
82 if (text + namelen + 20 >= end) {
83 PyErr_SetString(PyExc_ValueError, "SHA truncated");
89 item = Py_BuildValue("(NlN)", name, mode,
90 sha_to_pyhex((unsigned char *)text+namelen+1));
96 if (PyList_Append(ret, item) == -1) {
115 int cmp_tree_item(const void *_a, const void *_b)
117 const struct tree_item *a = _a, *b = _b;
118 const char *remain_a, *remain_b;
120 if (strlen(a->name) > strlen(b->name)) {
121 common = strlen(b->name);
122 remain_a = a->name + common;
123 remain_b = (S_ISDIR(b->mode)?"/":"");
124 } else if (strlen(b->name) > strlen(a->name)) {
125 common = strlen(a->name);
126 remain_a = (S_ISDIR(a->mode)?"/":"");
127 remain_b = b->name + common;
128 } else { /* strlen(a->name) == strlen(b->name) */
133 ret = strncmp(a->name, b->name, common);
136 return strcmp(remain_a, remain_b);
139 static void free_tree_items(struct tree_item *items, int num) {
141 for (i = 0; i < num; i++) {
142 Py_DECREF(items[i].tuple);
147 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
149 struct tree_item *qsort_entries;
153 PyObject *key, *value;
155 if (!PyDict_Check(entries)) {
156 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
160 num = PyDict_Size(entries);
161 qsort_entries = malloc(num * sizeof(struct tree_item));
162 if (qsort_entries == NULL) {
168 while (PyDict_Next(entries, &pos, &key, &value)) {
169 PyObject *py_mode, *py_int_mode, *py_sha;
171 if (!PyString_Check(key)) {
172 PyErr_SetString(PyExc_TypeError, "Name is not a string");
173 free_tree_items(qsort_entries, i);
177 if (PyTuple_Size(value) != 2) {
178 PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
179 free_tree_items(qsort_entries, i);
183 py_mode = PyTuple_GET_ITEM(value, 0);
184 py_int_mode = PyNumber_Int(py_mode);
186 PyErr_SetString(PyExc_TypeError, "Mode is not an integral type");
187 free_tree_items(qsort_entries, i);
191 py_sha = PyTuple_GET_ITEM(value, 1);
192 if (!PyString_Check(py_sha)) {
193 PyErr_SetString(PyExc_TypeError, "SHA is not a string");
194 Py_DECREF(py_int_mode);
195 free_tree_items(qsort_entries, i);
198 qsort_entries[i].name = PyString_AS_STRING(key);
199 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
200 qsort_entries[i].tuple = PyTuple_Pack(3, key, py_int_mode, py_sha);
201 Py_DECREF(py_int_mode);
205 qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
207 ret = PyList_New(num);
209 free_tree_items(qsort_entries, i);
214 for (i = 0; i < num; i++) {
215 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
223 static PyMethodDef py_objects_methods[] = {
224 { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
225 { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
226 { NULL, NULL, 0, NULL }
229 void init_objects(void)
233 m = Py_InitModule3("_objects", py_objects_methods, NULL);