LinuxRemovableDrivePlugin.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 glob
  6. import os
  7. import subprocess
  8. ## Support for removable devices on Linux.
  9. #
  10. # TODO: This code uses the most basic interfaces for handling this.
  11. # We should instead use UDisks2 to handle mount/unmount and hotplugging events.
  12. #
  13. class LinuxRemovableDrivePlugin(RemovableDrivePlugin.RemovableDrivePlugin):
  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. return_code = p.wait()
  31. if return_code != 0:
  32. return False
  33. else:
  34. return True