mountpoints 1.1.3 → 1.2.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.
- checksums.yaml +4 -4
- data/README.md +51 -12
- data/bin/mountpoints +7 -0
- data/doc/README.gen +51 -13
- data/lib/mountpoints/class/mountpoints.rb +425 -0
- data/lib/mountpoints/constants/constants.rb +19 -0
- data/lib/mountpoints/toplevel_methods/toplevel_methods.rb +92 -0
- data/lib/mountpoints/version/version.rb +1 -1
- data/lib/mountpoints.rb +1 -1
- data/mountpoints.gemspec +21 -27
- data/test/testing_mountpoints.rb +5 -2
- metadata +26 -27
- data/USAGE.md +0 -14
- data/lib/mountpoints/misc.rb +0 -146
- data/lib/mountpoints/mountpoints.rb +0 -270
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b0855050bed99b01644fa83a4ea8f15330234b30c6a79622968a1c07f4b5a71
|
4
|
+
data.tar.gz: 4970dd1d922f8e42ebc43e1912025d13512978c8604a7d9d3f5c525ab4738000
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef6336f27b1a1883fdd5881ea16d13a9f32b2e3eaf4ba868451fcac4d47ede3972d86471fb1fadd773021e684850aad2765af6d33c258d6537f5c380b209fbe7
|
7
|
+
data.tar.gz: 297b56feeab21918d8707c589ce6b5c16c169b7b14328c8ca384f5a247f8829ab4731be73fde617fd7df16bf38a3c307d9511812d03e76e80981c94de1554b6f
|
data/README.md
CHANGED
@@ -2,37 +2,76 @@
|
|
2
2
|
[](https://www.ruby-lang.org/en/)
|
3
3
|
[](https://badge.fury.io/rb/mountpoints)
|
4
4
|
|
5
|
-
This gem was <b>last updated</b> on the <span style="color: darkblue; font-weight: bold">15.05.2022</span> (dd.mm.yyyy notation), at <span style="color: steelblue; font-weight: bold">
|
5
|
+
This gem was <b>last updated</b> on the <span style="color: darkblue; font-weight: bold">15.05.2022</span> (dd.mm.yyyy notation), at <span style="color: steelblue; font-weight: bold">23:56:46</span> o'clock.
|
6
6
|
|
7
|
-
|
7
|
+
<img src="https://i.imgur.com/Oi4pNNi.png" style="margin: 1em">
|
8
8
|
|
9
|
-
|
10
|
-
USB devices.
|
9
|
+
## The goals for the mountpoints project
|
11
10
|
|
12
|
-
|
11
|
+
The mountpoints project shall simply display the available mountpoints
|
12
|
+
for USB devices. This should ideally work on Linux as well on Windows -
|
13
|
+
and, if possible, on other operating systems as well.
|
14
|
+
|
15
|
+
## Simple usage example
|
16
|
+
|
17
|
+
Consider using the mountpoints gem like this, after installation:
|
13
18
|
|
14
19
|
require 'mountpoints'
|
15
|
-
pp Mountpoints[] # This will return an Array, which contains the mounted devices.
|
20
|
+
pp Mountpoints[] # This will (quietly) return an Array, which contains the mounted devices.
|
21
|
+
|
22
|
+
Alternative you can use it from the commandline, in which case it
|
23
|
+
will report to you (the user) what it has found, such as "Discovered
|
24
|
+
mountpoint /mnt/usb1/." or something like that.
|
16
25
|
|
17
26
|
## Commandline flags
|
18
27
|
|
19
28
|
The flag **--extra-verbose** can be used to report more information about
|
20
|
-
the
|
29
|
+
the mountpoints (on **Linux**).
|
30
|
+
|
31
|
+
## Storing the mountpoints internally
|
32
|
+
|
33
|
+
The current policy is to store all mountpoints as **directories**, that
|
34
|
+
is via a trailing **/**. So if the mountpoint is normally **/Mount/USB1**
|
35
|
+
then this will be stored as **/Mount/USB1/**.
|
36
|
+
|
37
|
+
The reason I document this here is so that I can refer to this policy
|
38
|
+
in case anything changes - and if this document is updated. As of **May
|
39
|
+
2022 this is the case**. This may be subject to change one day, but
|
40
|
+
I expect this to remain as long as the mountpoints gem will be made
|
41
|
+
available here on **rubygems.org**.
|
21
42
|
|
22
43
|
## Using the project on windows
|
23
44
|
|
24
|
-
On
|
45
|
+
On **Linux** we can simply use "**fdisk -l**" and this should work
|
46
|
+
fine. If **fdisk** is not available then **df** may suffice. (Since
|
47
|
+
as of the rewrite in **May 2022** the mountpoints gem now defaults
|
48
|
+
to df on linux, actually.)
|
25
49
|
|
26
|
-
On Windows we have to use another way. I picked **wmic**.
|
50
|
+
On **Windows** we have to use another way. I picked **wmic**.
|
27
51
|
|
28
|
-
|
52
|
+
Usage example for **wmic**:
|
29
53
|
|
30
54
|
wmic logicaldisk where "drivetype=3" get DeviceID
|
31
55
|
|
32
56
|
This then gets parsed and the last line is assumed to be the
|
33
57
|
currently inserted USB harddisc. This evidently may break for
|
34
|
-
many use cases,
|
35
|
-
|
58
|
+
many use cases, in particular when there are several USB
|
59
|
+
devices mount, which is why I document this here - but for
|
60
|
+
my own simple use cases this actually works, and I verified
|
61
|
+
that this indeed does work for simple use cases.
|
62
|
+
|
63
|
+
In **May 2022* two ad-hoc methods were added called:
|
64
|
+
|
65
|
+
Mountpoints.windows1
|
66
|
+
Mountpoints.windows2
|
67
|
+
|
68
|
+
These will run the corresponding wmic commands via `` backticks
|
69
|
+
as well as output the command that is run.
|
70
|
+
|
71
|
+
Regular users won't really need to run this, but in the event
|
72
|
+
that something isn't right you can use these two methods
|
73
|
+
conveniently. It's mostly for debugging, though, so most users
|
74
|
+
will not need to run this.
|
36
75
|
|
37
76
|
|
38
77
|
## Contact information
|
data/bin/mountpoints
ADDED
data/doc/README.gen
CHANGED
@@ -1,34 +1,72 @@
|
|
1
|
-
|
2
|
-
ADD_MAIN_TIMESTAMP
|
1
|
+
ADD_RUBY_LOGO_AND_TIMESTAMP
|
3
2
|
|
4
|
-
|
3
|
+
<img src="https://i.imgur.com/Oi4pNNi.png" style="margin: 1em">
|
5
4
|
|
6
|
-
|
7
|
-
USB devices.
|
5
|
+
## The goals for the mountpoints project
|
8
6
|
|
9
|
-
|
7
|
+
The mountpoints project shall simply display the available mountpoints
|
8
|
+
for USB devices. This should ideally work on Linux as well on Windows -
|
9
|
+
and, if possible, on other operating systems as well.
|
10
|
+
|
11
|
+
## Simple usage example
|
12
|
+
|
13
|
+
Consider using the mountpoints gem like this, after installation:
|
10
14
|
|
11
15
|
require 'mountpoints'
|
12
|
-
pp Mountpoints[] # This will return an Array, which contains the mounted devices.
|
16
|
+
pp Mountpoints[] # This will (quietly) return an Array, which contains the mounted devices.
|
17
|
+
|
18
|
+
Alternative you can use it from the commandline, in which case it
|
19
|
+
will report to you (the user) what it has found, such as "Discovered
|
20
|
+
mountpoint /mnt/usb1/." or something like that.
|
13
21
|
|
14
22
|
## Commandline flags
|
15
23
|
|
16
24
|
The flag **--extra-verbose** can be used to report more information about
|
17
|
-
the
|
25
|
+
the mountpoints (on **Linux**).
|
26
|
+
|
27
|
+
## Storing the mountpoints internally
|
28
|
+
|
29
|
+
The current policy is to store all mountpoints as **directories**, that
|
30
|
+
is via a trailing **/**. So if the mountpoint is normally **/Mount/USB1**
|
31
|
+
then this will be stored as **/Mount/USB1/**.
|
32
|
+
|
33
|
+
The reason I document this here is so that I can refer to this policy
|
34
|
+
in case anything changes - and if this document is updated. As of **May
|
35
|
+
2022 this is the case**. This may be subject to change one day, but
|
36
|
+
I expect this to remain as long as the mountpoints gem will be made
|
37
|
+
available here on **rubygems.org**.
|
18
38
|
|
19
39
|
## Using the project on windows
|
20
40
|
|
21
|
-
On
|
41
|
+
On **Linux** we can simply use "**fdisk -l**" and this should work
|
42
|
+
fine. If **fdisk** is not available then **df** may suffice. (Since
|
43
|
+
as of the rewrite in **May 2022** the mountpoints gem now defaults
|
44
|
+
to df on linux, actually.)
|
22
45
|
|
23
|
-
On Windows we have to use another way. I picked **wmic**.
|
46
|
+
On **Windows** we have to use another way. I picked **wmic**.
|
24
47
|
|
25
|
-
|
48
|
+
Usage example for **wmic**:
|
26
49
|
|
27
50
|
wmic logicaldisk where "drivetype=3" get DeviceID
|
28
51
|
|
29
52
|
This then gets parsed and the last line is assumed to be the
|
30
53
|
currently inserted USB harddisc. This evidently may break for
|
31
|
-
many use cases,
|
32
|
-
|
54
|
+
many use cases, in particular when there are several USB
|
55
|
+
devices mount, which is why I document this here - but for
|
56
|
+
my own simple use cases this actually works, and I verified
|
57
|
+
that this indeed does work for simple use cases.
|
58
|
+
|
59
|
+
In **May 2022* two ad-hoc methods were added called:
|
60
|
+
|
61
|
+
Mountpoints.windows1
|
62
|
+
Mountpoints.windows2
|
63
|
+
|
64
|
+
These will run the corresponding wmic commands via `` backticks
|
65
|
+
as well as output the command that is run.
|
66
|
+
|
67
|
+
Regular users won't really need to run this, but in the event
|
68
|
+
that something isn't right you can use these two methods
|
69
|
+
conveniently. It's mostly for debugging, though, so most users
|
70
|
+
will not need to run this.
|
33
71
|
|
34
72
|
ADD_CONTACT_INFORMATION
|
@@ -0,0 +1,425 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# === Mountpoints
|
6
|
+
#
|
7
|
+
# The primary objective of this class is to scan for all mounted USB-devices.
|
8
|
+
#
|
9
|
+
# Note that toplevel-methods, that is in the form of Mountpoints.foobar(),
|
10
|
+
# will be stored in another .rb file, aptly named "toplevel_methods.rb".
|
11
|
+
#
|
12
|
+
# Colours may be used on the commandline if the .report() method is
|
13
|
+
# called and if use_colours? returns true (which is the default).
|
14
|
+
#
|
15
|
+
# Usage examples:
|
16
|
+
#
|
17
|
+
# Mountpoints.new(ARGV)
|
18
|
+
# Mountpoints.new
|
19
|
+
# Mountpoints.show
|
20
|
+
# Mountpoints.is_any_mountpoint_available?
|
21
|
+
#
|
22
|
+
# =========================================================================== #
|
23
|
+
# require 'mountpoints/class/mountpoints.rb'
|
24
|
+
# =========================================================================== #
|
25
|
+
class Mountpoints # === Mountpoints
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'colours'
|
29
|
+
rescue LoadError; end
|
30
|
+
|
31
|
+
begin
|
32
|
+
require 'opn'
|
33
|
+
rescue LoadError; end
|
34
|
+
|
35
|
+
require 'mountpoints/constants/constants.rb'
|
36
|
+
require 'mountpoints/toplevel_methods/toplevel_methods.rb'
|
37
|
+
require 'mountpoints/version/version.rb'
|
38
|
+
|
39
|
+
# ========================================================================= #
|
40
|
+
# === NAMESPACE
|
41
|
+
# ========================================================================= #
|
42
|
+
NAMESPACE = inspect
|
43
|
+
|
44
|
+
# ========================================================================= #
|
45
|
+
# === N
|
46
|
+
# ========================================================================= #
|
47
|
+
N = "\n"
|
48
|
+
|
49
|
+
# ========================================================================= #
|
50
|
+
# === LJUST
|
51
|
+
# ========================================================================= #
|
52
|
+
LJUST = 15
|
53
|
+
|
54
|
+
# ========================================================================= #
|
55
|
+
# === DF_COMMAND
|
56
|
+
# ========================================================================= #
|
57
|
+
DF_COMMAND = 'df -T -ah 2>&1'
|
58
|
+
|
59
|
+
# ========================================================================= #
|
60
|
+
# === DEBUG
|
61
|
+
#
|
62
|
+
# If true then we will debug this class.
|
63
|
+
# ========================================================================= #
|
64
|
+
DEBUG = false
|
65
|
+
|
66
|
+
# ========================================================================= #
|
67
|
+
# === initialize
|
68
|
+
# ========================================================================= #
|
69
|
+
def initialize(
|
70
|
+
commandline_arguments = nil,
|
71
|
+
run_already = true
|
72
|
+
)
|
73
|
+
reset
|
74
|
+
set_commandline_arguments(
|
75
|
+
commandline_arguments
|
76
|
+
)
|
77
|
+
if block_given?
|
78
|
+
yielded = yield
|
79
|
+
case yielded
|
80
|
+
# ===================================================================== #
|
81
|
+
# === :be_quiet
|
82
|
+
# ===================================================================== #
|
83
|
+
when :be_quiet
|
84
|
+
set_be_quiet
|
85
|
+
run_already = true
|
86
|
+
# ===================================================================== #
|
87
|
+
# === :be_verbose
|
88
|
+
# ===================================================================== #
|
89
|
+
when :be_verbose,
|
90
|
+
:be_extra_verbose
|
91
|
+
set_be_verbose
|
92
|
+
run_already = true
|
93
|
+
# ===================================================================== #
|
94
|
+
# === :normal_verbosity
|
95
|
+
# ===================================================================== #
|
96
|
+
when :normal_verbosity # Simply pass through here. No longer that important as of 2022, though.
|
97
|
+
end
|
98
|
+
end
|
99
|
+
if commandline_arguments.is_a? Array
|
100
|
+
case commandline_arguments.first
|
101
|
+
# ===================================================================== #
|
102
|
+
# === --extra-verbose
|
103
|
+
# ===================================================================== #
|
104
|
+
when /^-?-?extra(-|_)?verbose$/
|
105
|
+
set_be_verbose
|
106
|
+
run_already = true
|
107
|
+
end
|
108
|
+
end
|
109
|
+
run if run_already
|
110
|
+
end
|
111
|
+
|
112
|
+
# ========================================================================= #
|
113
|
+
# === reset (reset tag)
|
114
|
+
# ========================================================================= #
|
115
|
+
def reset
|
116
|
+
# ======================================================================= #
|
117
|
+
# === @namespace
|
118
|
+
# ======================================================================= #
|
119
|
+
@namespace = NAMESPACE
|
120
|
+
# ======================================================================= #
|
121
|
+
# === @internal_hash
|
122
|
+
# ======================================================================= #
|
123
|
+
@internal_hash = {}
|
124
|
+
# ======================================================================= #
|
125
|
+
# === :array_available_mountpoints
|
126
|
+
# ======================================================================= #
|
127
|
+
@internal_hash[:array_available_mountpoints] = []
|
128
|
+
# ======================================================================= #
|
129
|
+
# === :use_colours
|
130
|
+
# ======================================================================= #
|
131
|
+
@internal_hash[:use_colours] = true
|
132
|
+
# ======================================================================= #
|
133
|
+
# === :be_verbose
|
134
|
+
# ======================================================================= #
|
135
|
+
@internal_hash[:be_verbose] = true
|
136
|
+
# ======================================================================= #
|
137
|
+
# === :command_to_run
|
138
|
+
#
|
139
|
+
# This is the command that will be run via ``.
|
140
|
+
# ======================================================================= #
|
141
|
+
@internal_hash[:command_to_run] = ''.dup
|
142
|
+
end
|
143
|
+
|
144
|
+
# ========================================================================= #
|
145
|
+
# === set_command_to_run
|
146
|
+
# ========================================================================= #
|
147
|
+
def set_command_to_run(
|
148
|
+
i = :default_command_on_linux
|
149
|
+
)
|
150
|
+
case i
|
151
|
+
# ======================================================================= #
|
152
|
+
# === :default_command_on_linux
|
153
|
+
# ======================================================================= #
|
154
|
+
when :default_command_on_linux,
|
155
|
+
:default
|
156
|
+
i = DF_COMMAND
|
157
|
+
end
|
158
|
+
@internal_hash[:command_to_run] = i
|
159
|
+
end
|
160
|
+
|
161
|
+
# ========================================================================= #
|
162
|
+
# === run_the_system_command
|
163
|
+
# ========================================================================= #
|
164
|
+
def run_the_system_command
|
165
|
+
return `#{command_to_run?}`
|
166
|
+
end
|
167
|
+
|
168
|
+
# ========================================================================= #
|
169
|
+
# === command_to_run?
|
170
|
+
# ========================================================================= #
|
171
|
+
def command_to_run?
|
172
|
+
@internal_hash[:command_to_run]
|
173
|
+
end
|
174
|
+
|
175
|
+
# ========================================================================= #
|
176
|
+
# === set_be_quiet
|
177
|
+
# ========================================================================= #
|
178
|
+
def set_be_quiet
|
179
|
+
@internal_hash[:be_verbose] = false
|
180
|
+
end
|
181
|
+
|
182
|
+
# ========================================================================= #
|
183
|
+
# === set_be_verbose
|
184
|
+
# ========================================================================= #
|
185
|
+
def set_be_verbose
|
186
|
+
@internal_hash[:be_verbose] = true
|
187
|
+
end
|
188
|
+
|
189
|
+
# ========================================================================= #
|
190
|
+
# === set_commandline_arguments
|
191
|
+
# ========================================================================= #
|
192
|
+
def set_commandline_arguments(i = '')
|
193
|
+
i = [i].flatten.compact
|
194
|
+
@commandline_arguments = i
|
195
|
+
end
|
196
|
+
|
197
|
+
# ========================================================================= #
|
198
|
+
# === commandline_arguments?
|
199
|
+
# ========================================================================= #
|
200
|
+
def commandline_arguments?
|
201
|
+
@commandline_arguments
|
202
|
+
end
|
203
|
+
|
204
|
+
# ========================================================================= #
|
205
|
+
# === first_argument?
|
206
|
+
# ========================================================================= #
|
207
|
+
def first_argument?
|
208
|
+
@commandline_arguments.first
|
209
|
+
end; alias first? first_argument? # === first?
|
210
|
+
|
211
|
+
# ========================================================================= #
|
212
|
+
# === available_mountpoints?
|
213
|
+
# ========================================================================= #
|
214
|
+
def available_mountpoints?
|
215
|
+
@internal_hash[:array_available_mountpoints]
|
216
|
+
end; alias mountpoints? available_mountpoints? # === mountpoints?
|
217
|
+
alias array? available_mountpoints? # === array?
|
218
|
+
alias array_mountpoints? available_mountpoints? # === array_mountpoints?
|
219
|
+
alias array_mountpoints available_mountpoints? # === array_mountpoints
|
220
|
+
alias mounted_here available_mountpoints? # === mounted_here
|
221
|
+
alias result? available_mountpoints? # === result?
|
222
|
+
alias result available_mountpoints? # === result
|
223
|
+
|
224
|
+
# ========================================================================= #
|
225
|
+
# === add_this_entry_to_the_available_mountpoints (add tag)
|
226
|
+
# ========================================================================= #
|
227
|
+
def add_this_entry_to_the_available_mountpoints(i)
|
228
|
+
unless i == '/' # Ignore the root entry here.
|
229
|
+
unless i.end_with? '/'
|
230
|
+
i = i.dup if i.frozen?
|
231
|
+
i << '/'
|
232
|
+
end
|
233
|
+
@internal_hash[:array_available_mountpoints] << i
|
234
|
+
end
|
235
|
+
end; alias add add_this_entry_to_the_available_mountpoints # === add
|
236
|
+
|
237
|
+
# ========================================================================= #
|
238
|
+
# === e
|
239
|
+
# ========================================================================= #
|
240
|
+
def e(i = '')
|
241
|
+
if use_colours?
|
242
|
+
::Colours.e(i)
|
243
|
+
else
|
244
|
+
puts i
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
# ========================================================================= #
|
249
|
+
# === debug?
|
250
|
+
# ========================================================================= #
|
251
|
+
def debug?
|
252
|
+
DEBUG
|
253
|
+
end
|
254
|
+
|
255
|
+
# ========================================================================= #
|
256
|
+
# === rds
|
257
|
+
# ========================================================================= #
|
258
|
+
def rds(i)
|
259
|
+
i.squeeze('/')
|
260
|
+
end
|
261
|
+
|
262
|
+
# ========================================================================= #
|
263
|
+
# === use_colours?
|
264
|
+
# ========================================================================= #
|
265
|
+
def use_colours?
|
266
|
+
@internal_hash[:use_colours]
|
267
|
+
end
|
268
|
+
|
269
|
+
# ========================================================================= #
|
270
|
+
# === be_verbose?
|
271
|
+
# ========================================================================= #
|
272
|
+
def be_verbose?
|
273
|
+
@internal_hash[:be_verbose]
|
274
|
+
end; alias be_extra_verbose? be_verbose? # === be_extra_verbose?
|
275
|
+
|
276
|
+
# ========================================================================= #
|
277
|
+
# === report_the_determined_mountpoints (report tag)
|
278
|
+
# ========================================================================= #
|
279
|
+
def report_the_determined_mountpoints(
|
280
|
+
available_mountpoints = available_mountpoints?,
|
281
|
+
be_verbose = be_verbose?
|
282
|
+
)
|
283
|
+
if be_verbose
|
284
|
+
if available_mountpoints.empty?
|
285
|
+
opn; e 'No mount point was found. Thus, it is assumed '\
|
286
|
+
'that no USB device is currently mounted.'
|
287
|
+
else # else report the mountpoints here.
|
288
|
+
opn; e 'The following mount points were found:'
|
289
|
+
e
|
290
|
+
available_mountpoints.each {|entry|
|
291
|
+
e " #{forestgreen(entry)}".ljust(LJUST)
|
292
|
+
}
|
293
|
+
e
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end; alias report report_the_determined_mountpoints # === report
|
297
|
+
alias report_colourized_result report_the_determined_mountpoints # === report_colourized_result
|
298
|
+
alias report_result report_the_determined_mountpoints # === report_result
|
299
|
+
alias determine_result report_the_determined_mountpoints # === determine_result
|
300
|
+
|
301
|
+
# ========================================================================= #
|
302
|
+
# === forestgreen
|
303
|
+
# ========================================================================= #
|
304
|
+
def forestgreen(i)
|
305
|
+
return ::Colours.forestgreen(i) if use_colours?
|
306
|
+
return i
|
307
|
+
end
|
308
|
+
|
309
|
+
# ========================================================================= #
|
310
|
+
# === determine_the_available_mountpoints
|
311
|
+
#
|
312
|
+
# This method will branch into a linux-specific clause and a
|
313
|
+
# windows-specific clause.
|
314
|
+
# ========================================================================= #
|
315
|
+
def determine_the_available_mountpoints
|
316
|
+
clear_the_mountpoints # First clear the mountpoints - we don't want old entries.
|
317
|
+
# ======================================================================= #
|
318
|
+
# === Handle windows next
|
319
|
+
# ======================================================================= #
|
320
|
+
if use_windows?
|
321
|
+
determine_the_mountpoints_on_windows
|
322
|
+
# ======================================================================= #
|
323
|
+
# === Handle linux next
|
324
|
+
# ======================================================================= #
|
325
|
+
else # This branch is specifically for linux
|
326
|
+
determine_the_mountpoints_on_linux
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
# ========================================================================= #
|
331
|
+
# === determine_the_mountpoints_on_linux (linux tag, linux branch)
|
332
|
+
# ========================================================================= #
|
333
|
+
def determine_the_mountpoints_on_linux
|
334
|
+
set_command_to_run(:default_command_on_linux)
|
335
|
+
if debug?
|
336
|
+
e "Now running this command: #{sfancy(command_to_run?)}"
|
337
|
+
end
|
338
|
+
result = run_the_system_command.split(N)
|
339
|
+
# ======================================================================= #
|
340
|
+
# Next parse it for results such as:
|
341
|
+
#
|
342
|
+
# /dev/sdc1 2048 3907029134 3907027087 1.8T Linux filesystem
|
343
|
+
# /dev/sdc1 459G 436G 0 100% /Mount/USB3
|
344
|
+
#
|
345
|
+
# This stands for:
|
346
|
+
#
|
347
|
+
# Device Start End Sectors Size Type
|
348
|
+
#
|
349
|
+
# ======================================================================= #
|
350
|
+
matches = result.select {|entry| entry =~ /^\/dev\// } # Or check for: '/dev/sd'
|
351
|
+
matches.each {|line|
|
352
|
+
splitted = line.split(' ').map(&:strip)
|
353
|
+
last_entry = splitted.last.strip
|
354
|
+
add_this_entry_to_the_available_mountpoints(last_entry)
|
355
|
+
}
|
356
|
+
end
|
357
|
+
|
358
|
+
# ========================================================================= #
|
359
|
+
# === set_mountpoints
|
360
|
+
#
|
361
|
+
# Note that all directories will be stored via a trailing '/' in this
|
362
|
+
# method.
|
363
|
+
# ========================================================================= #
|
364
|
+
def set_mountpoints(i)
|
365
|
+
i = [i].flatten.compact
|
366
|
+
i.map! {|entry|
|
367
|
+
if File.directory? entry
|
368
|
+
entry = rds(entry)
|
369
|
+
end
|
370
|
+
if File.directory?(entry) and !entry.end_with?('/')
|
371
|
+
entry << '/'
|
372
|
+
end
|
373
|
+
entry
|
374
|
+
}
|
375
|
+
@internal_hash[:array_mountpoints] = i
|
376
|
+
end
|
377
|
+
|
378
|
+
# ========================================================================= #
|
379
|
+
# === clear_the_mountpoints
|
380
|
+
# ========================================================================= #
|
381
|
+
def clear_the_mountpoints
|
382
|
+
@internal_hash[:array_available_mountpoints].clear
|
383
|
+
end
|
384
|
+
|
385
|
+
# ========================================================================= #
|
386
|
+
# === use_windows?
|
387
|
+
# ========================================================================= #
|
388
|
+
def use_windows?
|
389
|
+
return Mountpoints.use_windows?
|
390
|
+
end; alias is_on_windows? use_windows? # === is_on_windows?
|
391
|
+
|
392
|
+
# ========================================================================= #
|
393
|
+
# === determine_the_mountpoints_on_windows
|
394
|
+
# ========================================================================= #
|
395
|
+
def determine_the_mountpoints_on_windows
|
396
|
+
result = ''.dup
|
397
|
+
set_command_to_run(WINDOWS_COMMAND1)
|
398
|
+
result << `#{command_to_run?}`.split(N).reject {|line| line.to_s.strip.empty? }.join(N) # This will keep the main result.
|
399
|
+
set_command_to_run(WINDOWS_COMMAND2)
|
400
|
+
result << `#{command_to_run?}`.split(N).reject {|line| line.to_s.strip.empty? }.join(N) # This will keep the main result.
|
401
|
+
result = result.split(N).reverse.select {|entry|
|
402
|
+
entry.include? ':'
|
403
|
+
}.map {|entry|
|
404
|
+
entry.strip
|
405
|
+
}.reject {|entry| entry.include? 'DeviceID' } .uniq
|
406
|
+
set_mountpoints(result)
|
407
|
+
end
|
408
|
+
|
409
|
+
# ========================================================================= #
|
410
|
+
# === run (run tag)
|
411
|
+
# ========================================================================= #
|
412
|
+
def run
|
413
|
+
determine_the_available_mountpoints
|
414
|
+
report_the_determined_mountpoints
|
415
|
+
end
|
416
|
+
|
417
|
+
end
|
418
|
+
|
419
|
+
if __FILE__ == $PROGRAM_NAME
|
420
|
+
Mountpoints.new(ARGV)
|
421
|
+
# To query the mountpoints do:
|
422
|
+
#
|
423
|
+
# pp _.mountpoints?
|
424
|
+
#
|
425
|
+
end # rawmountpoints
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# require 'mountpoints/constants/constants.rb'
|
6
|
+
# =========================================================================== #
|
7
|
+
class Mountpoints
|
8
|
+
|
9
|
+
# ========================================================================= #
|
10
|
+
# === WINDOWS_COMMAND1
|
11
|
+
# ========================================================================= #
|
12
|
+
WINDOWS_COMMAND1 = 'wmic logicaldisk where "drivetype=3" get DeviceID'
|
13
|
+
|
14
|
+
# ========================================================================= #
|
15
|
+
# === WINDOWS_COMMAND2
|
16
|
+
# ========================================================================= #
|
17
|
+
WINDOWS_COMMAND2 = 'wmic logicaldisk where "drivetype=2" get DeviceID'
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# Encoding: UTF-8
|
3
|
+
# frozen_string_literal: true
|
4
|
+
# =========================================================================== #
|
5
|
+
# require 'mountpoints/toplevel_methods/toplevel_methods.rb'
|
6
|
+
# =========================================================================== #
|
7
|
+
class Mountpoints # === Mountpoints
|
8
|
+
|
9
|
+
require 'mountpoints/constants/constants.rb'
|
10
|
+
|
11
|
+
# ========================================================================= #
|
12
|
+
# === Mountpoints.use_windows?
|
13
|
+
#
|
14
|
+
# This boolean method will return true if we are on windows, and
|
15
|
+
# false otherwise.
|
16
|
+
# ========================================================================= #
|
17
|
+
def self.use_windows?
|
18
|
+
case RUBY_PLATFORM
|
19
|
+
when /win/,
|
20
|
+
/mingw/,
|
21
|
+
/msys/
|
22
|
+
return true
|
23
|
+
else
|
24
|
+
return false
|
25
|
+
end
|
26
|
+
end; self.instance_eval { alias is_on_windows? use_windows? } # === Mountpoints.is_on_windows?
|
27
|
+
|
28
|
+
# ========================================================================= #
|
29
|
+
# === Mountpoints[]
|
30
|
+
#
|
31
|
+
# This method will return an Array, such as:
|
32
|
+
#
|
33
|
+
# ["/Mount/USB1/"]
|
34
|
+
#
|
35
|
+
# It will be quiet by default.
|
36
|
+
|
37
|
+
# Usage example:
|
38
|
+
#
|
39
|
+
# x = Mountpoints # => ["/Mount/USB3/"]
|
40
|
+
#
|
41
|
+
# ========================================================================= #
|
42
|
+
def self.[](i = ARGV)
|
43
|
+
new(i) { :be_quiet }.array_mountpoints # The Array will be returned as well.
|
44
|
+
end
|
45
|
+
|
46
|
+
# ========================================================================= #
|
47
|
+
# === Mountpoints.array?
|
48
|
+
#
|
49
|
+
# This variant will only return the mountpoints, as Array.
|
50
|
+
# ========================================================================= #
|
51
|
+
def self.array?(i = ARGV)
|
52
|
+
new(i) { :be_quiet }.array?
|
53
|
+
end; self.instance_eval { alias mountpoints_as_array array? } # === Mountpoints.mountpoints_as_array
|
54
|
+
self.instance_eval { alias array_mountpoints array? } # === Mountpoints.mountpoints_as_array
|
55
|
+
self.instance_eval { alias mountpoints? array? } # === Mountpoints.mountpoints_as_array
|
56
|
+
|
57
|
+
# ========================================================================= #
|
58
|
+
# === Mountpoints.report
|
59
|
+
# ========================================================================= #
|
60
|
+
def self.report
|
61
|
+
new(i) { :be_verbose }
|
62
|
+
end; self.instance_eval { alias show report } # === Mountpoints.show
|
63
|
+
|
64
|
+
# ========================================================================= #
|
65
|
+
# === Mountpoints.is_any_mountpoint_available?
|
66
|
+
# ========================================================================= #
|
67
|
+
def self.is_any_mountpoint_available?
|
68
|
+
!new { :be_quiet }.array_mountpoints.empty?
|
69
|
+
end
|
70
|
+
|
71
|
+
# ========================================================================= #
|
72
|
+
# === Mountpoints.windows1
|
73
|
+
# ========================================================================= #
|
74
|
+
def self.windows1(cmd_to_run = WINDOWS_COMMAND1)
|
75
|
+
e cmd_to_run
|
76
|
+
`#{cmd_to_run}`
|
77
|
+
end
|
78
|
+
|
79
|
+
# ========================================================================= #
|
80
|
+
# === Mountpoints.windows2
|
81
|
+
# ========================================================================= #
|
82
|
+
def self.windows2(cmd_to_run = WINDOWS_COMMAND2)
|
83
|
+
e cmd_to_run
|
84
|
+
`#{cmd_to_run}`
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
if __FILE__ == $PROGRAM_NAME
|
90
|
+
require 'mountpoints/class/mountpoints.rb'
|
91
|
+
pp Mountpoints.array?
|
92
|
+
end # rawmountpoints
|
@@ -7,7 +7,7 @@ class Mountpoints
|
|
7
7
|
# ========================================================================= #
|
8
8
|
# === Mountpoints::VERSION
|
9
9
|
# ========================================================================= #
|
10
|
-
VERSION = '1.
|
10
|
+
VERSION = '1.2.3'
|
11
11
|
|
12
12
|
# ========================================================================= #
|
13
13
|
# === Mountpoints::LAST_UPDATE
|
data/lib/mountpoints.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'mountpoints/mountpoints.rb'
|
1
|
+
require 'mountpoints/class/mountpoints.rb'
|
data/mountpoints.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# Gemspec for Project Mountpoints.
|
3
3
|
# =========================================================================== #
|
4
4
|
require 'mountpoints/version/version.rb'
|
5
|
-
require 'roebe'
|
5
|
+
require 'roebe/toplevel_methods/misc.rb'
|
6
6
|
|
7
7
|
Gem::Specification.new { |s|
|
8
8
|
|
@@ -12,40 +12,34 @@ Gem::Specification.new { |s|
|
|
12
12
|
|
13
13
|
DESCRIPTION = <<-EOF
|
14
14
|
|
15
|
-
This class will
|
16
|
-
System. It may also work on Windows,
|
15
|
+
This class will yield to the user the information about all
|
16
|
+
mountpoints on a Linux System. It may also work on Windows,
|
17
|
+
by making use of "wmic".
|
17
18
|
|
18
19
|
Please do note that on Linux this works only via "fdisk",
|
19
|
-
and even then not always.
|
20
|
-
gem that
|
21
|
-
the output of "fdisk -l"
|
20
|
+
and even then not always. This gem was initially started
|
21
|
+
as a "throw-away" gem that can be used quickly, rather
|
22
|
+
than having to parse the output of "fdisk -l" manually
|
23
|
+
(or by eyes only).
|
22
24
|
|
23
|
-
Also do note that we are only interested in external
|
24
|
-
|
25
|
-
|
25
|
+
Also do note that we are only interested in external devices
|
26
|
+
that are mounted that way, NOT other mountpoints. The
|
27
|
+
reason for this is because this gem here serves as a
|
28
|
+
foundation for backing up data onto external USB devices.
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
require 'mountpoints'
|
30
|
-
Mountpoints.show
|
31
|
-
|
32
|
-
# or this variant:
|
33
|
-
|
34
|
-
require 'mountpoints'
|
35
|
-
Mountpoints[]
|
30
|
+
For more information pertaining to this gem have a look
|
31
|
+
at the official homepage of this gem.
|
36
32
|
|
37
33
|
EOF
|
34
|
+
|
38
35
|
s.summary = DESCRIPTION
|
39
36
|
s.description = DESCRIPTION
|
40
|
-
|
41
|
-
s.
|
42
|
-
|
43
|
-
s.
|
44
|
-
s.
|
45
|
-
s.
|
46
|
-
s.files << 'USAGE.md'
|
47
|
-
s.license = 'GPL-2.0'
|
48
|
-
s.homepage = 'https://rubygems.org/gems/mountpoints'
|
37
|
+
|
38
|
+
s.authors = ['Robert A. Heiler']
|
39
|
+
s.email = Roebe.email?
|
40
|
+
s.files = Dir['**/*']
|
41
|
+
s.license = 'GPL-2.0'
|
42
|
+
s.homepage = 'https://rubygems.org/gems/mountpoints'
|
49
43
|
|
50
44
|
s.required_ruby_version = '>= '+Roebe.third_most_stable_version_of_ruby
|
51
45
|
s.required_rubygems_version = '>= '+Gem::VERSION
|
data/test/testing_mountpoints.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mountpoints
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert A. Heiler
|
@@ -40,27 +40,23 @@ dependencies:
|
|
40
40
|
version: '0'
|
41
41
|
description: |2+
|
42
42
|
|
43
|
-
This class will
|
44
|
-
System. It may also work on Windows,
|
43
|
+
This class will yield to the user the information about all
|
44
|
+
mountpoints on a Linux System. It may also work on Windows,
|
45
|
+
by making use of "wmic".
|
45
46
|
|
46
47
|
Please do note that on Linux this works only via "fdisk",
|
47
|
-
and even then not always.
|
48
|
-
gem that
|
49
|
-
the output of "fdisk -l"
|
48
|
+
and even then not always. This gem was initially started
|
49
|
+
as a "throw-away" gem that can be used quickly, rather
|
50
|
+
than having to parse the output of "fdisk -l" manually
|
51
|
+
(or by eyes only).
|
50
52
|
|
51
|
-
Also do note that we are only interested in external
|
52
|
-
|
53
|
-
|
53
|
+
Also do note that we are only interested in external devices
|
54
|
+
that are mounted that way, NOT other mountpoints. The
|
55
|
+
reason for this is because this gem here serves as a
|
56
|
+
foundation for backing up data onto external USB devices.
|
54
57
|
|
55
|
-
|
56
|
-
|
57
|
-
require 'mountpoints'
|
58
|
-
Mountpoints.show
|
59
|
-
|
60
|
-
# or this variant:
|
61
|
-
|
62
|
-
require 'mountpoints'
|
63
|
-
Mountpoints[]
|
58
|
+
For more information pertaining to this gem have a look
|
59
|
+
at the official homepage of this gem.
|
64
60
|
|
65
61
|
email: shevy@inbox.lt
|
66
62
|
executables: []
|
@@ -68,11 +64,12 @@ extensions: []
|
|
68
64
|
extra_rdoc_files: []
|
69
65
|
files:
|
70
66
|
- README.md
|
71
|
-
-
|
67
|
+
- bin/mountpoints
|
72
68
|
- doc/README.gen
|
73
69
|
- lib/mountpoints.rb
|
74
|
-
- lib/mountpoints/
|
75
|
-
- lib/mountpoints/
|
70
|
+
- lib/mountpoints/class/mountpoints.rb
|
71
|
+
- lib/mountpoints/constants/constants.rb
|
72
|
+
- lib/mountpoints/toplevel_methods/toplevel_methods.rb
|
76
73
|
- lib/mountpoints/version/version.rb
|
77
74
|
- mountpoints.gemspec
|
78
75
|
- test/testing_mountpoints.rb
|
@@ -98,12 +95,14 @@ requirements: []
|
|
98
95
|
rubygems_version: 3.3.13
|
99
96
|
signing_key:
|
100
97
|
specification_version: 4
|
101
|
-
summary:
|
102
|
-
also work on Windows,
|
103
|
-
via "fdisk", and even then not always.
|
104
|
-
|
98
|
+
summary: This class will yield to the user the information about all mountpoints on
|
99
|
+
a Linux System. It may also work on Windows, by making use of "wmic". Please do
|
100
|
+
note that on Linux this works only via "fdisk", and even then not always. This gem
|
101
|
+
was initially started as a "throw-away" gem that can be used quickly, rather than
|
102
|
+
having to parse the output of "fdisk -l" manually (or by eyes only). Also do note
|
105
103
|
that we are only interested in external devices that are mounted that way, NOT other
|
106
|
-
mountpoints.
|
107
|
-
|
104
|
+
mountpoints. The reason for this is because this gem here serves as a foundation
|
105
|
+
for backing up data onto external USB devices. For more information pertaining
|
106
|
+
to this gem have a look at the official homepage of this gem.
|
108
107
|
test_files: []
|
109
108
|
...
|
data/USAGE.md
DELETED
data/lib/mountpoints/misc.rb
DELETED
@@ -1,146 +0,0 @@
|
|
1
|
-
#!/usr/bin/ruby -w
|
2
|
-
# Encoding: UTF-8
|
3
|
-
# frozen_string_literal: true
|
4
|
-
# =========================================================================== #
|
5
|
-
# require 'mountpoints/misc.rb'
|
6
|
-
# =========================================================================== #
|
7
|
-
class Mountpoints
|
8
|
-
|
9
|
-
# ========================================================================= #
|
10
|
-
# === debug?
|
11
|
-
# ========================================================================= #
|
12
|
-
def debug?
|
13
|
-
DEBUG
|
14
|
-
end
|
15
|
-
|
16
|
-
# ========================================================================= #
|
17
|
-
# === rds
|
18
|
-
# ========================================================================= #
|
19
|
-
def rds(i)
|
20
|
-
i.squeeze('/')
|
21
|
-
end
|
22
|
-
|
23
|
-
# ========================================================================= #
|
24
|
-
# === determine_the_mountpoints_on_linux
|
25
|
-
# ========================================================================= #
|
26
|
-
def determine_the_mountpoints_on_linux
|
27
|
-
set_command_to_run(DF_COMMAND)
|
28
|
-
if debug?
|
29
|
-
e "Now running this command: #{sfancy(command_to_run?)}"
|
30
|
-
end
|
31
|
-
result = `#{command_to_run?}`.split("\n").select {|entry|
|
32
|
-
entry.include?('/dev/sd')
|
33
|
-
}.reject {|line|
|
34
|
-
splitted = line.split(' ').map {|inner_entry| inner_entry.strip }
|
35
|
-
splitted.last == '/'
|
36
|
-
}
|
37
|
-
result.map! {|entry|
|
38
|
-
# =================================================================== #
|
39
|
-
# The line may look like this:
|
40
|
-
#
|
41
|
-
# "/dev/sdc1 459G 436G 0 100% /Mount/USB3"
|
42
|
-
#
|
43
|
-
# =================================================================== #
|
44
|
-
entry.split(' ').last.strip
|
45
|
-
}
|
46
|
-
set_mountpoints(result)
|
47
|
-
end
|
48
|
-
|
49
|
-
# ========================================================================= #
|
50
|
-
# === determine_the_mountpoints_on_windows
|
51
|
-
# ========================================================================= #
|
52
|
-
def determine_the_mountpoints_on_windows
|
53
|
-
result = ''.dup
|
54
|
-
set_command_to_run('wmic logicaldisk where "drivetype=3" get DeviceID')
|
55
|
-
result << `#{command_to_run?}`.split(N).reject {|line| line.to_s.strip.empty? }.join(N) # This will keep the main result.
|
56
|
-
set_command_to_run('wmic logicaldisk where "drivetype=2" get DeviceID')
|
57
|
-
result << `#{command_to_run?}`.split(N).reject {|line| line.to_s.strip.empty? }.join(N) # This will keep the main result.
|
58
|
-
result = result.split(N).reverse.select {|entry|
|
59
|
-
entry.include? ':'
|
60
|
-
}.map {|entry|
|
61
|
-
entry.strip
|
62
|
-
}.reject {|entry| entry.include? 'DeviceID' } .uniq
|
63
|
-
set_mountpoints(result)
|
64
|
-
end
|
65
|
-
|
66
|
-
# ========================================================================= #
|
67
|
-
# === use_windows?
|
68
|
-
# ========================================================================= #
|
69
|
-
def use_windows?
|
70
|
-
return Mountpoints.use_windows?
|
71
|
-
end; alias is_on_windows? use_windows? # === is_on_windows?
|
72
|
-
|
73
|
-
# ========================================================================= #
|
74
|
-
# === Mountpoints.use_windows?
|
75
|
-
# ========================================================================= #
|
76
|
-
def self.use_windows?
|
77
|
-
case RUBY_PLATFORM
|
78
|
-
when /win/,
|
79
|
-
/mingw/,
|
80
|
-
/msys/
|
81
|
-
return true
|
82
|
-
else
|
83
|
-
return false
|
84
|
-
end
|
85
|
-
end; self.instance_eval { alias is_on_windows? use_windows? } # === Mountpoints.is_on_windows?
|
86
|
-
|
87
|
-
# ========================================================================= #
|
88
|
-
# === Mountpoints.report
|
89
|
-
# ========================================================================= #
|
90
|
-
def self.report
|
91
|
-
Mountpoints[:be_extra_verbose]
|
92
|
-
end
|
93
|
-
|
94
|
-
# ========================================================================= #
|
95
|
-
# === Mountpoints.is_any_mountpoint_available?
|
96
|
-
# ========================================================================= #
|
97
|
-
def self.is_any_mountpoint_available?
|
98
|
-
!new(:be_quiet).array_mountpoints.empty?
|
99
|
-
end
|
100
|
-
|
101
|
-
# ========================================================================= #
|
102
|
-
# === Mountpoints.show
|
103
|
-
# ========================================================================= #
|
104
|
-
def self.show
|
105
|
-
new :be_extra_verbose
|
106
|
-
end
|
107
|
-
|
108
|
-
# ========================================================================= #
|
109
|
-
# === Mountpoints[]
|
110
|
-
#
|
111
|
-
# This method will return an array such as:
|
112
|
-
#
|
113
|
-
# ["/Mount/USB1/"]
|
114
|
-
#
|
115
|
-
# In other words, it will be a silent result.
|
116
|
-
#
|
117
|
-
# Usage examples:
|
118
|
-
#
|
119
|
-
# Mountpoints[]
|
120
|
-
# Mountpoints[:be_extra_verbose]
|
121
|
-
#
|
122
|
-
# ========================================================================= #
|
123
|
-
def self.[](
|
124
|
-
be_extra_verbose = :normal_verbosity
|
125
|
-
)
|
126
|
-
case be_extra_verbose
|
127
|
-
# ======================================================================= #
|
128
|
-
# === :be_extra_verbose
|
129
|
-
# ======================================================================= #
|
130
|
-
when :be_extra_verbose
|
131
|
-
_ = new(be_extra_verbose)
|
132
|
-
_.report_result
|
133
|
-
return _.array_mountpoints
|
134
|
-
else
|
135
|
-
return new(be_extra_verbose).array_mountpoints
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# ========================================================================= #
|
140
|
-
# === Mountpoints.array_mountpoints
|
141
|
-
# ========================================================================= #
|
142
|
-
def self.array_mountpoints
|
143
|
-
new { :be_quiet }.array_mountpoints
|
144
|
-
end; self.instance_eval { alias mountpoints_as_array array_mountpoints } # === Mountpoints.mountpoints_as_array
|
145
|
-
|
146
|
-
end
|
@@ -1,270 +0,0 @@
|
|
1
|
-
#!/usr/bin/ruby -w
|
2
|
-
# Encoding: UTF-8
|
3
|
-
# frozen_string_literal: true
|
4
|
-
# =========================================================================== #
|
5
|
-
# === Mountpoints
|
6
|
-
#
|
7
|
-
# The primary goal for this class is to scan for all mounted USB-devices.
|
8
|
-
#
|
9
|
-
# Colours may be used on the commandline if the .report() method is
|
10
|
-
# called.
|
11
|
-
#
|
12
|
-
# Usage examples:
|
13
|
-
#
|
14
|
-
# Mountpoints.new(ARGV)
|
15
|
-
# Mountpoints.new
|
16
|
-
# Mountpoints.show
|
17
|
-
# Mountpoints.is_any_mountpoint_available?
|
18
|
-
#
|
19
|
-
# =========================================================================== #
|
20
|
-
# require 'mountpoints/mountpoints.rb'
|
21
|
-
# Mountpoints[]
|
22
|
-
# =========================================================================== #
|
23
|
-
class Mountpoints # === Mountpoints
|
24
|
-
|
25
|
-
require 'mountpoints/misc.rb'
|
26
|
-
require 'mountpoints/version/version.rb'
|
27
|
-
|
28
|
-
begin
|
29
|
-
require 'colours'
|
30
|
-
include Colours
|
31
|
-
rescue LoadError; end
|
32
|
-
|
33
|
-
begin
|
34
|
-
require 'opn'
|
35
|
-
rescue LoadError; end
|
36
|
-
|
37
|
-
# ========================================================================= #
|
38
|
-
# === NAMESPACE
|
39
|
-
# ========================================================================= #
|
40
|
-
NAMESPACE = inspect
|
41
|
-
|
42
|
-
# ========================================================================= #
|
43
|
-
# === N
|
44
|
-
# ========================================================================= #
|
45
|
-
N = "\n"
|
46
|
-
|
47
|
-
# ========================================================================= #
|
48
|
-
# === LJUST
|
49
|
-
# ========================================================================= #
|
50
|
-
LJUST = 15
|
51
|
-
|
52
|
-
# ========================================================================= #
|
53
|
-
# === DF_COMMAND
|
54
|
-
# ========================================================================= #
|
55
|
-
DF_COMMAND = 'df -T -ah'
|
56
|
-
|
57
|
-
# ========================================================================= #
|
58
|
-
# === DEBUG
|
59
|
-
#
|
60
|
-
# If true then we will debug this class.
|
61
|
-
# ========================================================================= #
|
62
|
-
DEBUG = false
|
63
|
-
|
64
|
-
# ========================================================================= #
|
65
|
-
# === initialize
|
66
|
-
#
|
67
|
-
# If the first argument to this method is :be_extra_verbose then the
|
68
|
-
# class will show more information on the commandline.
|
69
|
-
# ========================================================================= #
|
70
|
-
def initialize(
|
71
|
-
commandline_arguments = ARGV,
|
72
|
-
run_already = true
|
73
|
-
)
|
74
|
-
reset
|
75
|
-
case run_already
|
76
|
-
# ======================================================================= #
|
77
|
-
# === :be_quiet
|
78
|
-
# ======================================================================= #
|
79
|
-
when :be_quiet
|
80
|
-
@internal_hash[:be_extra_verbose] = false
|
81
|
-
run_already = true
|
82
|
-
# ======================================================================= #
|
83
|
-
# === :be_extra_verbose
|
84
|
-
# ======================================================================= #
|
85
|
-
when :be_extra_verbose
|
86
|
-
@internal_hash[:be_extra_verbose] = true
|
87
|
-
run_already = true
|
88
|
-
# ======================================================================= #
|
89
|
-
# === :normal_verbosity
|
90
|
-
# ======================================================================= #
|
91
|
-
when :normal_verbosity # Simply pass through here.
|
92
|
-
end
|
93
|
-
if commandline_arguments.is_a? Array
|
94
|
-
first = commandline_arguments.first
|
95
|
-
case first
|
96
|
-
when /^-?-?extra(-|_)?verbose$/
|
97
|
-
@internal_hash[:be_extra_verbose] = true
|
98
|
-
run_already = true
|
99
|
-
end
|
100
|
-
end
|
101
|
-
run if run_already
|
102
|
-
end
|
103
|
-
|
104
|
-
# ========================================================================= #
|
105
|
-
# === reset (reset tag)
|
106
|
-
# ========================================================================= #
|
107
|
-
def reset
|
108
|
-
# ======================================================================= #
|
109
|
-
# === @namespace
|
110
|
-
# ======================================================================= #
|
111
|
-
@namespace = NAMESPACE
|
112
|
-
# ======================================================================= #
|
113
|
-
# === @internal_hash
|
114
|
-
# ======================================================================= #
|
115
|
-
@internal_hash = {}
|
116
|
-
# ======================================================================= #
|
117
|
-
# === :array_mountpoints
|
118
|
-
# ======================================================================= #
|
119
|
-
@internal_hash[:array_mountpoints] = []
|
120
|
-
# ======================================================================= #
|
121
|
-
# === :be_extra_verbose
|
122
|
-
# ======================================================================= #
|
123
|
-
@internal_hash[:be_extra_verbose] = false
|
124
|
-
# ======================================================================= #
|
125
|
-
# === :command_to_run
|
126
|
-
#
|
127
|
-
# This is the command that will be run via ``.
|
128
|
-
# ======================================================================= #
|
129
|
-
@internal_hash[:command_to_run] = ''.dup
|
130
|
-
end
|
131
|
-
|
132
|
-
# ========================================================================= #
|
133
|
-
# === be_extra_verbose?
|
134
|
-
# ========================================================================= #
|
135
|
-
def be_extra_verbose?
|
136
|
-
@internal_hash[:be_extra_verbose]
|
137
|
-
end
|
138
|
-
|
139
|
-
# ========================================================================= #
|
140
|
-
# === set_command_to_run
|
141
|
-
# ========================================================================= #
|
142
|
-
def set_command_to_run(i)
|
143
|
-
@internal_hash[:command_to_run] = i
|
144
|
-
end
|
145
|
-
|
146
|
-
# ========================================================================= #
|
147
|
-
# === command_to_run?
|
148
|
-
# ========================================================================= #
|
149
|
-
def command_to_run?
|
150
|
-
@internal_hash[:command_to_run]
|
151
|
-
end
|
152
|
-
|
153
|
-
# ========================================================================= #
|
154
|
-
# === run_the_system_command
|
155
|
-
# ========================================================================= #
|
156
|
-
def run_the_system_command
|
157
|
-
return `#{command_to_run?}`
|
158
|
-
end
|
159
|
-
|
160
|
-
# ========================================================================= #
|
161
|
-
# === set_mountpoints
|
162
|
-
#
|
163
|
-
# Note that all directories will be stored via a trailing '/' in this
|
164
|
-
# method.
|
165
|
-
# ========================================================================= #
|
166
|
-
def set_mountpoints(i)
|
167
|
-
i = [i].flatten.compact
|
168
|
-
i.map! {|entry|
|
169
|
-
if File.directory? entry
|
170
|
-
entry = rds(entry)
|
171
|
-
end
|
172
|
-
if File.directory?(entry) and !entry.end_with?('/')
|
173
|
-
entry << '/'
|
174
|
-
end
|
175
|
-
entry
|
176
|
-
}
|
177
|
-
@internal_hash[:array_mountpoints] = i
|
178
|
-
end
|
179
|
-
|
180
|
-
# ========================================================================= #
|
181
|
-
# === mountpoints?
|
182
|
-
# ========================================================================= #
|
183
|
-
def mountpoints?
|
184
|
-
@internal_hash[:array_mountpoints]
|
185
|
-
end; alias array_mountpoints? mountpoints? # === array_mountpoints?
|
186
|
-
alias array_mountpoints mountpoints? # === array_mountpoints
|
187
|
-
alias mounted_here mountpoints? # === mounted_here
|
188
|
-
alias result? mountpoints? # === result?
|
189
|
-
alias result mountpoints? # === result
|
190
|
-
|
191
|
-
# ========================================================================= #
|
192
|
-
# === report_result (report tag)
|
193
|
-
# ========================================================================= #
|
194
|
-
def report_result(
|
195
|
-
be_verbose = be_extra_verbose?
|
196
|
-
)
|
197
|
-
if be_verbose
|
198
|
-
if mountpoints?.empty?
|
199
|
-
opn; e 'No mount point was found. Thus, it is included '\
|
200
|
-
'that no USB device is currently mounted.'
|
201
|
-
else
|
202
|
-
opn; e 'The following mount points were found:'
|
203
|
-
e
|
204
|
-
mountpoints = mountpoints?
|
205
|
-
result = run_the_system_command.split("\n").select {|line|
|
206
|
-
# ================================================================= #
|
207
|
-
# The line may look like this:
|
208
|
-
#
|
209
|
-
# "/dev/sdc1 ext4 459G 436G 0 100% /Mount/USB3\n" +
|
210
|
-
#
|
211
|
-
# ================================================================= #
|
212
|
-
mountpoints.any? {|discovered_mountpoint|
|
213
|
-
line.include? discovered_mountpoint
|
214
|
-
}
|
215
|
-
}
|
216
|
-
|
217
|
-
mountpoints.each {|line|
|
218
|
-
if is_on_windows?
|
219
|
-
e line.ljust(LJUST)
|
220
|
-
else
|
221
|
-
line = result.select {|entry|
|
222
|
-
entry.include? entry
|
223
|
-
}.first
|
224
|
-
line = result.select {|inner_line|
|
225
|
-
inner_line.end_with? line
|
226
|
-
}.first
|
227
|
-
line = line.split(' ').map(&:strip)
|
228
|
-
filesize = line[2]
|
229
|
-
name_of_device = line[1]
|
230
|
-
mounted_here = line[0]
|
231
|
-
efancy ' '+
|
232
|
-
mounted_here.ljust(LJUST)+
|
233
|
-
' '+
|
234
|
-
swarn(name_of_device)+' '+
|
235
|
-
'('+Colours.green(filesize)+')'
|
236
|
-
end
|
237
|
-
}
|
238
|
-
e
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end; alias report_colourized_result report_result # === report_colourized_result
|
242
|
-
alias report report_result # === report
|
243
|
-
alias determine_result report_result # === determine_result
|
244
|
-
|
245
|
-
# ========================================================================= #
|
246
|
-
# === run (run tag)
|
247
|
-
# ========================================================================= #
|
248
|
-
def run
|
249
|
-
# ======================================================================= #
|
250
|
-
# === Handle windows next
|
251
|
-
# ======================================================================= #
|
252
|
-
if use_windows?
|
253
|
-
determine_the_mountpoints_on_windows
|
254
|
-
# ======================================================================= #
|
255
|
-
# === Handle linux next
|
256
|
-
# ======================================================================= #
|
257
|
-
else # This branch is specifically for linux
|
258
|
-
determine_the_mountpoints_on_linux
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
end
|
263
|
-
|
264
|
-
if __FILE__ == $PROGRAM_NAME
|
265
|
-
_ = Mountpoints.new :be_extra_verbose
|
266
|
-
# pp _
|
267
|
-
#_.report
|
268
|
-
pp _.mountpoints?
|
269
|
-
# p _.result?
|
270
|
-
end # rubymountpoints --extra-verbose
|