LinuxRemovableDrivePlugin.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Copyright (c) 2013 David Braam
  3. # Uranium is released under the terms of the LGPLv3 or higher.
  4. from . import RemovableDrivePlugin
  5. from UM.Logger import Logger
  6. import glob
  7. import os
  8. import subprocess
  9. class LinuxRemovableDrivePlugin(RemovableDrivePlugin.RemovableDrivePlugin):
  10. """Support for removable devices on Linux.
  11. TODO: This code uses the most basic interfaces for handling this.
  12. We should instead use UDisks2 to handle mount/unmount and hotplugging events.
  13. """
  14. def checkRemovableDrives(self):
  15. drives = {}
  16. for volume in glob.glob("/media/*"):
  17. if os.path.ismount(volume):
  18. drives[volume] = os.path.basename(volume)
  19. elif volume == "/media/"+os.getenv("USER"):
  20. for volume in glob.glob("/media/"+os.getenv("USER")+"/*"):
  21. if os.path.ismount(volume):
  22. drives[volume] = os.path.basename(volume)
  23. for volume in glob.glob("/run/media/" + os.getenv("USER") + "/*"):
  24. if os.path.ismount(volume):
  25. drives[volume] = os.path.basename(volume)
  26. return drives
  27. def performEjectDevice(self, device):
  28. p = subprocess.Popen(["umount", device.getId()], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  29. output = p.communicate()
  30. Logger.log("d", "umount returned: %s.", repr(output))
  31. return_code = p.wait()
  32. if return_code != 0:
  33. return False
  34. else:
  35. return True