view python/mountusb.py @ 664:ffb75d832afe

make sneaky urls
author Jeff Hammel <k0scist@gmail.com>
date Mon, 07 Apr 2014 19:24:57 -0700
parents 0e15d5aa78a2
children 0eff3f3658ed
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
[   33.854905] usb-storage 1-1.2:1.0: USB Mass Storage device detected
[   33.854946] scsi6 : usb-storage 1-1.2:1.0
[   33.855002] usbcore: registered new interface driver usb-storage
[   34.855894] scsi 6:0:0:0: Direct-Access     PNY      USB 2.0 FD       8.07 PQ: 0 ANSI: 4
[   34.856108] sd 6:0:0:0: Attached scsi generic sg2 type 0
[   34.857281] sd 6:0:0:0: [sdb] 249999360 512-byte logical blocks: (127 GB/119 GiB)
[   34.858452] sd 6:0:0:0: [sdb] Write Protect is off
[   34.858455] sd 6:0:0:0: [sdb] Mode Sense: 23 00 00 00
[   34.859678] sd 6:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[   40.782414]  sdb: sdb1
[   40.787010] sd 6:0:0:0: [sdb] Attached SCSI removable disk

"""

import argparse
import os
import subprocess
import sys

string = (str, unicode)

def main(args=sys.argv[1:]):

    parser = argparse.ArgumentParser(description=__doc__)
    options = parser.parse_args(args)

    dmesg = subprocess.check_output(['dmesg']).splitlines()
    string = 'usbcore: registered new interface driver usb-storage'
    for index in reversed(range(len(dmesg))):
        line = dmesg[index]
        if string in line:
            break
    else:
        sys.exit(1) # nothing found

    for line in dmesg[index:]:
        line = line.split(']', 1)[-1].strip()
        if ':' in line:
            try:
                disk, partition = line.split(':')
            except ValueError:
                continue
            disk = disk.strip()
            partition = partition.strip()
            if partition.startswith(disk):
                print (partition)
                sys.exit(0)

if __name__ == '__main__':
    main()