OSXRemovableDrivePlugin.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Copyright (c) 2013 David Braam
  3. # Uranium is released under the terms of the AGPLv3 or higher.
  4. from . import RemovableDrivePlugin
  5. from UM.Logger import Logger
  6. import subprocess
  7. import os
  8. import plistlib
  9. ## Support for removable devices on Mac OSX
  10. class OSXRemovableDrivePlugin(RemovableDrivePlugin.RemovableDrivePlugin):
  11. def checkRemovableDrives(self):
  12. drives = {}
  13. p = subprocess.Popen(["system_profiler", "SPUSBDataType", "-xml"], stdout = subprocess.PIPE)
  14. plist = plistlib.loads(p.communicate()[0])
  15. p.wait()
  16. for entry in plist:
  17. if "_items" in entry:
  18. for item in entry["_items"]:
  19. for dev in item["_items"]:
  20. if "removable_media" in dev and dev["removable_media"] == "yes" and "volumes" in dev and len(dev["volumes"]) > 0:
  21. for vol in dev["volumes"]:
  22. if "mount_point" in vol:
  23. volume = vol["mount_point"]
  24. drives[volume] = os.path.basename(volume)
  25. p = subprocess.Popen(["system_profiler", "SPCardReaderDataType", "-xml"], stdout=subprocess.PIPE)
  26. plist = plistlib.loads(p.communicate()[0])
  27. p.wait()
  28. for entry in plist:
  29. if "_items" in entry:
  30. for item in entry["_items"]:
  31. for dev in item["_items"]:
  32. if "removable_media" in dev and dev["removable_media"] == "yes" and "volumes" in dev and len(dev["volumes"]) > 0:
  33. for vol in dev["volumes"]:
  34. if "mount_point" in vol:
  35. volume = vol["mount_point"]
  36. drives[volume] = os.path.basename(volume)
  37. return drives
  38. def performEjectDevice(self, device):
  39. p = subprocess.Popen(["diskutil", "eject", device.getId()], stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  40. output = p.communicate()
  41. Logger.log("d", "umount returned: %s.", repr(output))
  42. return_code = p.wait()
  43. if return_code != 0:
  44. return False
  45. else:
  46. return True