Mycroft Community Forum

Python behavior in a skill

I am working on a skill that requires me to mount and unmount a usb stick based on a plug/unplugged event. I am able to successfully detect the insertion and removal of the usb key but mounting is a whole other issue. When i attempt to mount the detected volume the mount is not successful indicating the device may be in use. If i execute the exact python code at the linux CLI the drive is detected and successfully mounts. I am attempting this with picroft (venv). I assume this is privilege related but i am at a loss to figure out how to achieve this. Any assistance is welcome.

https://docs.python.org/3/library/os.path.html#os.path.ismount might be of use?

Can you share a code snippet of what you’re doing?

I have this code running in a thread as part of the mycroft skill (getMountPathUSBDevice) is the function that will mount the drive

    def start_usb_thread(self, my_id, terminate):
        LOG.info("USB Monitoring Loop Started!")
        while not terminate():  # wait while this interval completes
            time.sleep(1)  # Todo make the polling time a variable or make it a separate thread
            # get the status of the connected usb device
            self.status = self.usbdevice.isDeviceConnected()
            # LOG.info("Checking USB Device: " + str(self.status))
            if self.status != self.prev_status:
                LOG.info("USB Status Changed!")
                self.prev_status = self.status
                if self.status:  #Device inserted
                    LOG.info("Device Inserted!")
                    device = self.usbdevice.getDevData()
                    # mount the device and get the path
                    self.path = self.usbdevice.getMountPathUsbDevice('mycroft')  #todo add sudo password to websettings
                    LOG.info("Stat: " + str(self.status))
                    LOG.info("dev: " + str(device))
                    LOG.info("path: " + str(self.path))
                    LOG.info("---------------------------------")
                    self.speak_dialog('update.library', expect_response=False)
                    self.song_list = self.create_library(self.path)
                    #LOG.info(str(self.song_list))
                else:
                    # unmount the path
                    self.usbdevice.uMountPathUsbDevice('mycroft')  #todo add sudo password to websettings
                    LOG.info("Device Removed!")
                    self.speak_dialog('usb.removed', expect_response=False)
                    self.song_list = []
                    self.path = ""
        self.usbdevice.stopListener(self.observer)

The code that does the mounting is here

# returns the accesible path of the device on the Raspberry pi
# you can change how the path gets calulated.
def getMountPathUsbDevice(password):
    sudoPassword = password #'mycroft'
    global USBDEV_DEVPATH
    if not isDeviceConnected() or USBDEV_DEVPATH == None:
        return None
    # check if the dev path exists
    if os.path.exists(USBDEV_DEVPATH):
        # create a mount directory
        if not os.path.exists('usb-music'):
            os.makedirs('usb-music')
            while not os.path.exists('usb-music'):
                time.sleep(1)
        #command = "sudo mount -t auto " + USBDEV_DEVPATH + " " + os.getcwd() + '/usb-music'
        command = "sudo mount -t auto /dev/sdb1 /home/pi/mycroft-core/usb-music"
        p = os.system(command)
        # return the path to the folder from root
        truePath = os.getcwd() + '/usb-music'
        return truePath
    return None

This code does work if I execute it as stand alone python code at the CL after exiting the mycroft-cli-interface

Thanks