diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-06-23 15:13:37 +0200 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-06-23 15:13:37 +0200 |
commit | 0b44d2af973250008f477dd1834e06df9d1fc2a1 (patch) | |
tree | ea57eb5495dd398203fd1e4bdc0c1dab289651ee | |
parent | fac3242f9f2aa0657d354df097934cec0ed398c9 (diff) |
bin: add udev_match
Installed in the desktop profile.
-rwxr-xr-x | bin/udev_match | 39 | ||||
-rw-r--r-- | desktop.conf.yaml | 1 |
2 files changed, 40 insertions, 0 deletions
diff --git a/bin/udev_match b/bin/udev_match new file mode 100755 index 0000000..de51650 --- /dev/null +++ b/bin/udev_match @@ -0,0 +1,39 @@ +#!/usr/bin/python3 + +import argparse +import subprocess + +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) + +# 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) diff --git a/desktop.conf.yaml b/desktop.conf.yaml index 0e869b4..24700c8 100644 --- a/desktop.conf.yaml +++ b/desktop.conf.yaml @@ -5,6 +5,7 @@ - link: ~/.config/mpv/config: dotfiles/mpv ~/.local/bin/ssh_sk_clean: bin/ssh_sk_clean + ~/.local/bin/udev_match: bin/udev_match ~/.urxvt/ext/reselect: urxvt/reselect/reselect ~/.xscreensaver: dotfiles/xscreensaver ~/.Xresources: dotfiles/Xresources |