-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusbstick.py
executable file
·516 lines (464 loc) · 16.4 KB
/
usbstick.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/usr/bin/env python3
# coding=utf-8
"""
USB-Stick.
abstraction class for USB-Sticks handling.
needs a pyudev Device Class object as input.
this could eventually be used for fixing the label-permission problem:
https://unix.stackexchange.com/questions/229987/udev-rule-to-match-any-usb-storage-device
and hopefully someone posts a real solution:
https://unix.stackexchange.com/questions/399010/change-volume-name-without-sudo-root
here is a script that can manage discs with the help of shell commands
https://stackoverflow.com/a/22454071/574981
the documentation to pyudev:
http://pyudev.readthedocs.io/en/latest/guide.html
"""
import os
import shutil
import subprocess
import threading
# import readline
import pyudev
import configdict
class Error(Exception):
"""Base class for exceptions in this module."""
pass
class USBStick(threading.Thread):
"""Class providing abstraction of inserted USBStick."""
default_config = {
'source_folder': "~/StickDataToCopy/",
'mount_base': "~/ustick_copy/",
'disc_label': "SUN",
'files_to_remove': [
'example.file'
],
'auto_run_steps': {
'format_as_fat32': False,
'update_label': False,
'copy_files_to_me': False,
'remove_all_meta_files': False,
'remove_files': False,
},
}
def __init__(self, device_path, config, queue=None):
"""Create new USBStick Object."""
# threading.Thread.__init__(self)
super(USBStick, self).__init__()
self.queue = queue
self.udev_context = pyudev.Context()
device = pyudev.Devices.from_path(
self.udev_context,
device_path
)
self.device = device
self.path = self.device.device_path
# device_path: /sys/devices/pci0000:00/0000:00:1d.0/usb2/
# 2-1/2-1.2/2-1.2.2/2-1.2.2.4/2-1.2.2.4:1.0/
# host7/target7:0:0/7:0:0:0/block/sdc/sdc1
self.node = self.device.device_node
# device_node: /dev/sdc1
self.label = self.device['ID_FS_LABEL']
self.mount_point = None
self.config = configdict.merge_deep(self.default_config, config)
self.usb_port_path = self.get_usb_port_path()
self.usb_port_name = self.get_usb_port_name()
# usb port helper
def get_usb_port(self):
"""Get USB-Port device."""
return self.device.find_parent('usb')
def get_usb_port_path(self):
"""Get USB-Port full path."""
return self.get_usb_port().device_path
def get_usb_port_id(self):
"""Get USB-Port id."""
path_full = self.get_usb_port_path()
# path_full /sys/devices/pci0000:00/0000:00:1d.0/usb2/
# 2-1/2-1.2/2-1.2.2/2-1.2.2.4/2-1.2.2.4:1.0
head, tail = os.path.split(path_full)
# 2-1.2.2.4:1.0
port_id = tail.replace(':1.0', '')
# 2-1.2.2.4
return port_id
def get_usb_port_name(self):
"""Get USB-Port name."""
port_id = self.get_usb_port_id()
# 2-1.2.2.4
port_name = port_id.replace('.', '_')
# 2-1_2_2_4
return port_name
def get_usb_port_tree(self):
"""Get USB-Port tree."""
port_id = self.get_usb_port_id()
# 2-1.2.2.4
levels = port_id.split('.')
# tree = {}
tree = self
for level in reversed(levels):
new_level = {}
new_level[level] = tree
tree = new_level
return tree
# mount
def _create_mount_point(self):
"""Create mount point for this Stick."""
# print("create mount point:")
rel_mount_point = os.path.join(
self.config['mount_base'],
self.get_usb_port_name()
)
abs_mount_point = os.path.expanduser(rel_mount_point)
self.mount_point = abs_mount_point
# print("mount_point:", self.mount_point)
# check if mount point allready exsists
# path.exists only works reliable on absolute paths
if not os.path.exists(self.mount_point):
# print("create path")
os.makedirs(self.mount_point)
def _remove_mount_point(self):
"""Remove mount point for this Stick."""
if os.path.exists(self.mount_point):
# print("remove path")
os.rmdir(self.mount_point)
self.mount_point = None
def mount(self):
"""Mount this Stick."""
self._create_mount_point()
if self.mount_point:
# mount
# --source /dev/sdc1
# --target /home/stefan/ustick_copy/2-1_2_2_4
# mount needs root / sudo
command = [
"mount",
"--source={}".format(self.node),
"--target={}".format(self.mount_point),
]
result_string = ""
try:
result_string += subprocess.check_output(command).decode()
# print("result_string", result_string)
except subprocess.CalledProcessError as e:
error_message = "failed: {}".format(e)
print(error_message)
result_string += error_message
return result_string
def unmount(self):
"""Unmount this Stick."""
if self.mount_point:
# umount
# /dev/sdc1
# /home/stefan/ustick_copy/2-1_2_2_4
# umount needs root / sudo
command = [
"umount",
"{}".format(self.mount_point),
]
result_string = ""
try:
result_string += subprocess.check_output(command).decode()
# print("result_string", result_string)
except subprocess.CalledProcessError as e:
error_message = "failed: {}".format(e)
print(error_message)
result_string += error_message
else:
self._remove_mount_point()
return result_string
else:
print("stick not mounted by this scirpt.")
# mount with user rights
def user_mount(self):
"""Mount this stick with user rights."""
# self._create_mount_point()
mount_point_label = "usbstick_" + self.get_usb_port_name()
if mount_point_label:
# pmount /dev/sdc1 ustick_copy/2-1_2_2_4
command = [
"pmount",
"{}".format(self.node),
"{}".format(mount_point_label),
]
result_string = ""
try:
result_string += subprocess.check_output(command).decode()
# print("result_string", result_string)
except subprocess.CalledProcessError as e:
error_message = "failed: {}".format(e)
print(error_message)
result_string += error_message
else:
self.mount_point = os.path.join(
"/media",
mount_point_label
)
return result_string
def user_unmount(self):
"""Unmount this stick with user rights."""
if self.mount_point:
# pumount /dev/sdc1
command = [
"pumount",
"{}".format(self.node),
]
result_string = ""
try:
result_string += subprocess.check_output(command).decode()
# print("result_string", result_string)
except subprocess.CalledProcessError as e:
error_message = "failed: {}".format(e)
print(error_message)
result_string += error_message
else:
self.mount_point = None
return result_string
else:
print("stick not mounted by this scirpt.")
# label things
def update_label(self, label=None):
"""Update the Filesystem Label."""
# fatlabel /dev/sda1 MyNewLabel
if label is None:
label = self.config['disc_label']
command = [
"fatlabel",
"{}".format(self.node),
"{}".format(label),
]
result_string = ""
try:
result_string += subprocess.check_output(command).decode()
# print("result_string", result_string)
except subprocess.CalledProcessError as e:
error_message = "failed: {}".format(e)
print(error_message)
result_string += error_message
return result_string
# format
def format_as_fat32(self, label=None):
"""Format as fat32 and set label."""
if label is None:
label = self.config['disc_label']
# mkfs -t fat -F 32 -n "world" /dev/sdb1
# mkfs -t fat -F 32 -n "world" /dev/sdb1
command = [
"mkfs.fat",
# "-t=fat",
"-F 32",
"-n {}".format(label),
"{}".format(self.mount_point),
]
result_string = ""
try:
result_string += subprocess.check_output(command).decode()
# print("result_string", result_string)
except subprocess.CalledProcessError as e:
error_message = "failed: {}".format(e)
print(error_message)
result_string += error_message
return result_string
# copy files
def copy_files_to_me(self, src=None):
"""Copy Files from source_folder to this Stick."""
# based on
# https://docs.python.org/3/library/shutil.html#shutil.copy2
dst = self.mount_point
if src is None:
src = self.config['source_folder']
src_abs = os.path.expanduser(src)
# first check if device is mounted.
if os.path.exists(dst):
self._copy_files(src_abs, dst)
else:
print(
"error: destination '{}' does not exist! "
"check if Stick is mounted!".format(dst)
)
def _copy_files(self, src, dst):
errors = []
names = os.listdir(src)
for name in names:
self._copy_file(src, dst, name, errors)
try:
shutil.copystat(src, dst)
except OSError as why:
# can't copy file access times on Windows
if why.winerror is None:
errors.extend((src, dst, str(why)))
if errors:
raise Error(errors)
def _copy_file(self, src, dst, name, errors):
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
os.makedirs(dstname)
self._copy_files(srcname, dstname)
else:
shutil.copy2(srcname, dstname)
except OSError as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error as err:
errors.extend(err.args[0])
if errors:
raise Error(errors)
# remove files
def remove_all_meta_files(self):
"""Remove all meta files from this Stick."""
# based on
# https://docs.python.org/3/library/os.html#os.walk
meta_files = [
'.DS_Store'
]
for root, dirs, files in os.walk(self.mount_point, topdown=False):
for name in files:
if name in meta_files:
full_path = os.path.join(root, name)
print("remove: {}".format(full_path))
os.remove(full_path)
def remove_files(self, file_list=None):
"""Remove Files in file_list from this Stick."""
if file_list is None:
file_list = self.config['files_to_remove']
errors = []
infos = []
for file_name in file_list:
try:
full_path = os.path.join(self.mount_point, file_name)
except Exception as why:
info = {
'mount_point': self.mount_point,
'file_name': file_name,
}
errors.extend((info, str(why)))
try:
os.remove(full_path)
print("removed: {}".format(full_path))
except FileNotFoundError as why:
infos.extend(why)
except Exception as why:
info = {
'mount_point': self.mount_point,
'file_name': file_name,
}
errors.extend((info, str(why)))
if infos:
print(infos)
if errors:
raise Error(errors)
# helper
def print_properties(self):
"""Print all properties with values for an given device."""
for prop in self.device.properties:
print("{}:{}".format(
prop,
self.device[prop]
))
def prettyprint(self):
"""Copy Files from source_folder to this Stick."""
print(
(
# 42*'*' + "\n"
"{}\n"
"device_node: {}\n"
"ID_FS_LABEL: {}\n"
).format(
self.device,
self.device.device_node,
self.device.properties.get("ID_FS_LABEL")
)
)
def show_port_message(self, message):
"""Show message for a device with port_path."""
if self.queue:
# queue_item = (self.port_path, message)
# print(queue_item)
# self.queue.put(queue_item)
self.queue.put((self.usb_port_path, message))
else:
print("Port {}: {}".format(
self.usb_port_name,
message
))
def _run_mounted(self):
auto_run_steps = self.config['auto_run_steps']
# ******************************************
# real work to do:
if auto_run_steps['copy_files_to_me']:
# copy files
self.show_port_message("copy")
self.copy_files_to_me()
if auto_run_steps['remove_all_meta_files']:
# remove meta files
self.show_port_message("rm meta")
self.remove_all_meta_files()
if auto_run_steps['remove_files']:
# remove files in rm_files_list
self.show_port_message("rm files")
self.remove_files()
# ******************************************
# thread runner
def run(self):
"""Auto perform Stick programming."""
auto_run_steps = self.config['auto_run_steps']
self.show_port_message("start")
try:
if auto_run_steps['format_as_fat32']:
# format_as_fat32
self.show_port_message("fat32")
self.format_as_fat32()
if auto_run_steps['update_label']:
# update label
self.show_port_message("label")
self.update_label()
except Exception as e:
raise e
else:
try:
# mount
self.mount()
# self.user_mount()
except Exception as e:
raise e
else:
try:
self._run_mounted()
except Exception as e:
raise e
finally:
try:
# un-mount
self.unmount()
# self.user_unmount()
except Exception as e:
raise e
# done :-)
# now we have to let the user know
# print("stick '{}' done".format(self.get_usb_port_id()))
self.show_port_message("done")
##########################################
if __name__ == '__main__':
context = pyudev.Context()
usbstick_list = []
for device in context.list_devices(
subsystem='block',
DEVTYPE='partition'
):
if 'ID_BUS' in device:
if device['ID_BUS'] == 'usb':
usbstick_list.append(
USBStick(device.device_path, {})
)
print("Sticks", "-"*42)
for stick in usbstick_list:
stick.prettyprint()
print("X"*42)
# get device:
# device = pyudev.Devices.from_path(context,
# '/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2.2/2-1.2.2.4/'
# '2-1.2.2.4:1.0/host7/target7:0:0/7:0:0:0/block/sdc/sdc1'
# )