| 
646
 | 
     1 #!/usr/bin/env python
 | 
| 
 | 
     2 # -*- coding: utf-8 -*-
 | 
| 
 | 
     3 
 | 
| 
 | 
     4 """
 | 
| 
672
 | 
     5 mount inserted usb disk
 | 
| 
 | 
     6 
 | 
| 
646
 | 
     7 [   33.854905] usb-storage 1-1.2:1.0: USB Mass Storage device detected
 | 
| 
 | 
     8 [   33.854946] scsi6 : usb-storage 1-1.2:1.0
 | 
| 
 | 
     9 [   33.855002] usbcore: registered new interface driver usb-storage
 | 
| 
 | 
    10 [   34.855894] scsi 6:0:0:0: Direct-Access     PNY      USB 2.0 FD       8.07 PQ: 0 ANSI: 4
 | 
| 
 | 
    11 [   34.856108] sd 6:0:0:0: Attached scsi generic sg2 type 0
 | 
| 
 | 
    12 [   34.857281] sd 6:0:0:0: [sdb] 249999360 512-byte logical blocks: (127 GB/119 GiB)
 | 
| 
 | 
    13 [   34.858452] sd 6:0:0:0: [sdb] Write Protect is off
 | 
| 
 | 
    14 [   34.858455] sd 6:0:0:0: [sdb] Mode Sense: 23 00 00 00
 | 
| 
 | 
    15 [   34.859678] sd 6:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
 | 
| 
 | 
    16 [   40.782414]  sdb: sdb1
 | 
| 
 | 
    17 [   40.787010] sd 6:0:0:0: [sdb] Attached SCSI removable disk
 | 
| 
 | 
    18 
 | 
| 
 | 
    19 """
 | 
| 
 | 
    20 
 | 
| 
 | 
    21 import argparse
 | 
| 
 | 
    22 import os
 | 
| 
 | 
    23 import subprocess
 | 
| 
 | 
    24 import sys
 | 
| 
 | 
    25 
 | 
| 
 | 
    26 
 | 
| 
 | 
    27 def main(args=sys.argv[1:]):
 | 
| 
850
 | 
    28     """CLI"""
 | 
| 
646
 | 
    29 
 | 
| 
 | 
    30     parser = argparse.ArgumentParser(description=__doc__)
 | 
| 
672
 | 
    31     parser.add_argument('-m', '--mount', dest='mount_point',
 | 
| 
 | 
    32                         default='/mnt/media',
 | 
| 
 | 
    33                         help="mount point")
 | 
| 
646
 | 
    34     options = parser.parse_args(args)
 | 
| 
 | 
    35 
 | 
| 
 | 
    36     dmesg = subprocess.check_output(['dmesg']).splitlines()
 | 
| 
737
 | 
    37     for string in  ('usbcore: registered new interface driver usb-storage',
 | 
| 
 | 
    38                     'USB Mass Storage device detecte'):
 | 
| 
 | 
    39         for index in reversed(range(len(dmesg))):
 | 
| 
 | 
    40             line = dmesg[index]
 | 
| 
 | 
    41             if string in line:
 | 
| 
 | 
    42                 break
 | 
| 
 | 
    43         else:
 | 
| 
 | 
    44             continue
 | 
| 
 | 
    45         break
 | 
| 
646
 | 
    46     else:
 | 
| 
737
 | 
    47         sys.exit(1)
 | 
| 
646
 | 
    48 
 | 
| 
 | 
    49     for line in dmesg[index:]:
 | 
| 
 | 
    50         line = line.split(']', 1)[-1].strip()
 | 
| 
 | 
    51         if ':' in line:
 | 
| 
 | 
    52             try:
 | 
| 
 | 
    53                 disk, partition = line.split(':')
 | 
| 
 | 
    54             except ValueError:
 | 
| 
 | 
    55                 continue
 | 
| 
 | 
    56             disk = disk.strip()
 | 
| 
 | 
    57             partition = partition.strip()
 | 
| 
 | 
    58             if partition.startswith(disk):
 | 
| 
672
 | 
    59                 print ("partition: {}".format(partition))
 | 
| 
 | 
    60                 break
 | 
| 
 | 
    61     else:
 | 
| 
 | 
    62         parser.error("No partition found")
 | 
| 
 | 
    63 
 | 
| 
 | 
    64     device = os.path.join('/dev', partition)
 | 
| 
 | 
    65     assert os.path.exists(device)
 | 
| 
849
 | 
    66     print ("Device: {}".format(device))
 | 
| 
672
 | 
    67 
 | 
| 
 | 
    68     command = ['sudo', 'mount', device, options.mount_point]
 | 
| 
 | 
    69     print (' '.join(command))
 | 
| 
646
 | 
    70 
 | 
| 
 | 
    71 if __name__ == '__main__':
 | 
| 
 | 
    72     main()
 |