From 9b1a21031187e83de61d999b70a6d1cda7b68444 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Mon, 28 Dec 2009 01:21:27 +0100 Subject: net: Support usage/help of subcommands implemented in Python. --- source4/utils/net/net.c | 70 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 14 deletions(-) (limited to 'source4/utils/net') diff --git a/source4/utils/net/net.c b/source4/utils/net/net.c index 29d736fb15..298204b01f 100644 --- a/source4/utils/net/net.c +++ b/source4/utils/net/net.c @@ -52,6 +52,29 @@ #include #include "scripting/python/modules.h" +static PyObject *py_commands(void) +{ + PyObject *netcmd_module, *py_cmds; + netcmd_module = PyImport_ImportModule("samba.netcmd"); + if (netcmd_module == NULL) { + PyErr_Print(); + return NULL; + } + + py_cmds = PyObject_GetAttrString(netcmd_module, "commands"); + if (py_cmds == NULL) { + PyErr_Print(); + return NULL; + } + + if (!PyDict_Check(py_cmds)) { + fprintf(stderr, "Python net commands is not a dictionary\n"); + return NULL; + } + + return py_cmds; +} + /* run a function from a function table. If not found then call the specified usage function @@ -62,6 +85,7 @@ int net_run_function(struct net_context *ctx, int (*usage_fn)(struct net_context *ctx, int argc, const char **argv)) { int i; + PyObject *py_cmds, *py_cmd; if (argc == 0) { return usage_fn(ctx, argc, argv); @@ -75,6 +99,21 @@ int net_run_function(struct net_context *ctx, return functable[i].fn(ctx, argc-1, argv+1); } + py_cmds = py_commands(); + if (py_cmds == NULL) { + return 1; + } + + py_cmd = PyDict_GetItemString(py_cmds, argv[0]); + if (py_cmd != NULL) { + PyObject *ret = PyObject_CallMethod(py_cmd, "run", ""); + if (ret == NULL) { + PyErr_Print(); + return 1; + } + return PyInt_AsLong(ret); + } + d_printf("No command: %s\n", argv[0]); return usage_fn(ctx, argc, argv); } @@ -87,6 +126,7 @@ int net_run_usage(struct net_context *ctx, const struct net_functable *functable) { int i; + PyObject *py_cmds, *py_cmd; for (i=0; functable[i].name; i++) { if (strcasecmp_m(argv[0], functable[i].name) == 0) @@ -95,6 +135,21 @@ int net_run_usage(struct net_context *ctx, } } + py_cmds = py_commands(); + if (py_cmds == NULL) { + return 1; + } + + py_cmd = PyDict_GetItemString(py_cmds, argv[0]); + if (py_cmd != NULL) { + PyObject *ret = PyObject_CallMethod(py_cmd, "usage", ""); + if (ret == NULL) { + PyErr_Print(); + return 1; + } + return PyInt_AsLong(ret); + } + d_printf("No usage information for command: %s\n", argv[0]); return 1; @@ -135,25 +190,12 @@ static int net_help_builtin(const struct net_functable *ftable) static int net_help_python(void) { - PyObject *netcmd_module; PyObject *py_cmds; PyObject *key, *value; Py_ssize_t pos = 0; - netcmd_module = PyImport_ImportModule("samba.netcmd"); - if (netcmd_module == NULL) { - PyErr_Print(); - return 1; - } - - py_cmds = PyObject_GetAttrString(netcmd_module, "commands"); + py_cmds = py_commands(); if (py_cmds == NULL) { - PyErr_Print(); - return 1; - } - - if (!PyDict_Check(py_cmds)) { - fprintf(stderr, "Python net commands is not a dictionary\n"); return 1; } -- cgit