Skip to content

mockslurm.mock_squeue

Mock of the squeue command of slurm.

The squeue -o, --format argument is supported except for the %all directive

Functions:

Name Description
count_nodes

Count the number of nodes requested by users in the nodelist.

parse_squeue_format

Convert squeue format argument (-o format) to a python format string and

count_nodes

count_nodes(nodelist)

Count the number of nodes requested by users in the nodelist.

Parameters:

Name Type Description Default
nodelist str

Nodelist string in slurm format: comma separated list of nodes or node ranges example: "cp12,cp18", "cp[10-14],cp28"

required

Returns:

Type Description
int

Number of nodes concerned by the nodelist

Source code in src/mockslurm/mock_squeue.py
def count_nodes(nodelist: str) -> int:
    """Count the number of nodes requested by users in the nodelist.

    Parameters
    ----------
    nodelist : str
        Nodelist string in slurm format: comma separated list of nodes or node ranges
        example: "cp12,cp18", "cp[10-14],cp28"

    Returns
    -------
    int
        Number of nodes concerned by the nodelist
    """
    if not nodelist:  # mock considers no nodelist jobs get allocated 1 node
        return 1

    nodecount = 0
    nlist = nodelist.split(",")
    for node_specs in nlist:
        if "[" in node_specs:
            _, node_specs = node_specs.split("[")  # ignore node name
        if "-" in node_specs:  # count nodes in range
            first, last = node_specs.split("-")
            last = last.split("]")[0]  # ignore closing "]" if any
            nodecount += int(last) - int(first) + 1
        else:  # not a range: single node
            nodecount += 1

    return nodecount

parse_squeue_format

parse_squeue_format(squeue_format_str)

Convert squeue format argument (-o format) to a python format string and list of function filling the values of the format string for each job DB row.

Parameters:

Name Type Description Default
squeue_format_str str

Squeue format string, eg "%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R".

required
Warning

Does not support squeue's "%all" formatting string.

Returns:

Type Description
tuple[str, list[str], list[Callable]]

python format string, and a list of callable that should be used to fill the values in the formatted string with a job DB row and job_index.

Examples:

>>> job_DB_row = {
...     "PID": -1,
...     "NAME": b"jobname",
...     "USER": b"bob",
...     "ACCOUNT": b"bob_name",
...     "PARTITION": b"",
...     "RESERVATION": b"",
...     "NODELIST": b"mocknode1",
...     "TIME": 0,
...     "START_TIME": datetime.datetime.now().timestamp(),
...     "CMD": "",
...     "STATE": JobState.PENDING,
...     "REASON": JobReason.WaitingForScheduling,
...     "EXIT_CODE": np.iinfo(np.int16).max,
... }
>>> row_idx = 0
>>> format_str, fields_header, fields_filler_fcts = parse_squeue_format("%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R")
>>> squeue_output = format_str.format(*[fct(job_DB_row, row_idx) for fct in fields_filler_fcts])
Source code in src/mockslurm/mock_squeue.py
def parse_squeue_format(squeue_format_str: str) -> tuple[str, list[str], list[Callable]]:
    """Convert squeue format argument (-o format) to a python format string and
    list of function filling the values of the format string for each job DB row.

    Parameters
    ----------
    squeue_format_str : str
        Squeue format string, eg "%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R".

    Warning
    -------
    Does not support squeue's "%all" formatting string.

    Returns
    -------
    tuple[str, list[str], list[Callable]]
        python format string, and a list of callable that should be used to fill the values
        in the formatted string with a job DB row and job_index.

    Examples
    --------
    >>> job_DB_row = {
    ...     "PID": -1,
    ...     "NAME": b"jobname",
    ...     "USER": b"bob",
    ...     "ACCOUNT": b"bob_name",
    ...     "PARTITION": b"",
    ...     "RESERVATION": b"",
    ...     "NODELIST": b"mocknode1",
    ...     "TIME": 0,
    ...     "START_TIME": datetime.datetime.now().timestamp(),
    ...     "CMD": "",
    ...     "STATE": JobState.PENDING,
    ...     "REASON": JobReason.WaitingForScheduling,
    ...     "EXIT_CODE": np.iinfo(np.int16).max,
    ... }
    >>> row_idx = 0
    >>> format_str, fields_header, fields_filler_fcts = parse_squeue_format("%.18i %.9P %.8j %.8u %.2t %.10M %.6D %R")
    >>> squeue_output = format_str.format(*[fct(job_DB_row, row_idx) for fct in fields_filler_fcts])
    """
    # Iterate on the format string and for each found %
    # add a "{:ndigit}" to the python string, with optional ">" if "." follows "%"
    # ndigit is found by converting the characters following %
    # the function filling the value is taken from the map of squeue format letters to DB function
    python_format_string = ""
    fields_header = []
    fields_filler_functions = []
    # split 1st value is empty if string starts with delimiter
    for field_format_str in squeue_format_str.split("%")[1:]:
        python_format_string += "{:"
        if field_format_str[0] == ".":
            python_format_string += ">"
            field_format_str = field_format_str[1:]
        idx = 0
        while field_format_str[idx].isdigit():
            python_format_string += field_format_str[idx]
            idx += 1
        header, fct = SQUEUE_FORMAT_LETTER_TO_DB_FIELD[field_format_str[idx]]
        fields_header.append(header)
        fields_filler_functions.append(fct)
        python_format_string += "}" + field_format_str[idx + 1 :]

    return python_format_string, fields_header, fields_filler_functions