Skip to content

Commit

Permalink
Remove the --monitor option
Browse files Browse the repository at this point in the history
  • Loading branch information
sphuber committed Dec 14, 2022
1 parent 4fbaea2 commit 46bb011
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 61 deletions.
24 changes: 3 additions & 21 deletions aiida/cmdline/commands/cmd_calcjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,42 +124,24 @@ def calcjob_inputcat(calcjob, path):
@verdi_calcjob.command('remotecat')
@arguments.CALCULATION('calcjob', type=CalculationParamType(sub_classes=('aiida.node:process.calculation.calcjob',)))
@click.argument('path', type=str, required=False)
@click.option('--monitor', is_flag=True, default=False, help='Monitor the file using `tail -f` instead.')
@decorators.with_dbenv()
def calcjob_remotecat(calcjob, path, monitor):
def calcjob_remotecat(calcjob, path):
"""Show the contents of a file in the remote working directory.
The file to show can be specified using the PATH argument. If PATH is not specified, the default output file path
as defined by the `CalcJob` plugin class will be used instead.
"""
from shutil import copyfileobj
import shutil
import sys
import tempfile

from aiida.common.exceptions import NotExistent

remote_folder, path = get_remote_and_path(calcjob, path)

if monitor:
try:
transport = calcjob.get_transport()
except NotExistent as exception:
echo.echo_critical(str(exception))

remote_workdir = calcjob.get_remote_workdir()

if not remote_workdir:
echo.echo_critical('no remote work directory for this calcjob, maybe it has not yet started running?')
cmds = f"-c 'tail -f {path}'"
command = transport.gotocomputer_command(remote_workdir, cmds)
os.system(command)
return

with tempfile.NamedTemporaryFile() as tmp_path:
try:
remote_folder.getfile(path, tmp_path.name)
with open(tmp_path.name, 'rb') as handle:
copyfileobj(handle, sys.stdout.buffer)
shutil.copyfileobj(handle, sys.stdout.buffer)
except IOError as exception:
echo.echo_critical(str(exception))

Expand Down
4 changes: 2 additions & 2 deletions aiida/transports/plugins/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ def line_encoder(iterator, encoding='utf-8'):

return retval, output_text, stderr_text

def gotocomputer_command(self, remotedir, extra_args=None):
def gotocomputer_command(self, remotedir):
"""
Return a string to be run using os.system in order to connect
via the transport to the remote directory.
Expand All @@ -829,7 +829,7 @@ def gotocomputer_command(self, remotedir, extra_args=None):
:param str remotedir: the full path of the remote directory
"""
connect_string = self._gotocomputer_string(remotedir, extra_args)
connect_string = self._gotocomputer_string(remotedir)
cmd = f'bash -c {connect_string}'
return cmd

Expand Down
4 changes: 2 additions & 2 deletions aiida/transports/plugins/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ def exec_command_wait_bytes(self, command, stdin=None, combine_stderr=False, buf

return (retval, b''.join(stdout_bytes), b''.join(stderr_bytes))

def gotocomputer_command(self, remotedir, extra_args=None):
def gotocomputer_command(self, remotedir):
"""
Specific gotocomputer string to connect to a given remote computer via
ssh and directly go to the calculation folder.
Expand All @@ -1505,7 +1505,7 @@ def gotocomputer_command(self, remotedir, extra_args=None):

further_params_str = ' '.join(further_params)

connect_string = self._gotocomputer_string(remotedir, extra_args)
connect_string = self._gotocomputer_string(remotedir)
cmd = f'ssh -t {self._machine} {further_params_str} {connect_string}'
return cmd

Expand Down
12 changes: 4 additions & 8 deletions aiida/transports/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ def rmtree(self, path):
"""

@abc.abstractmethod
def gotocomputer_command(self, remotedir, extra_args=None):
def gotocomputer_command(self, remotedir):
"""
Return a string to be run using os.system in order to connect
via the transport to the remote directory.
Expand All @@ -700,7 +700,6 @@ def gotocomputer_command(self, remotedir, extra_args=None):
* A reasonable error message is produced if the folder does not exist
:param str remotedir: the full path of the remote directory
:param str extra_args: optional extra arguments to be passed to the shell
"""

@abc.abstractmethod
Expand Down Expand Up @@ -816,16 +815,13 @@ def glob0(self, dirname, basename):
def has_magic(self, string):
return self._MAGIC_CHECK.search(string) is not None

def _gotocomputer_string(self, remotedir, extra_args=None):
def _gotocomputer_string(self, remotedir):
"""command executed when goto computer."""
connect_string = (
""" "if [ -d {escaped_remotedir} ] ;"""
""" then cd {escaped_remotedir} ; {bash_command} {extra_args}; else echo ' ** The directory' ; """
""" then cd {escaped_remotedir} ; {bash_command} ; else echo ' ** The directory' ; """
"""echo ' ** {remotedir}' ; echo ' ** seems to have been deleted, I logout...' ; fi" """.format(
bash_command=self._bash_command_str,
extra_args=extra_args or '',
escaped_remotedir="'{}'".format(remotedir),
remotedir=remotedir
bash_command=self._bash_command_str, escaped_remotedir="'{}'".format(remotedir), remotedir=remotedir
)
)

Expand Down
11 changes: 1 addition & 10 deletions tests/cmdline/commands/test_calcjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,9 @@ def test_calcjob_inoutputcat_old(self):
assert len(get_result_lines(result)) == 1
assert get_result_lines(result)[0] == '5'

def test_calcjob_remotecat(self, monkeypatch):
def test_calcjob_remotecat(self):
"""Test the remotecat command that prints the remote file for a given calcjob"""
# Specifying no filtering options and no explicit calcjobs should exit with non-zero status
import os

options = []
result = self.cli_runner.invoke(command.calcjob_remotecat, options)
assert result.exception is not None, result.output
Expand All @@ -359,10 +357,3 @@ def test_calcjob_remotecat(self, monkeypatch):
options = [str(self.result_job.uuid), 'fileA.txt']
result = self.cli_runner.invoke(command.calcjob_remotecat, options)
assert result.stdout == 'test stringA'

# To test the ``--monitor`` option, mock the ``os.system`` command and simply print the command it receives.
with monkeypatch.context() as ctx:
ctx.setattr(os, 'system', lambda x: print(x)) # pylint: disable=unnecessary-lambda
options = ['--monitor', str(self.result_job.uuid), 'fileA.txt']
result = self.cli_runner.invoke(command.calcjob_remotecat, options)
assert "bash -l -c 'tail -f fileA.txt'" in result.stdout
9 changes: 0 additions & 9 deletions tests/transports/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,3 @@ def test_gotocomputer():
"""echo ' ** /remote_dir/' ; echo ' ** seems to have been deleted, I logout...' ; fi" """
)
assert cmd_str == expected_str

cmd_str = transport.gotocomputer_command('/remote_dir/', "-c 'echo Hello World'")

expected_str = (
"""bash -c "if [ -d '/remote_dir/' ] ;"""
""" then cd '/remote_dir/' ; bash -l -c 'echo Hello World'; else echo ' ** The directory' ; """
"""echo ' ** /remote_dir/' ; echo ' ** seems to have been deleted, I logout...' ; fi" """
)
assert cmd_str == expected_str
9 changes: 0 additions & 9 deletions tests/transports/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,6 @@ def test_gotocomputer():
)
assert cmd_str == expected_str

cmd_str = transport.gotocomputer_command('/remote_dir/', '-c "echo Hello World"')

expected_str = (
"""ssh -t localhost -o ProxyCommand='ssh -W localhost:22 localhost' "if [ -d '/remote_dir/' ] ;"""
""" then cd '/remote_dir/' ; bash -c "echo Hello World"; else echo ' ** The directory' ; """
"""echo ' ** /remote_dir/' ; echo ' ** seems to have been deleted, I logout...' ; fi" """
)
assert cmd_str == expected_str


def test_gotocomputer_proxyjump():
"""Test gotocomputer"""
Expand Down

0 comments on commit 46bb011

Please sign in to comment.