<s> import time <EOL> import serial <EOL> from serial . tools import list_ports <EOL> from biicode . common . settings . osinfo import OSInfo <EOL> from biicode . common . output_stream import Color <EOL> import platform <EOL> def _detect_arduino_port ( ) : <EOL> """<STR_LIT>""" <EOL> port_patterns = { "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] } <EOL> found = [ ] <EOL> ports = [ { "<STR_LIT:port>" : _port , "<STR_LIT>" : _desc , "<STR_LIT>" : _hwid } <EOL> for _port , _desc , _hwid in list_ports . comports ( ) ] <EOL> for port in ports : <EOL> for pattern in port_patterns [ platform . system ( ) ] : <EOL> if port [ "<STR_LIT>" ] and pattern . lower ( ) in port [ "<STR_LIT>" ] . lower ( ) or port [ "<STR_LIT:port>" ] and pattern . lower ( ) in port [ "<STR_LIT:port>" ] . lower ( ) : <EOL> found . append ( port [ "<STR_LIT:port>" ] ) <EOL> return found <EOL> def check_port ( user_io , current_port , wizard = False ) : <EOL> '''<STR_LIT>''' <EOL> port_list = _detect_arduino_port ( ) <EOL> if len ( port_list ) == <NUM_LIT:1> : <EOL> port = port_list [ <NUM_LIT:0> ] <EOL> if current_port : <EOL> if port != current_port : <EOL> user_io . out . warn ( "<STR_LIT>" <EOL> % ( current_port , port ) ) <EOL> current_port = port <EOL> elif len ( port_list ) > <NUM_LIT:1> : <EOL> user_io . out . warn ( "<STR_LIT>" <EOL> % ( '<STR_LIT:U+002C>' . join ( port_list ) ) ) <EOL> if current_port and current_port not in port_list : <EOL> user_io . out . warn ( "<STR_LIT>" % current_port ) <EOL> port_list . append ( current_port ) <EOL> if wizard : <EOL> current_port = user_io . request_option ( '<STR_LIT:port>' , <EOL> default_option = current_port , <EOL> options = port_list , <EOL> one_line_options = True ) <EOL> else : <EOL> user_io . out . warn ( "<STR_LIT>" ) <EOL> elif len ( port_list ) == <NUM_LIT:0> : <EOL> user_io . out . error ( "<STR_LIT>" ) <EOL> if wizard : <EOL> current_port = user_io . request_string ( '<STR_LIT>' ) <EOL> else : <EOL> user_io . out . warn ( "<STR_LIT>" ) <EOL> current_port = current_port or '<STR_LIT:None>' <EOL> user_io . out . writeln ( "<STR_LIT>" % current_port , front = Color . GREEN ) <EOL> return current_port <EOL> def refresh_port ( user_io , old_port , reset = False , wizard = False ) : <EOL> '''<STR_LIT>''' <EOL> new_port = check_port ( user_io , old_port , wizard ) <EOL> if reset : <EOL> new_port = _reset_serial ( user_io . out , new_port , wait_for_upload_port = True ) <EOL> return new_port <EOL> def get_boards_need_reset ( sdk_path , arduino_sdk_version ) : <EOL> '''<STR_LIT>''' <EOL> from biicode . client . dev . hardware . arduino . arduino_converter import boards_pretty_settings <EOL> board_settings = boards_pretty_settings ( sdk_path , arduino_sdk_version ) <EOL> boards_need_reset = [ ] <EOL> for board , value in board_settings . iteritems ( ) : <EOL> for _setting , _value in value : <EOL> if '<STR_LIT:file>' in _setting and '<STR_LIT>' in _value . lower ( ) : <EOL> boards_need_reset . append ( board ) <EOL> return boards_need_reset <EOL> def _touch_serial_port ( serial_port , baudrate = <NUM_LIT> ) : <EOL> '''<STR_LIT>''' <EOL> current_serial = serial . Serial ( ) <EOL> current_serial . port = serial_port <EOL> current_serial . baudrate = baudrate <EOL> current_serial . bytesize = serial . EIGHTBITS <EOL> current_serial . stopbits = serial . STOPBITS_ONE <EOL> current_serial . parity = serial . PARITY_NONE <EOL> current_serial . open ( ) <EOL> current_serial . close ( ) <EOL> def _reset_serial ( out , serial_port , wait_for_upload_port = False ) : <EOL> '''<STR_LIT>''' <EOL> caterina_serial_port = '<STR_LIT>' <EOL> before_serial_list = _detect_arduino_port ( ) <EOL> if serial_port in before_serial_list : <EOL> non_serial_list = before_serial_list [ : ] <EOL> non_serial_list . remove ( serial_port ) <EOL> out . success ( '<STR_LIT>' % serial_port ) <EOL> _touch_serial_port ( serial_port , <NUM_LIT> ) <EOL> if not wait_for_upload_port : <EOL> time . sleep ( <NUM_LIT> ) <EOL> return serial_port <EOL> time . sleep ( <NUM_LIT:3> if OSInfo . is_win ( ) else <NUM_LIT> ) <EOL> elapsed = <NUM_LIT:0> <EOL> while ( elapsed < <NUM_LIT> ) : <EOL> now_serial_list = _detect_arduino_port ( ) <EOL> diff_serial_list = [ v for v in now_serial_list if v not in non_serial_list ] <EOL> out . success ( '<STR_LIT>' <EOL> % ( before_serial_list , now_serial_list , diff_serial_list ) ) <EOL> if len ( diff_serial_list ) > <NUM_LIT:0> : <EOL> caterina_serial_port = diff_serial_list [ <NUM_LIT:0> ] <EOL> out . success ( '<STR_LIT>' % caterina_serial_port ) <EOL> break <EOL> time . sleep ( <NUM_LIT> ) <EOL> elapsed += <NUM_LIT> <EOL> if ( ( ( not OSInfo . is_win ( ) and elapsed >= <NUM_LIT> ) <EOL> or elapsed >= <NUM_LIT> ) and ( serial_port in now_serial_list ) ) : <EOL> out . success ( '<STR_LIT>' % serial_port ) <EOL> caterina_serial_port = serial_port <EOL> break <EOL> if not caterina_serial_port : <EOL> out . error ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return caterina_serial_port or '<STR_LIT:None>' </s>
