<commit_before>/* dtkDistributedServerManagerOar.cpp ---
 *
 * Author: <NAME>
 * Copyright (C) 2008-2011 - <NAME>, Inria.
 * Created: Tue May 31 23:10:24 2011 (+0200)
 * Version: $Id$
 * Last-Updated: Thu Apr  5 16:14:20 2012 (+0200)
 *           By: <NAME>
 *     Update #: 387
 */

/* Commentary:
 *
 */

/* Change log:
 *
 */

#include "dtkDistributedServerManager_p.h"
#include "dtkDistributedServerManagerOar.h"

#include <dtkCore/dtkGlobal.h>

#include <dtkJson/dtkJson.h>

#include <dtkLog/dtkLog.h>

// /////////////////////////////////////////////////////////////////
// dtkDistributedServerManagerOar implementation
// /////////////////////////////////////////////////////////////////

QString  dtkDistributedServerManagerOar::submit(QString input)
{
    QString oarsub = "oarsub ";

    QVariantMap json = dtkJson::parse(input).toMap();

    QVariantMap jprops = json["properties"].toMap();
    QString properties ;
    if (jprops.contains("cpu_model")) {
        properties +=  " -p \"cputype='"+jprops["cpu_model"].toString()+"'\"";
    } else if (jprops.contains("cluster")) {
        properties +=  " -p \"cluster='"+jprops["cluster"].toString()+"'\"";
    }
    // TODO:handle other properties

    QVariantMap res = json["resources"].toMap();

    if (res["nodes"].toInt() == 0) {
        // no nodes, only cores;
        oarsub += " -l /core="+res["cores"].toString();
    } else if (res["cores"].toInt() == 0) {
        // no cores, only nodes;
        oarsub += " -l /nodes="+res["nodes"].toString();
    } else {
        oarsub += " -l /nodes="+res["nodes"].toString()+"/core="+res["cores"].toString();
    }

    // walltime, syntax=HH:MM:SS
    if (json.contains("walltime")) {
        oarsub += ",walltime="+json["walltime"].toString();
    }

    // script
    oarsub += " "+json["script"].toString();

    // queue
    if (json.contains("queue")) {
        oarsub += " -q "+json["queue"].toString();
    }
    // options
    if (json.contains("options")) {
        oarsub += " "+json["options"].toString();
    }

    dtkDebug() << DTK_PRETTY_FUNCTION << oarsub;
    QProcess stat; stat.start(oarsub);

    if (!stat.waitForStarted()) {
        dtkError() << "Unable to launch oarsub command";
        return QString("error");
    }

    if (!stat.waitForFinished()) {
        dtkError() << "Unable to completed oarsub command";
        return QString("error");
    }
    if (stat.exitCode() > 0) {
        QString error = stat.readAllStandardError();
        dtkError() << "Error running oarsub :" << error;
        return QString("error");
    } else {
        QString oarsubout = stat.readAll();

        QRegExp rx("OAR_JOB_ID=(\\d+)");
        int pos = rx.indexIn(oarsubout);
        QStringList jobid = rx.capturedTexts();

        Q_UNUSED(pos);
        
        dtkDebug() << DTK_PRETTY_FUNCTION << jobid.at(0);
        return jobid.at(0);
    }<commit_msg>fix OAR job submit<commit_after>/* dtkDistributedServerManagerOar.cpp ---
 *
 * Author: <NAME>
 * Copyright (C) 2008-2011 - <NAME>, Inria.
 * Created: Tue May 31 23:10:24 2011 (+0200)
 * Version: $Id$
 * Last-Updated: ven. avril 13 15:30:00 2012 (+0200)
 *           By: <NAME>
 *     Update #: 396
 */

/* Commentary:
 *
 */

/* Change log:
 *
 */

#include "dtkDistributedServerManager_p.h"
#include "dtkDistributedServerManagerOar.h"

#include <dtkCore/dtkGlobal.h>

#include <dtkJson/dtkJson.h>

#include <dtkLog/dtkLog.h>

// /////////////////////////////////////////////////////////////////
// dtkDistributedServerManagerOar implementation
// /////////////////////////////////////////////////////////////////

QString  dtkDistributedServerManagerOar::submit(QString input)
{
    QString oarsub = "oarsub ";

    QVariantMap json = dtkJson::parse(input).toMap();

    QVariantMap jprops = json["properties"].toMap();
    QString properties ;
    if (jprops.contains("cpu_model")) {
        properties +=  " -p \"cputype='"+jprops["cpu_model"].toString()+"'\"";
    } else if (jprops.contains("cluster")) {
        properties +=  " -p \"cluster='"+jprops["cluster"].toString()+"'\"";
    }
    // TODO:handle other properties

    QVariantMap res = json["resources"].toMap();

    if (res["nodes"].toInt() == 0) {
        // no nodes, only cores;
        oarsub += " -l /core="+res["cores"].toString();
    } else if (res["cores"].toInt() == 0) {
        // no cores, only nodes;
        oarsub += " -l /nodes="+res["nodes"].toString();
    } else {
        oarsub += " -l /nodes="+res["nodes"].toString()+"/core="+res["cores"].toString();
    }

    // walltime, syntax=HH:MM:SS
    if (json.contains("walltime")) {
        oarsub += ",walltime="+json["walltime"].toString();
    }


    // script
    if (json.contains("script")) {
        oarsub += " "+json["script"].toString();
    } else if (json.contains("application")) {

        QString scriptName = qApp->applicationDirPath() + "/dtkDistributedServerScript.sh";
        QFile script(scriptName);

        if (!script.open(QFile::WriteOnly|QFile::Truncate)) {
            dtkWarn() << "unable to open script for writing";
        } else {
            script.setPermissions(QFile::ExeOwner|QFile::ReadOwner|QFile::WriteOwner);
            QTextStream out(&script);
            out << "#!/bin/bash\n";
            out << "mpirun "
                + qApp->applicationDirPath()
                + "/"
                + json["application"].toString();
        }

        script.close();

        oarsub += " " + scriptName;

    } else {
        dtkError() << "no script and no application";
        return QString("ERROR");
    }

    // queue
    if (json.contains("queue")) {
        oarsub += " -q "+json["queue"].toString();
    }
    // options
    if (json.contains("options")) {
        oarsub += " "+json["options"].toString();
    }

    dtkDebug() << DTK_PRETTY_FUNCTION << oarsub;
    QProcess stat; stat.start(oarsub);

    if (!stat.waitForStarted()) {
        dtkError() << "Unable to launch oarsub command";
        return QString("error");
    }

    if (!stat.waitForFinished()) {
        dtkError() << "Unable to completed oarsub command";
        return QString("error");
    }
    if (stat.exitCode() > 0) {
        QString error = stat.readAllStandardError();
        dtkError() << "Error running oarsub :" << error;
        return QString("error");
    } else {
        QString oarsubout = stat.readAll();

        QRegExp rx("OAR_JOB_ID=(\\d+)");
        int pos = rx.indexIn(oarsubout);
        QStringList jobid = rx.capturedTexts();

        Q_UNUSED(pos);

        dtkDebug() << DTK_PRETTY_FUNCTION << jobid.at(1);
        return jobid.at(1);
    }
{'fraction_non_alphanumeric': 0.21355267129489888, 'fraction_numerical': 0.014337458496830668, 'mean_word_length': 2.2693635915145536, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3829603', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2089, 'n_words': 399}