pyboinc / boincmodule.cpp

Nicolás Alvarez cf7b831 

Nicolás Alvarez fcd0ce0 
Nicolás Alvarez cf7b831 















Michael Tughan d26a01d 
Nicolás Alvarez 0128fe9 
Michael Tughan d26a01d 
Nicolás Alvarez 0128fe9 
Michael Tughan d26a01d 
Nicolás Alvarez cf7b831 


Jeremy Cowles 4f99e80 

Nicolás Alvarez cf7b831 
Nicolás Alvarez 7f68748 

Nicolás Alvarez 4fccab8 

Nicolás Alvarez e7192a7 
Nicolás Alvarez 4fccab8 
Nicolás Alvarez ca4c49f 
Nicolás Alvarez 4fccab8 


Nicolás Alvarez 58f6090 







Nicolás Alvarez 8139630 
Jeremy Cowles 0e19556 
Jeremy Cowles 1bc02ce 









Nicolás Alvarez ca4c49f 
Jeremy Cowles 1bc02ce 






Jeremy Cowles 4f99e80 



Nicolás Alvarez cf7b831 









Nicolás Alvarez 58f6090 
Nicolás Alvarez cf7b831 

Nicolás Alvarez b3e6c6f 

Nicolás Alvarez 7f68748 

Nicolás Alvarez b3e6c6f 

Nicolás Alvarez 7f68748 
Nicolás Alvarez b3e6c6f 


Nicolás Alvarez 58f6090 














Jeremy Cowles 0e19556 





Jeremy Cowles f8fcc81 



Nicolás Alvarez 898c1e7 
Jeremy Cowles f8fcc81 

Jeremy Cowles 0e19556 



Jeremy Cowles 1bc02ce 






Jeremy Cowles 6ea3936 
Nicolás Alvarez 612b418 
Jeremy Cowles 1bc02ce 

























Nicolás Alvarez 612b418 


Jeremy Cowles 1bc02ce 
Jeremy Cowles 55ae496 
Jeremy Cowles 1bc02ce 










Jeremy Cowles 55ae496 
Jeremy Cowles 1bc02ce 










Nicolás Alvarez 612b418 
Jeremy Cowles 1bc02ce 








































Jeremy Cowles 4f99e80 








// PyBOINCAPI - Python bindings to the BOINC API
//
// Copyright (C) 2009 Nicolás Alvarez, Jeremy Cowles
//
// 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/>.

#include "boincmodule.h"

#ifdef __APPLE__
# include <Python/Python.h>
#else
# include <Python.h>
#endif

#include "boinc_api.h"

char* pyboinc_app_name;

PyMethodDef BoincMethods[] = {
    {"init",   pyboinc_init, METH_VARARGS, "init()\n\n"
        "Initializes the BOINC runtime."},
    {"finish", pyboinc_finish, METH_VARARGS,
    "finish(status)\n\n"
    "Terminates the BOINC science app. Must be called at the end of the Python"
    "program.\n"
    "If the status is zero, it means we have successfully finished the task.\n"
    "If the status is non-zero, the task will exit with an unrecoverable error,\n"
    "which will be reported back to the server by the core client."
    },
    {"resolve_filename", pyboinc_resolve_filename, METH_VARARGS,
    "resolve_filename(filename) -> string\n\n"
    "Resolves a BOINC soft link and returns the real path to the file. If the\n"
    "passed filename is not a soft link, the string is returned unchanged.\n"
    "Note this function is only needed for files described in the input or\n"
    "output templates. Temporary files needed by the application should be just\n"
    "written into the current directory."
    },
    {"set_fraction_done", pyboinc_fraction_done, METH_VARARGS,
     "Indicates the fraction done for the current workunit."},
    {"time_to_checkpoint", pyboinc_time_to_checkpoint, METH_VARARGS,
     "Returns True when it's time to checkpoint"},
    {"checkpoint_completed", pyboinc_checkpoint_completed, METH_VARARGS, 
     "Call after a successful checkpoint."},
    {"wu_cpu_time", pyboinc_wu_cpu_time, METH_VARARGS, 
     "Gets the total CPU time from the beginning of the workunit."},
    {"elapsed_time", pyboinc_elapsed_time, METH_VARARGS, 
     "Gets the elapsed time since the start of the current episode."},
    {"is_standalone", pyboinc_is_standalone, METH_VARARGS, 
     "Returns True if the application is running in standalone mode.\n"
     "This is useful for testing or adding additional debugging output \n"
     "when in standalone mode."},
    {"need_network", pyboinc_need_network, METH_VARARGS, 
     "Alert the user that a network connection is needed."},
    {"network_poll", pyboinc_network_poll, METH_VARARGS, 
     "Returns True when the network is ready."},
    {"network_done", pyboinc_network_done, METH_VARARGS,
     "Indicates that the network connection is no longer needed."},
    {"get_app_name", pyboinc_get_app_name, METH_VARARGS,
     "Returns the name of the executable originally launched\n"
     "This can be useful in determining the platform that the app is\n"
     "currently running on."},
    {NULL, NULL, 0, NULL}
};

PyObject* pyboinc_init(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }
    int retval = boinc_init();
    //FIXME should throw an exception if init fails
    return PyInt_FromLong(retval);
}
PyObject* pyboinc_finish(PyObject* self, PyObject* args)
{
    int status;
    if (!PyArg_ParseTuple(args, "i", &status)) {
        return NULL;
    }
    boinc_finish(status);
    // should never even reach here
    Py_RETURN_NONE;
}
PyObject* pyboinc_resolve_filename(PyObject* self, PyObject* args)
{
    const char* filename;
    if (!PyArg_ParseTuple(args, "s", &filename)) {
        return NULL;
    }
    std::string result;
    int retval = boinc_resolve_filename_s(filename, result);
    if (retval) {
        //FIXME return an appropriate exception instead
        Py_RETURN_NONE;
    }
    return PyString_FromStringAndSize(result.data(), result.length());
}

PyObject* pyboinc_fraction_done(PyObject* self, PyObject* args)
{
    double fraction;
    if (!PyArg_ParseTuple(args, "d", &fraction)) {
        return NULL;
    }

    // sanity checks on fraction, 0 <= fraction <= 1
    if ((fraction < 0) || (fraction > 1)){
        PyErr_SetString(PyExc_ValueError, "Fraction argument must been in the range [0...1]");
        return NULL;
    }

    boinc_fraction_done(fraction);
    Py_RETURN_NONE;
}

PyObject* pyboinc_time_to_checkpoint(PyObject* self, PyObject* args) 
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }

    if(boinc_time_to_checkpoint()){
        Py_RETURN_TRUE;
    }

    Py_RETURN_FALSE;
}

PyObject* pyboinc_checkpoint_completed(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }

    boinc_checkpoint_completed();
    Py_RETURN_NONE;
}

PyObject* pyboinc_wu_cpu_time(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }

    double time = 0;
    int stat = boinc_wu_cpu_time(time);
    if (!stat){
        // internal BOINC error
        PyErr_SetString(PyExc_RuntimeError, "BOINC Call failed for unknown reason (returned 0)");
        return NULL;
    }
    return PyFloat_FromDouble(time);
}


PyObject* pyboinc_elapsed_time(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }

    double time = boinc_elapsed_time();
    return PyFloat_FromDouble(time);
}

PyObject* pyboinc_is_standalone(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }

    int sa = boinc_is_standalone();
    if (sa){
        Py_RETURN_TRUE;
    }

    Py_RETURN_FALSE;
}

PyObject* pyboinc_need_network(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }

    boinc_need_network();
    Py_RETURN_NONE;
}

PyObject* pyboinc_network_poll(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }

    int val = boinc_network_poll();
    if (val == 0){
        Py_RETURN_TRUE;
    }

    Py_RETURN_FALSE;
}

PyObject* pyboinc_network_done(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }

    boinc_network_done();
    Py_RETURN_NONE;
}

PyObject* pyboinc_get_app_name(PyObject* self, PyObject* args)
{
    //make sure no arguments are passed
    if (!PyArg_ParseTuple(args, "")) {
        return NULL;
    }
    
    return PyString_FromString(pyboinc_app_name);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.