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

Unable to write to file using script in container #222

Open
hawkspar opened this issue Aug 14, 2024 · 4 comments
Open

Unable to write to file using script in container #222

hawkspar opened this issue Aug 14, 2024 · 4 comments

Comments

@hawkspar
Copy link

hawkspar commented Aug 14, 2024

Expected Behavior

I'm expecting spython.main.Client.execute to behave exactly like singularity exec from the command line when running a dummy python script to write to a file.

Actual Behavior

I can create a file using the command line but not using spython.main.Client.execute from inside a python script.

Steps to Reproduce

Consider running singularity exec --bind ./local_dir:instance_dir any_image_with_python.sif python -c "with open('instance_dir/test.tx','w') as f: f.write('test')" and compare the result with the same command in a python script :

from spython.main import Client

Client.execute('any_instance_with_python.sif',["python","-c","with open('instance_dir/test.tx','w') as f: f.write('test')"],bind=[./local_dir:instance_dir`])

I am confident these are supposed to be identical because I've run the above with the quiet optional argument set to False. Yet one created a file, the other doesn't.

Context

  • singularity version: 4.1.4-1
  • spython version: 0.3.13
  • python version: 3.9.18

Failure Logs

I would love to have those. The python script just continues without a sound...

Possible Fix

My guess is this is a permission issue which requires increasing permissions for the script ?

@vsoch
Copy link
Member

vsoch commented Aug 15, 2024

It's probably something to do with how the command is executed - perhaps try running that via a script instead?

@hawkspar
Copy link
Author

hawkspar commented Aug 15, 2024 via email

@hawkspar
Copy link
Author

hawkspar commented Aug 20, 2024 via email

@vsoch
Copy link
Member

vsoch commented Aug 20, 2024

As suggested, you need to write into a script. The library here uses subprocess, which expects a list of commands (using shlex split) and here is what is happening:

# From the command line
 python -c "print('hello')"
hello

From Python

import subprocess
p = subprocess.Popen(['python', '-c', "'print(\"hello\")'"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
# note that there is no output
(b'', b'')

The function is here:

def run_command(
cmd,
sudo=False,
capture=True,
no_newline_regexp="Progess",
quiet=False,
sudo_options=None,
environ=None,
background=False,
):
. If you'd like to suggest a way to use subprocess to get your desired output, I'd be happy to review a PR or put in a quick one myself. I suspect there is some kind of fork or other so it's not picked up here, but I haven't looked into it. The only way I can grep to see output (not capture it) would be to do:

os.system('python -c \"print(\'hello\')\"')
hello

But that is doing os.system and would not be good to put into the library. Thanks.

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

No branches or pull requests

2 participants