comparison python/mountusb.py @ 646:0e15d5aa78a2

add mount usb disk stub
author Jeff Hammel <k0scist@gmail.com>
date Tue, 11 Mar 2014 23:00:50 -0700
parents
children 0eff3f3658ed
comparison
equal deleted inserted replaced
645:753a5358535f 646:0e15d5aa78a2
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 [ 33.854905] usb-storage 1-1.2:1.0: USB Mass Storage device detected
6 [ 33.854946] scsi6 : usb-storage 1-1.2:1.0
7 [ 33.855002] usbcore: registered new interface driver usb-storage
8 [ 34.855894] scsi 6:0:0:0: Direct-Access PNY USB 2.0 FD 8.07 PQ: 0 ANSI: 4
9 [ 34.856108] sd 6:0:0:0: Attached scsi generic sg2 type 0
10 [ 34.857281] sd 6:0:0:0: [sdb] 249999360 512-byte logical blocks: (127 GB/119 GiB)
11 [ 34.858452] sd 6:0:0:0: [sdb] Write Protect is off
12 [ 34.858455] sd 6:0:0:0: [sdb] Mode Sense: 23 00 00 00
13 [ 34.859678] sd 6:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
14 [ 40.782414] sdb: sdb1
15 [ 40.787010] sd 6:0:0:0: [sdb] Attached SCSI removable disk
16
17 """
18
19 import argparse
20 import os
21 import subprocess
22 import sys
23
24 string = (str, unicode)
25
26 def main(args=sys.argv[1:]):
27
28 parser = argparse.ArgumentParser(description=__doc__)
29 options = parser.parse_args(args)
30
31 dmesg = subprocess.check_output(['dmesg']).splitlines()
32 string = 'usbcore: registered new interface driver usb-storage'
33 for index in reversed(range(len(dmesg))):
34 line = dmesg[index]
35 if string in line:
36 break
37 else:
38 sys.exit(1) # nothing found
39
40 for line in dmesg[index:]:
41 line = line.split(']', 1)[-1].strip()
42 if ':' in line:
43 try:
44 disk, partition = line.split(':')
45 except ValueError:
46 continue
47 disk = disk.strip()
48 partition = partition.strip()
49 if partition.startswith(disk):
50 print (partition)
51 sys.exit(0)
52
53 if __name__ == '__main__':
54 main()