Skip to content

Commit 93c9813

Browse files
committed
Allow setting JAVA_HOME
1 parent a44c2f0 commit 93c9813

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

nextflow/command.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def run(*args, **kwargs):
2626
:param resume: whether to resume an existing execution.
2727
:param function runner: a function to run the pipeline command.
2828
:param str version: the nextflow version to use.
29+
:param str java_home: the path to the Java installation to use.
2930
:param list configs: any config files to be applied.
3031
:param dict params: the parameters to pass.
3132
:param list profiles: any profiles to be applied.
@@ -51,6 +52,7 @@ def run_and_poll(*args, **kwargs):
5152
:param function runner: a function to run the pipeline command.
5253
:param io: an optional custom io object to handle file operations.
5354
:param str version: the nextflow version to use.
55+
:param str java_home: the path to the Java installation to use.
5456
:param list configs: any config files to be applied.
5557
:param dict params: the parameters to pass.
5658
:param list profiles: any profiles to be applied.
@@ -68,7 +70,7 @@ def run_and_poll(*args, **kwargs):
6870

6971
def _run(
7072
pipeline_path, resume=False, poll=False, run_path=None, output_path=None,
71-
log_path=None, runner=None, io=None,
73+
log_path=None, runner=None, io=None, java_home=None,
7274
version=None, configs=None, params=None, profiles=None, timezone=None,
7375
report=None, timeline=None, dag=None, trace=None, sleep=1
7476
):
@@ -81,6 +83,7 @@ def _run(
8183
runner=runner,
8284
io=io,
8385
version=version,
86+
java_home=java_home,
8487
configs=configs,
8588
dag=dag,
8689
trace=trace,
@@ -113,6 +116,7 @@ def submit_execution(
113116
runner=None,
114117
io=None,
115118
version=None,
119+
java_home=None,
116120
configs=None,
117121
params=None,
118122
profiles=None,
@@ -133,6 +137,7 @@ def submit_execution(
133137
:param function runner: a function to run the pipeline command.
134138
:param io: an optional custom io object to handle file operations.
135139
:param str version: the nextflow version to use.
140+
:param str java_home: the path to the Java installation to use.
136141
:param list configs: any config files to be applied.
137142
:param dict params: the parameters to pass.
138143
:param list profiles: any profiles to be applied.
@@ -148,8 +153,8 @@ def submit_execution(
148153
if not output_path: output_path = run_path
149154
if not log_path: log_path = output_path
150155
nextflow_command = make_nextflow_command(
151-
run_path, output_path, log_path, pipeline_path, resume, version, configs,
152-
params, profiles, timezone, report, timeline, dag, trace, io
156+
run_path, output_path, log_path, pipeline_path, resume, version, java_home,
157+
configs, params, profiles, timezone, report, timeline, dag, trace, io
153158
)
154159
start = datetime.now()
155160
if runner:
@@ -168,7 +173,7 @@ def submit_execution(
168173
return submission
169174

170175

171-
def make_nextflow_command(run_path, output_path, log_path, pipeline_path, resume,version, configs, params, profiles, timezone, report, timeline, dag, trace, io):
176+
def make_nextflow_command(run_path, output_path, log_path, pipeline_path, resume, version, java_home, configs, params, profiles, timezone, report, timeline, dag, trace, io):
172177
"""Generates the `nextflow run` commmand.
173178
174179
:param str run_path: the location to run the pipeline in.
@@ -177,6 +182,7 @@ def make_nextflow_command(run_path, output_path, log_path, pipeline_path, resume
177182
:param str pipeline_path: the absolute path to the pipeline .nf file.
178183
:param bool resume: whether to resume an existing execution.
179184
:param str version: the nextflow version to use.
185+
:param str java_home: the path to the Java installation to use.
180186
:param list configs: any config files to be applied.
181187
:param dict params: the parameters to pass.
182188
:param list profiles: any profiles to be applied.
@@ -188,7 +194,7 @@ def make_nextflow_command(run_path, output_path, log_path, pipeline_path, resume
188194
:param io: an optional custom io object to handle file operations.
189195
:rtype: ``str``"""
190196

191-
env = make_nextflow_command_env_string(version, timezone, output_path, run_path)
197+
env = make_nextflow_command_env_string(version, timezone, output_path, run_path, java_home)
192198
if env: env += " "
193199
nf = "nextflow -Duser.country=US"
194200
log = make_nextflow_command_log_string(log_path, run_path)
@@ -210,19 +216,22 @@ def make_nextflow_command(run_path, output_path, log_path, pipeline_path, resume
210216
return command
211217

212218

213-
def make_nextflow_command_env_string(version, timezone, output_path, run_path):
219+
def make_nextflow_command_env_string(version, timezone, output_path, run_path, java_home):
214220
"""Creates the environment variable setting portion of the nextflow run
215221
command string.
216222
217223
:param str version: the nextflow version to use.
218224
:param str timezone: the timezone to use.
219225
:param str output_path: the location to store the output in.
226+
:param str run_path: the location to run the pipeline in.
227+
:param str java_home: the path to the Java installation to use.
220228
:rtype: ``str``"""
221229

222230
env = {"NXF_ANSI_LOG": "false"}
223231
if version: env["NXF_VER"] = version
224232
if timezone: env["TZ"] = timezone
225233
if output_path != run_path: env["NXF_WORK"] = os.path.join(output_path, "work")
234+
if java_home: env["JAVA_HOME"] = java_home
226235
return " ".join([f"{k}={v}" for k, v in env.items()])
227236

228237

tests/unit/test_command.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def test_can_run_with_custom_values(self, mock_ex, mock_sleep, mock_submit):
3131
mock_ex.side_effect = [[None, 0], [mock_executions[0], 40], [mock_executions[1], 20]]
3232
io = Mock()
3333
executions = list(_run(
34-
"main.nf", run_path="/exdir", output_path="/out", log_path="/log", resume="a_b", version="21.10", configs=["conf1"],
34+
"main.nf", run_path="/exdir", output_path="/out", log_path="/log", resume="a_b",
35+
version="21.10", java_home="/java", configs=["conf1"],
3536
params={"param": "2"}, profiles=["docker"], timezone="UTC", report="report.html",
3637
timeline="time.html", dag="dag.html", trace="trace.html", sleep=4, io=io
3738
))
@@ -77,14 +78,13 @@ def test_can_run_with_poll(self, mock_run):
7778

7879
class SubmitTests(TestCase):
7980

80-
8181
@patch("os.path.abspath")
8282
@patch("nextflow.command.make_nextflow_command")
8383
@patch("subprocess.Popen")
8484
def test_can_submit_with_default_values(self, mock_run, mock_nc, mock_abs):
8585
mock_abs.side_effect = ["/run", "/out", "/log"]
8686
submission = submit_execution("main.nf")
87-
mock_nc.assert_called_with("/run", "/run", "/run", "main.nf", False, None, None, None, None, None, None, None, None, None, None)
87+
mock_nc.assert_called_with("/run", "/run", "/run", "main.nf", False, None, None, None, None, None, None, None, None, None, None, None)
8888
mock_abs.assert_called_once_with(".")
8989
mock_run.assert_called_with(
9090
mock_nc.return_value,
@@ -106,10 +106,10 @@ def test_can_submit_with_custom_values(self, mock_wait, mock_run, mock_nc):
106106
io = Mock()
107107
submission = submit_execution(
108108
"main.nf", run_path="/exdir", output_path="/out", log_path="/log", resume="a_b", version="21.10", configs=["conf1"],
109-
params={"param": "2"}, profiles=["docker"], timezone="UTC", report="report.html",
109+
params={"param": "2"}, profiles=["docker"], timezone="UTC", report="report.html", java_home="/java",
110110
timeline="time.html", dag="dag.html", trace="trace.html", io=io
111111
)
112-
mock_nc.assert_called_with("/exdir", "/out", "/log", "main.nf", "a_b", "21.10", ["conf1"], {"param": "2"}, ["docker"], "UTC", "report.html", "time.html", "dag.html", "trace.html", io)
112+
mock_nc.assert_called_with("/exdir", "/out", "/log", "main.nf", "a_b", "21.10", "/java", ["conf1"], {"param": "2"}, ["docker"], "UTC", "report.html", "time.html", "dag.html", "trace.html", io)
113113
mock_run.assert_called_with(
114114
mock_nc.return_value,
115115
universal_newlines=True, shell=True
@@ -128,7 +128,7 @@ def test_can_submit_with_custom_values(self, mock_wait, mock_run, mock_nc):
128128
def test_can_submit_with_custom_io(self, mock_run, mock_nc):
129129
io = Mock()
130130
submission = submit_execution("main.nf", io=io)
131-
mock_nc.assert_called_with(io.abspath.return_value, io.abspath.return_value, io.abspath.return_value, "main.nf", False, None, None, None, None, None, None, None, None, None, io)
131+
mock_nc.assert_called_with(io.abspath.return_value, io.abspath.return_value, io.abspath.return_value, "main.nf", False, None, None, None, None, None, None, None, None, None, None, io)
132132
io.abspath.assert_called_once_with(".")
133133
mock_run.assert_called_with(
134134
mock_nc.return_value,
@@ -146,7 +146,7 @@ def test_can_submit_with_custom_io(self, mock_run, mock_nc):
146146
def test_can_run_with_custom_runner(self, mock_nc):
147147
runner = MagicMock()
148148
submission = submit_execution("main.nf", runner=runner)
149-
mock_nc.assert_called_with(os.path.abspath("."), os.path.abspath("."), os.path.abspath("."), "main.nf", False, None, None, None, None, None, None, None, None, None, None)
149+
mock_nc.assert_called_with(os.path.abspath("."), os.path.abspath("."), os.path.abspath("."), "main.nf", False, None, None, None, None, None, None, None, None, None, None, None)
150150
runner.assert_called_with(mock_nc.return_value)
151151
self.assertEqual(submission.pipeline_path, "main.nf")
152152
self.assertEqual(submission.run_path, os.path.abspath("."))
@@ -175,8 +175,8 @@ def test_can_get_full_nextflow_command(self, mock_report, mock_prof, mock_params
175175
mock_prof.return_value = "-profile docker,test"
176176
mock_report.return_value = "--dag.html"
177177
io = Mock()
178-
command = make_nextflow_command("/exdir", "/out", "/log", "main.nf", True, "21.10", ["conf1"], {"param": "2"}, ["docker"], "UTC", "report.html", "time.html", "dag.html", "trace.html", io)
179-
mock_env.assert_called_with("21.10", "UTC", "/out", "/exdir")
178+
command = make_nextflow_command("/exdir", "/out", "/log", "main.nf", True, "21.10", "/java", ["conf1"], {"param": "2"}, ["docker"], "UTC", "report.html", "time.html", "dag.html", "trace.html", io)
179+
mock_env.assert_called_with("21.10", "UTC", "/out", "/exdir", "/java")
180180
mock_conf.assert_called_with(["conf1"])
181181
mock_params.assert_called_with({"param": "2"})
182182
mock_prof.assert_called_with(["docker"])
@@ -201,8 +201,8 @@ def test_can_get_minimal_nextflow_command(self, mock_abspath, mock_report, mock_
201201
mock_prof.return_value = ""
202202
mock_report.return_value = ""
203203
mock_abspath.return_value = "/exdir"
204-
command = make_nextflow_command("/exdir", "/exdir", "/exdir", "main.nf", False, "21.10", ["conf1"], {"param": "2"}, ["docker"], None, None, None, None, None, None)
205-
mock_env.assert_called_with("21.10", None, "/exdir", "/exdir")
204+
command = make_nextflow_command("/exdir", "/exdir", "/exdir", "main.nf", False, "21.10", "/java", ["conf1"], {"param": "2"}, ["docker"], None, None, None, None, None, None)
205+
mock_env.assert_called_with("21.10", None, "/exdir", "/exdir", "/java")
206206
mock_conf.assert_called_with(["conf1"])
207207
mock_resume.assert_called_with(False)
208208
mock_params.assert_called_with({"param": "2"})
@@ -228,8 +228,8 @@ def test_can_use_custom_io(self, mock_report, mock_prof, mock_params, mock_resum
228228
mock_report.return_value = ""
229229
io = Mock()
230230
io.abspath.return_value = "/exdir"
231-
command = make_nextflow_command("/exdir", "/exdir", "/exdir", "main.nf", False, "21.10", ["conf1"], {"param": "2"}, ["docker"], None, None, None, None, None, io)
232-
mock_env.assert_called_with("21.10", None, "/exdir", "/exdir")
231+
command = make_nextflow_command("/exdir", "/exdir", "/exdir", "main.nf", False, "21.10", "/java", ["conf1"], {"param": "2"}, ["docker"], None, None, None, None, None, io)
232+
mock_env.assert_called_with("21.10", None, "/exdir", "/exdir", "/java")
233233
mock_conf.assert_called_with(["conf1"])
234234
mock_resume.assert_called_with(False)
235235
mock_params.assert_called_with({"param": "2"})
@@ -242,19 +242,23 @@ def test_can_use_custom_io(self, mock_report, mock_prof, mock_params, mock_resum
242242
class EnvStringTests(TestCase):
243243

244244
def test_can_get_env_without_args(self):
245-
self.assertEqual(make_nextflow_command_env_string(None, None, "/out", "/out"), "NXF_ANSI_LOG=false")
245+
self.assertEqual(make_nextflow_command_env_string(None, None, "/out", "/out", None), "NXF_ANSI_LOG=false")
246246

247247

248248
def test_can_get_env_with_version(self):
249-
self.assertEqual(make_nextflow_command_env_string("22.1", None, "/out", "/out"), "NXF_ANSI_LOG=false NXF_VER=22.1")
249+
self.assertEqual(make_nextflow_command_env_string("22.1", None, "/out", "/out", None), "NXF_ANSI_LOG=false NXF_VER=22.1")
250250

251251

252252
def test_can_get_env_with_timezone(self):
253-
self.assertEqual(make_nextflow_command_env_string(None, "UTC", "/out", "/out"), "NXF_ANSI_LOG=false TZ=UTC")
253+
self.assertEqual(make_nextflow_command_env_string(None, "UTC", "/out", "/out", None), "NXF_ANSI_LOG=false TZ=UTC")
254254

255255

256256
def test_can_get_env_with_work_location(self):
257-
self.assertEqual(make_nextflow_command_env_string(None, None, "/out", "/run"), "NXF_ANSI_LOG=false NXF_WORK=/out/work")
257+
self.assertEqual(make_nextflow_command_env_string(None, None, "/out", "/run", None), "NXF_ANSI_LOG=false NXF_WORK=/out/work")
258+
259+
260+
def test_can_get_env_with_java_home(self):
261+
self.assertEqual(make_nextflow_command_env_string(None, None, "/out", "/out", "/java"), "NXF_ANSI_LOG=false JAVA_HOME=/java")
258262

259263

260264

0 commit comments

Comments
 (0)