eric6/Utilities/__init__.py

changeset 8062
8dc5acb30a8b
parent 7960
e8fc383322f7
child 8067
a467ab075be0
--- a/eric6/Utilities/__init__.py	Wed Feb 03 19:14:35 2021 +0100
+++ b/eric6/Utilities/__init__.py	Wed Feb 03 19:15:58 2021 +0100
@@ -1238,15 +1238,22 @@
     return dirs
 
 
-def findVolume(volumeName):
+def findVolume(volumeName, all=False):
     """
     Function to find the directory belonging to a given volume name.
     
     @param volumeName name of the volume to search for
     @type str
-    @return directory path of the given volume name
-    @rtype str
+    @param all flag indicating to get the directories for all volumes
+        starting with the given name (defaults to False)
+    @type bool (optional)
+    @return directory path or list of directory paths for the given volume
+        name
+    @rtype str or list of str
     """
+    # TODO: add option to get all (i.e. all starting with volumeName)
+    #       and return a list of found directories
+    volumeDirectories = []
     volumeDirectory = None
     
     if isWindowsPlatform():
@@ -1277,10 +1284,14 @@
         try:
             for disk in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
                 dirpath = "{0}:\\".format(disk)
-                if (os.path.exists(dirpath) and
-                        getVolumeName(dirpath) == volumeName):
-                    volumeDirectory = dirpath
-                    break
+                if os.path.exists(dirpath):
+                    if all:
+                        if getVolumeName(dirpath).startswith(volumeName):
+                            volumeDirectories.append(dirpath)
+                    else:
+                        if getVolumeName(dirpath) == volumeName:
+                            volumeDirectory = dirpath
+                            break
         finally:
             ctypes.windll.kernel32.SetErrorMode(oldMode)
     else:
@@ -1291,16 +1302,26 @@
                     subprocess.check_output(mountCommand).splitlines()  # secok
                 )
                 mountedVolumes = [x.split()[2] for x in mountOutput]
-                for volume in mountedVolumes:
-                    if volume.decode("utf-8").endswith(volumeName):
-                        volumeDirectory = volume.decode("utf-8")
+                if all:
+                    for volume in mountedVolumes:
+                        if volumeName in volume.decode("utf-8"):
+                            volumeDirectories.append(volume.decode("utf-8"))
+                    if volumeDirectories:
                         break
-                if volumeDirectory:
-                    break
+                else:
+                    for volume in mountedVolumes:
+                        if volume.decode("utf-8").endswith(volumeName):
+                            volumeDirectory = volume.decode("utf-8")
+                            break
+                    if volumeDirectory:
+                        break
             except FileNotFoundError:
                 pass
     
-    return volumeDirectory
+    if all:
+        return volumeDirectories
+    else:
+        return volumeDirectory
 
 
 def getTestFileName(fn):

eric ide

mercurial