annotate python/mountusb.py @ 650:9cb40c01c10a

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