From 970d2ae77f7988e4af0dc604ff6444306f5d9be4 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 18:08:22 -0700 Subject: Update plugin_pmount.py Currently the pmount plugin uses static device letters and partition numbers. this change uses present devices and partitions. - Changes the binds to remove the need for uppercase device letters, if a device has no partitions mounts/unmounts the device. - With only one partition the same binds will handle it instead of the whole device. - With greater than one partitions, map for each. This is still not fully functional, devices not present when ranger is started *(usb drives)* won't have a map created unless ranger is restarted :| ... Is there a simple way to have ranger 'reload' this plugin, or use a different hook, etc.? --- examples/plugin_pmount.py | 67 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index 38c4a17c..feb54c83 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -3,38 +3,65 @@ # This plugin creates a bunch of keybindings used to mount and unmount # the devices using pmount(1). # -# alt+m : mount /dev/sd -# alt+m : mount /dev/sd -# alt+shift+m : unmount /dev/sd -# alt+shift+m : unmount /dev/sd -# alt+shift+n : list the devices +# (multiple partitions): alt+m : mount /dev/sd +# (one partition): alt+m : mount /dev/sd1 +# (no partitions): alt+m : mount /dev/sd +# +# (multiple partitions): alt+M : unmount /dev/sd +# (one partition): alt+M : unmount /dev/sd1 +# (no partitions): alt+M : unmount /dev/sd +# +# alt+n : list the devices from __future__ import (absolute_import, division, print_function) import ranger.api +import subprocess MOUNT_KEY = 'm' UMOUNT_KEY = 'M' -LIST_MOUNTS_KEY = 'N' - - +LIST_MOUNTS_KEY = 'n' HOOK_INIT_OLD = ranger.api.hook_init - def hook_init(fm): fm.execute_console("map {key} shell -p lsblk".format(key=LIST_MOUNTS_KEY)) - for disk in "abcdefgh": - fm.execute_console("map {key}{0} chain shell pmount sd{1}; cd /media/sd{1}".format( - disk.upper(), disk, key=MOUNT_KEY)) - fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{1}".format( - disk.upper(), disk, key=UMOUNT_KEY)) - for part in "123456789": + + diskcmd = "lsblk -lno NAME | grep -w 'sd[a-z]' | sed 's/sd//'" + disks = subprocess.check_output(diskcmd, + shell=True).decode('utf-8').replace('\r', '').replace('\n', '') + + for disk in disks: + partcmd = "lsblk -lno NAME /dev/sd{0} | sed 's/sd{0}//' | tail -n 1".format(disk) + + try: + numparts = int(subprocess.check_output(partcmd, + shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) + except ValueError: + numparts = 0 + + if numparts == 0: + # no partition, mount the whole device + fm.execute_console("map {key}{0} chain shell pmount sd{0}; cd /media/sd{0}".format( + disk, key=MOUNT_KEY)) + fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{0}".format( + disk, key=UMOUNT_KEY)) + + elif numparts == 1: + # only one partition, mount the partition fm.execute_console( - "map {key}{0}{1} chain shell pmount sd{0}{1}; cd /media/sd{0}{1}".format( - disk, part, key=MOUNT_KEY) - ) - fm.execute_console("map {key}{0}{1} chain cd; shell pumount sd{0}{1}".format( - disk, part, key=UMOUNT_KEY)) + "map {key}{0} chain shell pmount sd{0}1; cd /media/sd{0}1".format( + disk, key=MOUNT_KEY)) + fm.execute_console("map {key}{0} chain cd; shell pumount sd{0}1".format( + disk, key=UMOUNT_KEY)) + + else: + # use range start 1, /dev/sd{device}0 doesn't exist + for part in range(1, numparts + 1): + fm.execute_console( + "map {key}{0}{1} chain shell pmount sd{0}{1}; cd /media/sd{0}{1}".format( + disk, part, key=MOUNT_KEY)) + fm.execute_console("map {key}{0}{1} chain cd; shell pumount sd{0}{1}".format( + disk, part, key=UMOUNT_KEY)) return HOOK_INIT_OLD(fm) -- cgit 1.4.1-2-gfad0