diff --git a/aiida/cmdline/commands/cmd_calcjob.py b/aiida/cmdline/commands/cmd_calcjob.py index 648b74be7f..72cec957f2 100644 --- a/aiida/cmdline/commands/cmd_calcjob.py +++ b/aiida/cmdline/commands/cmd_calcjob.py @@ -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)) diff --git a/aiida/transports/plugins/local.py b/aiida/transports/plugins/local.py index cc56a76a2d..c41f4f276a 100644 --- a/aiida/transports/plugins/local.py +++ b/aiida/transports/plugins/local.py @@ -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. @@ -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 diff --git a/aiida/transports/plugins/ssh.py b/aiida/transports/plugins/ssh.py index 4849ce766c..f47c9ef198 100644 --- a/aiida/transports/plugins/ssh.py +++ b/aiida/transports/plugins/ssh.py @@ -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. @@ -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 diff --git a/aiida/transports/transport.py b/aiida/transports/transport.py index 0031e57708..1f3a01c757 100644 --- a/aiida/transports/transport.py +++ b/aiida/transports/transport.py @@ -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. @@ -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 @@ -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 ) ) diff --git a/tests/cmdline/commands/test_calcjob.py b/tests/cmdline/commands/test_calcjob.py index 801761bf6a..a763b12751 100644 --- a/tests/cmdline/commands/test_calcjob.py +++ b/tests/cmdline/commands/test_calcjob.py @@ -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 @@ -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 diff --git a/tests/transports/test_local.py b/tests/transports/test_local.py index 102f0323d8..fa88e00468 100644 --- a/tests/transports/test_local.py +++ b/tests/transports/test_local.py @@ -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 diff --git a/tests/transports/test_ssh.py b/tests/transports/test_ssh.py index 9587487238..8b2043f878 100644 --- a/tests/transports/test_ssh.py +++ b/tests/transports/test_ssh.py @@ -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"""