~ubuntu-branches/ubuntu/utopic/libuser/utopic-proposed

« back to all changes in this revision

Viewing changes to python/misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Ghe Rivero
  • Date: 2005-09-30 16:22:04 UTC
  • Revision ID: james.westby@ubuntu.com-20050930162204-qubxaa7e2lbovdgh
Tags: upstream-0.54.dfsg.1
ImportĀ upstreamĀ versionĀ 0.54.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2001,2002 Red Hat, Inc.
 
2
 *
 
3
 * This is free software; you can redistribute it and/or modify it under
 
4
 * the terms of the GNU Library General Public License as published by
 
5
 * the Free Software Foundation; either version 2 of the License, or
 
6
 * (at your option) any later version.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Library General Public
 
14
 * License along with this program; if not, write to the Free Software
 
15
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
16
 */
 
17
 
 
18
#ident "$Id: misc.c,v 1.21 2005/09/12 21:52:02 mitr Exp $"
 
19
 
 
20
#include <Python.h>
 
21
#ifdef HAVE_CONFIG_H
 
22
#include "config.h"
 
23
#endif
 
24
#include <pwd.h>
 
25
#include <grp.h>
 
26
#include <stdlib.h>
 
27
#include <unistd.h>
 
28
#include <glib.h>
 
29
#include "../lib/user.h"
 
30
#include "../lib/user_private.h"
 
31
#include "common.h"
 
32
 
 
33
static PyTypeObject PromptType;
 
34
#define Prompt_Check(__x) ((__x)->ob_type == &PromptType)
 
35
 
 
36
static struct libuser_prompt *libuser_prompt_new(void);
 
37
 
 
38
static gboolean
 
39
libuser_admin_python_prompter(struct lu_prompt *prompts, int count,
 
40
                              gpointer callback_data,
 
41
                              struct lu_error **error)
 
42
{
 
43
        PyObject *list = NULL, *tuple = NULL, *ret;
 
44
        PyObject **prompt_data = (PyObject **) callback_data;
 
45
        int i;
 
46
 
 
47
        DEBUG_ENTRY;
 
48
        if (count > 0) {
 
49
                if (!PyCallable_Check(prompt_data[0])) {
 
50
                        lu_error_new(error, lu_error_generic, NULL);
 
51
                        PyErr_SetString(PyExc_RuntimeError,
 
52
                                        "prompter is not callable");
 
53
                        DEBUG_EXIT;
 
54
                        return FALSE;
 
55
                }
 
56
                list = PyList_New(0);
 
57
                for (i = 0; i < count; i++) {
 
58
                        struct libuser_prompt *prompt;
 
59
 
 
60
                        prompt = libuser_prompt_new();
 
61
                        prompt->prompt.key = g_strdup(prompts[i].key);
 
62
                        prompt->prompt.prompt = g_strdup(prompts[i].prompt);
 
63
                        prompt->prompt.domain = g_strdup(prompts[i].domain);
 
64
                        prompt->prompt.visible = prompts[i].visible;
 
65
                        prompt->prompt.default_value
 
66
                          = g_strdup(prompts[i].default_value);
 
67
                        prompt->prompt.value = g_strdup(prompts[i].value);
 
68
                        prompt->prompt.free_value = g_free;
 
69
                        PyList_Append(list, (PyObject *) prompt);
 
70
                        Py_DECREF(prompt);
 
71
                }
 
72
                tuple = PyTuple_New(PyTuple_Check(prompt_data[1]) ?
 
73
                                    PyTuple_Size(prompt_data[1]) + 1 : 1);
 
74
                PyTuple_SetItem(tuple, 0, list);
 
75
                if (PyTuple_Check(prompt_data[1])) {
 
76
                        for (i = 0; i < PyTuple_Size(prompt_data[1]); i++) {
 
77
                                PyObject *obj;
 
78
 
 
79
                                obj = PyTuple_GetItem(prompt_data[1], i);
 
80
                                Py_INCREF(obj);
 
81
                                PyTuple_SetItem(tuple, i + 1, obj);
 
82
                        }
 
83
                }
 
84
                ret = PyObject_CallObject(prompt_data[0], tuple);
 
85
                if (PyErr_Occurred()) {
 
86
                        PyErr_Print();
 
87
                        Py_DECREF(tuple);
 
88
                        DEBUG_EXIT;
 
89
                        lu_error_new(error, lu_error_generic,
 
90
                                     _
 
91
                                     ("error while prompting for necessary information"));
 
92
                        return FALSE;
 
93
                }
 
94
                for (i = 0; i < count; i++) {
 
95
                        struct libuser_prompt *prompt;
 
96
                        prompt =
 
97
                            (struct libuser_prompt *) PyList_GetItem(list,
 
98
                                                                     i);
 
99
                        prompts[i].value = g_strdup(prompt->prompt.value);
 
100
                        prompts[i].free_value = g_free;
 
101
                }
 
102
                Py_DECREF(tuple);
 
103
                Py_DECREF(ret);
 
104
        }
 
105
 
 
106
        DEBUG_EXIT;
 
107
        return TRUE;
 
108
}
 
109
 
 
110
static PyObject *
 
111
libuser_admin_prompt(struct libuser_admin *self, PyObject * args,
 
112
                     PyObject * kwargs, lu_prompt_fn * prompter)
 
113
{
 
114
        int count, i;
 
115
        PyObject *list = NULL, *item = NULL, *moreargs = NULL;
 
116
        struct lu_prompt *prompts = NULL;
 
117
        struct lu_error *error = NULL;
 
118
        gboolean success = FALSE;
 
119
        char *keywords[] = { "prompt_list", "more_args", NULL };
 
120
 
 
121
        g_return_val_if_fail(self != NULL, NULL);
 
122
 
 
123
        DEBUG_ENTRY;
 
124
        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O", keywords,
 
125
                                         &PyList_Type, &list,
 
126
             &moreargs)) {
 
127
                DEBUG_EXIT;
 
128
                return NULL;
 
129
        }
 
130
        DEBUG_CALL;
 
131
        count = PyList_Size(list);
 
132
        DEBUG_CALL;
 
133
        for (i = 0; i < count; i++) {
 
134
                item = PyList_GetItem(list, i);
 
135
                DEBUG_CALL;
 
136
                if (!Prompt_Check(item)) {
 
137
                        PyErr_SetString(PyExc_TypeError,
 
138
                                        "expected list of Prompt objects");
 
139
                        DEBUG_EXIT;
 
140
                        return NULL;
 
141
                }
 
142
                DEBUG_CALL;
 
143
        }
 
144
        DEBUG_CALL;
 
145
        count = PyList_Size(list);
 
146
        DEBUG_CALL;
 
147
        prompts = g_malloc0(count * sizeof(struct lu_prompt));
 
148
        DEBUG_CALL;
 
149
 
 
150
        for (i = 0; i < count; i++) {
 
151
                struct libuser_prompt *obj;
 
152
                obj = (struct libuser_prompt *) PyList_GetItem(list, i);
 
153
                Py_INCREF(obj);
 
154
                prompts[i].key = g_strdup(obj->prompt.key ? : "");
 
155
                prompts[i].domain = g_strdup(obj->prompt.domain ? : "");
 
156
                prompts[i].prompt = g_strdup(obj->prompt.prompt ? : "");
 
157
                prompts[i].default_value =
 
158
                    obj->prompt.default_value ? g_strdup(obj->prompt.default_value) :
 
159
                    NULL;
 
160
                prompts[i].visible = obj->prompt.visible;
 
161
                /* FIXME: free the values sometime? */
 
162
        }
 
163
#ifdef DEBUG_BINDING
 
164
        fprintf(stderr, "Prompter function promptConsole is at <%p>.\n",
 
165
                lu_prompt_console);
 
166
        fprintf(stderr,
 
167
                "Prompter function promptConsoleQuiet is at <%p>.\n",
 
168
                lu_prompt_console_quiet);
 
169
        fprintf(stderr, "Calling prompter function at <%p>.\n", prompter);
 
170
#endif
 
171
        success = prompter(prompts, count, self->prompt_data, &error);
 
172
        if (success) {
 
173
                for (i = 0; i < count; i++) {
 
174
                        struct libuser_prompt *obj;
 
175
                        obj = (struct libuser_prompt *) PyList_GetItem(list, i);
 
176
                        obj->prompt.value = g_strdup(prompts[i].value ? : "");
 
177
                        obj->prompt.free_value = (typeof(obj->prompt.free_value)) g_free;
 
178
                        if (prompts[i].value && prompts[i].free_value) {
 
179
                                prompts[i].free_value(prompts[i].value);
 
180
                                prompts[i].value = NULL;
 
181
                                prompts[i].free_value = NULL;
 
182
                        }
 
183
                        Py_DECREF(obj);
 
184
                }
 
185
                DEBUG_EXIT;
 
186
                return Py_BuildValue("");
 
187
        } else {
 
188
                for (i = 0; i < count; i++) {
 
189
                        PyObject *obj;
 
190
 
 
191
                        obj = PyList_GetItem(list, i);
 
192
                        Py_DECREF(obj);
 
193
                }
 
194
                PyErr_SetString(PyExc_RuntimeError,
 
195
                                "error prompting the user for information");
 
196
                DEBUG_EXIT;
 
197
                return NULL;
 
198
        }
 
199
}
 
200
 
 
201
static PyObject *
 
202
libuser_admin_prompt_console(PyObject * self, PyObject * args,
 
203
                             PyObject * kwargs)
 
204
{
 
205
        DEBUG_CALL;
 
206
        return libuser_admin_prompt((struct libuser_admin *) self, args,
 
207
                                    kwargs, lu_prompt_console);
 
208
}
 
209
 
 
210
static PyObject *
 
211
libuser_admin_prompt_console_quiet(PyObject * self, PyObject * args,
 
212
                                   PyObject * kwargs)
 
213
{
 
214
        DEBUG_CALL;
 
215
        return libuser_admin_prompt((struct libuser_admin *) self, args,
 
216
                                    kwargs, lu_prompt_console_quiet);
 
217
}
 
218
 
 
219
static void
 
220
libuser_prompt_destroy(struct libuser_prompt *self)
 
221
{
 
222
        DEBUG_ENTRY;
 
223
        if (self->prompt.value && self->prompt.free_value)
 
224
                self->prompt.free_value(self->prompt.value);
 
225
        g_free((void *)self->prompt.key);
 
226
        g_free((void *)self->prompt.prompt);
 
227
        g_free((void *)self->prompt.domain);
 
228
        g_free((void *)self->prompt.default_value);
 
229
        memset(&self->prompt, 0, sizeof(self->prompt));
 
230
        PyMem_DEL(self);
 
231
        DEBUG_EXIT;
 
232
}
 
233
 
 
234
static PyObject *
 
235
libuser_prompt_getattr(struct libuser_prompt *self, char *attr)
 
236
{
 
237
        DEBUG_ENTRY;
 
238
        if (strcmp(attr, "key") == 0) {
 
239
                DEBUG_EXIT;
 
240
                return PyString_FromString(self->prompt.key);
 
241
        }
 
242
        if (strcmp(attr, "prompt") == 0) {
 
243
                DEBUG_EXIT;
 
244
                return PyString_FromString(self->prompt.prompt);
 
245
        }
 
246
        if (strcmp(attr, "domain") == 0) {
 
247
                DEBUG_EXIT;
 
248
                return PyString_FromString(self->prompt.domain ?: "");
 
249
        }
 
250
        if (strcmp(attr, "visible") == 0) {
 
251
                DEBUG_EXIT;
 
252
                return PyInt_FromLong(self->prompt.visible);
 
253
        }
 
254
        if ((strcmp(attr, "default_value") == 0) ||
 
255
            (strcmp(attr, "defaultValue") == 0)) {
 
256
                DEBUG_EXIT;
 
257
                return self->prompt.
 
258
                    default_value ? PyString_FromString(self->prompt.
 
259
                                                        default_value) :
 
260
                    Py_BuildValue("");
 
261
        }
 
262
        if (strcmp(attr, "value") == 0) {
 
263
                DEBUG_EXIT;
 
264
                return self->prompt.value ? PyString_FromString(self->
 
265
                                                                prompt.
 
266
                                                                value) :
 
267
                    Py_BuildValue("");
 
268
        }
 
269
        DEBUG_EXIT;
 
270
        return Py_FindMethod(NULL, (PyObject *) self, attr);
 
271
}
 
272
 
 
273
static int
 
274
libuser_prompt_setattr(struct libuser_prompt *self, const char *attr,
 
275
                       PyObject * args)
 
276
{
 
277
        DEBUG_ENTRY;
 
278
        if (strcmp(attr, "prompt") == 0) {
 
279
                if (!PyString_Check(args)) {
 
280
                        PyErr_SetString(PyExc_TypeError,
 
281
                                        "prompt must be a string");
 
282
                        DEBUG_EXIT;
 
283
                        return -1;
 
284
                }
 
285
                g_free((char *) self->prompt.prompt);
 
286
                self->prompt.prompt = g_strdup(PyString_AsString(args));
 
287
                DEBUG_EXIT;
 
288
                return 0;
 
289
        }
 
290
        if (strcmp(attr, "domain") == 0) {
 
291
                if (!PyString_Check(args)) {
 
292
                        PyErr_SetString(PyExc_TypeError,
 
293
                                        "domain must be a string");
 
294
                        DEBUG_EXIT;
 
295
                        return -1;
 
296
                }
 
297
                g_free((char *) self->prompt.domain);
 
298
                self->prompt.domain = g_strdup(PyString_AsString(args));
 
299
                DEBUG_EXIT;
 
300
                return 0;
 
301
        }
 
302
        if (strcmp(attr, "key") == 0) {
 
303
                if (!PyString_Check(args)) {
 
304
                        PyErr_SetString(PyExc_TypeError,
 
305
                                        "key must be a string");
 
306
                        DEBUG_EXIT;
 
307
                        return -1;
 
308
                }
 
309
                g_free((char *) self->prompt.key);
 
310
                self->prompt.key = g_strdup(PyString_AsString(args));
 
311
                DEBUG_EXIT;
 
312
                return 0;
 
313
        }
 
314
        if (strcmp(attr, "visible") == 0) {
 
315
                self->prompt.visible = PyObject_IsTrue(args);
 
316
                DEBUG_EXIT;
 
317
                return 0;
 
318
        }
 
319
        if ((strcmp(attr, "default_value") == 0) ||
 
320
            (strcmp(attr, "defaultValue") == 0)) {
 
321
                if (!PyString_Check(args)) {
 
322
                        PyErr_SetString(PyExc_TypeError,
 
323
                                        "default value must be a string");
 
324
                        DEBUG_EXIT;
 
325
                        return -1;
 
326
                }
 
327
                g_free((char *) self->prompt.default_value);
 
328
                self->prompt.default_value =
 
329
                    (args == Py_None) ?
 
330
                    NULL :
 
331
                    g_strdup(PyString_AsString(args));
 
332
                DEBUG_EXIT;
 
333
                return 0;
 
334
        }
 
335
        if (strcmp(attr, "value") == 0) {
 
336
                if (!PyString_Check(args)) {
 
337
                        PyErr_SetString(PyExc_TypeError,
 
338
                                        "value must be a string");
 
339
                        DEBUG_EXIT;
 
340
                        return -1;
 
341
                }
 
342
                if (self->prompt.value && self->prompt.free_value)
 
343
                        self->prompt.free_value(self->prompt.value);
 
344
                self->prompt.value = g_strdup(PyString_AsString(args));
 
345
                self->prompt.free_value =
 
346
                    (typeof(self->prompt.free_value)) g_free;
 
347
                DEBUG_EXIT;
 
348
                return 0;
 
349
        }
 
350
        DEBUG_EXIT;
 
351
        PyErr_SetString(PyExc_AttributeError, "invalid attribute");
 
352
        return -1;
 
353
}
 
354
 
 
355
static int
 
356
libuser_prompt_print(struct libuser_prompt *self, FILE * fp, int flags)
 
357
{
 
358
        (void)flags;
 
359
        fprintf(fp,
 
360
                "(key = \"%s\", prompt = \"%s\", domain = \"%s\", visible = %s, default_value = \"%s\", value = \"%s\")",
 
361
                self->prompt.key ? : "",
 
362
                self->prompt.prompt ? : "",
 
363
                self->prompt.domain ? : "",
 
364
                self->prompt.visible ? "true" : "false",
 
365
                self->prompt.default_value ? : "",
 
366
                self->prompt.value ? : "");
 
367
        return 0;
 
368
}
 
369
 
 
370
static struct libuser_prompt *
 
371
libuser_prompt_new(void)
 
372
{
 
373
        struct libuser_prompt *ret = NULL;
 
374
        DEBUG_ENTRY;
 
375
        ret = PyObject_NEW(struct libuser_prompt, &PromptType);
 
376
        if (ret != NULL) {
 
377
                memset(&ret->prompt, 0, sizeof(ret->prompt));
 
378
        }
 
379
        DEBUG_EXIT;
 
380
        return ret;
 
381
}
 
382
 
 
383
static PyTypeObject PromptType = {
 
384
        PyObject_HEAD_INIT(&PyType_Type)
 
385
            0,
 
386
        "Prompt",
 
387
        sizeof(struct libuser_prompt),
 
388
        0,
 
389
 
 
390
        (destructor) libuser_prompt_destroy,
 
391
        (printfunc) libuser_prompt_print,
 
392
        (getattrfunc) libuser_prompt_getattr,
 
393
        (setattrfunc) libuser_prompt_setattr,
 
394
        (cmpfunc) NULL,
 
395
        (reprfunc) NULL,
 
396
 
 
397
        (PyNumberMethods *) NULL,
 
398
        (PySequenceMethods *) NULL,
 
399
        (PyMappingMethods *) NULL,
 
400
        (hashfunc) NULL,
 
401
        (ternaryfunc) NULL,
 
402
        (reprfunc) NULL,
 
403
};