diff -r a71f50b3a503 -r a95800b414b7 src/eric7/MicroPython/Devices/EspDevices.py --- a/src/eric7/MicroPython/Devices/EspDevices.py Sat May 06 16:22:17 2023 +0200 +++ b/src/eric7/MicroPython/Devices/EspDevices.py Sat May 06 19:21:40 2023 +0200 @@ -1116,6 +1116,95 @@ clientsList = ast.literal_eval(out.decode("utf-8")) return clientsList, "" + def enableWebrepl(self, password): + """ + Public method to write the given WebREPL password to the connected device and + modify the start script to start the WebREPL server. + + @param password password needed to authenticate + @type str + @return tuple containing a flag indicating success and an error message + @rtype tuple of (bool, str) + """ + command = """ +def modify_boot(): + import os + + try: + with open('/boot.py', 'r') as old_f, open('/boot.py.tmp', 'w') as new_f: + found = False + for l in old_f.read().splitlines(): + if 'webrepl' in l: + found = True + if l.startswith('#'): + l = l[1:] + new_f.write(l + '\\n') + if not found: + new_f.write('\\nimport webrepl\\nwebrepl.start()\\n') + + os.remove('/boot.py') + os.rename('/boot.py.tmp', '/boot.py') + except: + pass + + print(True) + +modify_boot() +del modify_boot +""" + + try: + # write config file + config = "PASS = {0}\n".format(repr(password)) + self.putData("/webrepl_cfg.py", config.encode("utf-8")) + except OSError as err: + return False, str(err) + + # modify boot.py + out, err = self.executeCommands(command, mode=self._submitMode) + if err: + return False, err + + return out.decode("utf-8").strip() == "True", "" + + def disableWebrepl(self): + """ + Public method to write the given WebREPL password to the connected device and + modify the start script to start the WebREPL server. + + @return tuple containing a flag indicating success and an error message + @rtype tuple of (bool, str) + """ + command = """ +def modify_boot(): + import os + + try: + with open('/boot.py', 'r') as old_f, open('/boot.py.tmp', 'w') as new_f: + for l in old_f.read().splitlines(): + if 'webrepl' in l: + if not l.startswith('#'): + l = '#' + l + new_f.write(l + '\\n') + + os.remove('/boot.py') + os.rename('/boot.py.tmp', '/boot.py') + except: + pass + + print(True) + +modify_boot() +del modify_boot +""" + + # modify boot.py + out, err = self.executeCommands(command, mode=self._submitMode) + if err: + return False, err + + return out.decode("utf-8").strip() == "True", "" + ################################################################## ## Methods below implement Bluetooth related methods ##################################################################