Example #1
0
def load_arduino_cli(sketch_path=None):
    """
    Launches a subprocess to invoke the Arduino IDE command line to open,
    verify or upload an sketch, the location of which is indicated in the input
    parameter.
    :param sketch_path: Path to the sketch to load into the Arduino IDE.
    :return: A tuple with the following data (success, conclusion, out, error,
            exit_code)
    """
    success = True
    conclusion = error = out = exit_code = ''

    # Input sanitation and output defaults
    if not sketch_path:
        sketch_path = create_sketch_default()
    else:
        if not os.path.isfile(sketch_path):
            conclusion = error = 'Provided sketch path is not a valid file: %s'\
                                 % sketch_path
            success = False
            return success, conclusion, out, error, exit_code

    settings = ServerCompilerSettings()

    # Check if CLI flags have been set
    if not settings.compiler_dir:
        success = False
        conclusion = 'Unable to find Arduino IDE'
        error = 'The compiler directory has not been set.\n' + \
                'Please set it in the Settings.'
    else:
        if not settings.load_ide_option:
            success = False
            conclusion = 'What should we do with the Sketch?'
            error = 'The launch IDE option has not been set.\n' + \
                    'Please select an IDE option in the Settings.'
        elif settings.load_ide_option == 'upload':
            if not settings.get_serial_port_flag():
                success = False
                conclusion = 'Serial Port unavailable'
                error = 'The Serial Port does not exist.\n' + \
                        'Please check if the Arduino is correctly ' + \
                        'connected to the PC and select the Serial Port in ' +\
                        'the Settings.'
            if not settings.get_arduino_board_flag():
                success = False
                conclusion = 'Unknown Arduino Board'
                error = 'The Arduino Board has not been set.\n' + \
                        'Please select the appropriate Arduino Board from ' + \
                        'the settings.'

    if success:
        # Concatenates the CLI command and execute if the flags are valid
        cli_command = [settings.compiler_dir]
        if settings.load_ide_option == 'upload':
            print('\nUploading sketch to Arduino...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'Successfully Uploaded Sketch'
            cli_command.append('--upload')
            cli_command.append('--port')
            cli_command.append(settings.get_serial_port_flag())
            cli_command.append('--board')
            cli_command.append(settings.get_arduino_board_flag())
        elif settings.load_ide_option == 'verify':
            print('\nVerifying the sketch...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'Successfully Verified Sketch'
            cli_command.append('--verify')
        elif settings.load_ide_option == 'open':
            print('\nOpening the sketch in the Arduino IDE...')
            conclusion = 'Sketch opened in IDE'
            out = 'The sketch should be loaded in the Arduino IDE.'
        cli_command.append("%s" % sketch_path)
        print('CLI command: %s' % ' '.join(cli_command))
        # Python 2 needs the input to subprocess.Popen to be in system encoding
        if sys.version_info[0] < 3:
            for item in six_moves.range(len(cli_command)):
                cli_command[item] = cli_command[item].encode(
                    locale.getpreferredencoding())

        if settings.load_ide_option == 'open':
            # Open IDE in a subprocess without capturing outputs
            subprocess.Popen(cli_command, shell=False)
            # Wait a few seconds to allow IDE to open before sending back data
            time.sleep(5)
        else:
            # Launch the Arduino CLI in a subprocess and capture output data
            process = subprocess.Popen(cli_command,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       shell=False)
            out, error = process.communicate()
            out = six.u(out)
            error = six.u(error)
            exit_code = process.returncode
            print('Arduino output:\n%s' % out)
            print('Arduino Error output:\n%s' % error)
            print('Arduino Exit code: %s' % exit_code)
            # For some reason Arduino CLI can return 256 on success
            if (process.returncode != 0) and (process.returncode != 256):
                success = False
                if exit_code == 1:
                    conclusion = 'Build or Upload failed'
                elif exit_code == 2:
                    conclusion = 'Sketch not found'
                elif exit_code == 3:
                    conclusion = 'Invalid command line argument'
                elif exit_code == 4:
                    conclusion =\
                        'Preference passed to "get-pref" flag does not exist'
                else:
                    conclusion = 'Unexpected exit error code: %s' % exit_code

    return success, conclusion, out, error, exit_code
Example #2
0
def load_arduino_cli(sketch_path=None):
    """
    Launches a subprocess to invoke the Arduino IDE command line to open,
    verify or upload an sketch, the location of which is indicated in the input
    parameter.
    :param sketch_path: Path to the sketch to load into the Arduino IDE.
    :return: A tuple with the following data (success, conclusion, out, error,
            exit_code)
    """
    success = True
    conclusion = error = out = exit_code = ''

    # Input sanitation and output defaults
    if not sketch_path:
        sketch_path = create_sketch_default()
    else:
        if not os.path.isfile(sketch_path):
            conclusion = error = 'Provided sketch path is not a valid file: %s'\
                                 % sketch_path
            success = False
            return success, conclusion, out, error, exit_code

    settings = ServerCompilerSettings()

    # Check if CLI flags have been set
    if not settings.compiler_dir:
        success = False
        conclusion = 'arduinoOpErrorIdeDirTitle'
        error = 'arduinoOpErrorIdeDirBody'
    else:
        if not settings.load_ide_option:
            success = False
            conclusion = 'arduinoOpErrorIdeOptionTitle'
            error = 'arduinoOpErrorIdeOptionBody'
        elif settings.load_ide_option == 'upload':
            if not settings.get_serial_port_flag():
                success = False
                conclusion = 'arduinoOpErrorIdePortTitle'
                error = 'arduinoOpErrorIdePortBody'
            if not settings.get_arduino_board_flag():
                success = False
                conclusion = 'arduinoOpErrorIdeBoardTitle'
                error = 'arduinoOpErrorIdeBoardBody'

    if success:
        # Concatenates the CLI command and execute if the flags are valid
        cli_command = [settings.compiler_dir]
        if settings.load_ide_option == 'upload':
            print('\nUploading sketch to Arduino...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'arduinoOpUploadedTitle'
            cli_command.append('--upload')
            cli_command.append('--port')
            cli_command.append(settings.get_serial_port_flag())
            cli_command.append('--board')
            cli_command.append(settings.get_arduino_board_flag())
        elif settings.load_ide_option == 'verify':
            print('\nVerifying the sketch...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'arduinoOpVerifiedTitle'
            cli_command.append('--verify')
        elif settings.load_ide_option == 'open':
            print('\nOpening the sketch in the Arduino IDE...')
            conclusion = 'arduinoOpOpenedTitle'
            out = 'arduinoOpOpenedBody'
        cli_command.append("%s" % sketch_path)
        print('CLI command: %s' % ' '.join(cli_command))
        # Python 2 needs the input to subprocess.Popen to be in system encoding
        if sys.version_info[0] < 3:
            for item in six_moves.range(len(cli_command)):
                cli_command[item] = cli_command[item].encode(
                    locale.getpreferredencoding())

        if settings.load_ide_option == 'open':
            # Open IDE in a subprocess without capturing outputs
            subprocess.Popen(cli_command, shell=False)
            # Wait a few seconds to allow IDE to open before sending back data
            time.sleep(5)
        else:
            # Launch the Arduino CLI in a subprocess and capture output data
            process = subprocess.Popen(
                cli_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                shell=False)
            out, error = process.communicate()
            out = six.u(out)
            error = six.u(error)
            exit_code = process.returncode
            print('Arduino output:\n%s' % out)
            print('Arduino Error output:\n%s' % error)
            print('Arduino Exit code: %s' % exit_code)
            # For some reason Arduino CLI can return 256 on success
            if (process.returncode != 0) and (process.returncode != 256):
                success = False
                if exit_code == 1:
                    conclusion = 'arduinoOpErrorUpVerTitle'
                elif exit_code == 2:
                    conclusion = 'arduinoOpErrorSketchTitle'
                elif exit_code == 3:
                    conclusion = 'arduinoOpErrorFlagTitle'
                elif exit_code == 4:
                    conclusion = 'arduinoOpErrorFlagPrefTitle'
                else:
                    conclusion = 'Unexpected exit error code: %s' % exit_code

    return success, conclusion, out, error, exit_code
def load_arduino_cli(sketch_path=None):
    """
    Launches a subprocess to invoke the Arduino IDE command line to open,
    verify or upload an sketch, the location of which is indicated in the input
    parameter.
    :param sketch_path: Path to the sketch to load into the Arduino IDE.
    :return: A tuple with the following data (success, conclusion, out, error,
            exit_code)
    """
    success = True
    conclusion = error = out = exit_code = ''

    # Input sanitation and output defaults
    if not sketch_path:
        sketch_path = create_sketch_default()
    else:
        if not os.path.isfile(sketch_path):
            conclusion = error = 'Provided sketch path is not a valid file: %s'\
                                 % sketch_path
            success = False
            return success, conclusion, out, error, exit_code

    settings = ServerCompilerSettings()

    # Check if CLI flags have been set
    if not settings.compiler_dir:
        success = False
        conclusion = 'Unable to find Arduino IDE'
        error = 'The compiler directory has not been set.\n' + \
                'Please set it in the Settings.'
    else:
        if not settings.load_ide_option:
            success = False
            conclusion = 'What should we do with the Sketch?'
            error = 'The launch IDE option has not been set.\n' + \
                    'Please select an IDE option in the Settings.'
        elif settings.load_ide_option == 'upload':
            if not settings.get_serial_port_flag():
                success = False
                conclusion = 'Serial Port unavailable'
                error = 'The Serial Port does not exist.\n' + \
                        'Please check if the Arduino is correctly ' + \
                        'connected to the PC and select the Serial Port in ' +\
                        'the Settings.'
            if not settings.get_arduino_board_flag():
                success = False
                conclusion = 'Unknown Arduino Board'
                error = 'The Arduino Board has not been set.\n' + \
                        'Please select the appropriate Arduino Board from ' + \
                        'the settings.'

    if success:
        # Concatenates the CLI command and execute if the flags are valid
        cli_command = [settings.compiler_dir]
        if settings.load_ide_option == 'upload':
            print('\nUploading sketch to Arduino...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'Successfully Uploaded Sketch'
            cli_command.append('--upload')
            cli_command.append('--port')
            cli_command.append(settings.get_serial_port_flag())
            cli_command.append('--board')
            cli_command.append(settings.get_arduino_board_flag())
        elif settings.load_ide_option == 'verify':
            print('\nVerifying the sketch...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'Successfully Verified Sketch'
            cli_command.append('--verify')
        elif settings.load_ide_option == 'open':
            print('\nOpening the sketch in the Arduino IDE...')
            conclusion = 'Sketch opened in IDE'
            out = 'The sketch should be loaded in the Arduino IDE.'
        cli_command.append("%s" % sketch_path)
        print('CLI command: %s' % ' '.join(cli_command))
        # Python 2 needs the input to subprocess.Popen to be in system encoding
        if sys.version_info[0] < 3:
            for item in six_moves.range(len(cli_command)):
                cli_command[item] = cli_command[item].encode(
                    locale.getpreferredencoding())

        if settings.load_ide_option == 'open':
            # Open IDE in a subprocess without capturing outputs
            subprocess.Popen(cli_command, shell=False)
            # Wait a few seconds to allow IDE to open before sending back data
            time.sleep(5)
        else:
            # Launch the Arduino CLI in a subprocess and capture output data
            process = subprocess.Popen(
                cli_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                shell=False)
            out, error = process.communicate()
            out = six.u(out)
            error = six.u(error)
            exit_code = process.returncode
            print('Arduino output:\n%s' % out)
            print('Arduino Error output:\n%s' % error)
            print('Arduino Exit code: %s' % exit_code)
            # For some reason Arduino CLI can return 256 on success
            if (process.returncode != 0) and (process.returncode != 256):
                success = False
                if exit_code == 1:
                    conclusion = 'Build or Upload failed'
                elif exit_code == 2:
                    conclusion = 'Sketch not found'
                elif exit_code == 3:
                    conclusion = 'Invalid command line argument'
                elif exit_code == 4:
                    conclusion =\
                        'Preference passed to "get-pref" flag does not exist'
                else:
                    conclusion = 'Unexpected exit error code: %s' % exit_code

    return success, conclusion, out, error, exit_code
Example #4
0
def load_arduino_cli(sketch_path=None):
    """
    Launches a subprocess to invoke the Arduino IDE command line to open,
    verify or upload an sketch, the location of which is indicated in the input
    parameter.
    :param sketch_path: Path to the sketch to load into the Arduino IDE.
    :return: A tuple with the following data (success, conclusion, out, error,
            exit_code)
    """
    success = True
    conclusion = error = out = exit_code = ''

    # Input sanitation and output defaults
    if not sketch_path:
        sketch_path = create_sketch_default()
    else:
        if not os.path.isfile(sketch_path):
            conclusion = error = 'Provided sketch path is not a valid file: %s'\
                                 % sketch_path
            success = False
            return success, conclusion, out, error, exit_code

    settings = ServerCompilerSettings()

    # Check if CLI flags have been set
    if not settings.compiler_dir:
        success = False
        conclusion = 'arduinoOpErrorIdeDirTitle'
        error = 'arduinoOpErrorIdeDirBody'
    else:
        if not settings.load_ide_option:
            success = False
            conclusion = 'arduinoOpErrorIdeOptionTitle'
            error = 'arduinoOpErrorIdeOptionBody'
        elif settings.load_ide_option == 'upload':
            if not settings.get_serial_port_flag():
                success = False
                conclusion = 'arduinoOpErrorIdePortTitle'
                error = 'arduinoOpErrorIdePortBody'
            if not settings.get_arduino_board_flag():
                success = False
                conclusion = 'arduinoOpErrorIdeBoardTitle'
                error = 'arduinoOpErrorIdeBoardBody'

    if success:
        # Concatenates the CLI command and execute if the flags are valid
        cli_command = [settings.compiler_dir]
        if settings.load_ide_option == 'upload':
            print('\nUploading sketch to Arduino...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'arduinoOpUploadedTitle'
            cli_command.append('--upload')
            cli_command.append('--port')
            cli_command.append(settings.get_serial_port_flag())
            cli_command.append('--board')
            cli_command.append(settings.get_arduino_board_flag())
        elif settings.load_ide_option == 'verify':
            print('\nVerifying the sketch...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'arduinoOpVerifiedTitle'
            cli_command.append('--board')
            cli_command.append(settings.get_arduino_board_flag())
            cli_command.append('--verify')
        elif settings.load_ide_option == 'open':
            print('\nOpening the sketch in the Arduino IDE...')
            conclusion = 'arduinoOpOpenedTitle'
            out = 'arduinoOpOpenedBody'
        cli_command.append("%s" % sketch_path)
        print('CLI command: %s' % ' '.join(cli_command))
        # Python 2 needs the input to subprocess.Popen to be in system encoding
        if sys.version_info[0] < 3:
            for item in six_moves.range(len(cli_command)):
                cli_command[item] = cli_command[item].encode(
                    locale.getpreferredencoding())

        if settings.load_ide_option == 'open':
            # Open IDE in a subprocess without capturing outputs
            subprocess.Popen(cli_command, shell=False)
            # Wait a few seconds to allow IDE to open before sending back data
            time.sleep(5)
        else:
            # Launch the Arduino CLI in a subprocess and capture output data
            process = subprocess.Popen(cli_command,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       shell=False)
            out, error = process.communicate()
            out = six.u(out)
            error = six.u(error)
            exit_code = process.returncode
            print('Arduino output:\n%s' % out)
            print('Arduino Error output:\n%s' % error)
            print('Arduino Exit code: %s' % exit_code)
            # For some reason Arduino CLI can return 256 on success
            if (process.returncode != 0) and (process.returncode != 256):
                success = False
                if exit_code == 1:
                    conclusion = 'arduinoOpErrorUpVerTitle'
                elif exit_code == 2:
                    conclusion = 'arduinoOpErrorSketchTitle'
                elif exit_code == 3:
                    conclusion = 'arduinoOpErrorFlagTitle'
                elif exit_code == 4:
                    conclusion = 'arduinoOpErrorFlagPrefTitle'
                else:
                    conclusion = 'Unexpected exit error code: %s' % exit_code

    return success, conclusion, out, error, exit_code
Example #5
0
def load_arduino_cli(sketch_path=None):
    """
    Launches a subprocess to invoke the Arduino IDE command line to open,
    verify or upload an sketch, the location of which is indicated in the input
    parameter.
    :param sketch_path: Path to the sketch to load into the Arduino IDE.
    :return: A tuple with the following data (success, conclusion, out, error,
            exit_code)
    """
    success = True
    conclusion = error = out = exit_code = ''

    # Input sanitation and output defaults
    if not sketch_path:
        sketch_path = create_sketch_default()
    else:
        if not os.path.isfile(sketch_path):
            conclusion = error = 'Provided sketch path is not a valid file: %s'\
                                 % sketch_path
            success = False
            return success, conclusion, out, error, exit_code

    settings = ServerCompilerSettings()

    # Check if CLI flags have been set
    if not settings.compiler_dir:
        success = False
        conclusion = 'arduinoOpErrorIdeDirTitle'
        error = 'arduinoOpErrorIdeDirBody'
    else:
        if not settings.load_ide_option:
            success = False
            conclusion = 'arduinoOpErrorIdeOptionTitle'
            error = 'arduinoOpErrorIdeOptionBody'
        elif settings.load_ide_option == 'upload':
            if not settings.get_serial_port_flag():
                success = False
                conclusion = 'arduinoOpErrorIdePortTitle'
                error = 'arduinoOpErrorIdePortBody'
            if not settings.get_arduino_board_flag():
                success = False
                conclusion = 'arduinoOpErrorIdeBoardTitle'
                error = 'arduinoOpErrorIdeBoardBody'

    if success:
        # Concatenates the CLI command and execute if the flags are valid
        cli_command = [settings.compiler_dir]

        # 設定 Arduino sketchbook 與 preferences.txt 路徑
        sketch_full_dir = os.path.join(
            ServerCompilerSettings().sketch_dir,
            ServerCompilerSettings().sketch_name)
        pref_filename = os.path.join(sketch_full_dir, 'preferences.txt')
        cli_command.append('--preferences-file')
        cli_command.append(pref_filename)
        cli_command.append('--save-prefs')
        if settings.load_ide_option == 'open':
            # cli_command.append('--preferences-file')
            # cli_command.append(pref_filename)

            # 動態修改 preferences.txt 內的 COM Port
            pref = read_properties(pref_filename)
            pref['serial.port'] = settings.get_serial_port_flag()
            pref['serial.port.file'] = settings.get_serial_port_flag()
            write_properties(pref_filename, pref)
        else:
            # cli_command.append('--verbose-upload')
            cli_command.append('--pref')
            cli_command.append('update.check=false')
            cli_command.append('--pref')
            cli_command.append('sketchbook.path=' + sketch_full_dir)

        if settings.load_ide_option == 'upload':
            print('\nUploading sketch to Arduino...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'arduinoOpUploadedTitle'
            cli_command.append('--upload')
            cli_command.append('--port')
            cli_command.append(settings.get_serial_port_flag())
            cli_command.append('--board')
            cli_command.append(settings.get_arduino_board_flag())
        elif settings.load_ide_option == 'verify':
            print('\nVerifying the sketch...')
            # This success conclusion message gets overwritten in case of error
            conclusion = 'arduinoOpVerifiedTitle'
            cli_command.append('--verify')
        elif settings.load_ide_option == 'open':
            print('\nOpening the sketch in the Arduino IDE...')
            conclusion = 'arduinoOpOpenedTitle'
            out = 'arduinoOpOpenedBody'
        cli_command.append("%s" % sketch_path)
        print('CLI command: %s' % ' '.join(cli_command))
        # Python 2 needs the input to subprocess.Popen to be in system encoding
        if sys.version_info[0] < 3:
            for item in six_moves.range(len(cli_command)):
                cli_command[item] = cli_command[item].encode(
                    locale.getpreferredencoding())

        if settings.load_ide_option == 'open':
            # Open IDE in a subprocess without capturing outputs
            subprocess.Popen(cli_command, shell=False)
            # Wait a few seconds to allow IDE to open before sending back data
            time.sleep(5)
        else:
            # Launch the Arduino CLI in a subprocess and capture output data
            process = subprocess.Popen(
                cli_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                shell=False)
            out, error = process.communicate()
            try:
                # 接收中文訊息
                print("processing message....")
                # out = out.decode(sys.stdin.encoding)
                out = out.decode('UTF-8')
                print("out OK.")
                # error = error.decode(sys.stdin.encoding)
                error = error.decode('UTF-8')
                print("error OK.")

                # returncode 0 : success
                #            1 : error 
                exit_code = process.returncode
                print('Arduino output:\n%s' % out)
                print('Arduino Error output:\n%s' % error)
                print('Arduino Exit code: %s' % exit_code)
            except Exception as err:
                print("Unexpected error after "
                    "capturing output of Arduino CLI: \n%s\n" % err)
            # For some reason Arduino CLI can return 256 on success
            if (process.returncode != 0) and (process.returncode != 256):
                success = False
                if exit_code == 1:
                    conclusion = 'arduinoOpErrorUpVerTitle'
                elif exit_code == 2:
                    conclusion = 'arduinoOpErrorSketchTitle'
                elif exit_code == 3:
                    conclusion = 'arduinoOpErrorFlagTitle'
                elif exit_code == 4:
                    conclusion = 'arduinoOpErrorFlagPrefTitle'
                else:
                    conclusion = 'Unexpected exit error code: %s' % exit_code

    return success, conclusion, out, error, exit_code