Example #1
0
def test_extract_not_valid_hex():
    """
    Return a sensible message if the hex file isn't valid
    """
    with pytest.raises(ValueError) as e:
        uflash.extract_script('invalid input')
    assert 'Bad input hex file' in e.value.args[0]
Example #2
0
def test_extract_sandwiched():
    """
    The script hex is packed with additional data above and bellow and should
    still be returned as a the original string only.
    """
    python_hex_lines = TEST_SCRIPT_HEXLIFIED.split("\n")
    python_sandwiched = [
        python_hex_lines[0],
        ":10DFE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF41",
        "\n".join(python_hex_lines[1:]),
        ":10E50000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1B",
        "",
    ]
    v1_hex_sandiwtched_data = (
        "020000040000FA\n"
        + ":1000000000400020218E01005D8E01005F8E010006\n"
        + "\n".join(python_sandwiched)
        + ":020000041000EA\n"
        + ":1010C0007CB0EE17FFFFFFFF0A0000000000E30006\n"
        + ":0C10D000FFFFFFFF2D6D0300000000007B\n"
        + ":0400000500018E2147\n"
        + ":00000001FF\n"
    )
    extracted = uflash.extract_script(v1_hex_sandiwtched_data)
    assert extracted == TEST_SCRIPT.decode("utf-8")
Example #3
0
def test_extract():
    """
    The script should be returned as a string (if there is one).
    """
    python = uflash.hexlify(TEST_SCRIPT)
    result = uflash.embed_hex(uflash._RUNTIME, python)
    extracted = uflash.extract_script(result)
    assert extracted == TEST_SCRIPT.decode('utf-8')
Example #4
0
def test_extract():
    """
    The script should be returned as a string (if there is one).
    """
    python = uflash.hexlify(TEST_SCRIPT)
    result = uflash.embed_hex(uflash._RUNTIME, python)
    extracted = uflash.extract_script(result)
    assert extracted == TEST_SCRIPT.decode('utf-8')
Example #5
0
def test_extract_sandwiched():
    """
    The script hex is packed with additional data above and bellow and should
    still be returned as a the original string only.
    """
    python = uflash.hexlify(TEST_SCRIPT)
    python_hex_lines = python.split('\n')
    python_sandwiched = [python_hex_lines[0]] + \
        [':10DFE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF41'] + \
        python_hex_lines[1:] + [':10E50000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1B']
    result = uflash.embed_hex(uflash._RUNTIME, '\n'.join(python_sandwiched))
    extracted = uflash.extract_script(result)
    assert extracted == TEST_SCRIPT.decode('utf-8')
Example #6
0
 def open_file(self, path):
     """
     Tries to open a MicroPython hex file with an embedded Python script.
     """
     text = None
     if path.lower().endswith('.hex'):
         # Try to open the hex and extract the Python script
         try:
             with open(path, newline='') as f:
                 text = uflash.extract_script(f.read())
         except Exception:
             return None
     return text
Example #7
0
def test_extract_no_python():
    """
    Ensure that if there's no Python in the input hex then just return an empty
    (False) string.
    """
    v1_hex_no_py_code = (
        "020000040000FA\n"
        + ":1000000000400020218E01005D8E01005F8E010006\n"
        + ":020000041000EA\n"
        + ":1010C0007CB0EE17FFFFFFFF0A0000000000E30006\n"
        + ":0C10D000FFFFFFFF2D6D0300000000007B\n"
        + ":0400000500018E2147\n"
        + ":00000001FF\n"
    )
    assert uflash.extract_script(v1_hex_no_py_code) == ""
Example #8
0
def test_extract():
    """
    The script should be returned as a string (if there is one).
    """
    v1_hex = (
        "020000040000FA\n"
        + ":1000000000400020218E01005D8E01005F8E010006\n"
        + TEST_SCRIPT_HEXLIFIED
        + "\n"
        + ":020000041000EA\n"
        + ":1010C0007CB0EE17FFFFFFFF0A0000000000E30006\n"
        + ":0C10D000FFFFFFFF2D6D0300000000007B\n"
        + ":0400000500018E2147\n"
        + ":00000001FF\n"
    )
    extracted = uflash.extract_script(v1_hex)
    assert extracted == TEST_SCRIPT.decode("utf-8")
Example #9
0
def read_python_code():
    """Read the MicroPython user code from the micro:bit flash.

    :return: String with the MicroPython code.
    """
    flash_data = programmer.read_flash(
        address=programmer.PYTHON_CODE_START,
        count=(programmer.PYTHON_CODE_END - programmer.PYTHON_CODE_START),
    )
    py_code_hex = _bytes_to_intel_hex(flash_data,
                                      offset=programmer.PYTHON_CODE_START)
    try:
        python_code = extract_script(py_code_hex)
    except Exception as e:
        sys.stderr.write(format_exc(e))
        raise Exception("Could not decode the MicroPython code from flash")
    return python_code
Example #10
0
def test_extract_no_python():
    """
    Ensure that if there's no Python in the input hex then just return an empty
    (False) string.
    """
    assert uflash.extract_script(uflash._RUNTIME) == ''
Example #11
0
def test_extract_not_valid_hex():
    """
    Return a sensible message if the hex file isn't valid
    """
    assert uflash.extract_script('invalid input') == ''
Example #12
0
def test_extract_no_python():
    """
    Ensure that if there's no Python in the input hex then just return an empty
    (False) string.
    """
    assert uflash.extract_script(uflash._RUNTIME) == ''
Example #13
0
def test_extract_not_valid_hex():
    """
    Return a sensible message if the hex file isn't valid
    """
    assert uflash.extract_script('invalid input') == ''
Example #14
0
def test_extract_no_python():
    """
    What to do here?
    """
    assert uflash.extract_script(uflash._RUNTIME) == ''