-
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?
Changes from all commits
1b4368b
de73ecd
f8b0a3a
786900f
7da2a02
da9af7d
bb4667a
9ca9722
baac45d
bf3f823
787b5b5
665b2fa
af43b3c
abe99cd
d3aafae
e3219e0
b5aaefc
49e485d
4a50e2d
ecedc2e
757f378
5afda7e
ca5b95a
8c575a0
04e4a1f
018bc2b
8074de2
19e7e24
4203810
17fb5c9
10a063a
f91f9b9
a1296d3
a125be9
7bb2cf1
25974d2
1191b57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I think that where you have replaced |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # (C) Crown copyright Met Office. All rights reserved. | ||
| # The file LICENCE, distributed with this code, contains details of the terms | ||
| # under which the code may be used. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
| from mocilib import shellout | ||
| from hypothesis import given, strategies as st | ||
|
|
||
| class ExecTets(unittest.TestCase): | ||
| ''' Unit tests for executing shellout commands''' | ||
|
|
||
| def test_semicolon_commands(self): | ||
| cmd = "echo Hello There;echo General Kenobi" | ||
| _,rcode = shellout._exec_subprocess(cmd=cmd) | ||
| assert rcode == 0 | ||
|
|
||
| def test_and_commands(self): | ||
| cmd ="echo Hello There&&echo General Kenobi" | ||
| _,rcode = shellout._exec_subprocess(cmd=cmd) | ||
| assert rcode == 0 | ||
|
|
||
| @given(st.text()) | ||
| def test_called_process_error(self,directory): | ||
| cmd = f"ls /{directory}" | ||
| _,rcode = shellout._exec_subprocess(cmd=cmd) | ||
| assert rcode != 0 | ||
|
|
||
| def test_timeout_expired(self): | ||
| cmd = "sleep 15" | ||
| _,rcode = shellout._exec_subprocess(cmd=cmd,timeout=10) | ||
| assert rcode != 0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ___all__ = ["shellout"] | ||
| from . import shellout |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple of things to note here:
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # ----------------------------------------------------------------------------- | ||
| # (C) Crown copyright Met Office. All rights reserved. | ||
| # The file LICENCE, distributed with this code, contains details of the terms | ||
| # under which the code may be used. | ||
| # ----------------------------------------------------------------------------- | ||
|
|
||
| import subprocess | ||
| import os | ||
| import sys | ||
| import shlex | ||
|
|
||
|
|
||
| def _exec_subprocess(cmd, verbose=False, timeout=None ,current_working_directory=os.getcwd()): | ||
| """ | ||
| Execute a given shell command | ||
|
|
||
| :param cmd: The command to be executed given as a string | ||
| :param verbose: A boolean value to determine if the stdout | ||
| stream is displayed during the runtime. | ||
| :param current_working_directory: The directory in which the | ||
| command should be executed. | ||
| """ | ||
|
|
||
| cmd = shlex.split(cmd) | ||
|
|
||
| try: | ||
|
|
||
| output = subprocess.run( | ||
| cmd, | ||
| capture_output=True, | ||
| cwd=current_working_directory, | ||
| timeout=timeout, | ||
| check=True | ||
| ) | ||
| rcode = output.returncode | ||
| output_message = output.stdout.decode() | ||
|
|
||
| if verbose and output: | ||
| sys.stdout.write(f"[DEBUG]{output.stdout}\n") | ||
| if output.stderr and output.returncode != 0: | ||
| sys.stderr.write(f"[ERROR] {output.stderr}\n") | ||
|
|
||
| except subprocess.CalledProcessError as exc: | ||
| output_message = exc.stdout.decode() if exc.stdout else "" | ||
| rcode = exc.returncode | ||
|
|
||
| except subprocess.TimeoutExpired as exc: | ||
| output_message = exc.stdout.decode() if exc.stdout else "" | ||
| rcode = exc.returncode | ||
|
|
||
| return rcode,output_message |
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.