<gh_stars>0
def submit_sbatch(id, dirs):
    """
    Helper function to submit sbatch scripts. It does so from a "crashes" file, such that any
    nipype related crash files would dump to a "crashes" directory corresponding to the sbatch submission.
    This hopefully makes debugging a bit easier?
    :param id: sbatch_id (int)
    :param dirs: dirs dictionary generated by calculate_directories.
    :return:
    """
    from pathlib import Path
    import subprocess
    import os

    script_to_submit = Path(dirs["slurm_scripts"]) / f"sb-{str(id).zfill(4)}.sh"
    if not script_to_submit.exists():
        raise FileNotFoundError(
            f"Script for sbatch_id {id} not found\n\t(expected: {str(script_to_submit)})"
        )

    from_path = Path(dirs["crashes"])
    if not from_path.exists():
        raise FileNotFoundError(
            "Crashes directory not found. Did you initialize this wd correctly?"
        )

    # create directory from which we submit this
    # wd/crashes/sb-####/
    from_path = from_path / f"sb-{str(id).zfill(4)}"
    from_path.mkdir(parents=True, exist_ok=True)

    cmd_output = subprocess.check_output(
        ["sbatch", str(script_to_submit)],
        encoding="UTF-8",
        cwd=str(from_path),  # run from the pertinent crashes dir, so things are neat
    )

    # Output looks like this: 'Submitted batch job 18334739\n'
    slurm_id = cmd_output.strip().replace("Submitted batch job ", "")

    print(
        f"Sbatch job sb-{str(id).zfill(4)}.sh submitted.\n"
        f"The Slurm ID for this job (seen in squeue) is {slurm_id}."
    )

    # TODO: Save this sbatch_id and slurm_id crosswalk to an internal database, allowing us to track what slurm job id corresponds with our internal
    #       sbatch_id.

    return cmd_output

def submit_sbatch(id, dirs):
    """
    Helper function to submit sbatch scripts. It does so from a "crashes" file, such that any
    nipype related crash files would dump to a "crashes" directory corresponding to the sbatch submission.
    This hopefully makes debugging a bit easier?
    :param id: sbatch_id (int)
    :param dirs: dirs dictionary generated by calculate_directories.
    :return:
    """
    from pathlib import Path
    import subprocess
    import os

    script_to_submit = Path(dirs["slurm_scripts"]) / f"sb-{str(id).zfill(4)}.sh"
    if not script_to_submit.exists():
        raise FileNotFoundError(
            f"Script for sbatch_id {id} not found\n\t(expected: {str(script_to_submit)})"
        )

    from_path = Path(dirs["crashes"])
    if not from_path.exists():
        raise FileNotFoundError(
            "Crashes directory not found. Did you initialize this wd correctly?"
        )

    # create directory from which we submit this
    # wd/crashes/sb-####/
    from_path = from_path / f"sb-{str(id).zfill(4)}"
    from_path.mkdir(parents=True, exist_ok=True)

    cmd_output = subprocess.check_output(
        ["sbatch", str(script_to_submit)],
        encoding="UTF-8",
        cwd=str(from_path),  # run from the pertinent crashes dir, so things are neat
    )

    # Output looks like this: 'Submitted batch job 18334739\n'
    slurm_id = cmd_output.strip().replace("Submitted batch job ", "")

    print(
        f"Sbatch job sb-{str(id).zfill(4)}.sh submitted.\n"
        f"The Slurm ID for this job (seen in squeue) is {slurm_id}."
    )

    # TODO: Save this sbatch_id and slurm_id crosswalk to an internal database, allowing us to track what slurm job id corresponds with our internal
    #       sbatch_id.

    return cmd_output
Helper function to submit sbatch scripts. It does so from a "crashes" file, such that any     nipype related crash files would dump to a "crashes" directory corresponding to the sbatch submission.     This hopefully makes debugging a bit easier?     :param id: sbatch_id (int)     :param dirs: dirs dictionary generated by calculate_directories.     :return: # create directory from which we submit this # wd/crashes/sb-####/ # run from the pertinent crashes dir, so things are neat # Output looks like this: 'Submitted batch job 18334739\n' # TODO: Save this sbatch_id and slurm_id crosswalk to an internal database, allowing us to track what slurm job id corresponds with our internal #       sbatch_id.