Synopsis:
I wanted something for macOS (and Linux; maybe eventually Windows) that would simply wait for the user to connect a storage device and automatically select, or otherwise output the information to be used, read, manipulated, etc.
In its current form, it just prints to the shell, but you could assign the output to a list or variable for read/write operations and so on. It will respond to any new entries in the /dev
system directory, including most USB devices, SD Cards, Webcams, and so on. You can test it by running the script in one window, and running something like sudo touch /dev/{x,y,z}
in another.
I plan to use it to help people (those of us who are less technically inclined) migrate to Linux by automating the creation of bootable flash drives, but you can do what you like with it.
Open-ended feedback and suggestions are welcome. Please try to post example code pertaining to your suggestions, and don't be afraid to say something positive.
Usage:
user@macOS:~$ ./devlisten.py
/dev/disk2
/dev/rdisk2
/dev/disk2s1
/dev/rdisk2s1
Code:
#!/usr/bin/env python3
import os
import re
import time
import difflib
try:
os.mkdir('/tmp/dev')
except FileExistsError:
pass
except FileNotFoundError:
print('No /tmp directory found.')
exit()
except OSError:
print('Read-only file system.')
exit()
file1 = open('/tmp/dev/1', 'w')
for x in os.listdir('/dev'):
file1.write(x + '\n')
file1.close()
try:
diff = False
while diff == False:
time.sleep(0.25)
file2 = open('/tmp/dev/2', 'w')
for x in os.listdir('/dev'):
file2.write(x + '\n')
file2.close()
text1 = open('/tmp/dev/1').readlines()
text2 = open('/tmp/dev/2').readlines()
for line in difflib.unified_diff(text1, text2):
for line in re.finditer(r'(?<=^\+)\w.*$', line, re.MULTILINE):
print('/dev/' + line.group(0))
diff = True
except KeyboardInterrupt:
print()
exit()