-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from nspcc-dev/added_replace_existing_flag
- Loading branch information
Showing
7 changed files
with
432 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import subprocess | ||
import json | ||
|
||
|
||
def neofs_cli_execute(cmd: str, json_output: bool = False, timeout: int = None): | ||
""" | ||
Executes a given command and returns its output. | ||
:param cmd: Command to execute. | ||
:param json_output: Specifies if the command output is JSON. | ||
:param timeout: Optional timeout for command execution. | ||
:return: Command output as a string or a JSON object. | ||
""" | ||
|
||
try: | ||
compl_proc = subprocess.run( | ||
cmd, | ||
check=True, | ||
text=True, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
timeout=timeout, | ||
shell=True, | ||
) | ||
|
||
print(f"RC: {compl_proc.returncode}") | ||
print(f"Output: {compl_proc.stdout}") | ||
|
||
if json_output: | ||
try: | ||
return json.loads(compl_proc.stdout) | ||
except json.JSONDecodeError: | ||
output_list = compl_proc.stdout.splitlines() | ||
return json.dumps(output_list) | ||
else: | ||
return compl_proc.stdout.splitlines() | ||
|
||
except subprocess.CalledProcessError as e: | ||
raise Exception( | ||
f"Command failed: {e.cmd}\n" | ||
f"Error code: {e.returncode}\n" | ||
f"Output: {e.output}\n" | ||
f"Stdout: {e.stdout}\n" | ||
f"Stderr: {e.stderr}\n" | ||
) |
Oops, something went wrong.