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. import threading
  6. import subprocess
  7. import time
  8. import os
  9. import plistlib
  10. ## Support for removable devices on Mac OSX
  11. class OSXRemovableDrivePlugin(RemovableDrivePlugin.RemovableDrivePlugin):
  12. def checkRemovableDrives(self):
  13. drives = {}
  14. p = subprocess.Popen(["system_profiler", "SPUSBDataType", "-xml"], stdout=subprocess.PIPE)
  15. plist = plistlib.loads(p.communicate()[0])
  16. p.wait()
  17. for dev in self._findInTree(plist, "Mass Storage Device"):
  18. if "removable_media" in dev and dev["removable_media"] == "yes" and "volumes" in dev and len(dev["volumes"]) > 0:
  19. for vol in dev["volumes"]:
  20. if "mount_point" in vol:
  21. volume = vol["mount_point"]
  22. drives[volume] = os.path.basename(volume)
  23. p = subprocess.Popen(["system_profiler", "SPCardReaderDataType", "-xml"], stdout=subprocess.PIPE)
  24. plist = plistlib.loads(p.communicate()[0])
  25. p.wait()
  26. for entry in plist:
  27. if "_items" in entry:
  28. for item in entry["_items"]:
  29. for dev in item["_items"]:
  30. if "removable_media" in dev and dev["removable_media"] == "yes" and "volumes" in dev and len(dev["volumes"]) > 0:
  31. for vol in dev["volumes"]:
  32. if "mount_point" in vol:
  33. volume = vol["mount_point"]
  34. drives[volume] = os.path.basename(volume)
  35. return drives
  36. def performEjectDevice(self, device):
  37. p = subprocess.Popen(["diskutil", "eject", path], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  38. output = p.communicate()
  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