Skip to content

Instantly share code, notes, and snippets.

@karaketir16
Last active October 9, 2023 12:53
Show Gist options
  • Save karaketir16/1d24000cbd194a7da548ec66beaeb5ff to your computer and use it in GitHub Desktop.
Save karaketir16/1d24000cbd194a7da548ec66beaeb5ff to your computer and use it in GitHub Desktop.
import os
import subprocess
import re
RULES_DIR = "/etc/udev/rules.d/"
RULES_FILE_NAME = "99-usb-serial.rules"
list_ttyUSB_cmd = "ls /dev/ | grep ttyUSB"
ttyUSB_list = subprocess.getoutput(list_ttyUSB_cmd).split()
with open(os.path.join(RULES_DIR, RULES_FILE_NAME), "w") as rules_file:
for ttyUSB in ttyUSB_list:
id_path_cmd = f"udevadm info --name=/dev/{ttyUSB} --query=property"
properties = subprocess.getoutput(id_path_cmd)
# ID_PATH bilgisini regex ile çıkartalım
match = re.search(r'ID_PATH=(?P<id_path>[^\s]+)', properties)
if match:
id_path = match.group('id_path')
new_ttyUSB_name = f"{ttyUSB}_static"
rule_line = f'SUBSYSTEM=="tty", ENV{{ID_PATH}}=="{id_path}", SYMLINK+="{new_ttyUSB_name}", MODE="0666"\n'
rules_file.write(rule_line)
os.system("udevadm control --reload-rules && udevadm trigger")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment