def submit_job(self, job_type, settings, path_out, nr_items=1,
                   batch_size=1, version=DEFAULT_VERSION):

        path_out_settings = join(path_out, 'cluster')
        makedirs(path_out_settings)
        filename_settings = join(path_out_settings, 'settings.conf')

        f = file(filename_settings, 'w')
        f.write(settings)
        f.close()

        args = ['-s', filename_settings]

        # adjust the number of job items according to the batch size
        nr_items = int(math.ceil(float(nr_items)/batch_size))

        is_bulk_job = True #if nr_items > 1 else False

        jt = self._session.createJobTemplate()
        jt = cecog_job_template(jt, path_out, args, version,
                                batch_size, is_bulk_job)

        if is_bulk_job:
            job_id = self._session.runBulkJobs(jt, 1, nr_items, 1)
        else:
            job_id = self._session.runJob(jt)
        print job_id
        return job_id
def cecog_job_template(jt, path_out, args, version, batch_size=1,
                       is_bulk_job=False):

    job_name = 'CellCognition'
    base_path = join(VERSIONS_PATH, version)
    batchpy = join(base_path, 'bin', 'cecog_batch.py')

    # I want the almost the same environment as for the gateway!
    pypath = join(base_path, 'lib', 'python2.7', 'site-packages')
    if os.environ.has_key('PYTHONPATH'):
        os.environ["PYTHONPATH"] = pypath+os.pathsep+os.environ["PYTHONPATH"]
    else:
        os.putenv("PYTHONPATH", pypath)

    jt.jobName = job_name
    jt.workingDirectory = os.environ['HOME']
    print jt.workingDirectory

    pybin = 'python'
    if os.environ.has_key('PYTHON_BIN'):
        pybin = os.environ['PYTHON_BIN']

    jt.jobEnvironment = os.environ
    print jt.jobEnvironment
    jt.remoteCommand = pybin
    print jt.remoteCommand
    jt.args = [batchpy] + args
    jt.joinFiles = True

    jt.nativeSpecification = os.environ['JOB_PARAMS']

    path_out_cluster = join(path_out, 'cluster', 'log')
    makedirs(path_out_cluster)
    path_out_cluster = ':' + path_out_cluster
    if is_bulk_job:
        jt.outputPath = path_out_cluster
        # FIXME: another DRMAA hack: the PARAMETRIC_INDEX
        # is NOT resolved in args!

	# workaround for older batch scripts that don't use argparse module
        if version >= "1.7": 
            jt.args += ['--cluster-index', 'SGE_TASK_ID',
                        '--batch-size', str(batch_size)]
        else:
            jt.args += ['--cluster_index', 'SGE_TASK_ID',
                        '--batch_size', str(batch_size)]
    else:
        jt.outputPath = path_out_cluster
    return jt