comparison env/lib/python3.9/site-packages/cwltool/mpi.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Experimental support for MPI."""
2 import inspect
3 import os
4 import re
5 from typing import List, Mapping, MutableMapping, Optional, Type, TypeVar, Union
6
7 from ruamel import yaml
8
9 MpiConfigT = TypeVar("MpiConfigT", bound="MpiConfig")
10
11 MPIRequirementName = "http://commonwl.org/cwltool#MPIRequirement"
12
13
14 class MpiConfig:
15 def __init__(
16 self,
17 runner: str = "mpirun",
18 nproc_flag: str = "-n",
19 default_nproc: Union[int, str] = 1,
20 extra_flags: Optional[List[str]] = None,
21 env_pass: Optional[List[str]] = None,
22 env_pass_regex: Optional[List[str]] = None,
23 env_set: Optional[Mapping[str, str]] = None,
24 ) -> None:
25 """
26 Initialize from the argument mapping.
27
28 Defaults are:
29 runner: "mpirun"
30 nproc_flag: "-n"
31 default_nproc: 1
32 extra_flags: []
33 env_pass: []
34 env_pass_regex: []
35 env_set: {}
36
37 Any unknown keys will result in an exception.
38 """
39 self.runner = runner
40 self.nproc_flag = nproc_flag
41 self.default_nproc = int(default_nproc)
42 self.extra_flags = extra_flags or []
43 self.env_pass = env_pass or []
44 self.env_pass_regex = env_pass_regex or []
45 self.env_set = env_set or {}
46
47 @classmethod
48 def load(cls: Type[MpiConfigT], config_file_name: str) -> MpiConfigT:
49 """Create the MpiConfig object from the contents of a YAML file.
50
51 The file must contain exactly one object, whose attributes must
52 be in the list allowed in the class initialiser (all are
53 optional).
54 """
55 with open(config_file_name) as cf:
56 data = yaml.main.round_trip_load(cf)
57 try:
58 return cls(**data)
59 except TypeError as e:
60 unknown = set(data.keys()) - set(inspect.signature(cls).parameters)
61 raise ValueError(f"Unknown key(s) in MPI configuration: {unknown}")
62
63 def pass_through_env_vars(self, env: MutableMapping[str, str]) -> None:
64 """Take the configured list of environment variables and pass them to the executed process."""
65 for var in self.env_pass:
66 if var in os.environ:
67 env[var] = os.environ[var]
68
69 for var_re in self.env_pass_regex:
70 r = re.compile(var_re)
71 for k in os.environ:
72 if r.match(k):
73 env[k] = os.environ[k]
74
75 def set_env_vars(self, env: MutableMapping[str, str]) -> None:
76 """Set some variables to the value configured."""
77 env.update(self.env_set)