forked from linuxmint/mintstick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mintstick.py
executable file
·554 lines (463 loc) · 20.9 KB
/
mintstick.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/python3
from unidecode import unidecode
from subprocess import Popen, PIPE
import getopt
import gettext
import gi
import locale
import os
import signal
import subprocess
import sys
gi.require_version('Polkit', '1.0')
gi.require_version('Gtk', '3.0')
gi.require_version('UDisks', '2.0')
gi.require_version('XApp', '1.0')
from gi.repository import Polkit, Gtk, GLib, UDisks, XApp
try:
gi.require_version('Unity', '7.0')
from gi.repository import Unity
Using_Unity = True
except Exception:
Using_Unity = False
if Using_Unity:
launcher = Unity.LauncherEntry.get_for_desktop_id("mintstick.desktop")
APP = 'mintstick'
LOCALE_DIR = "/usr/share/linuxmint/locale"
locale.bindtextdomain(APP, LOCALE_DIR)
gettext.bindtextdomain(APP, LOCALE_DIR)
gettext.textdomain(APP)
_ = gettext.gettext
# https://technet.microsoft.com/en-us/library/bb490925.aspx
FORBIDDEN_CHARS = ["*", "?", "/", "\\", "|", ".", ",", ";", ":", "+", "=", "[", "]", "<", ">", "\""]
RELEVANT_UDISK_PROPERTIES = ['connection-bus', 'ejectable', 'id', \
'media-available', 'media-compatibility', 'media-removable', \
'model', 'vendor', 'optical', 'removable', 'size']
# noinspection PyUnusedLocal
class MintStick:
def __init__(self, iso_path_arg=None, usb_path_arg=None, filesystem_arg=None, mode_arg=None, debug_arg=False):
self.debug = debug_arg
self.filesystem = filesystem_arg
def devices_changed_callback(client):
self.get_devices()
self.udisks_client = UDisks.Client.new_sync()
self.udisk_listener_id = self.udisks_client.connect("changed", devices_changed_callback)
self.wTree = Gtk.Builder()
self.wTree.set_translation_domain(APP)
self.wTree.add_from_file("/usr/share/mintstick/mintstick.ui")
self.process = None
self.source_id = None
self.dev = None
self.write_progress = None
if mode_arg == "iso":
self.mode = "normal"
self.devicelist = self.wTree.get_object("device_combobox")
self.go_button = self.wTree.get_object("write_button")
self.verify_button = self.wTree.get_object("verify_button")
self.progressbar = self.wTree.get_object("progressbar")
self.chooser = self.wTree.get_object("filechooserbutton")
# Making file chooser accessible for users with screen reader
label = self.wTree.get_object("label_write_image")
button = self.chooser.get_children()[0]
label.set_mnemonic_widget(button)
# Devicelist model
self.devicemodel = Gtk.ListStore(str, str)
# Renderer
renderer_text = Gtk.CellRendererText()
self.devicelist.pack_start(renderer_text, True)
self.devicelist.add_attribute(renderer_text, "text", 1)
self.get_devices()
# get globally needed widgets
self.window = self.wTree.get_object("main_window")
self.window.connect("destroy", self.close)
# set default file filter to *.iso/*.img
filt = Gtk.FileFilter()
filt.add_pattern("*.[iI][mM][gG]")
filt.add_pattern("*.[iI][sS][oO]")
self.chooser.set_filter(filt)
self.devicelist.connect("changed", self.device_selected)
self.go_button.connect("clicked", self.do_write)
self.verify_button.connect("clicked", self.verify)
self.chooser.connect("file-set", self.file_selected)
if iso_path_arg:
if os.path.exists(iso_path_arg):
self.chooser.set_filename(iso_path_arg)
self.file_selected(self.chooser)
self.verify_button.set_sensitive(True)
if mode_arg == "format":
self.mode = "format"
self.devicelist = self.wTree.get_object("formatdevice_combobox")
self.go_button = self.wTree.get_object("format_formatbutton")
self.label_entry = self.wTree.get_object("volume_label_entry")
try:
self.label_entry.set_text(unidecode(self.label_entry.get_text()))
except:
self.label_entry.set_text("USB STICK")
self.label_entry_changed_id = self.label_entry.connect("changed", self.on_label_entry_text_changed)
self.window = self.wTree.get_object("format_window")
self.window.connect("destroy", self.close)
self.progressbar = self.wTree.get_object("format_progressbar")
self.filesystemlist = self.wTree.get_object("filesystem_combobox")
self.go_button.connect("clicked", self.do_format)
self.filesystemlist.connect("changed", self.filesystem_selected)
self.devicelist.connect("changed", self.device_selected)
# Filesystemlist
self.fsmodel = Gtk.ListStore(str, str, int, bool, bool)
# id label max-length force-upper-case force-alpha-numeric
self.fsmodel.append(["fat32", "FAT32", 11, True, True])
self.fsmodel.append(["exfat", "exFAT", 15, False, False])
self.fsmodel.append(["ntfs", "NTFS", 32, False, False])
self.fsmodel.append(["ext4", "EXT4", 16, False, False])
self.filesystemlist.set_model(self.fsmodel)
# Renderer
renderer_text = Gtk.CellRendererText()
self.filesystemlist.pack_start(renderer_text, True)
self.filesystemlist.add_attribute(renderer_text, "text", 1)
# Devicelist model
self.devicemodel = Gtk.ListStore(str, str)
# Renderer
renderer_text = Gtk.CellRendererText()
self.devicelist.pack_start(renderer_text, True)
self.devicelist.add_attribute(renderer_text, "text", 1)
self.filesystemlist.set_sensitive(True)
# Default's to fat32
self.filesystemlist.set_active(0)
if filesystem_arg is not None:
itererator = self.fsmodel.get_iter_first()
while itererator is not None:
value = self.fsmodel.get_value(itererator, 0)
if value == filesystem_arg:
self.filesystemlist.set_active_iter(itererator)
itererator = self.fsmodel.iter_next(itererator)
self.filesystem_selected(self.filesystemlist)
self.get_devices()
if usb_path_arg is not None:
itererator = self.devicemodel.get_iter_first()
while itererator is not None:
value = self.devicemodel.get_value(itererator, 0)
if usb_path_arg in value:
self.devicelist.set_active_iter(itererator)
itererator = self.devicemodel.iter_next(itererator)
self.window.show()
def verify(self, button):
subprocess.Popen(["mint-iso-verify", self.chooser.get_filename()])
def print_drive(self, drive):
# print drive info to stdout
# this is done for debugging purposes
try:
for prop in drive.list_properties():
name = prop.name
if name in RELEVANT_UDISK_PROPERTIES:
print(f" {prop.name}: {drive.get_property(prop.name)}")
print()
except Exception as e:
print(e)
def get_devices(self):
self.go_button.set_sensitive(False)
self.devicemodel.clear()
detected_drives = []
self.dev = None
manager = self.udisks_client.get_object_manager()
for obj in manager.get_objects():
if obj is not None:
block = obj.get_block()
if block is not None:
drive = self.udisks_client.get_drive_for_block(block)
if drive is not None:
drive_id = drive.get_property('id')
if drive_id in detected_drives:
continue
detected_drives.append(drive_id)
self.print_drive(drive)
is_usb = str(drive.get_property('connection-bus')) in ['usb', 'cpio', 'sdio']
size = int(drive.get_property('size'))
optical = bool(drive.get_property('optical'))
removable = bool(drive.get_property('removable'))
if is_usb and size > 0 and removable and not optical:
name = block.get_property('device')
name = ''.join([i for i in name if not i.isdigit()])
drive_vendor = str(drive.get_property('vendor'))
drive_model = str(drive.get_property('model'))
if drive_vendor.strip() != "":
drive_model = "%s %s" % (drive_vendor, drive_model)
if size >= 1000000000000:
size = "%.0fTB" % round(size / 1000000000000)
elif size >= 1000000000:
size = "%.0fGB" % round(size / 1000000000)
elif size >= 1000000:
size = "%.0fMB" % round(size / 1000000)
elif size >= 1000:
size = "%.0fkB" % round(size / 1000)
else:
size = "%.0fB" % round(size)
item = "%s (%s) - %s" % (drive_model, name, size)
self.devicemodel.append([name, item])
self.devicelist.set_model(self.devicemodel)
def device_selected(self, widget):
iterator = self.devicelist.get_active_iter()
if iterator is not None:
self.dev = self.devicemodel.get_value(iterator, 0)
self.go_button.set_sensitive(True)
def filesystem_selected(self, widget):
itererator = self.filesystemlist.get_active_iter()
if itererator is not None:
self.filesystem = self.fsmodel.get_value(itererator, 0)
self.activate_devicelist()
self.label_entry.set_max_length(self.fsmodel.get_value(itererator, 2))
self.on_label_entry_text_changed(self, self.label_entry)
def file_selected(self, widget):
self.activate_devicelist()
filename = self.chooser.get_filename()
if filename is not None and os.path.exists(filename):
self.verify_button.set_sensitive(True)
else:
self.verify_button.set_sensitive(False)
def on_label_entry_text_changed(self, widget, data=None):
self.label_entry.handler_block(self.label_entry_changed_id)
active_iter = self.filesystemlist.get_active_iter()
value = self.fsmodel.get_value(active_iter, 0)
cursor_pos = self.label_entry.props.cursor_position
text = self.label_entry.get_text()
if self.fsmodel.get_value(active_iter, 3):
text = text.upper()
if self.fsmodel.get_value(active_iter, 4):
for char in FORBIDDEN_CHARS:
text = text.replace(char, "")
try:
text = unidecode(text)
except Exception as e:
print(f"Exception in sanitizing the label {text}")
print(str(e))
text = "USB STICK"
self.label_entry.set_text(text)
self.go_button.set_sensitive(len(text) > 0)
length = self.label_entry.get_buffer().get_length()
self.label_entry.select_region(length, -1)
self.label_entry.set_position(cursor_pos)
self.label_entry.handler_unblock(self.label_entry_changed_id)
def do_format(self, widget):
if self.debug:
print("DEBUG: Format %s as %s" % (self.dev, self.filesystem))
return
self.udisks_client.handler_block(self.udisk_listener_id)
self.devicelist.set_sensitive(False)
self.filesystemlist.set_sensitive(False)
self.go_button.set_sensitive(False)
self.label_entry.set_sensitive(False)
label = self.label_entry.get_text()
self.raw_format(self.dev, self.filesystem, label)
def check_format_job(self):
self.process.poll()
if self.process.returncode is None:
self.pulse_progress()
return True
else:
GLib.idle_add(self.format_job_done, self.process.returncode)
self.process = None
return False
def raw_format(self, usb_path_arg, fstype, label):
if os.geteuid() > 0:
polkit_exec = 'pkexec'
self.process = Popen(
[polkit_exec, '/usr/bin/mint-stick-format', '-d', usb_path_arg,
'-f', fstype, '-u', str(os.geteuid()), '-g', str(os.getgid()), '--', label],
shell=False, stdout=PIPE, preexec_fn=os.setsid)
else:
self.process = Popen(
['/usr/bin/mint-stick-format', '-d', usb_path_arg,
'-f', fstype, '-u', str(os.geteuid()), '-g', str(os.getgid()), "--", label],
shell=False, stdout=PIPE, preexec_fn=os.setsid)
self.progressbar.show()
self.pulse_progress()
GLib.timeout_add(500, self.check_format_job)
def format_job_done(self, rc):
if rc == 0:
self.show_format_result("dialog-information", _('The USB stick was formatted successfully.'))
return False
elif rc == 5:
message = _("An error occured while creating a partition on %s.") % usb_path
elif rc == 127:
message = _('Authentication Error.')
elif rc == 126: # Cancelled by the user.
self.clear_progress()
message = None
else:
message = _('An error occurred.')
if message is not None:
self.show_format_result("dialog-error", message)
self.set_format_sensitive(False)
self.udisks_client.handler_unblock(self.udisk_listener_id)
return False
def do_write(self, widget):
if self.debug:
print("DEBUG: Write %s to %s" % (self.chooser.get_filename(), self.dev))
return
source = self.chooser.get_filename()
target = self.dev
filename = os.path.basename(source).lower()
# Don't write Windows ISO
for keyword in ["windows", "win7", "win8", "win10", "winxp"]:
if keyword in filename:
self.wTree.get_object("stack").set_visible_child_name("windows_page")
return
self.udisks_client.handler_block(self.udisk_listener_id)
self.go_button.set_sensitive(False)
self.verify_button.set_sensitive(False)
self.devicelist.set_sensitive(False)
self.chooser.set_sensitive(False)
self.progressbar.show()
self.raw_write(source, target)
def set_progress(self, size):
self.progressbar.set_fraction(size)
str_progress = "%3.0f%%" % (float(size) * 100)
int_progress = int(float(size) * 100)
self.progressbar.set_text(str_progress)
XApp.set_window_progress_pulse(self.window, False)
XApp.set_window_progress(self.window, int_progress)
def clear_progress(self):
self.progressbar.hide()
XApp.set_window_progress_pulse(self.window, False)
def pulse_progress(self):
self.progressbar.pulse()
XApp.set_window_progress_pulse(self.window, True)
def update_progress(self, fd, condition):
if Using_Unity:
launcher.set_property("progress_visible", True)
if condition is GLib.IO_IN:
line = fd.readline()
# noinspection PyBroadException
try:
size = float(line.strip())
progress = round(size * 100)
if progress > self.write_progress:
self.write_progress = progress
GLib.idle_add(self.set_progress, size)
if Using_Unity:
launcher.set_property("progress", size)
except:
pass
return True
else:
GLib.source_remove(self.source_id)
return False
def check_write_job(self):
self.process.poll()
if self.process.returncode is None:
return True
else:
GLib.idle_add(self.write_job_done, self.process.returncode)
self.process = None
return False
def raw_write(self, source, target):
if os.geteuid() > 0:
polkit_exec = 'pkexec'
self.process = Popen(
[polkit_exec, '/usr/bin/mint-stick-write', '-s', source, '-t', target],
shell=False, stdout=PIPE, preexec_fn=os.setsid)
else:
self.process = Popen(
['/usr/bin/mint-stick-write', '-s', source, '-t', target],
shell=False, stdout=PIPE, preexec_fn=os.setsid)
self.write_progress = 0
self.source_id = GLib.io_add_watch(self.process.stdout, GLib.IO_IN | GLib.IO_HUP, self.update_progress)
GLib.timeout_add(500, self.check_write_job)
def write_job_done(self, rc):
self.udisks_client.handler_unblock(self.udisk_listener_id)
if rc == 0:
if Using_Unity:
launcher.set_property("progress_visible", False)
launcher.set_property("urgent", True)
self.set_progress(1.0)
self.show_result("dialog-information", _('The image was successfully written.'))
return False
elif rc == 3:
message = _('Not enough space on the USB stick.')
elif rc == 4:
message = _('An error occured while copying the image.')
elif rc == 127:
message = _('Authentication Error.')
elif rc == 126: # Cancelled by the user.
self.clear_progress()
self.set_iso_sensitive()
message = None
else:
message = _('An error occurred.')
if message is not None:
self.show_format_result("dialog-error", message)
return False
def show_format_result(self, icon_name, text):
self.wTree.get_object("format_stack").set_visible_child_name("format_result_page")
self.wTree.get_object("format_result_image").set_from_icon_name(icon_name, Gtk.IconSize.DIALOG)
self.wTree.get_object("format_result_label").set_text(text)
def show_result(self, icon_name, text):
self.wTree.get_object("stack").set_visible_child_name("result_page")
self.wTree.get_object("result_image").set_from_icon_name(icon_name, Gtk.IconSize.DIALOG)
self.wTree.get_object("result_label").set_text(text)
def close(self, widget):
if self.process is not None:
try:
os.killpg(self.process.pid, signal.SIGTERM)
except:
pass
finally:
Gtk.main_quit()
else:
Gtk.main_quit()
def activate_devicelist(self):
self.devicelist.set_sensitive(True)
def set_iso_sensitive(self):
self.chooser.set_sensitive(True)
self.devicelist.set_sensitive(True)
self.go_button.set_sensitive(True)
self.verify_button.set_sensitive(True)
def set_format_sensitive(self, reset=True):
if reset:
self.get_devices()
self.filesystemlist.set_sensitive(True)
self.devicelist.set_sensitive(True)
self.label_entry.set_sensitive(True)
self.go_button.set_sensitive(True)
if __name__ == "__main__":
usb_path = None
iso_path = None
filesystem = None
mode = None
def usage():
print("Usage: mintstick [--debug] -m [format|iso] : mode (format usb stick or burn iso image)")
print(" mintstick [--debug] -m iso [-i|--iso] iso_path")
print(" mintstick [--debug] -m format [-u|--usb] usb_device ")
print(" [-f|--filesystem] filesystem")
exit(0)
try:
opts, args = getopt.getopt(sys.argv[1:], "hm:i:u:f:", ["debug", "help", "mode=", "iso=", "usb=", "filesystem="])
except getopt.error as msg:
print(msg)
print("for help use --help")
sys.exit(2)
debug = False
for o, a in opts:
if o in ("-h", "--help"):
usage()
elif o in ("-i", "--iso"):
iso_path = a
elif o in ("-u", "--usb"):
# hack. KDE Solid application gives partition name not device name.
# Need to remove extra digit from the string.
# ie. /dev/sdj1 -> /dev/sdj
usb_path = ''.join([i for i in a if not i.isdigit()])
elif o in ("-f", "--filesystem"):
filesystem = a
elif o in ("-m", "--mode"):
mode = a
elif o == "--debug":
debug = True
argc = len(sys.argv)
if argc > 8:
print("Too many arguments")
print("for help use --help")
exit(2)
# Mandatory argument
if (mode is None) or ((mode != "format") and (mode != "iso")):
usage()
MintStick(iso_path, usb_path, filesystem, mode, debug)
Gtk.main()