Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
try:
if self._master.wait(timeout=30) != 0:
raise ExecutionError(
"failed to connect to {} with args {}, returncode={} {},{} ".format(
self.host, args, self._master.wait(),
self._master.stdout.readlines(),
self._master.stderr.readlines()
)
)
except subprocess.TimeoutExpired:
raise ExecutionError(
"failed to connect (timeout) to {} with args {} {},{}".format(
self.host, args, self._master.stdout.readlines(),
self._master.stderr.readlines()
)
)
if not os.path.exists(control):
raise ExecutionError("no control socket to {}".format(self.host))
self._socket = control
self._logger.debug('Connected to %s', self.host)
match = re.search(r'Size:\s+(?P\d+)', '\n'.join(out))
if ret != 0 or not match or not match.group("size"):
raise ExecutionError("Could not stat '{}' on target".format(remotefile))
file_size = int(match.group('size'))
self.logger.debug('file size on target is %d', file_size)
self._start_xmodem_transfer(cmd)
modem = xmodem.XMODEM(self._xmodem_getc, self._xmodem_putc)
recvd_size = modem.recv(buf)
self.logger.debug('xmodem.recv() returned %r', recvd_size)
# remove CPMEOF (0x1a) padding
if recvd_size < file_size:
raise ExecutionError('Only received {} bytes of {} expected'.
format(recvd_size, file_size))
self.logger.debug('received %d bytes of payload', file_size)
buf.truncate(file_size)
self.console.expect(self.prompt, timeout=30)
# return everything as bytes
buf.seek(0)
return buf.read()
def measure(self):
out = processwrapper.check_output(
self._get_sigrok_prefix() + ["--show"]
)
res = {}
for line in out.splitlines():
line = line.strip()
if b':' not in line:
continue
k, v = line.split(b':', 1)
if k == b'voltage':
res['voltage'] = float(v)
elif k == b'current':
res['current'] = float(v)
if len(res) != 2:
raise ExecutionError("Cannot parse --show output {}".format(out))
return res
def monitor_command(self, command):
"""Execute a monitor_command via the QMP"""
if not self.status:
raise ExecutionError(
"Can't use monitor command on non-running target")
self.qmp.execute(command)
# following format:
#
# Again the status of all ports is made available on all pages.
index = int(index)
assert 1 <= index <= 24
# get the contents of the main page
r = requests.get("http://{}:{}/ov.html".format(host, port))
r.raise_for_status()
for line in r.text.splitlines():
if line.find("content=\"Power Port {}".format(index)) > 0:
if line.find(",0") > 0:
return False
if line.find(",1") > 0:
return True
raise ExecutionError("failed to parse the port status")
# if we got this far, something is wrong with the website
raise ExecutionError("failed to find the port")
"scp",
*self.ssh_prefix,
"-P", str(self.networkservice.port),
"{user}@{host}:{filename}".format(
user=self.networkservice.username,
host=self.networkservice.address,
filename=filename),
destination
]
try:
sub = subprocess.call(
transfer_cmd
) #, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except:
raise ExecutionError(
"error executing command: {}".format(transfer_cmd)
)
if sub != 0:
raise ExecutionError(
"error executing command: {}".format(transfer_cmd)
)
def _get_bytes(self, remotefile: str):
buf = io.BytesIO()
cmd = self._get_xmodem_sx_cmd(remotefile)
self.logger.info('XMODEM send command on target: %s', cmd)
# get file size to remove XMODEM's CPMEOF padding at the end of the last packet
out, _, ret = self._run("stat '{}'".format(remotefile))
match = re.search(r'Size:\s+(?P\d+)', '\n'.join(out))
if ret != 0 or not match or not match.group("size"):
raise ExecutionError("Could not stat '{}' on target".format(remotefile))
file_size = int(match.group('size'))
self.logger.debug('file size on target is %d', file_size)
self._start_xmodem_transfer(cmd)
modem = xmodem.XMODEM(self._xmodem_getc, self._xmodem_putc)
recvd_size = modem.recv(buf)
self.logger.debug('xmodem.recv() returned %r', recvd_size)
# remove CPMEOF (0x1a) padding
if recvd_size < file_size:
raise ExecutionError('Only received {} bytes of {} expected'.
format(recvd_size, file_size))
self.logger.debug('received %d bytes of payload', file_size)
def close(self):
"""Stops the subprocess, does nothing if it is already closed"""
if not self.status:
return
if self._child.poll() is not None:
raise ExecutionError("child has vanished")
self._child.terminate()
try:
outs, errs = self._child.communicate(timeout=1)
except subprocess.TimeoutExpired:
self._child.kill()
outs, errs = self._child.communicate()
if outs:
self.logger.info("child stdout while closing: %s", outs)
if errs:
self.logger.warning("child error while closing: %s", errs)
if mode == Mode.DD:
block_size = '512' if skip or seek else '4M'
args = [
"dd",
"if={}".format(mf.get_remote_path()),
"of={}{}".format(self.storage.path, partition),
"status=progress",
"bs={}".format(block_size),
"skip={}".format(skip),
"seek={}".format(seek),
"conv=fdatasync"
]
elif mode == Mode.BMAPTOOL:
if skip or seek:
raise ExecutionError("bmaptool does not support skip or seek")
args = [
"bmaptool",
"copy",
"{}".format(mf.get_remote_path()),
"{}{}".format(self.storage.path, partition),
]
else:
raise ValueError
processwrapper.check_output(
self.storage.command_prefix + args
)