Hi Jacques,
Thanks for the quick reply! I have successfully use the module() command
to setup environments without issue. What I'm hoping to do is capture any
stderr messages to a variable. I have several use cases for this, most
importantly I'm attempting to automate checks against our hundreds of
modules to determine if any of them are broken when loaded. I'm unable to
redirect stdout/stderr in a manner that I've used before which I believe is
due to the command being a subprocess. I'm not a python guru, but the
popen statement declares " stdout=subprocess.PIPE" which I thought was the
right way to get the stdout returned to the originating process.
All that being said, here's my test script:
import os
import sys
from io import StringIO
# Setup variables to hold stdout/stderr
result_stdout = StringIO()
result_stderr = StringIO()
sys.stdout = result_stdout
sys.stderr = result_stderr
# setup environment module
exec(open('/p/app/Modules/4.7.1/init/python.py').read())
# run
print("----- TRY RUNNING MODULE COMMAND -----")
module('--version')
print("----- DONE RUNNING MODULE COMMAND -----")
module_stdout = result_stdout.getvalue()
module_stderr = result_stderr.getvalue()
# reset stdout/stderr to defaults
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
# print out what was captured to the variables
print("===== TRY PRINTING STDOUT =====")
sys.stdout.write(module_stdout)
print("===== DONE PRINTING STDOUT =====")
print("***** TRY PRINTING STDERR *****")
sys.stderr.write(module_stderr)
print("***** DONE PRINTING STDERR *****")
As you can see, it's simple. I make sure to reset stdout/stderr to
defaults, however these are my results:
~> python3 module_test.py
Modules Release 4.7.1 (2021-04-06)
===== TRY PRINTING JSON STDOUT =====
----- TRY RUNNING MODULE COMMAND -----
----- DONE RUNNING MODULE COMMAND -----
===== DONE PRINTING JSON STDOUT =====
***** TRY PRINTING JSON STDERR *****
***** DONE PRINTING JSON STDERR *****
~>
This shows that my stdout redirect is capturing the 'print' statements
for running the module command. However the output for the module
command prints at the very beginning and doesn't show up. Any
thoughts? I found a fancy way to do the same thing in bash, and I
proved that in bash the same output is going to stdout.
Very Respectfully,
Jonathan
On Tue, Mar 22, 2022 at 8:06 AM Contact <dv...@cr...> wrote:
> Hello,
>
> In my case I used modules from python with the following logic:
>
> import os
> exec(open(/home/jrp/modules/init/python.py')).read())
> module('avail')
> module('load', 'gcc/7')
> module('list')
>
> From the above example,
>
> - exec call will allow me to use modules from python itself and define
> module function
> - Each module function invocation will be redirected to module to
> perform required action
>
> As reminder, the content of stdout is used by modules to setup all
> required environment functions, functions/alias (if supported by current
> shell), ...
>
> I hope it will help you.
>
> Jacques
>
>
> Le 21/03/2022 à 20:09, Jonathan Buck a écrit :
>
> Hi,
>
> I'm attempting to capture and act on the stdout and stderr of the module
> command. The python implementation appears to be a subprocess for which I
> wasn't able to redirect with io.StringIO() as I have with other output
> capture.
>
> I attempted to look through the code, which I admit I don't fully
> understand and appears to have a one-liner style of executing the
> subprocess. I'm writing code that integrates with a production system, so
> I'm not able to modify the python.py code.
>
> Does anyone know how else this might be accomplished? Or should I make
> this a feature request?
>
> Thanks for your help,
> Jonathan
>
>
> _______________________________________________
> Modules-interest mailing lis...@li...https://lists.sourceforge.net/lists/listinfo/modules-interest
>
>
|