aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2018-08-05 10:43:20 +0200
committerneodarz <neodarz@neodarz.net>2018-08-05 10:43:20 +0200
commitc32ae915c43089ff4e1706a2c59266b57cc7a902 (patch)
tree66d54060b2d07cddf1cd5687545c2e059a4c0a1d
parent2d8e5833d7e5e7f7735c44f7dd56500b7edd1664 (diff)
downloaddotfiles_ascii-c32ae915c43089ff4e1706a2c59266b57cc7a902.tar.xz
dotfiles_ascii-c32ae915c43089ff4e1706a2c59266b57cc7a902.zip
Add script for write on disk
-rw-r--r--scripts/.scripts/write_on_disk.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/scripts/.scripts/write_on_disk.rb b/scripts/.scripts/write_on_disk.rb
new file mode 100644
index 0000000..dc6f280
--- /dev/null
+++ b/scripts/.scripts/write_on_disk.rb
@@ -0,0 +1,63 @@
+
+
+ #!/usr/bin/env ruby
+ require 'dbus'
+ require 'awesome_print'
+ require 'filesize'
+ require 'colorize'
+
+ loop = DBus::Main.new
+ bus = DBus::SystemBus.instance
+
+ udisk2 = bus['org.freedesktop.UDisks2']
+ device = nil
+
+ EXPECTED_INTERFACES = %w[org.freedesktop.UDisks2.PartitionTable org.freedesktop.UDisks2.Block]
+
+ manager = udisk2['/org/freedesktop/UDisks2']['org.freedesktop.DBus.ObjectManager']
+ manager.on_signal 'InterfacesAdded' do |path|
+ interfaces = udisk2[path].interfaces
+ next unless (EXPECTED_INTERFACES - interfaces).empty?
+ block = udisk2[path]['org.freedesktop.UDisks2.Block']
+
+ path = path.split('/').last
+
+ drive = block['Drive']
+ drive = udisk2[drive]['org.freedesktop.UDisks2.Drive']
+ id = drive['Id']
+ size = Filesize.new drive['Size']
+
+ device = { path: path, id: id, size: size }
+
+ loop.quit
+ end
+
+ iso = ARGV.first
+ size = File.size iso
+ puts "#{'Plug a drive'.colorize :red} to begin to write #{iso.colorize :green} [#{Filesize.new(size).pretty.colorize :yellow}]"
+
+ loop << bus
+ loop.run
+
+ print "Erase #{device[:id].colorize :red} [#{device[:size].pretty.colorize :yellow}]? [y/N] "
+ answer = STDIN.gets.chomp
+
+ exit -1 unless answer.downcase == 'y'
+ device = File.join '/dev/', device[:path]
+ current = 0
+ chunk = Filesize.from('4MiB').to_i
+
+ File.open iso, 'rb' do |iso|
+ File.open device, 'wb' do |device|
+ while read = iso.read(chunk) do
+ current += read.size
+ device.write read
+ print "\r[%6.2f%%] %d/%d" % [current.to_f / size * 100, current, size]
+ end
+ puts
+ puts 'Finalizing...'.colorize :yellow
+ end
+ end
+ puts 'Done!'.colorize :green
+
+