eric6/Utilities/__init__.py

branch
micropython
changeset 7098
fbdf76af88ed
parent 6942
2602857055c5
child 7186
ccd55666e9d2
equal deleted inserted replaced
7097:f8c503462aac 7098:fbdf76af88ed
30 import codecs 30 import codecs
31 import re 31 import re
32 import fnmatch 32 import fnmatch
33 import glob 33 import glob
34 import getpass 34 import getpass
35 import ctypes
36 import subprocess
35 37
36 38
37 def __showwarning(message, category, filename, lineno, file=None, line=""): 39 def __showwarning(message, category, filename, lineno, file=None, line=""):
38 """ 40 """
39 Module function to raise a SyntaxError for a SyntaxWarning. 41 Module function to raise a SyntaxError for a SyntaxWarning.
1273 for name in dirs[:]: 1275 for name in dirs[:]:
1274 if not os.path.islink(name): 1276 if not os.path.islink(name):
1275 dirs = dirs + getDirs(name, excludeDirs) 1277 dirs = dirs + getDirs(name, excludeDirs)
1276 1278
1277 return dirs 1279 return dirs
1280
1281
1282 def findVolume(volumeName):
1283 """
1284 Function to find the directory belonging to a given volume name.
1285
1286 @param volumeName name of the volume to search for
1287 @type str
1288 @return directory path of the given volume name
1289 @rtype str
1290 """
1291 volumeDirectory = None
1292
1293 if isWindowsPlatform():
1294 # we are on a Windows platform
1295 def getVolumeName(diskName):
1296 """
1297 Local function to determine the volume of a disk or device.
1298
1299 Each disk or external device connected to windows has an
1300 attribute called "volume name". This function returns the
1301 volume name for the given disk/device.
1302
1303 Code from http://stackoverflow.com/a/12056414
1304 """
1305 volumeNameBuffer = ctypes.create_unicode_buffer(1024)
1306 ctypes.windll.kernel32.GetVolumeInformationW(
1307 ctypes.c_wchar_p(diskName), volumeNameBuffer,
1308 ctypes.sizeof(volumeNameBuffer), None, None, None, None, 0)
1309 return volumeNameBuffer.value
1310
1311 #
1312 # In certain circumstances, volumes are allocated to USB
1313 # storage devices which cause a Windows popup to raise if their
1314 # volume contains no media. Wrapping the check in SetErrorMode
1315 # with SEM_FAILCRITICALERRORS (1) prevents this popup.
1316 #
1317 oldMode = ctypes.windll.kernel32.SetErrorMode(1)
1318 try:
1319 for disk in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
1320 dirpath = "{0}:\\".format(disk)
1321 if (os.path.exists(dirpath) and
1322 getVolumeName(dirpath) == volumeName):
1323 volumeDirectory = dirpath
1324 break
1325 finally:
1326 ctypes.windll.kernel32.SetErrorMode(oldMode)
1327 else:
1328 # we are on a Linux or macOS platform
1329 for mountCommand in ["mount", "/sbin/mount", "/usr/sbin/mount"]:
1330 try:
1331 mountOutput = (subprocess.check_output(mountCommand)
1332 .splitlines())
1333 mountedVolumes = [x.split()[2] for x in mountOutput]
1334 for volume in mountedVolumes:
1335 if volume.decode("utf-8").endswith(volumeName):
1336 volumeDirectory = volume.decode("utf-8")
1337 break
1338 if volumeDirectory:
1339 break
1340 except FileNotFoundError:
1341 pass
1342
1343 return volumeDirectory
1278 1344
1279 1345
1280 def getTestFileName(fn): 1346 def getTestFileName(fn):
1281 """ 1347 """
1282 Function to build the filename of a unittest file. 1348 Function to build the filename of a unittest file.

eric ide

mercurial