summaryrefslogtreecommitdiff
path: root/source4/utils/net
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2009-12-28 01:21:27 +0100
committerJelmer Vernooij <jelmer@ganieda.vernstok.nl>2009-12-29 16:26:19 +0100
commit9b1a21031187e83de61d999b70a6d1cda7b68444 (patch)
tree753c2b8baf8509240685373f4b2344811e5f221f /source4/utils/net
parentc064549e2e29b1a7e100300fa7d851451a90a6a7 (diff)
downloadsamba-9b1a21031187e83de61d999b70a6d1cda7b68444.tar.gz
samba-9b1a21031187e83de61d999b70a6d1cda7b68444.tar.bz2
samba-9b1a21031187e83de61d999b70a6d1cda7b68444.zip
net: Support usage/help of subcommands implemented in Python.
Diffstat (limited to 'source4/utils/net')
-rw-r--r--source4/utils/net/net.c70
1 files changed, 56 insertions, 14 deletions
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 <Python.h>
#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;
}