2021-09-10 12:44:40 +00:00
|
|
|
#!/usr/bin/python3.8
|
|
|
|
|
|
|
|
"""
|
|
|
|
Helper functions to use with `neofs-cli`, `neo-go`
|
|
|
|
and other CLIs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
|
2022-06-09 13:08:11 +00:00
|
|
|
import pexpect
|
2021-09-10 12:44:40 +00:00
|
|
|
from robot.api import logger
|
|
|
|
|
|
|
|
ROBOT_AUTO_KEYWORDS = False
|
|
|
|
|
|
|
|
|
2022-01-10 11:02:57 +00:00
|
|
|
def _cmd_run(cmd, timeout=30):
|
2021-09-10 12:44:40 +00:00
|
|
|
"""
|
|
|
|
Runs given shell command <cmd>, in case of success returns its stdout,
|
|
|
|
in case of failure returns error message.
|
|
|
|
"""
|
|
|
|
try:
|
2022-03-15 11:58:59 +00:00
|
|
|
logger.info(f"Executing command: {cmd}")
|
2021-09-10 12:44:40 +00:00
|
|
|
compl_proc = subprocess.run(cmd, check=True, universal_newlines=True,
|
2022-06-09 13:08:11 +00:00
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=timeout,
|
|
|
|
shell=True)
|
2021-09-10 12:44:40 +00:00
|
|
|
output = compl_proc.stdout
|
|
|
|
logger.info(f"Output: {output}")
|
|
|
|
return output
|
|
|
|
except subprocess.CalledProcessError as exc:
|
|
|
|
raise RuntimeError(f"Error:\nreturn code: {exc.returncode} "
|
2022-06-09 13:08:11 +00:00
|
|
|
f"\nOutput: {exc.output}") from exc
|
2022-02-07 11:41:34 +00:00
|
|
|
except Exception as exc:
|
|
|
|
return_code, _ = subprocess.getstatusoutput(cmd)
|
|
|
|
logger.info(f"Error:\nreturn code: {return_code}\nOutput: "
|
2022-06-09 13:08:11 +00:00
|
|
|
f"{exc.output.decode('utf-8') if type(exc.output) is bytes else exc.output}")
|
2022-02-07 11:41:34 +00:00
|
|
|
raise
|
2021-11-03 12:48:31 +00:00
|
|
|
|
2022-06-09 13:08:11 +00:00
|
|
|
|
2021-11-03 12:48:31 +00:00
|
|
|
def _run_with_passwd(cmd):
|
2022-02-07 11:41:34 +00:00
|
|
|
child = pexpect.spawn(cmd)
|
|
|
|
child.expect(".*")
|
|
|
|
child.sendline('\r')
|
|
|
|
child.wait()
|
|
|
|
cmd = child.read()
|
2021-11-03 12:48:31 +00:00
|
|
|
return cmd.decode()
|