source: TI05-delivery/trunk/src/python_ext/bbftpc.c @ 1248

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/python_ext/bbftpc.c@1248
Revision 1248, 2.8 KB checked in by spascoe, 16 years ago (diff)

renamed delivery.bbftpc.run() delivery.bbftpc.connect().

Line 
1/**
2 * NDG python embedded bbftp daemon module.
3 *
4 * @author Stephen Pascoe
5 *
6 * Copyright (C) 2006 CCLRC & NERC
7 *
8 * This software may be distributed under the terms of the Q Public Licence, version 1.0 or later.
9 *
10 */
11
12#include <Python.h>
13#include <stdio.h>
14
15#include "util.h"
16
17extern char **environ;
18
19int bbftpc_init(int argc, char **argv, char **envp);
20void bbftpc_final(void);
21int treatcommand(char *cmd);
22
23
24/**
25 * Main entry point into the bbftp client from python.
26 */
27static PyObject *bbftpc_connect(PyObject *self, PyObject *args) {
28  int i, argc;
29  struct ndg_argv argv_s;
30  char *arg;
31  PyObject *client_args, *item;
32
33  if (!PyArg_ParseTuple(args, "O", &client_args)) {
34    return NULL;
35  }
36  Py_INCREF(client_args);
37
38  /*
39   * Convert arguments into a standard argv sequence.
40   */
41
42  if ((argc = PySequence_Size(client_args)) == -1) {
43    return NULL;
44  }
45  if (!ndg_argv_init(&argv_s, argc + 1)) {
46    PyErr_SetString(PyExc_MemoryError, "malloc failed");
47    Py_DECREF(client_args);
48    return NULL;
49  }
50
51  ndg_argv_add(&argv_s, "bbftpc_connect");
52  for (i=0; i<argc; i++) {
53    if ((item = PySequence_GetItem(client_args, i)) == NULL) {
54      ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s);
55      Py_DECREF(client_args);
56      return NULL;
57    }
58    if ((arg = PyString_AsString(item)) == NULL) {
59      ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s);
60      Py_DECREF(item);
61      Py_DECREF(client_args);
62      return NULL;
63    }
64
65    if (!ndg_argv_add(&argv_s, arg)) {
66
67      PyErr_SetString(PyExc_MemoryError, "ndg_argv_add failed");
68      ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s);
69      Py_DECREF(item);
70      Py_DECREF(client_args);
71      return NULL;
72    }
73
74    Py_DECREF(item);
75
76  }
77  Py_DECREF(client_args);
78
79  bbftpc_init(argv_s.argc, argv_s.argv, environ);
80  ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s);
81
82  Py_RETURN_NONE;
83}
84
85/**
86 * Close the connection
87 */
88static PyObject *bbftpc_close(PyObject *self, PyObject *args) {
89
90 
91
92  // Check there are no arguments
93  if (!PyArg_ParseTuple(args, "", NULL)) {
94    return NULL;
95  }
96
97  bbftpc_final();
98
99  Py_RETURN_NONE;
100}
101
102static PyObject *bbftpc_docommand(PyObject *self, PyObject *args) {
103  char *cmd;
104  PyObject *ret;
105
106  if (!PyArg_ParseTuple(args, "s", &cmd)) {
107    return NULL;
108  }
109
110  if ((ret = Py_BuildValue("i", treatcommand(cmd))) == NULL) {
111    return NULL;
112  }
113
114  return ret;
115}
116
117   
118
119static PyMethodDef BbftpcMethods[] = {
120  {
121    "connect", bbftpc_connect, METH_VARARGS,
122    "Execute the bbftp client.\n"
123    "\n"
124    "@param args: command line arguments"
125  },
126  {
127    "close", bbftpc_close, METH_VARARGS,
128    "Close the bbftp client."
129  },
130  {
131    "docommand", bbftpc_docommand, METH_VARARGS,
132    "Run a command in the client.\n"
133    "\n"
134    "@param cmd: The command string."
135  },
136  {NULL, NULL, 0, NULL}
137};
138
139PyMODINIT_FUNC initbbftpc(void) {
140  (void) Py_InitModule("bbftpc", BbftpcMethods);
141}
Note: See TracBrowser for help on using the repository browser.