Skip to content

Commit 9ad668c

Browse files
committed
Allow node to be run in a specified working directory.
1 parent e6af7ab commit 9ad668c

File tree

2 files changed

+107
-71
lines changed

2 files changed

+107
-71
lines changed

python/BioSimSpace/Node/_node.py

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def help(name):
9090
print(proc.stdout)
9191

9292

93-
def run(name, args={}):
93+
def run(name, args={}, work_dir=None):
9494
"""
9595
Run a node.
9696
@@ -103,6 +103,10 @@ def run(name, args={}):
103103
args : dict
104104
A dictionary of arguments to be passed to the node.
105105
106+
work_dir : str, optional
107+
The working directory in which to run the node. If not specified,
108+
the current working directory is used.
109+
106110
Returns
107111
-------
108112
@@ -112,9 +116,18 @@ def run(name, args={}):
112116

113117
# Validate the input.
114118

119+
if not isinstance(name, str):
120+
raise TypeError("'name' must be of type 'str'.")
121+
115122
if not isinstance(args, dict):
116123
raise TypeError("'args' must be of type 'dict'.")
117124

125+
if work_dir is not None:
126+
if not isinstance(work_dir, str):
127+
raise TypeError("'work_dir' must be of type 'str'.")
128+
else:
129+
work_dir = _os.getcwd()
130+
118131
# Apped the node directory name.
119132
full_name = _node_dir + "/" + name
120133

@@ -129,53 +142,44 @@ def run(name, args={}):
129142
else:
130143
full_name += ".py"
131144

132-
# get pid so that we can associate a unique directory with this run
133-
import uuid
145+
with _Utils.chdir(work_dir):
146+
# Write a YAML configuration file for the BioSimSpace node.
147+
if len(args) > 0:
148+
with open("input.yaml", "w") as file:
149+
_yaml.dump(args, file, default_flow_style=False)
134150

135-
input_unique_id = str(uuid.uuid4())
136-
input_name = f"input_{input_unique_id}.yaml"
151+
# Create the command.
152+
command = "%s/python %s --config input.yaml" % (
153+
_SireBase.getBinDir(),
154+
full_name,
155+
)
137156

138-
# Special case argument for file_prefix, allows users to associate
139-
# a unique identifier with the output files.
140-
if "file_prefix" in args:
141-
output_unique_id = args["file_prefix"]
142-
out_name = f"{output_unique_id}.yaml"
143-
else:
144-
out_name = "output.yaml"
145-
146-
# Write a YAML configuration file for the BioSimSpace node.
147-
if len(args) > 0:
148-
with open(input_name, "w") as file:
149-
_yaml.dump(args, file, default_flow_style=False)
150-
151-
# Create the command.
152-
command = "%s/python %s --config %s" % (
153-
_SireBase.getBinDir(),
154-
full_name,
155-
input_name,
157+
# No arguments.
158+
else:
159+
command = "%s/python %s" % (_SireBase.getBinDir(), full_name)
160+
161+
# Run the node as a subprocess.
162+
proc = _subprocess.run(
163+
_Utils.command_split(command),
164+
shell=False,
165+
text=True,
166+
stderr=_subprocess.PIPE,
156167
)
157168

158-
# No arguments.
159-
else:
160-
command = "%s/python %s" % (_SireBase.getBinDir(), full_name)
169+
if proc.returncode == 0:
170+
# Read the output YAML file into a dictionary.
171+
with open("output.yaml", "r") as file:
172+
output = _yaml.safe_load(file)
161173

162-
# Run the node as a subprocess.
163-
proc = _subprocess.run(
164-
_Utils.command_split(command), shell=False, text=True, stderr=_subprocess.PIPE
165-
)
174+
# Delete the redundant YAML files.
175+
_os.remove("input.yaml")
176+
_os.remove("output.yaml")
166177

167-
if proc.returncode == 0:
168-
# Read the output YAML file into a dictionary.
169-
with open(out_name, "r") as file:
170-
output = _yaml.safe_load(file)
171-
# Delete the redundant YAML files.
172-
_os.remove(input_name)
173-
_os.remove(out_name)
174-
return output
178+
return output
175179

176-
else:
177-
# Print the standard error, decoded as UTF-8.
178-
print(proc.stderr)
180+
else:
181+
# Print the standard error, decoded as UTF-8.
182+
print(proc.stderr)
179183

180184

181185
def setNodeDirectory(dir):

python/BioSimSpace/Sandpit/Exscientia/Node/_node.py

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from glob import glob as _glob
2323

2424
from .._Utils import _try_import
25+
2526
from .. import _Utils
2627

2728
import os as _os
@@ -35,7 +36,7 @@
3536
# Set the default node directory.
3637
_node_dir = _os.path.dirname(__file__) + "/_nodes"
3738

38-
__all__ = ["list", "help", "run", "setNodeDirectory"]
39+
__all__ = ["list", "help", "run", "setNodeDirectory", "getNodeDirectory"]
3940

4041

4142
def list():
@@ -89,7 +90,7 @@ def help(name):
8990
print(proc.stdout)
9091

9192

92-
def run(name, args={}):
93+
def run(name, args={}, work_dir=None):
9394
"""
9495
Run a node.
9596
@@ -102,6 +103,10 @@ def run(name, args={}):
102103
args : dict
103104
A dictionary of arguments to be passed to the node.
104105
106+
work_dir : str, optional
107+
The working directory in which to run the node. If not specified,
108+
the current working directory is used.
109+
105110
Returns
106111
-------
107112
@@ -111,9 +116,18 @@ def run(name, args={}):
111116

112117
# Validate the input.
113118

119+
if not isinstance(name, str):
120+
raise TypeError("'name' must be of type 'str'.")
121+
114122
if not isinstance(args, dict):
115123
raise TypeError("'args' must be of type 'dict'.")
116124

125+
if work_dir is not None:
126+
if not isinstance(work_dir, str):
127+
raise TypeError("'work_dir' must be of type 'str'.")
128+
else:
129+
work_dir = _os.getcwd()
130+
117131
# Apped the node directory name.
118132
full_name = _node_dir + "/" + name
119133

@@ -122,45 +136,50 @@ def run(name, args={}):
122136
if not _os.path.isfile(full_name + ".py"):
123137
raise ValueError(
124138
"Cannot find node: '%s'. " % name
139+
+ "in directory '%s'. " % _node_dir
125140
+ "Run 'Node.list()' to see available nodes!"
126141
)
127142
else:
128143
full_name += ".py"
129144

130-
# Write a YAML configuration file for the BioSimSpace node.
131-
if len(args) > 0:
132-
with open("input.yaml", "w") as file:
133-
_yaml.dump(args, file, default_flow_style=False)
145+
with _Utils.chdir(work_dir):
146+
# Write a YAML configuration file for the BioSimSpace node.
147+
if len(args) > 0:
148+
with open("input.yaml", "w") as file:
149+
_yaml.dump(args, file, default_flow_style=False)
134150

135-
# Create the command.
136-
command = "%s/python %s --config input.yaml" % (
137-
_SireBase.getBinDir(),
138-
full_name,
139-
)
140-
141-
# No arguments.
142-
else:
143-
command = "%s/python %s" % (_SireBase.getBinDir(), full_name)
151+
# Create the command.
152+
command = "%s/python %s --config input.yaml" % (
153+
_SireBase.getBinDir(),
154+
full_name,
155+
)
144156

145-
# Run the node as a subprocess.
146-
proc = _subprocess.run(
147-
_Utils.command_split(command), shell=False, text=True, stderr=_subprocess.PIPE
148-
)
157+
# No arguments.
158+
else:
159+
command = "%s/python %s" % (_SireBase.getBinDir(), full_name)
160+
161+
# Run the node as a subprocess.
162+
proc = _subprocess.run(
163+
_Utils.command_split(command),
164+
shell=False,
165+
text=True,
166+
stderr=_subprocess.PIPE,
167+
)
149168

150-
if proc.returncode == 0:
151-
# Read the output YAML file into a dictionary.
152-
with open("output.yaml", "r") as file:
153-
output = _yaml.safe_load(file)
169+
if proc.returncode == 0:
170+
# Read the output YAML file into a dictionary.
171+
with open("output.yaml", "r") as file:
172+
output = _yaml.safe_load(file)
154173

155-
# Delete the redundant YAML files.
156-
_os.remove("input.yaml")
157-
_os.remove("output.yaml")
174+
# Delete the redundant YAML files.
175+
_os.remove("input.yaml")
176+
_os.remove("output.yaml")
158177

159-
return output
178+
return output
160179

161-
else:
162-
# Print the standard error, decoded as UTF-8.
163-
print(proc.stderr)
180+
else:
181+
# Print the standard error, decoded as UTF-8.
182+
print(proc.stderr)
164183

165184

166185
def setNodeDirectory(dir):
@@ -179,3 +198,16 @@ def setNodeDirectory(dir):
179198

180199
global _node_dir
181200
_node_dir = dir
201+
202+
203+
def getNodeDirectory():
204+
"""
205+
Get the directory of the node library.
206+
207+
Returns
208+
-------
209+
210+
dir : str
211+
The path to the node library.
212+
"""
213+
return _node_dir

0 commit comments

Comments
 (0)