##########################################################################
# Hopla - Copyright (C) AGrigis, 2015 - 2025
# Distributed under the terms of the CeCILL-B license, as published by
# the CEA-CNRS-INRIA. Refer to the LICENSE file or to
# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
# for details.
##########################################################################

import sys
import subprocess
from joblib import Parallel, delayed


def run_command(cmd):
    """
    Run a single command line string.
    Returns a dictionary with command, status code, stdout, stderr.
    """
    try:
        result = subprocess.run(
            cmd,
            shell=True,
            capture_output=True,
            text=True
        )
        return {{
            "command": cmd,
            "returncode": result.returncode,
            "stdout": result.stdout.strip(),
            "stderr": result.stderr.strip()
        }}
    except Exception as e:
        return {{
            "command": cmd,
            "returncode": -1,
            "stdout": "",
            "stderr": str(e)
        }}


if __name__ == "__main__":

    commands = [
        {commands}
    ]
    n_jobs = {njobs}

    results = Parallel(n_jobs=n_jobs)(
        delayed(run_command)(cmd) for cmd in commands
    )

    for item in results:
        print("="*40)
        print(f"Command   : {{item['command']}}")
        print(f"Returncode: {{item['returncode']}}")
        print(f"Stdout    : {{item['stdout']}}")
        print(f"Stderr    : {{item['stderr']}}")

    if any(item["returncode"] != 0 for item in results):
        sys.exit(1)
    else:
        sys.exit(0)

