Created
May 25, 2023 13:38
-
-
Save adeguet1/76b9b3b75d07b67c5f1a2ebb46a56035 to your computer and use it in GitHub Desktop.
Python script to detect any new SD card and copy some files on it. Used to flash firmware for dVRK controllers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Script used to monitor new block device (SD card) and copy new dVRK | |
# firmware files automatically | |
# | |
# Anton Deguet | |
# | |
import pyudev | |
import subprocess | |
import time | |
import datetime | |
import os | |
import shutil | |
context = pyudev.Context() | |
monitor = pyudev.Monitor.from_netlink(context) | |
monitor.filter_by(subsystem='block') | |
now = datetime.datetime.now() | |
date_time = now.strftime("%Y-%m-%d-%H.%M.%S") | |
for device in iter(monitor.poll, None): | |
if device.action == 'add': | |
# find the device and path | |
dir(device) | |
print('udev device: {}'.format(device)) | |
slash_dev = device.device_node | |
print('/dev device: {}'.format(slash_dev)) | |
time.sleep(1.0) # 1 second to let the OS mount the device | |
mount_path = subprocess.run(['findmnt', '-n', '-o', 'TARGET', slash_dev], | |
stdout=subprocess.PIPE).stdout.decode('utf-8').strip() | |
print('mount point: {}'.format(mount_path)) | |
# create backup dirctory if needed | |
existing_files = os.listdir(mount_path) | |
if existing_files: | |
# create back dir | |
backup_path = mount_path + '/backups' | |
if not os.path.exists(backup_path): | |
os.mkdir(backup_path) | |
backup_path = backup_path + '/' + date_time | |
if not os.path.exists(backup_path): | |
os.mkdir(backup_path) | |
# move files | |
for f in existing_files: | |
if f != 'backups': | |
full_file = mount_path + '/' + f | |
print('move {} to backup directory {}'.format(full_file, backup_path)) | |
shutil.move(full_file, backup_path) | |
# copy files | |
files = ['BOOT.bin', 'firmware.xsvf'] | |
for f in files: | |
print('copy {} to {}'.format(f, mount_path)) | |
shutil.copy(f, mount_path) | |
# leave a timestamp on card | |
subprocess.run(['touch', mount_path + '/created-' + date_time], | |
stdout=subprocess.PIPE) | |
# and finally, umount | |
umount_result = subprocess.run(['umount', mount_path], | |
stdout=subprocess.PIPE).stdout.decode('utf-8').strip() | |
print('umount {}'.format(umount_result)) |
keshuaixu
commented
Aug 14, 2023
Looks o.k., but why not unzip the file directly to the SD card (with -o option to overwrite)? That would preserve the original file dates, which may be useful as another check that it is the released version.
I was trying to save CPU time by only unzipping once, but preserving the original file dates is more important.
For the dVRK, the script has been moved to the main repository: jhu-dvrk/sawIntuitiveResearchKit@1313fba
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment