<commit_before>"""Run bcbio generated CWL with a supported tool.

Handles wrapping and integrating with multiple tools making it easier
to run bcbio in a standard way in many environments.
"""
import glob
import os
import subprocess

from bcbio import utils

def _get_main_and_json(directory):
    """Retrieve the main CWL and sample JSON files from a bcbio generated directory.
    """
    directory = os.path.normpath(os.path.abspath(directory))
    main_cwl = glob.glob(os.path.join(directory, "main-*.cwl"))
    assert len(main_cwl) == 1, "Did not find main CWL in %s" % directory
    main_json = glob.glob(os.path.join(directory, "main-*-samples.json"))
    assert len(main_json) == 1, "Did not find main json in %s" % directory
    return main_cwl[0], main_json[0]

def _run_tool(cmd):
    if isinstance(cmd, (list, tuple)):
        cmd = " ".join([str(x) for x in cmd])
    cmd = utils.local_path_export() + cmd
    subprocess.check_call(cmd, shell=True)

def _run_cwltool(args):
    """Run with cwltool -- reference implementation.
    """
    main_file, json_file = _get_main_and_json(args.directory)
    if args.no_container:
        flags = ["--no-container", "--preserve-environment", "PATH", "HOME"]
    else:
        flags = []
    cmd = ["cwltool"] + flags + args.toolargs + [main_file, json_file]
    _run_tool(cmd)

def _run_arvados(args):
    """Run CWL on Aravdos.
    """
    assert not args.no_container, "No container is not available with Arvados runs"
    assert "ARVADOS_API_TOKEN" in os.environ and "ARVADOS_API_HOST" in os.environ, \
        "Need to set ARVADOS_API_TOKEN and ARVADOS_API_HOST in environment to run"
    main_file, json_file = _get_main_and_json(args.directory)
    flags = ["--local", "--enable-reuse"]
    cmd = ["arvados-cwl-runner"] + flags + args.toolargs + [main_file, json_file]
    _run_tool(cmd)

def _run_toil(args):
    """Run CWL with Toil.
    """
    main_file, json_file = _get_main_and_json(args.directory)
    work_dir = utils.safe_makedir(os.path.join(os.getcwd(), "cwltoil_work"))
    log_file = os.path.join(work_dir, "cwltoil.log")
    jobstore = os.path.join(work_dir, "cwltoil_jobstore")
    flags = ["--jobStore", jobstore, "--logFile", log_file, "--workDir", work_dir]
    if os.path.exists(jobstore):
        flags += ["--restart"]
    if args.no_container:
        flags += ["--no-container", "--preserve-environment", "PATH", "HOME"]
    if "--batchSystem" in args.toolargs and "--disableSharedCache" not in args.toolargs:
        flags += ["--disableSharedCache"]
    cmd = ["cwltoil"] + flags + args.toolargs + [main_file, json_file]
    with utils.chdir(work_dir):
        _run_tool(cmd)<commit_msg>CWL: Toil no longer supports --disableSharedCache<commit_after>"""Run bcbio generated CWL with a supported tool.

Handles wrapping and integrating with multiple tools making it easier
to run bcbio in a standard way in many environments.
"""
import glob
import os
import subprocess

from bcbio import utils

def _get_main_and_json(directory):
    """Retrieve the main CWL and sample JSON files from a bcbio generated directory.
    """
    directory = os.path.normpath(os.path.abspath(directory))
    main_cwl = glob.glob(os.path.join(directory, "main-*.cwl"))
    assert len(main_cwl) == 1, "Did not find main CWL in %s" % directory
    main_json = glob.glob(os.path.join(directory, "main-*-samples.json"))
    assert len(main_json) == 1, "Did not find main json in %s" % directory
    return main_cwl[0], main_json[0]

def _run_tool(cmd):
    if isinstance(cmd, (list, tuple)):
        cmd = " ".join([str(x) for x in cmd])
    cmd = utils.local_path_export() + cmd
    subprocess.check_call(cmd, shell=True)

def _run_cwltool(args):
    """Run with cwltool -- reference implementation.
    """
    main_file, json_file = _get_main_and_json(args.directory)
    if args.no_container:
        flags = ["--no-container", "--preserve-environment", "PATH", "HOME"]
    else:
        flags = []
    cmd = ["cwltool"] + flags + args.toolargs + [main_file, json_file]
    _run_tool(cmd)

def _run_arvados(args):
    """Run CWL on Aravdos.
    """
    assert not args.no_container, "No container is not available with Arvados runs"
    assert "ARVADOS_API_TOKEN" in os.environ and "ARVADOS_API_HOST" in os.environ, \
        "Need to set ARVADOS_API_TOKEN and ARVADOS_API_HOST in environment to run"
    main_file, json_file = _get_main_and_json(args.directory)
    flags = ["--local", "--enable-reuse"]
    cmd = ["arvados-cwl-runner"] + flags + args.toolargs + [main_file, json_file]
    _run_tool(cmd)

def _run_toil(args):
    """Run CWL with Toil.
    """
    main_file, json_file = _get_main_and_json(args.directory)
    work_dir = utils.safe_makedir(os.path.join(os.getcwd(), "cwltoil_work"))
    log_file = os.path.join(work_dir, "cwltoil.log")
    jobstore = os.path.join(work_dir, "cwltoil_jobstore")
    flags = ["--jobStore", "file:%s" % jobstore, "--logFile", log_file, "--workDir", work_dir]
    if os.path.exists(jobstore):
        flags += ["--restart"]
    if args.no_container:
        flags += ["--no-container", "--preserve-environment", "PATH", "HOME"]
    cmd = ["cwltoil"] + flags + args.toolargs + [main_file, json_file]
    with utils.chdir(work_dir):
        _run_tool(cmd)
{'fraction_non_alphanumeric': 0.13694207027540362, 'fraction_numerical': 0.0015194681861348527, 'mean_word_length': 3.595113438045375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '461093', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1827, 'n_words': 460}