• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pyiron / executorlib / 11749909901

08 Nov 2024 09:35PM UTC coverage: 95.011% (-0.2%) from 95.203%
11749909901

Pull #481

github

web-flow
Merge 9afcb5665 into 213202831
Pull Request #481: Fix working directory

2 of 7 new or added lines in 2 files covered. (28.57%)

895 of 942 relevant lines covered (95.01%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/executorlib/standalone/cache/queue.py
1
import os
1✔
2
import subprocess
1✔
3
from typing import List, Optional, Union
1✔
4

5
from pysqa import QueueAdapter
1✔
6

7

8
def execute_with_pysqa(
1✔
9
    command: str,
10
    resource_dict: dict,
11
    task_dependent_lst: List[int] = [],
12
    config_directory: Optional[str] = None,
13
    backend: Optional[str] = None,
14
    cache_directory: Optional[str] = None,
15
) -> int:
16
    """
17
    Execute a command by submitting it to the queuing system
18

19
    Args:
20
        command (list): The command to be executed.
21
        task_dependent_lst (list): A list of subprocesses that the current subprocess depends on. Defaults to [].
22
        resource_dict (dict): resource dictionary, which defines the resources used for the execution of the function.
23
                              Example resource dictionary: {
24
                                  cwd: None,
25
                              }
26
        config_directory (str, optional): path to the config directory.
27
        backend (str, optional): name of the backend used to spawn tasks.
28
        cache_directory (str): The directory to store the HDF5 files.
29

30
    Returns:
31
        int: queuing system ID
32
    """
33
    if resource_dict is None:
1✔
NEW
34
        resource_dict = {"cwd": cache_directory}
×
35
    elif len(resource_dict) == 0:
1✔
NEW
36
        resource_dict = {"cwd": cache_directory}
×
37
    elif "cwd" in resource_dict and resource_dict["cwd"] is None:
1✔
NEW
38
        resource_dict["cwd"] = cache_directory
×
39
    qa = QueueAdapter(
1✔
40
        directory=config_directory,
41
        queue_type=backend,
42
        execute_command=_pysqa_execute_command,
43
    )
44
    submit_kwargs = {
1✔
45
        "command": " ".join(command),
46
        "dependency_list": [str(qid) for qid in task_dependent_lst],
47
        "working_directory": os.path.abspath(resource_dict["cwd"]),
48
    }
49
    del resource_dict["cwd"]
1✔
50
    unsupported_keys = [
1✔
51
        "threads_per_core",
52
        "gpus_per_core",
53
        "openmpi_oversubscribe",
54
        "slurm_cmd_args",
55
    ]
56
    for k in unsupported_keys:
1✔
57
        if k in resource_dict:
1✔
58
            del resource_dict[k]
1✔
59
    submit_kwargs.update(resource_dict)
1✔
60
    return qa.submit_job(**submit_kwargs)
1✔
61

62

63
def _pysqa_execute_command(
1✔
64
    commands: str,
65
    working_directory: Optional[str] = None,
66
    split_output: bool = True,
67
    shell: bool = False,
68
    error_filename: str = "pysqa.err",
69
) -> Union[str, List[str]]:
70
    """
71
    A wrapper around the subprocess.check_output function. Modified from pysqa to raise an exception if the subprocess
72
    fails to submit the job to the queue.
73

74
    Args:
75
        commands (str): The command(s) to be executed on the command line
76
        working_directory (str, optional): The directory where the command is executed. Defaults to None.
77
        split_output (bool, optional): Boolean flag to split newlines in the output. Defaults to True.
78
        shell (bool, optional): Additional switch to convert commands to a single string. Defaults to False.
79
        error_filename (str, optional): In case the execution fails, the output is written to this file. Defaults to "pysqa.err".
80

81
    Returns:
82
        Union[str, List[str]]: Output of the shell command either as a string or as a list of strings
83
    """
84
    if shell and isinstance(commands, list):
1✔
85
        commands = " ".join(commands)
1✔
86
    out = subprocess.check_output(
1✔
87
        commands,
88
        cwd=working_directory,
89
        stderr=subprocess.STDOUT,
90
        universal_newlines=True,
91
        shell=not isinstance(commands, list),
92
    )
93
    if out is not None and split_output:
1✔
94
        return out.split("\n")
1✔
95
    else:
96
        return out
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc