annotate python/mountusb.py @ 924:fc88a12acf48 default tip

add lfs filter
author Jeff Hammel <k0scist@gmail.com>
date Wed, 04 Dec 2024 15:50:53 -0800
parents c44637991a01
children
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 """
672
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
5 mount inserted usb disk
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
6
646
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 [ 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
8 [ 33.854946] scsi6 : usb-storage 1-1.2:1.0
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 [ 33.855002] usbcore: registered new interface driver usb-storage
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 [ 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
11 [ 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
12 [ 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
13 [ 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
14 [ 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
15 [ 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
16 [ 40.782414] sdb: sdb1
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 [ 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
18
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 """
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 import argparse
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 import os
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 import subprocess
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 import sys
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
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 def main(args=sys.argv[1:]):
850
c44637991a01 [mountusb] docstring
Jeff Hammel <k0scist@gmail.com>
parents: 849
diff changeset
28 """CLI"""
646
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 parser = argparse.ArgumentParser(description=__doc__)
672
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
31 parser.add_argument('-m', '--mount', dest='mount_point',
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
32 default='/mnt/media',
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
33 help="mount point")
646
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 options = parser.parse_args(args)
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 dmesg = subprocess.check_output(['dmesg']).splitlines()
737
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
37 for string in ('usbcore: registered new interface driver usb-storage',
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
38 'USB Mass Storage device detecte'):
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
39 for index in reversed(range(len(dmesg))):
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
40 line = dmesg[index]
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
41 if string in line:
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
42 break
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
43 else:
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
44 continue
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
45 break
646
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 else:
737
8806a699ff94 wip, untested
Jeff Hammel <k0scist@gmail.com>
parents: 672
diff changeset
47 sys.exit(1)
646
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 for line in dmesg[index:]:
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 line = line.split(']', 1)[-1].strip()
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 if ':' in line:
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 try:
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 disk, partition = line.split(':')
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 except ValueError:
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55 continue
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 disk = disk.strip()
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 partition = partition.strip()
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 if partition.startswith(disk):
672
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
59 print ("partition: {}".format(partition))
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
60 break
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
61 else:
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
62 parser.error("No partition found")
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
63
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
64 device = os.path.join('/dev', partition)
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
65 assert os.path.exists(device)
849
81d2f5c20ceb python 3 string compat
Jeff Hammel <k0scist@gmail.com>
parents: 737
diff changeset
66 print ("Device: {}".format(device))
672
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
67
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
68 command = ['sudo', 'mount', device, options.mount_point]
0eff3f3658ed STUB: python/mountusb.py
Jeff Hammel <k0scist@gmail.com>
parents: 646
diff changeset
69 print (' '.join(command))
646
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71 if __name__ == '__main__':
0e15d5aa78a2 add mount usb disk stub
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 main()