I have HSDS running on a CentOS 7 server. I am wanting to write a Python script that can call one of the various h5pyd apps available (hstouch, hsls, hsget, hsdel, etc etc) and be able to also get the standard output that was sent to the console as a string when the app is done. Is there a way to do this?
I was researching it and one route that could possibly be taken is to launch a subprocess in my script that simply runs the app that I want, and then include a parameter that captures the standard output. However, if there’s a way to just directly call into the code and get the output when finished, that would be even better. I am unsure on how the subprocess method would work with h5pyd apps that require multiple standard inputs throughout execution (for example, hsconfigure).
I noticed that hsls (and possibly other apps) are using sys.exit() when there is an error, and I’m concerned that command will also bring down my Python script as well.
I’ve talked with Joey offline about this, but for other forum readers, I’ll convey the gist of our conversation here…
If you run one of the h5pyd apps as a subprocess, if the app calls sys.exit, it should just end the subprocess without killing the parent app. You can check the return code like this:
rc = subprocess.run(["hsload", src_hdf5_file, des_folder])
if rc.returncode > 0:
print("there was a problem with hsload")
else:
print("file loaded successfully!")
In general, for programmatic use, I’d recommend on using the classes in h5pyd package rather than running the h5pyd app and capturing their output. E.g. using h5pyd.Folder rather than running hsls. Mostly the apps are thin wrappers around the h5pyd classes. The exeption is hsload and hsget. It would probably make sense to move some of the logic here into h5pyd classes that would enable re-use by other applications.
Re: hsconfigure, there are a couple of ways to setup the information needed for h5pyd to communicate with the server (namely: endpoint, username, and password). First the File and Folder classes have name parameters: “endpoint”, “username”, and “password” that you can utilize. Alternatively, you can set the environment variables: “HS_ENDPOINT”, “HS_USERNAME”, and “HS_PASSWORD” to pass these values.