Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Containerized code: double quotes setting of computer and code setup #5478

Merged

Conversation

unkcpz
Copy link
Member

@unkcpz unkcpz commented Apr 8, 2022

The PR is a part required for containerized code implementation (also for hyperqueue without hacky workaround to evaluate command-line variable by $ properly) and is separated from #5250. It allows users to set up computer and code with an extra option --use-double-quotes to using double quotes for the command run line of job script.

@unkcpz unkcpz requested review from ltalirz and sphuber April 8, 2022 11:07
@ltalirz
Copy link
Member

ltalirz commented Apr 8, 2022

Thanks @unkcpz . At a high level, I'm just wondering: do we really need to push this choice to the user?

I.e. can't we be clever about using double quotes where necessary (if we detect some environment variable / ...)? This will be yet another option in the computer an code setup and I wonder whether people really need to actively care about this.

@unkcpz
Copy link
Member Author

unkcpz commented Apr 8, 2022

Hi @ltalirz, yes ideally it should be intelligent enough to know when to use double quotes and not. But I think it is hard to decide by what criteria we escape it with double or single quotes. Setting it manually might be a safe way to keep the backward compatibility.

Copy link
Contributor

@sphuber sphuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @unkcpz . Mostly minor comments, but one fundamental question: is the separate control of quoting in the Code and Computer necessary? And if so, why should the quoting for the Computer only apply to the argument before the executable and not any that follows, such as the input and output redirection?

aiida/orm/computers.py Outdated Show resolved Hide resolved
Comment on lines 446 to 450
def get_use_double_quotes(self) -> bool:
return self.get_property('use_double_quotes', False)

def set_use_double_quotes(self, val: bool) -> None:
self.set_property('use_double_quotes', bool(val))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of type coercion, I think you should check the type.

from aiida.common.lang import type_check
type_check(val, bool)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed.

aiida/manage/tests/pytest_fixtures.py Outdated Show resolved Hide resolved
Comment on lines 355 to 358
"""
Set whether a code cmdline is escape with double quotes.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
Set whether a code cmdline is escape with double quotes.
"""
"""Set whether the command line invocation of this code should be escaped with double quotes.
:param use_double_quotes: boolean, True if to escape with double quotes, False otherwise.
"""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was resolved, but I don't see the changes?

Comment on lines 362 to 367
def get_use_double_quotes(self):
"""
Return whether a code is escape with double quotes (False, default) or not (True).
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    """Return whether the command line invocation of this code should be escaped with double quotes.
    
    :returns: boolean, True if to escape with double quotes, False otherwise which is also the default.
    """

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Don't see the changes.

aiida/cmdline/params/options/commands/code.py Outdated Show resolved Hide resolved
aiida/cmdline/params/options/commands/code.py Outdated Show resolved Hide resolved
is_eager=False,
default=False,
cls=InteractiveOption,
prompt='Whether use double quotes for prepend cmdline params?',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prompt='Whether use double quotes for prepend cmdline params?',
prompt='Escape CLI arguments in double quotes',

aiida/cmdline/params/options/commands/code.py Outdated Show resolved Hide resolved
aiida/cmdline/params/options/commands/computer.py Outdated Show resolved Hide resolved
Copy link
Member Author

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sphuber thanks for the review and sorry for the delay of changes and reply, suffer from COVID last week.

I implement most of your suggestions and also docstring of use_double_quotes of JobTemplateCodeInfo.

but one fundamental question: is the separate control of quoting in the Code and Computer necessary?

I think there are two reasons to apply it independently: 1. it is more flexible and more possible for backward compatibility if it can be controlled separately. 2. with initial containerized code implementation (the PR after this PR) the specific suffix for using containerized code is independent of cmdline_params parts of code and double quotes escape needed since I want to use $PWD to map working directory to the directory where the inputs locate. While for the code part it can keep the same.

aiida/manage/tests/pytest_fixtures.py Outdated Show resolved Hide resolved
Comment on lines 446 to 450
def get_use_double_quotes(self) -> bool:
return self.get_property('use_double_quotes', False)

def set_use_double_quotes(self, val: bool) -> None:
self.set_property('use_double_quotes', bool(val))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed.

cmdline_params: list[str] = field(default_factory=list)
use_double_quotes: list[bool] = field(default_factory=list)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prepend_cmdine_params is to collect all computer related params (mpi parameters, scheduler parameters) that are shown in front of the executable and arguments of code.

exec 2> _scheduler-stderr.txt


"mpirun" "-np" "1" '/bin/bash' < 'aiida.in' > 'aiida.out' 2> 'aiida.err'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so. I didn't implement it since it is not relevant to the following up features. It makes sense to also call these the computer related parameters. Changed.

codeinfo.stderr_name = 'aiida.err'

calcinfo = CalcInfo()
calcinfo.codes_info = [codeinfo]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is affected. test added.

'--on-computer',
f'--computer={aiida_localhost.label}',
'--remote-abs-path=/remote/abs/path',
'--use-double-quotes',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was added to check if this option can be used and take effect. But rather not test it here. Removed.

@unkcpz unkcpz force-pushed the design/double-quote-for-computer-code branch from 24eabaf to 964ba11 Compare April 19, 2022 16:17
@unkcpz unkcpz requested a review from sphuber April 19, 2022 16:18
@sphuber
Copy link
Contributor

sphuber commented Apr 20, 2022

Thanks @unkcpz . Hope you are feeling better. I noticed that you resolved a few comments that don't seem to actually have been addressed. At least I cannot see the changes of the suggestions I made. Also the tests and pre-commit are failing. Please fix those and update the branch.

@unkcpz unkcpz force-pushed the design/double-quote-for-computer-code branch 4 times, most recently from 1797d75 to 4a21ea5 Compare April 20, 2022 12:15
@unkcpz
Copy link
Member Author

unkcpz commented Apr 20, 2022

@sphuber sorry for the carelessness, I wrongly mix up commits and push to the actual container code implementation branch locally previously. Now it is rebased a bit and passes all the tests.

So far, the command line arguments of the code exeuction line in the
submit script written by the `Scheduler` plugin, would be wrapped by
single quotes to prevent special characters, such as spaces, from being
interpreted incorrectly. However, for certain use cases, the quoting has
an undesirable effect. For example, if an argument contains a bash
variable, indicated by a `$` sign, the single quotes will prevent it
from being dereferenced correctly.

A concrete use-case is the requested feature of being able to define a
Docker container as a `Code`, which needs certain arguments to be quoted
in double quotes. This PR adds a new property `use_double_quotes` to the
`Code` and `Computer` classes that determines whether arguments should
be quoted using single or double quotes.

The settings are communicated through the `cmdline_params` attribute of
a `JobTemplateCodeInfo` instance to the scheduler plugin. The plugin is
then responsible for respecting these booleans. To be able to
distinguish between the command line arguments of the computer and the
code, they are passed separately in `prepend_cmdline_params` and
`cmdline_params`, respectively.
@sphuber sphuber force-pushed the design/double-quote-for-computer-code branch from 4be3c8b to 38b14dc Compare April 22, 2022 09:16
@sphuber sphuber merged commit 84c67ba into aiidateam:develop Apr 22, 2022
@sphuber
Copy link
Contributor

sphuber commented Apr 22, 2022

Thanks @unkcpz !

@unkcpz unkcpz deleted the design/double-quote-for-computer-code branch April 22, 2022 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants