-
Notifications
You must be signed in to change notification settings - Fork 6
Move shell out commands into separate library #19
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
base: main
Are you sure you want to change the base?
Conversation
This reverts commit 787b5b5.
ericaneininger
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good start.
General Comments:
- I would like to see this new module made available via a python Package implementation - for example a new package directory at top level -
moci /
|-> moci_utils / (or moci_lib?)
|-> __init__.py
|-> shellout.py
|-> tests /
The idea would be to install this package along with the postproc or drivers code and import the required functions via from moci_utils import shellout. This gives us a clearer idea of where the methods are being imported form - especially useful as "shellout" and "timer" and "utils" are potentially common terms.
- All new code should be accompanied by unit tests.
unittestis the package predominantly used in MOCI at the moment, but a more modern alternative would be acceptable. - I suggest, particularly in the PostProc app case, that for the time being we do a proof of concept - without removing
utils.exec_subprocentirely for the time being and just place a "deprecated in favour of moci_utils.shellout" tag in the docstring. Choose one file and replace the calls toutils.exec_subprocin there. Make sure that rose-stem still runs. We will open further issues to replace all calls once we have proof of concept.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of things to note here:
- Copyright notice required. Please use the appropriate (Python) Momentum template:
https://github.com/MetOffice/Momentum/blob/main/docs/COPYRIGHT_TEMPLATE - Unit tests required for this code. MOCI codebase test coverage currently uses
unittest, mainly due to the age of the code.pytestmay also suitable. - Don't use
timerhere - this is PostProc functionality - leave it to the PostProc application to run any timing deemed necessary. As an aside - movingtimer.pyinto the newmocilibpackage is a good idea. - I think you've made a good decision to use
subprocess.run()here, to replace the driver use ofpopen()and PP use ofcheck_output(). However, a couple of things to note about the implementation:- L26 - I'm not convinced
subprocess.stdin=TrueKW argument is required here. I believe that it is implied in internal calls tosubprocess.communicate(), which we are not calling directly. - L29 - Hard-wiring
timeout=10is going to be a problem of for shell-outs which take minutes rather than seconds to complete - such as where it is required for means creation and compression of NetCDF files in PostProc. Perhaps an additional keyword argument to the routine at L9 would be useful -timeout=None? - L30 - Consider the use of the KW argument
run(check=True)- I cannot think of a reason why we might NOT want to check the output? This saves theifstatement at L35, since asubprocess.CalledProcessErrorexception would be raised if rcode != 0 - L37 - The
run(check_output)argument was added at Python3.7. This renders the version check here redundant. Policy, going forward, is not to support Python2 anyway. With this in mind - we may need to enforce Python3.7+ at package top level. - L40 - Consider catching the exception
subprocess.TimeoutExpired - L43 - What is the use-case for the
OSErrorexception being raised? Is this a copy and paste from PostProc, because if so, I'm not sure it is relevant for this implementation. Please use unit tests to ensure all code is executable as designed. - L47 - Please ensure that the return value types are (int, str) in all cases - In normal execution,
outputis currently an instance ofsubprocess.completedprocess. At L38, we needoutput = output.stdout.decode(). Possibly rename the return valueoutputto avoid confusion.
- L26 - I'm not convinced
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I think that where you have replaced common.exec_subproc_timeout(du_command, timeout) with shellout._exec_subprocess(du_command, timeout), what you have actually done is set keyword argument verbose=timeout. verbose is currently the only KW arg available currently.
Unit tests for this would make this error clear.
Coupled_Drivers/cpmip_xios.py
Outdated
| rcode, out = common.exec_subproc_timeout( | ||
| ['grep', 'total time', i_f], timeout) | ||
| rcode, out = shellout._exec_subprocess( | ||
| 'grep "total time" %s' % i_f, timeout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you are setting verbose=timeout here - not quite what you meant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have definitely replace all calls to these functions from drivers code, then by all means remove these methods. rose-stem will be required to check most of these. If you have only replaced a subset then please leave these here, with "Method deprecated in favour of moci_utils.shellout" added to the docstring.
PR Summary
Code Reviewer: @ericaneininger
This PR aims to unify shell out commands across MOCI so that they all have the same semantics. The changes involved include adding a separate library to handle shell operations and replacing current shell operations with a newly implemented
exec_subprocessfunction.closes #12
Code Quality Checklist
(Some checks are automatically carried out via the CI pipeline)
readability of the code
Testing
acceptable (eg. kgo changes)
tests, unit tests, etc.)
Security Considerations
Performance Impact
performance measurements have been conducted
AI Assistance and Attribution
of Generative AI tool name (e.g., Met Office Github Copilot Enterprise,
Github Copilot Personal, ChatGPT GPT-4, etc) and I have followed the
Simulation Systems AI policy
(including attribution labels)
Documentation
confirmed that it builds correctly
Code Review