Skip to content

Instantly share code, notes, and snippets.

@antevens
Last active July 19, 2016 16:23
Show Gist options
  • Save antevens/20517c7f5825a4bad3c54c68f50a9cba to your computer and use it in GitHub Desktop.
Save antevens/20517c7f5825a4bad3c54c68f50a9cba to your computer and use it in GitHub Desktop.
#include <Python.h>
#include <string.h>
/*
This program imports a Python module and checks for any instances of a
the predefined python class or instances of child classes.
Each instance is considered to represent a subcommand which will be made
available as a subcommand under this program.
Note that under no circumstances should the subcommands be spawned using
any shell or should shells be invoked due to security concerns.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright © 2015 SDElements Inc.
*/
#define PYTHON_MODULE "sde_admin"
#define PYTHON_COMMAND_CLASS "SdeCommand"
#define PYTHON_COMMAND_FACTORY "CommandFactory"
// Accept an arbitrary number of arguments to pass on to subcommands
int main(int argc, char *argv[]) {
PyObject *pModule, *pInst, *pHelpFunc, *pClass, *pMethod, *pFactory;
PyObject *pArgs, *pValue;
int i;
if (argc < 2 || ! strcmp(argv[1], "--help" ) || ! strcmp(argv[1], "-h" )) {
fprintf(stderr,"Usage: %s subcommand [args]\n For help on usage see: %s help\n", argv[0], argv[0]);
return 1;
}
Py_SetProgramName(argv[0]); // optional but recommended
Py_Initialize();
// Try importing the module and test if it worked
pModule = PyImport_Import(PyString_FromString(PYTHON_MODULE));
if (pModule != NULL) {
// Find the class that defines executables and make sure it's callable
// along with the factory with predefined commands
pClass = PyObject_GetAttrString(pModule, PYTHON_COMMAND_CLASS);
pFactory = PyObject_GetAttrString(pModule, PYTHON_COMMAND_FACTORY);
if (PyCallable_Check(pClass) && PyCallable_Check(pFactory) ) {
// Find the factory method for the subcommand, instantiate
pMethod = PyObject_GetAttrString(pFactory, argv[1]);
if (pMethod && PyCallable_Check(pMethod)) {
pInst = PyObject_CallObject(pMethod, NULL);
if (pInst && PyObject_IsInstance(pInst, pClass)) {
// Make argument list
pArgs = PyTuple_New(argc - 2);
for (i = 0; i < argc - 2; ++i) {
pValue = PyString_FromString(argv[i + 2]);
if (!pValue) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
PyTuple_SetItem(pArgs, i, pValue);
}
// Call the instance since it implements it's own __call__
pValue = PyObject_CallObject(pInst, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
//printf("Result of call: %ld\n", PyInt_AsLong(pValue));
//printf("Successfully executed command: \"%s\" \n", argv[1]);
Py_DECREF(pValue);
} else {
Py_DECREF(pInst);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
} else {
if (PyErr_Occurred()) {
PyErr_Print();
}
if (strcmp(argv[1], "help" ) != 0) {
fprintf(stderr, "Cannot find \"%s\" command on this system\n", argv[1]);
}
pHelpFunc = PyObject_GetAttrString(pClass, "help");
if (pHelpFunc && PyCallable_Check(pHelpFunc)) {
PyObject_CallObject(pHelpFunc, NULL);
}
return 1;
}
Py_XDECREF(pInst);
} else {
if (PyErr_Occurred()) {
PyErr_Print();
}
fprintf(stderr, "Cannot find \"%s\" command, it is not a recognized subcommand\n", argv[1]);
pHelpFunc = PyObject_GetAttrString(pClass, "help");
if (pHelpFunc && PyCallable_Check(pHelpFunc)) {
PyObject_CallObject(pHelpFunc, NULL);
}
return 1;
}
Py_XDECREF(pMethod);
Py_XDECREF(pClass);
Py_XDECREF(pModule);
} else {
if (PyErr_Occurred()) {
PyErr_Print();
}
fprintf(stderr, "Expected command and factory classes %s and %s not found in module %s \n", PYTHON_COMMAND_CLASS, PYTHON_COMMAND_FACTORY, PYTHON_MODULE);
return 1;
}
} else {
PyErr_Print();
fprintf(stderr, "Failed to load %s module\n", PYTHON_MODULE);
return 1;
}
// Clean up Python Interpreter
// Re-enable this once Python fixes threading/subprocess error on finalize
//Py_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment