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!")
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'
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.'
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"
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."
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')
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)
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)
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)
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.')