Project

General

Profile

Feature #3393

Allow gmxapi.commandline_operation to use STDIN

Added by Eric Irrgang 5 months ago. Updated 5 months ago.

Status:
Resolved
Priority:
Normal
Assignee:
Category:
gmxapi
Difficulty:
uncategorized
Close

Description

Some terminal programs require terminal I/O.

See also https://github.com/kassonlab/gmxapi/issues/255

Proposed additional input handling:


    Arguments:
        ...
         stdin (str): String input to send to STDIN (terminal input) of the executable.

    Multi-line text sent to *stdin* should be joined into a single string
    (e.g. ``'\n'.join(list_of_strings) + '\n'``).
    If multiple strings are provided to *stdin*, gmxapi will assume an ensemble,
    and will run one operation for each provided string.

    Only string input (:py:func:str) to *stdin* is currently supported.
    If you have a use case that requires streaming input or binary input,
    please open an issue or contact the author(s).

Example:

    def test_command_with_stdin(self):
        """Test that cli() can handle string input.""" 
        stdin = 'hi\nthere\n'
        subcommand = '{wc} -l | {grep} -q 2'.format(wc=shutil.which('wc'), grep=shutil.which('grep'))

        operation = commandline.cli(command=['/bin/sh', '-c', subcommand], shell=False, stdin=stdin)
        assert operation.output.returncode.result() == 0
        operation = commandline.commandline_operation('/bin/sh', ['-c', subcommand], stdin=stdin)
        assert operation.output.returncode.result() == 0

        subcommand = '{wc} -l | {grep} -q 1'.format(wc=shutil.which('wc'), grep=shutil.which('grep'))

        operation = commandline.cli(command=['/bin/sh', '-c', subcommand], shell=False, stdin=stdin)
        assert operation.output.returncode.result() != 0
        operation = commandline.commandline_operation('/bin/sh', ['-c', subcommand], stdin=stdin)
        assert operation.output.returncode.result() != 0

Associated revisions

Revision 86972abf (diff)
Added by Eric Irrgang 5 months ago

Let commandline_operation accept stdin.

Add a stdin key word to gmxapi.commandline.cli() and
gmxapi.commandline_operation(). Optionally accept a string object to
pass to the subprocess standard input pipe.

Fixes #3393

Change-Id: I563aeb19e944e7e9e755a72b1956b61ff7789c22

History

#1 Updated by Eric Irrgang 5 months ago

  • Status changed from New to Resolved

#2 Updated by Eric Irrgang 5 months ago

This feature will be available in gmxapi 0.2a1 on PyPI and in GROMACS master branch.

Should it also be back-ported to gmxapi 0.1 in the GROMACS release-2020 branch?

#3 Updated by Sergio PĂ©rez Conesa 5 months ago

I think it would make sense since it is confusing that you cannot run a lot of analysis commands because of this.

Also available in: Atom PDF