src/eric7/MicroPython/Devices/DeviceBase.py

branch
mpy_network
changeset 9979
dbafba79461d
parent 9957
0457d754fc9a
child 9989
286c2a21f36f
--- a/src/eric7/MicroPython/Devices/DeviceBase.py	Sat Apr 15 11:12:30 2023 +0200
+++ b/src/eric7/MicroPython/Devices/DeviceBase.py	Sat Apr 15 18:22:09 2023 +0200
@@ -59,6 +59,9 @@
     <li>getTime: get the current time</li>
     <li>showTime: show the current time of the connected device</li>
     <li>syncTime: synchronize the time of the connected device</li>
+    <li>mipInstall: install a MicroPython package with 'mip'</li>
+    <li>upipInstall: install a MicroPython package with 'upip'</li>
+    <li>getLibPaths: get a list of library paths contained in sys.path</li>
     </ul>
 
     Supported WiFi commands are:
@@ -136,6 +139,9 @@
         if connected:
             with contextlib.suppress(OSError):
                 self._deviceData = self.__getDeviceData()
+                self._deviceData["local_mip"] = (
+                    not self._deviceData["mip"] and not self._deviceData["upip"]
+                )
                 self._deviceData["wifi"], self._deviceData["wifi_type"] = self.hasWifi()
                 self._deviceData["bluetooth"] = self.hasBluetooth()
                 (
@@ -972,6 +978,27 @@
 
         return tuple(filesystemInfos)
 
+    def ensurePath(self, target):
+        """
+        Public method to ensure, that the given target path exists.
+
+        @param target target directory
+        @type str
+        """
+        pathParts = target.split("/")
+
+        # handle targets starting with "/"
+        if not pathParts[0]:
+            pathParts.pop(0)
+            pathParts[0] = "/" + pathParts[0]
+
+        directory = ""
+        for index in range(len(pathParts)):
+            directory += pathParts[index]
+            if not self.exists(directory):
+                self.mkdir(directory)
+            directory += "/"
+
     ##################################################################
     ## board information related methods below
     ##################################################################
@@ -1018,6 +1045,13 @@
     except AttributeError:
         res['mpy_version'] = 'unknown'
 
+    if hasattr(sys.implementation, '_mpy'):
+        res['mpy_file_version'] = sys.implementation._mpy & 0xff
+    elif hasattr(sys.implementation, 'mpy'):
+        res['mpy_file_version'] = sys.implementation.mpy & 0xff
+    else:
+        res['mpy_file_version'] = 0
+
     try:
         import pimoroni
         res['mpy_variant'] = 'Pimoroni Pico'
@@ -1322,20 +1356,28 @@
         )
         return self._interface.execute(command, mode=self._submitMode, timeout=60000)
 
-    def mipInstall(self, package, version, mpy):
+    def mipInstall(self, package, index=None, target=None, version=None, mpy=True):
         """
         Public method to install packages using 'mip'.
 
         @param package package name
         @type str
-        @param version package version
-        @type str
-        @param mpy flag indicating to install as '.mpy' file
-        @type bool
+        @param index URL of the package index to be used (defaults to None)
+        @type str (optional)
+        @param target target directory on the device (defaults to None)
+        @type str (optional)
+        @param version package version (defaults to None)
+        @type str (optional)
+        @param mpy flag indicating to install as '.mpy' file (defaults to True)
+        @type bool (optional)
         @return tuple containing the command output and errors
         @return tuple of (str, str)
         """
         parameterStr = repr(package)
+        if index:
+            parameterStr += ", index={0}".format(repr(index))
+        if target:
+            parameterStr += ", target={0}".format(repr(target))
         if version:
             parameterStr += ", version={0}".format(repr(version))
         if not mpy:
@@ -1353,6 +1395,28 @@
         )
         return self._interface.execute(command, mode=self._submitMode, timeout=60000)
 
+    def getLibPaths(self):
+        """
+        Public method to get the list of library paths contained in 'sys.path'.
+
+        @return list of library paths
+        @rtype list of str
+        @exception OSError raised to indicate an issue with the device
+        """
+        command = """
+def lib_paths():
+    import sys
+    print([p for p in sys.path if p.endswith('/lib')])
+
+lib_paths()
+del lib_paths
+"""
+        out, err = self._interface.execute(command, mode=self._submitMode)
+        if err:
+            raise OSError(self._shortError(err))
+
+        return ast.literal_eval(out.decode("utf-8"))
+
     ##################################################################
     ## Methods below implement WiFi related methods
     ##################################################################

eric ide

mercurial