LinuxRemovableDrivePlugin.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. ## Support for removable devices on Linux.
  10. #
  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. class LinuxRemovableDrivePlugin(RemovableDrivePlugin.RemovableDrivePlugin):
  15. def checkRemovableDrives(self):
  16. drives = {}
  17. for volume in glob.glob("/media/*"):
  18. if os.path.ismount(volume):
  19. drives[volume] = os.path.basename(volume)
  20. elif volume == "/media/"+os.getenv("USER"):
  21. for volume in glob.glob("/media/"+os.getenv("USER")+"/*"):
  22. if os.path.ismount(volume):
  23. drives[volume] = os.path.basename(volume)
  24. for volume in glob.glob("/run/media/" + os.getenv("USER") + "/*"):
  25. if os.path.ismount(volume):
  26. drives[volume] = os.path.basename(volume)
  27. return drives
  28. def performEjectDevice(self, device):
  29. p = subprocess.Popen(["umount", device.getId()], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  30. output = p.communicate()
  31. Logger.log("d", "umount returned: %s.", repr(output))
  32. return_code = p.wait()
  33. if return_code != 0:
  34. return False
  35. else:
  36. return True