CHIP-tools/verify.sh

119 lines
2.9 KiB
Bash
Raw Normal View History

2015-09-17 03:55:39 -04:00
#!/usr/bin/env python
2015-09-15 20:12:23 -04:00
2015-09-17 03:55:39 -04:00
import io
import sys
import serial
import re
2015-10-21 17:26:59 -04:00
import time
2015-09-17 03:55:39 -04:00
#------------------------------------------------------------------
def answer_prompt(sio,prompt_to_wait_for,answer_to_write,send_cr=True):
2015-09-17 03:55:39 -04:00
#------------------------------------------------------------------
sio.flush()
2015-09-17 03:55:39 -04:00
prompt_found = False
data = ''
#if send_cr:
#sio.write(unicode('\n'))
d='something'
while not len(d)==0:
d = sio.read(2000);
data += d
2015-10-21 17:26:59 -04:00
time.sleep(1)
# print '-' * 50
# print ' %d bytes read' % (len(data))
# print '-' * 50
2015-10-21 17:26:59 -04:00
#print data
2015-10-21 17:26:59 -04:00
line=''
2015-09-17 03:55:39 -04:00
while not prompt_found:
d = sio.read(100);
data += d
2015-10-21 17:26:59 -04:00
# print '-' * 50
# print ' %d bytes read' % (len(data))
# print '-' * 50
# print data
# print '-' * 50
if len(data.split())>0:
line=data.split()[-1]
# print "matching [%s] against [%s]" % (line,prompt_to_wait_for)
if(re.match(prompt_to_wait_for,line,re.M)):
2015-09-17 03:55:39 -04:00
sio.write(unicode(answer_to_write+'\n'))
2015-10-21 17:26:59 -04:00
# print '-' * 50
# print ' detected [%s] ' % prompt_to_wait_for
# print '-' * 50
2015-09-17 03:55:39 -04:00
prompt_found = True
else:
if send_cr:
sio.write(unicode('\n'))
2015-09-17 03:55:39 -04:00
sio.flush()
#sys.stdin.readline()
2015-09-17 03:55:39 -04:00
#------------------------------------------------------------------
def scanfor(sio,regexp_to_scan_for,answer_to_write):
#------------------------------------------------------------------
prompt_found = False
data = ''
while not prompt_found:
data += sio.read(100);
2015-10-21 17:26:59 -04:00
# print '-' * 50
# print ' %d bytes read' % (len(data))
# print '-' * 50
# print data
2015-09-17 03:55:39 -04:00
if re.search(regexp_to_scan_for,data):
2015-10-21 17:26:59 -04:00
# print '-' * 50
# print ' detected [%s] ' % regexp_to_scan_for
# print '-' * 50
2015-09-17 03:55:39 -04:00
sio.write(unicode(answer_to_write+'\n'))
prompt_found = True
sio.flush()
return data
#------------------------------------------------------------------
def main():
#------------------------------------------------------------------
if( len(sys.argv)>1 ):
serial_port=sys.argv[1]
else:
serial_port='/dev/ttyACM0';
2015-10-21 17:26:59 -04:00
#print 'reading from %s:' % serial_port
2015-09-17 03:55:39 -04:00
ser = serial.Serial(serial_port,115200, timeout=1);
sio = io.TextIOWrapper(io.BufferedRWPair(ser,ser))
#login
2015-10-21 17:26:59 -04:00
print "login...",
sys.stdout.flush()
answer_prompt(sio,'.*login:','chip',True)
print "OK\npassword...",
sys.stdout.flush()
answer_prompt(sio,'.*Password:','chip',False)
print "OK\npoweroff...",
sys.stdout.flush()
answer_prompt(sio,'.*[\$#]','sudo poweroff')
answer_prompt(sio,'.*:','chip')
time.sleep(2)
print "OK\n",
#d=scanfor(sio,r'.*### [^#]+ ###.*','poweroff')
# if re.search(r'.*### ALL TESTS PASSED ###.*',d):
# print "---> TESTS PASSED"
# ser.close();
# return 0
2015-09-17 03:55:39 -04:00
ser.close();
2015-10-21 17:26:59 -04:00
# print "---> TESTS FAILED"
2015-10-23 20:00:00 -04:00
return 0
2015-09-17 03:55:39 -04:00
#------------------------------------------------------------------
if __name__ == "__main__":
#------------------------------------------------------------------
exit( main() )