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. 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):"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()
            #"Checking USB Device: " + str(self.status))
            if self.status != self.prev_status:
      "USB Status Changed!")
                self.prev_status = self.status
                if self.status:  #Device inserted
          "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
          "Stat: " + str(self.status))
          "dev: " + str(device))
          "path: " + str(self.path))
                    self.speak_dialog('update.library', expect_response=False)
                    self.song_list = self.create_library(self.path)
                    # unmount the path
                    self.usbdevice.uMountPathUsbDevice('mycroft')  #todo add sudo password to websettings
          "Device Removed!")
                    self.speak_dialog('usb.removed', expect_response=False)
                    self.song_list = []
                    self.path = ""

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'
    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'):
            while not os.path.exists('usb-music'):
        #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