Exemple #1
0
rpmRC rpmpythonRunFile(rpmpython python, const char * fn, const char ** resultp)
{
    rpmRC rc = RPMRC_FAIL;


if (_rpmpython_debug)
fprintf(stderr, "==> %s(%p,%s)\n", __FUNCTION__, python, fn);

    if (python == NULL) python = rpmpythonI();

    if (fn != NULL) {
#if defined(WITH_PYTHONEMBED)
	const char * pyfn = ((fn == NULL || !strcmp(fn, "-")) ? "<stdin>" : fn);
	FILE * pyfp = (!strcmp(pyfn, "<stdin>") ? stdin : fopen(fn, "rb"));
	int closeit = (pyfp != stdin);
	PyCompilerFlags cf = { .cf_flags = 0 };
	
	if (pyfp != NULL) {
	    PyRun_AnyFileExFlags(pyfp, pyfn, closeit, &cf);
	    rc = RPMRC_OK;
	}
#endif
    }
    return rc;
}

static const char * rpmpythonSlurp(const char * arg)
	/*@*/
{
    rpmiob iob = NULL;
    const char * val = NULL;
    struct stat sb;
    int xx;

    if (!strcmp(arg, "-")) {	/* Macros from stdin arg. */
	xx = rpmiobSlurp(arg, &iob);
    } else
    if ((arg[0] == '/' || strchr(arg, ' ') == NULL)
     && !Stat(arg, &sb)
     && S_ISREG(sb.st_mode)) {	/* Macros from a file arg. */
	xx = rpmiobSlurp(arg, &iob);
    } else {			/* Macros from string arg. */
	iob = rpmiobAppend(rpmiobNew(strlen(arg)+1), arg, 0);
    }

    val = xstrdup(rpmiobStr(iob));
    iob = rpmiobFree(iob);
    return val;
}
//============================================================================
// MAIN PROGRAM
//============================================================================
int main( int argc, char** argv )
{
    if ( parseArgs(argc,argv) != 0 ) return 1;

    // Initialize the Python environment. Will clean it up when destroyed.
    UsingPython usingPython;

    // Load the readline module if available
    if (isatty(fileno(stdin)))
    {
        PyObject *v;
        v = PyImport_ImportModule("readline");
        if (v==NULL)
        {
            PyErr_Clear();
        }
        else
        {
            Py_DECREF(v);
        }
    }

    SPELLlog::instance().setLogFile("SHELL","SHELL");
    SPELLlog::instance().enableLog(true);
    SPELLlog::instance().enableTraces(true);

    // Install log support
    Log_Install();

    try
    {
        // Setup the execution environment
    	std::cerr << "Loading SPELL framework" << std::endl;
        SPELLpythonHelper::instance().loadFramework();
        // Load the SPELL configuration (will fail with exception if there is an error)
    	std::cerr << "Loading configuration" << std::endl;
        SPELLconfiguration::instance().loadConfig(configFile);
        // Load the configuration on python side for the language and drivers
        SPELLconfiguration::instance().loadPythonConfig(configFile);
        // Load user libraries
    	std::cerr << "Loading user libraries" << std::endl;
    	SPELLprocedureManager::instance().setup(contextName);
    	std::string libPath = SPELLprocedureManager::instance().getLibPath();
        if (isDirectory(libPath))
        {
            SPELLpythonHelper::instance().addToPath(libPath);
            std::list<std::string> files = getFilesInDir(libPath);
            std::list<std::string>::iterator it;
            std::list<std::string>::iterator end = files.end();
            for( it = files.begin(); it != end; it++)
            {
                std::string filename = (*it);
                if (filename == "__init__.py" ) continue;
                std::size_t idx = filename.find(".py");
                if ((idx != std::string::npos) && (idx>0))
                {
                	std::cerr << "    - library " << filename << std::endl;
                    std::string module = filename.substr(0,idx);
                    SPELLpythonHelper::instance().importAllFrom(module);
                }
            }
        }

    }
    catch(SPELLcoreException& ex)
    {
        std::cerr << "FATAL ERROR: cannot initialize: " << ex.what() << std::endl;
        return 1;
    }

    // Will cleanup the driver manager when destroyed
    UsingDriver usingDriver;

    try
    {
        // Load the driver
        SPELLdriverManager::instance().setup(contextName);
    }
    catch(SPELLcoreException& ex)
    {
        std::cerr << "FATAL ERROR: cannot setup driver: " << ex.what() << std::endl;
        return 1;
    }

    // Load predefined databases and interfaces
    PyObject* dbMgr = SPELLregistry::instance().get("DBMGR");
    if (dbMgr == NULL)
    {
        std::cerr << "WARNING: cannot install DB manager" << std::endl;
    }
    else
    {
        // Install the runtime databases
    	SPELLdatabaseManager::instance().loadBuiltinDatabases();
    }


    signal(SIGABRT, signalHandler);
    signal(SIGTERM, signalHandler);

    SPELLexecutorIF* executor = new SPELLshellExecutor();
    SPELLcif* cif = new SPELLshellCif();
    SPELLschedulerIF* scheduler = new SPELLscheduler(true);
    SPELLcontrollerIF* controller = new SPELLshellController();
    SPELLcallstackIF* callstack = new SPELLshellCallstack();
    executor->initialize( cif, controller, scheduler, callstack, NULL );
    SPELLexecutor::setInstance(executor);

    ShellExecutor_Install();
    ShellClientIF_Install( cif );

    // Launch the interactive loop
    int status = 0;
    try
    {
        status = PyRun_AnyFileExFlags( stdin, "<stdin>", 0, NULL );
    }
    catch(SPELLcoreException& ex)
    {
        std::cerr << "FATAL ERROR: " << ex.what() << std::endl;
        status = ex.getCode();
    }

    return status;
}