OSXRemovableDrivePlugin.py 2.5 KB

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