예제 #1
0
def flashF(folder):
    print("MicroBit is at: " + microfs.find_microbit()[0] +
          "\nMicroBit directory is at: " + uflash.find_microbit())
    try:
        mfiles = microfs.ls()
    except OSError as e:
        print(
            str(e) +
            "\nMicrobit is probably calibrating, calibrate and then try again\nIf it still does not work try to "
            "replug your microbit or close other programs that is accessing your microbit"
        )
        return "Could not write"
    print("Removing old stuff: " + str(mfiles))
    for file in mfiles:
        microfs.rm(file)

    files = os.listdir(folder)
    print("Flashing new stuff: " + str(files))
    for file in files:
        microfs.put(folder + "\\" + file)

    print("Flashed new stuff: " + str(microfs.ls()) + "\n")

    time.sleep(0.1)
    print("Done!" + "\n" + "Don't forget to name your main file \"main.py\"" +
          "\n" + InternalTools.bcolors.BOLD +
          "Reset your MicroBit to apply changes!")
예제 #2
0
def test_put_with_error():
    """
    Ensure an IOError is raised if stderr returns something.
    """
    with mock.patch('microfs.execute', return_value=(b'', b'error')):
        with pytest.raises(IOError) as ex:
            microfs.put('tests/fixture_file.txt')
    assert ex.value.args[0] == 'error'
예제 #3
0
def test_put_non_existent_file():
    """
    Raise an IOError if put attempts to work with a non-existent file on the
    local file system.
    """
    with pytest.raises(IOError) as ex:
        microfs.put('tests/foo.txt')
    assert ex.value.args[0] == 'No such file.'
예제 #4
0
def test_put_with_error():
    """
    Ensure an IOError is raised if stderr returns something.
    """
    mock_serial = mock.MagicMock()
    with mock.patch("microfs.execute", return_value=(b"", b"error")):
        with pytest.raises(IOError) as ex:
            microfs.put("tests/fixture_file.txt", mock_serial)
    assert ex.value.args[0] == "error"
예제 #5
0
def test_put_non_existent_file():
    """
    Raise an IOError if put attempts to work with a non-existent file on the
    local file system.
    """
    mock_serial = mock.MagicMock()
    with pytest.raises(IOError) as ex:
        microfs.put("tests/foo.txt", mock_serial)
    assert ex.value.args[0] == "No such file."
예제 #6
0
def flash():
    """ flash files to micro:bit """
    # Copy additional scripts
    if len(ADDONS) > 0:
        print('Copying additional scripts')
        for name in ADDONS:
            print(' ', name)
            microfs.put(name)
    print('Copying main script: ', MAIN)
    microfs.put(MAIN, 'main.py')
예제 #7
0
def test_put():
    """
    Ensure a put of an existing file results in the expected calls to the
    micro:bit and returns True.
    """
    path = 'tests/fixture_file.txt'
    with open(path, 'r') as fixture_file:
        content = fixture_file.read()
        with mock.patch('microfs.execute', return_value=(b'', b'')) as execute:
            assert microfs.put(path)
            command = "with open('{}', 'w') as f:\n    f.write('''{}''')"
            expected = command.format('fixture_file.txt', content)
            execute.assert_called_once_with(expected)
예제 #8
0
def test_put_no_target_python2():
    """
    Ensure a put of an existing file results in the expected calls to the
    micro:bit and returns True when running on Python 2.
    """
    path = 'tests/fixture_file.txt'
    mock_serial = mock.MagicMock()
    with open(path, 'r') as fixture_file:
        content = fixture_file.read()
        with mock.patch('microfs.execute', return_value=(b'', b'')) as execute:
            with mock.patch('microfs.PY2', True):
                with mock.patch.object(builtins,
                                       'repr',
                                       return_value="'{}'".format(content)):
                    assert microfs.put(path, None, mock_serial)
            commands = [
                "fd = open('{}', 'wb')".format('fixture_file.txt'),
                "f = fd.write",
                "f(b'{}')".format(content),
                "fd.close()",
            ]
            execute.assert_called_once_with(commands, mock_serial)
예제 #9
0
def test_put_no_target_python3():
    """
    Ensure a put of an existing file results in the expected calls to the
    micro:bit and returns True.
    """
    path = "tests/fixture_file.txt"
    mock_serial = mock.MagicMock()
    with open(path, "r") as fixture_file:
        content = fixture_file.read()
        with mock.patch("microfs.execute", return_value=(b"", b"")) as execute:
            with mock.patch("microfs.PY2", False):
                with mock.patch.object(builtins,
                                       "repr",
                                       return_value="b'{}'".format(content)):
                    assert microfs.put(path, None, mock_serial)
            commands = [
                "fd = open('{}', 'wb')".format("fixture_file.txt"),
                "f = fd.write",
                "f(b'{}')".format(content),
                "fd.close()",
            ]
            execute.assert_called_once_with(commands, mock_serial)
예제 #10
0
import os, microfs, sys, tempfile

code_dir = os.path.join(tempfile.gettempdir(), 'main.py')
print('Flashing current code...', end='')
sys.stdout.flush()
try:
    microfs.put(code_dir)
except Exception as e:
    sys.stderr.write('\n%s: %s' % (type(e).__name__, str(e)))
    sys.exit(1)
print('Done.')