-
Notifications
You must be signed in to change notification settings - Fork 1
/
shwrapper.py
49 lines (39 loc) · 1.49 KB
/
shwrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Wrappers for command lines tools"""
import os
import subprocess
def env(**kwargs):
"""Return a modified shell environement"""
_env = os.environ.copy()
for key, val in kwargs.items():
_env[key] = str(val)
return _env
def make_sh_cmd(cmd):
"""Create a subprocess wrapper around cmd"""
def sh_cmd(args=None, _env=None, _out=None):
"""Run cmd with args"""
if args is None:
args = []
if not isinstance (args, (list, tuple)):
args = [args, ]
if _env is None:
_env = env(OMP_NUM_THREADS=1)
if _out is None:
_out = subprocess.PIPE
full_cmd = cmd + args
if isinstance(_out, str):
with open(_out, 'w') as out_fh:
return subprocess.Popen(full_cmd, env=_env, stdout=out_fh,
stderr=subprocess.STDOUT,
universal_newlines=True)
else:
return subprocess.Popen(full_cmd, env=_env, stdout=_out,
stderr=subprocess.STDOUT,
universal_newlines=True)
return sh_cmd
_qdyn_optimize = make_sh_cmd(
['qdyn_optimize', '--internal-units=GHz_units.txt'])
def qdyn_optimize(args=None, _env=None, _out=None):
assert '--J_T=' in " ".join(args)
return _qdyn_optimize(args=args, _env=_env, _out=_out)
qdyn_prop_traj = make_sh_cmd(
['qdyn_prop_traj', '--internal-units=GHz_units.txt'])