OSXRemovableDrivePlugin.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 dev in self._findInTree(plist, "Mass Storage Device"):
  17. if "removable_media" in dev and dev["removable_media"] == "yes" and "volumes" in dev and len(dev["volumes"]) > 0:
  18. for vol in dev["volumes"]:
  19. if "mount_point" in vol:
  20. volume = vol["mount_point"]
  21. drives[volume] = os.path.basename(volume)
  22. p = subprocess.Popen(["system_profiler", "SPCardReaderDataType", "-xml"], stdout=subprocess.PIPE)
  23. plist = plistlib.loads(p.communicate()[0])
  24. p.wait()
  25. for entry in plist:
  26. if "_items" in entry:
  27. for item in entry["_items"]:
  28. for dev in item["_items"]:
  29. if "removable_media" in dev and dev["removable_media"] == "yes" and "volumes" in dev and len(dev["volumes"]) > 0:
  30. for vol in dev["volumes"]:
  31. if "mount_point" in vol:
  32. volume = vol["mount_point"]
  33. drives[volume] = os.path.basename(volume)
  34. return drives
  35. def performEjectDevice(self, device):
  36. p = subprocess.Popen(["diskutil", "eject", device.getId()], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  37. output = p.communicate()
  38. Logger.log("d", "umount returned: %s.", repr(output))
  39. return_code = p.wait()
  40. if return_code != 0:
  41. return False
  42. else:
  43. return True
  44. def _findInTree(self, t, n):
  45. ret = []
  46. if type(t) is dict:
  47. if "_name" in t and t["_name"] == n:
  48. ret.append(t)
  49. for k, v in t.items():
  50. ret += self._findInTree(v, n)
  51. if type(t) is list:
  52. for v in t:
  53. ret += self._findInTree(v, n)
  54. return ret