blob: 57cbb02444d1f1b7a1814d5955a9b8f92bd57aa9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#!/usr/bin/python3
import argparse
import subprocess
import time
parser = argparse.ArgumentParser(description = 'Run a command on matching udev events')
parser.add_argument('-s', '--subsystem')
parser.add_argument('-m', '--match', action = 'append')
parser.add_argument('command')
args = parser.parse_args()
def process_buf(buf):
for m in args.match:
if not m in buf:
return
subprocess.run(args.command, shell = True)
cmdline = ['udevadm', 'monitor', '--property']
if args.subsystem:
cmdline.append('--subsystem-match=' + args.subsystem)
child = subprocess.Popen(cmdline, text = True, stdout = subprocess.PIPE)
try:
# skip first three lines
[child.stdout.readline() for i in range(3)]
buf = []
while child.poll() is None:
line = child.stdout.readline().strip()
if not line:
process_buf(buf)
buf.clear()
continue
buf.append(line)
finally:
child.terminate()
if child.returncode is None:
time.sleep(0.1)
child.kill()
|