-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
48 lines (40 loc) · 1.45 KB
/
Copy pathutils.py
File metadata and controls
48 lines (40 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import logging
import select
import socket
import time
from .exceptions import WaitTimeout
logger = logging.getLogger(__name__)
def waitfor(condition, help=None, timeout=10, poll_interval=0.1):
if not help:
help = (condition.__doc__ or repr(condition)).strip()
expires = time.monotonic() + timeout
while time.monotonic() < expires:
logger.debug('Polling for %s ...', help)
rv = condition()
if rv:
logger.debug('Polling for %s successful: %r.', help, rv)
return rv
time.sleep(poll_interval)
raise WaitTimeout('Timeout expired')
def wait_for_ssh(port, timeout=10):
def ssh_tcp():
logger.debug('SSH: trying to connect to port %d ...', port)
try:
sock = socket.create_connection(('localhost', port))
except OSError as e:
logger.debug('SSH: got an OSError: %s', e)
return False
else:
sock.setblocking(0)
logger.debug('SSH: connected, reading 3 bytes ...')
ready = select.select([sock], [], [], 0.1)
logger.debug('SSH: ready: %r', ready)
if not ready[0]:
logger.debug('SSH: no data ready')
return False
buffer = sock.recv(3)
logger.debug('SSH: received %s', buffer)
if buffer == b'SSH':
logger.debug('SSH: success!')
return True
waitfor(ssh_tcp, timeout=timeout)