summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source4/scripting/python/samba/netcmd/__init__.py4
-rw-r--r--source4/utils/net/net.c70
2 files changed, 60 insertions, 14 deletions
diff --git a/source4/scripting/python/samba/netcmd/__init__.py b/source4/scripting/python/samba/netcmd/__init__.py
index 54f3134d2f..f644febba6 100644
--- a/source4/scripting/python/samba/netcmd/__init__.py
+++ b/source4/scripting/python/samba/netcmd/__init__.py
@@ -23,6 +23,9 @@ class Command(object):
def _get_description(self):
return self.__doc__
+ def usage(self):
+ raise NotImplementedError
+
description = property(_get_description)
def run(self):
@@ -31,3 +34,4 @@ class Command(object):
commands = {}
+commands["foo"] = Command()
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;
}