mausb 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/mausb +209 -0
  3. data/lib/mausb.rb +110 -0
  4. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7e9b5d57df747993800d857f0b5fadc7865cef644af9e461c0d50d3413d355fa
4
+ data.tar.gz: 360661a1ed00ff410161fbed3515bbe30da21f13b592ee76f0bcb6f925108681
5
+ SHA512:
6
+ metadata.gz: fd938ab8404b7037a63d66dcb197fdeb39665dbe58a981d36b243a932dea1604ecc97702c7a916a4919ccd501cf0af65b3238eace35813d152a9d99c35507dda
7
+ data.tar.gz: 5c6a539a36386b4a331ea12b8f2dd64d0a614f84b8ec87261edadcd4cb1e649c854f0f946b11922f427bc62f02b840dc32e4c4840ce47f5a5d0738670439b81d
data/bin/mausb ADDED
@@ -0,0 +1,209 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright 2018 Ahmet Cetinkaya
4
+
5
+ # This file is part of Mausb.
6
+
7
+ # Mausb is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+
12
+ # Mausb is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Mausb. If not, see <http://www.gnu.org/licenses/>.
19
+
20
+
21
+ require 'mausb'
22
+
23
+ $mounted_usb_devices = Mausb::mounted_usb_devices
24
+ $unmounted_usb_devices = Mausb::unmounted_usb_devices
25
+ $mc = $mounted_usb_devices.length
26
+ $uc = $unmounted_usb_devices.length
27
+
28
+ def no_device
29
+ puts "There is currently no device connected to the computer."
30
+ end
31
+
32
+ def no_device_to_mount
33
+ puts "There is currently no device to mount."
34
+ end
35
+
36
+ def no_device_to_poweroff
37
+ puts "There is currently no device to power-off."
38
+ end
39
+
40
+ def no_device_to_unmount
41
+ puts "There is currently no device to unmount."
42
+ end
43
+
44
+ def direct_mount_or_ask_to_mount
45
+ if $uc == 1
46
+ direct_mount
47
+ else
48
+ ask_to_mount
49
+ end
50
+ end
51
+
52
+ def direct_mount
53
+ puts "Mounting #{$unmounted_usb_devices[0].link}."
54
+ Mausb::udisksctl_mount($unmounted_usb_devices[0].link)
55
+ end
56
+
57
+ def direct_poweroff_or_ask_to_poweroff
58
+ if $uc == 1
59
+ direct_poweroff
60
+ else
61
+ ask_to_poweroff
62
+ end
63
+ end
64
+
65
+ def direct_poweroff
66
+ puts "Powering off #{$unmounted_usb_devices[0].link}."
67
+ Mausb::udisksctl_poweroff($unmounted_usb_devices[0].link)
68
+ end
69
+
70
+ def get_one_of(choices, show_choices=nil, default=nil)
71
+ while true
72
+ if show_choices
73
+ puts choices.join(", ")
74
+ end
75
+ print "> "
76
+ str = STDIN.gets.strip
77
+ if choices.index(str)
78
+ return str
79
+ end
80
+ if default and str == ""
81
+ return default
82
+ end
83
+ end
84
+ end
85
+
86
+ def ask_to_mount
87
+ puts "Select device to mount: "
88
+ choices = (1..$unmounted_usb_devices.length).to_a.map{|n| n.to_s}
89
+ $unmounted_usb_devices.each.with_index do |usb_device, i|
90
+ default_text = ""
91
+ default_text = " [DEFAULT]" if i == 0
92
+ puts "#{choices[i]}) #{usb_device.info_text}#{default_text}"
93
+ end
94
+ index = get_one_of(choices, show_choices=nil, default="1").to_i - 1
95
+ puts "Mounting #{$unmounted_usb_devices[index].link}."
96
+ Mausb::udisksctl_mount($unmounted_usb_devices[index].link)
97
+ end
98
+
99
+ def ask_to_poweroff
100
+ puts "Select device to power-off: "
101
+ choices = (1..$unmounted_usb_devices.length).to_a.map{|n| n.to_s}
102
+ $unmounted_usb_devices.each.with_index do |usb_device, i|
103
+ default_text = ""
104
+ default_text = " [DEFAULT]" if i == 0
105
+ puts "#{choices[i]}) #{usb_device.info_text}#{default_text}"
106
+ end
107
+ index = get_one_of(choices, show_choices=nil, default="1").to_i - 1
108
+ puts "Powering off #{$unmounted_usb_devices[index].link}."
109
+ Mausb::udisksctl_poweroff($unmounted_usb_devices[index].link)
110
+ end
111
+
112
+ def direct_unmount_or_ask_to_unmount
113
+ if $mc == 1
114
+ direct_unmount
115
+ else
116
+ ask_to_unmount
117
+ end
118
+ end
119
+
120
+ def direct_unmount
121
+ puts "Unmounting #{$mounted_usb_devices[0].link}."
122
+ Mausb::udisksctl_unmount($mounted_usb_devices[0].link)
123
+ end
124
+
125
+ def ask_to_unmount
126
+ puts "Select device to unmount: "
127
+ choices = (1..$mounted_usb_devices.length).to_a.map{|n| n.to_s}
128
+ $mounted_usb_devices.each.with_index do |usb_device, i|
129
+ default_text = ""
130
+ default_text = " [DEFAULT]" if i == 0
131
+ puts "#{choices[i]}) #{usb_device.info_text}#{default_text}"
132
+ end
133
+ index = get_one_of(choices, show_choices=nil, default="1").to_i - 1
134
+ puts "Unmounting #{$mounted_usb_devices[index].link}."
135
+ Mausb::udisksctl_unmount($mounted_usb_devices[index].link)
136
+ end
137
+
138
+ def ask_what_to_do
139
+ puts "Mount (m) unmounted devices:"
140
+ $unmounted_usb_devices.each.with_index do |usb_device, i|
141
+ puts " #{usb_device.info_text}"
142
+ end
143
+ puts "Unmount (u) mounted devices:"
144
+ $mounted_usb_devices.each.with_index do |usb_device, i|
145
+ puts " #{usb_device.info_text}"
146
+ end
147
+ puts "Choose operation m (mount, DEFAULT) or u (unmount)."
148
+ op = get_one_of(["m", "u"], show_choices=nil, default="m")
149
+ if op == "m"
150
+ direct_mount_or_ask_to_mount
151
+ else
152
+ direct_unmount_or_ask_to_unmount
153
+ end
154
+ end
155
+
156
+ def print_info
157
+ if $uc > 0
158
+ puts "Unmounted devices:"
159
+ $unmounted_usb_devices.each.with_index do |usb_device, i|
160
+ puts " #{usb_device.info_text}"
161
+ end
162
+ end
163
+ if $mc > 0
164
+ puts "Mounted devices:"
165
+ $mounted_usb_devices.each.with_index do |usb_device, i|
166
+ puts " #{usb_device.info_text}"
167
+ end
168
+ end
169
+ end
170
+
171
+ if ARGV.length == 0
172
+ if $mc == 0
173
+ if $uc > 0
174
+ direct_mount_or_ask_to_mount
175
+ else
176
+ no_device
177
+ end
178
+ else
179
+ if $uc == 0
180
+ direct_unmount_or_ask_to_unmount
181
+ else
182
+ ask_what_to_do
183
+ end
184
+ end
185
+ else
186
+ if ARGV[0] == "m"
187
+ if $uc > 0
188
+ direct_mount_or_ask_to_mount
189
+ else
190
+ no_device_to_mount
191
+ end
192
+ elsif ARGV[0] == "p"
193
+ if $uc > 0
194
+ direct_poweroff_or_ask_to_poweroff
195
+ else
196
+ no_device_to_poweroff
197
+ end
198
+ elsif ARGV[0] == "u"
199
+ if $mc > 0
200
+ direct_unmount_or_ask_to_unmount
201
+ else
202
+ no_device_to_unmount
203
+ end
204
+ else
205
+ if $mc > 0 or $uc > 0
206
+ print_info
207
+ end
208
+ end
209
+ end
data/lib/mausb.rb ADDED
@@ -0,0 +1,110 @@
1
+ # Copyright 2018 Ahmet Cetinkaya
2
+
3
+ # This file is part of Mausb.
4
+
5
+ # Mausb is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # Mausb is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with Mausb. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ module Mausb
19
+ class Mausb::USBDevice
20
+ attr_reader :id
21
+
22
+ def initialize(id)
23
+ @id = id
24
+ end
25
+
26
+ def link
27
+ @link = `readlink -f #{@id}`.strip unless @link
28
+ @link
29
+ end
30
+
31
+ def is_mounted?
32
+ Mausb.df.find_all{|l|
33
+ l[0] == link
34
+ }.length > 0
35
+ end
36
+
37
+ def info_text
38
+ keys = ["Id", "IdLabel"]
39
+ ui = Mausb.udisksctl_info(@id)
40
+ link + " (" + keys.map{|key| "#{key}: #{ui[key]}"}.join(", ") + ")"
41
+ end
42
+
43
+ def to_s
44
+ "Id: #{@id}, Link: #{link}"
45
+ end
46
+ end
47
+
48
+ def Mausb.usb_ids
49
+ folder = "/dev/disk/by-id"
50
+ ids = Dir.entries("/dev/disk/by-id").find_all{|entry|
51
+ entry[0..3] == "usb-" or entry[0..3] == "ata-"
52
+ }
53
+ ids.find_all{|id|
54
+ ids.find_all{|id2|
55
+ id2.index(id)
56
+ }.length == 1
57
+ }.map do |id|
58
+ File.join(folder, id)
59
+ end
60
+ end
61
+
62
+ def Mausb.usb_devices
63
+ Mausb.usb_ids.map do |id|
64
+ Mausb::USBDevice.new(id)
65
+ end
66
+ end
67
+
68
+ def Mausb.mounted_usb_devices
69
+ Mausb.usb_devices.find_all do |usb_device|
70
+ usb_device.is_mounted?
71
+ end
72
+ end
73
+
74
+ def Mausb.unmounted_usb_devices
75
+ Mausb.usb_devices.find_all do |usb_device|
76
+ not usb_device.is_mounted?
77
+ end
78
+ end
79
+
80
+ def Mausb.df
81
+ `df`.split("\n").drop(1).map do |line|
82
+ line.split(/\s+/)
83
+ end
84
+ end
85
+
86
+ def Mausb.udisksctl_info(id)
87
+ h = Hash.new
88
+ `udisksctl info -b #{id}`.split("\n").find_all{|line|
89
+ line =~ /\s+[A-Za-z]+:.+$/
90
+ }.each do |line|
91
+ mda = /\s+([A-Za-z]+):(.+)$/.match(line).to_a
92
+ key = mda[1]
93
+ value = mda[2].strip
94
+ h[key] = value
95
+ end
96
+ h
97
+ end
98
+
99
+ def Mausb.udisksctl_mount(link)
100
+ `udisksctl mount -b #{link}`
101
+ end
102
+
103
+ def Mausb.udisksctl_unmount(link)
104
+ `udisksctl unmount -b #{link}`
105
+ end
106
+
107
+ def Mausb.udisksctl_poweroff(link)
108
+ `udisksctl power-off -b #{link}`
109
+ end
110
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mausb
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ platform: ruby
6
+ authors:
7
+ - Ahmet Cetinkaya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: mausb is a command line tool for managing (mount/unmount/list) usb storage
14
+ devices.
15
+ email:
16
+ - cetinkayaahmet@yahoo.com
17
+ executables:
18
+ - mausb
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - bin/mausb
23
+ - lib/mausb.rb
24
+ homepage: https://github.org/cetinkaya/mausb
25
+ licenses:
26
+ - GPL-3.0
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.3.25
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Manage (mount/unmount/list) usb storage devices.
47
+ test_files: []