|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import time |
| 6 | + |
| 7 | +import pkg_resources |
| 8 | +from brainflow_emulator.emulate_common import TestFailureError, log_multilines |
| 9 | +from brainflow_emulator.knightboard_emulator import Listener |
| 10 | +from serial import Serial |
| 11 | + |
| 12 | + |
| 13 | +def write(port, data): |
| 14 | + return port.write(data) |
| 15 | + |
| 16 | + |
| 17 | +def read(port, num_bytes): |
| 18 | + return port.read(num_bytes) |
| 19 | + |
| 20 | + |
| 21 | +def get_isntaller(): |
| 22 | + return pkg_resources.resource_filename(__name__, os.path.join('com0com', 'setup_com0com_W7_x64_signed.exe')) |
| 23 | + |
| 24 | + |
| 25 | +def install_com0com(): |
| 26 | + this_directory = os.path.abspath(os.path.dirname(__file__)) |
| 27 | + directory = os.path.join(this_directory, 'com0com') |
| 28 | + if not os.path.exists(directory): |
| 29 | + os.makedirs(directory) |
| 30 | + cmds = [get_isntaller(), '/NCRC', '/S', '/D=%s' % directory] |
| 31 | + logging.info('running %s' % ' '.join(cmds)) |
| 32 | + p = subprocess.Popen(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 33 | + out, err = p.communicate() |
| 34 | + if p.returncode != 0: |
| 35 | + logging.error('stdout is %s' % out) |
| 36 | + logging.error('stderr is %s' % err) |
| 37 | + raise Exception('com0com installation failure') |
| 38 | + logging.info('Sleeping a few second, it doesnt work in appveyour without it') |
| 39 | + time.sleep(10) |
| 40 | + return directory |
| 41 | + |
| 42 | + |
| 43 | +def get_ports_windows(): |
| 44 | + directory = install_com0com() |
| 45 | + # remove ports from previous run if any |
| 46 | + p = subprocess.Popen([os.path.join(directory, 'setupc.exe'), 'remove', '0'], |
| 47 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=directory) |
| 48 | + stdout, stderr = p.communicate() |
| 49 | + logging.info('remove stdout is %s' % stdout) |
| 50 | + logging.info('remove stderr is %s' % stderr) |
| 51 | + |
| 52 | + m_name = 'COM14' |
| 53 | + s_name = 'COM15' |
| 54 | + |
| 55 | + p = subprocess.Popen( |
| 56 | + [os.path.join(directory, 'setupc.exe'), 'install', 'PortName=%s' % m_name, 'PortName=%s' % s_name], |
| 57 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=directory) |
| 58 | + stdout, stderr = p.communicate() |
| 59 | + logging.info('install stdout is %s' % stdout) |
| 60 | + logging.info('install stderr is %s' % stderr) |
| 61 | + |
| 62 | + if p.returncode != 0: |
| 63 | + raise Exception('com0com failure') |
| 64 | + logging.info('Sleeping a few second, it doesnt work in appveyour without it') |
| 65 | + time.sleep(10) |
| 66 | + return m_name, s_name |
| 67 | + |
| 68 | + |
| 69 | +def test_serial(cmd_list, m_name, s_name): |
| 70 | + master = Serial('\\\\.\\%s' % m_name, timeout=0) |
| 71 | + listen_thread = Listener(master, write, read) |
| 72 | + listen_thread.daemon = True |
| 73 | + listen_thread.start() |
| 74 | + |
| 75 | + cmd_to_run = cmd_list + [s_name] |
| 76 | + logging.info('Running %s' % ' '.join([str(x) for x in cmd_to_run])) |
| 77 | + process = subprocess.Popen(cmd_to_run, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 78 | + stdout, stderr = process.communicate() |
| 79 | + |
| 80 | + log_multilines(logging.info, stdout) |
| 81 | + log_multilines(logging.info, stderr) |
| 82 | + |
| 83 | + master.close() |
| 84 | + if process.returncode != 0: |
| 85 | + raise TestFailureError('Test failed with exit code %s' % str(process.returncode), process.returncode) |
| 86 | + |
| 87 | + return stdout, stderr |
| 88 | + |
| 89 | + |
| 90 | +def main(cmd_list): |
| 91 | + if not cmd_list: |
| 92 | + raise Exception('No command to execute') |
| 93 | + |
| 94 | + m_name, s_name = get_ports_windows() |
| 95 | + test_serial(cmd_list, m_name, s_name) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + logging.basicConfig(level=logging.INFO) |
| 100 | + main(sys.argv[1:]) |
0 commit comments