singularity-python icon indicating copy to clipboard operation
singularity-python copied to clipboard

Using the sed command in Singularity Exec does not work. Similar with Redirect ">" symbol.

Open jjacobson95 opened this issue 2 years ago • 6 comments

Command line works as expected: singularity exec --writable-tmpfs instance://reclusive_cat_2780 sed -i 's/REPLACE_THIS_LINE/ <parameter name="Raw data file names"><file>\/Work\/III_mzML\/Dorrestein_GnPS_P14_G10_7V_Rep01_Neg_29Dec20_Fiji_Infusion_Min50_MA-csum-Min20_3.mzML<\/file><\/parameter>/' /Work/MZmine_FeatureFinder-batch.xml

Python Command doesn't work:

file_name = "test_file"
command_list_1 = """sed -i \'s/REPLACE_THIS_LINE/        <parameter name="Raw data file names"><file>\/Work\/III_mzML\/""" +file_name + """<\/file><\/parameter>/\' /Work/MZmine_FeatureFinder-batch.xml"""`

Client.execute(myinstance,command_list_1, options=['--writable-tmpfs'],quiet=False)`

Error message: /bin/sed: -e expression #1, char 1: unknown command: `''

jjacobson95 avatar Apr 12 '23 16:04 jjacobson95

Try putting the sed command in a bash script and running that. We split the woman’s with shlex and your sed FU probably doesn’t work well with that approach.

vsoch avatar Apr 12 '23 17:04 vsoch

Just wrote a quick python script to do it instead. Still would be a great thing to fix for users. I’m converting a docker pipeline to singularity for HPC and took quite a while to track down exactly where it was failing.

jjacobson95 avatar Apr 12 '23 17:04 jjacobson95

Try running the command with subprocess as spython does and you will likely run into the same issue.

vsoch avatar Apr 12 '23 17:04 vsoch

spython isn’t a workflow tool - it’s a thin wrapper to running singularity. Likely a workflow tool would take a command and instead write to a temporary file first, but I consider that out of scope here because it’s not up to spython to make that decision for the user.

vsoch avatar Apr 12 '23 17:04 vsoch

Okay, your call of course! Just was surprised that the wrapper has a bit less functionality than the command line interface because it seems pretty well supported. Could be helpful to note in documentation for future users.

jjacobson95 avatar Apr 12 '23 18:04 jjacobson95

The wrapper uses subprocess, and any tool that does similar will have similar issue with things like pipe or sed. If you’d like to do a PR to update docs with a suggestion I’d be happy to review that. I do not see singularity Python as being responsible for managing a workflow for which it’s really better practice to write complex commands to a bash script so they are properly run with a shell.

vsoch avatar Apr 12 '23 19:04 vsoch