Module: Gonzui::Util
- Included in:
- AbstractAptGet, AbstractDBM, AbstractExtractor, AbstractFetcher, AbstractUpdater, AbstractVCS, AptGet, CommandLineApplication, CommandLineApplication, Config, ContentInfo, Deindexer, Extractor, Fetcher, GonzuiAbstractServlet, GonzuiServlet, Indexer, PhraseFinder, Searcher
- Defined in:
- lib/gonzui/util.rb
Defined Under Namespace
Classes: AssertionFailed, CommandNotFoundError
Constant Summary
collapse
- @@verbosity =
false
Class Method Summary
collapse
Class Method Details
.assert(bool) ⇒ Object
368
369
370
|
# File 'lib/gonzui/util.rb', line 368
def assert(bool)
raise AssertionFailed.new unless bool
end
|
.assert_equal(expected, value) ⇒ Object
357
358
359
|
# File 'lib/gonzui/util.rb', line 357
def assert_equal(expected, value)
raise AssertionFailed.new unless expected == value
end
|
.assert_equal_all(*values) ⇒ Object
361
362
363
364
365
366
|
# File 'lib/gonzui/util.rb', line 361
def assert_equal_all(*values)
first = values.first
unless values.all? {|value| first == value }
raise AssertionFailed.new
end
end
|
.assert_non_nil(object) ⇒ Object
349
350
351
|
# File 'lib/gonzui/util.rb', line 349
def assert_non_nil(object)
raise AssertionFailed.new if object.nil?
end
|
.assert_not_reached ⇒ Object
353
354
355
|
# File 'lib/gonzui/util.rb', line 353
def assert_not_reached
raise AssertionFailed.new
end
|
.benchmark ⇒ Object
318
319
320
321
322
323
324
325
326
|
# File 'lib/gonzui/util.rb', line 318
def benchmark
result = nil
Benchmark.bm {|x|
x.report {
result = yield
}
}
return result
end
|
.command_exist?(command) ⇒ Boolean
249
250
251
252
253
254
255
256
257
|
# File 'lib/gonzui/util.rb', line 249
def command_exist?(command)
paths = (ENV['PATH'] or "").split(File::PATH_SEPARATOR)
paths.each {|path|
return true if File.executable?(File.join(path, command))
return true if File.executable?(File.join(path, command + ".exe"))
}
return false
end
|
.commify(number) ⇒ Object
300
301
302
303
304
|
# File 'lib/gonzui/util.rb', line 300
def commify(number)
numstr = number.to_s
true while numstr.sub!(/^([-+]?\d+)(\d{3})/, '\1,\2')
return numstr
end
|
.eprintf(format, *args) ⇒ Object
286
287
288
289
|
# File 'lib/gonzui/util.rb', line 286
def eprintf(format, *args)
wprintf(format, *args)
exit(1)
end
|
306
307
308
309
310
311
312
313
314
315
316
|
# File 'lib/gonzui/util.rb', line 306
def format_bytes(bytes)
if bytes < 1024
sprintf("%dB", bytes)
elsif bytes < 1024 * 1000 sprintf("%.1fKB", bytes.to_f / 1024)
elsif bytes < 1024 * 1024 * 1000 sprintf("%.1fMB", bytes.to_f / 1024 / 1024)
else
sprintf("%.1fGB", bytes.to_f / 1024 / 1024 / 1024)
end
end
|
.program_name ⇒ Object
237
238
239
|
# File 'lib/gonzui/util.rb', line 237
def program_name
File.basename($0)
end
|
.protect_from_signals ⇒ Object
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
# File 'lib/gonzui/util.rb', line 330
def protect_from_signals
$protect_from_signals_mutex.synchronize {
interrupted = false
previous_handlers = {}
signals = ["INT", "TERM"]
signals.each {|signal|
previous_handlers[signal] = trap(signal) { interrupted = true }
}
yield
previous_handlers.each {|signal, handler|
trap(signal, handler)
}
raise Interrupt if interrupted
}
end
|
.require_command(command) ⇒ Object
260
261
262
263
264
|
# File 'lib/gonzui/util.rb', line 260
def require_command(command)
raise CommandNotFoundError.new("#{command}: command not found") unless
command_exist?(command)
return true
end
|
.set_verbosity(verbosity) ⇒ Object
292
293
294
|
# File 'lib/gonzui/util.rb', line 292
def set_verbosity(verbosity)
@@verbosity = verbosity
end
|
.shell_escape(file_name) ⇒ Object
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
# File 'lib/gonzui/util.rb', line 266
def shell_escape(file_name)
if (/mswin|mingw|bccwin/ =~ RUBY_PLATFORM)
fn = file_name
fn.gsub!(/["]/, "")
if /^\w+:\/\// =~ fn
fn.sub!(/^file:\/\/\/?(\w)\//, "file:///\\1:/")
else
fn.gsub!(/[\/]/, "\\")
end
'"' + fn + '"'
else
'"' + file_name.gsub(/([$"\\`])/, "\\\\\\1") + '"'
end
end
|
.unix? ⇒ Boolean
245
246
247
|
# File 'lib/gonzui/util.rb', line 245
def unix?
not windows?
end
|
.vprintf(format, *args) ⇒ Object
296
297
298
|
# File 'lib/gonzui/util.rb', line 296
def vprintf(format, *args)
printf(format + "\n", *args) if @@verbosity
end
|
.windows? ⇒ Boolean
241
242
243
|
# File 'lib/gonzui/util.rb', line 241
def windows?
/-(mswin32|cygwin|mingw|bccwin)/.match(RUBY_PLATFORM)
end
|
.wprintf(format, *args) ⇒ Object
282
283
284
|
# File 'lib/gonzui/util.rb', line 282
def wprintf(format, *args)
STDERR.printf(program_name + ": " + format + "\n", *args)
end
|