src/eric7/MicroPython/Devices/RP2040Devices.py

branch
mpy_network
changeset 10022
a95800b414b7
parent 9989
286c2a21f36f
child 10069
435cc5875135
diff -r a71f50b3a503 -r a95800b414b7 src/eric7/MicroPython/Devices/RP2040Devices.py
--- a/src/eric7/MicroPython/Devices/RP2040Devices.py	Sat May 06 16:22:17 2023 +0200
+++ b/src/eric7/MicroPython/Devices/RP2040Devices.py	Sat May 06 19:21:40 2023 +0200
@@ -1172,6 +1172,99 @@
         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
+"""
+
+        if self._deviceData["wifi_type"] == "picow":
+            config = "PASS = {0}\n".format(repr(password))
+        else:
+            return False, self.tr("WebREPL is not supported on this device.")
+
+        try:
+            # write config file
+            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 Ethernet related methods
     ##################################################################

eric ide

mercurial