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,
22 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
24 static PyObject *sha_to_pyhex(const unsigned char *sha)
28 for (i = 0; i < 20; i++) {
29 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
30 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
33 return PyString_FromStringAndSize(hexsha, 40);
36 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
40 PyObject *ret, *item, *name;
42 if (!PyArg_ParseTuple(args, "s#", &text, &len))
54 mode = strtol(text, &text, 8);
57 PyErr_SetString(PyExc_RuntimeError, "Expected space");
64 namelen = strlen(text);
66 name = PyString_FromStringAndSize(text, namelen);
72 item = Py_BuildValue("(lN)", mode, sha_to_pyhex((unsigned char *)text+namelen+1));
78 if (PyDict_SetItem(ret, name, item) == -1) {
92 static PyMethodDef py_objects_methods[] = {
93 { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
94 { NULL, NULL, 0, NULL }
97 void init_objects(void)
101 m = Py_InitModule3("_objects", py_objects_methods, NULL);