Module: MiscHacks

Defined in:
lib/mischacks.rb,
lib/mischacks.rb,
lib/mischacks.rb

Overview

mischacks – Miscellaneous methods that may or may not be useful Copyright © 2009 Johan Kiviniemi

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Defined Under Namespace

Modules: ExceptionMixin, IOMixin Classes: ChildError

Constant Summary collapse

VERSION =
'0.0.5'

Class Method Summary collapse

Class Method Details

.catching_exit(final_proc, fallthrough_status) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mischacks.rb', line 52

def self.catching_exit final_proc, fallthrough_status
  status = fallthrough_status

  begin
    yield
  rescue SystemExit => e
    status = e.status
  rescue Exception => e
    warn e.to_formatted_string
  ensure
    final_proc.call status
  end

  status
end

.do_and_exit(status = 1, &block) ⇒ Object



68
69
70
# File 'lib/mischacks.rb', line 68

def self.do_and_exit status=1, &block
  catching_exit method(:exit), status, &block
end

.do_and_exit!(status = 1, &block) ⇒ Object



72
73
74
# File 'lib/mischacks.rb', line 72

def self.do_and_exit! status=1, &block
  catching_exit method(:exit!), status, &block
end

.fork_and_checkObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/mischacks.rb', line 41

def self.fork_and_check
  fork do
    yield
  end.tap do |pid|
    _, status = Process.wait2 pid
    raise ChildError, status.exitstatus if status.exitstatus != 0
  end

  nil
end

.overwrite(path, mode = nil) ⇒ Object



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
# File 'lib/mischacks.rb', line 76

def self.overwrite path, mode=nil
  begin
    stat = File.stat path

    raise ArgumentError, "Not a file: #{path}",   caller unless stat.file?
    raise ArgumentError, "Not writable: #{path}", caller unless stat.writable?

    mode ||= stat.mode & 0777
  rescue Errno::ENOENT
    stat = nil
  end

  temppath, io = try_n_times do
    t  = tempname_for path
    io = File.open t, File::RDWR|File::CREAT|File::EXCL
    [t, io]
  end

  begin
    ret = yield io

    io.best_datasync

    File.chmod mode, temppath if mode

    File.rename temppath, path

  rescue
    File.unlink temppath
    raise

  ensure
    io.close
  end

  ret
end

.sh(cmd, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mischacks.rb', line 28

def self.sh cmd, *args
  env = if args.last.is_a? Hash then args.pop else {} end

  fork_and_check do
    do_and_exit! do
      env.each_pair do |k, v| ENV[k.to_s] = v.to_s end
      exec *(%W{sh -e -c #{cmd} sh} + args.map {|a| a.to_s })
    end
  end

  nil
end

.tempname_for(path) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mischacks.rb', line 114

def self.tempname_for path
  dirname  = File.dirname  path
  basename = File.basename path

  '%s/.%s.%s%s%s' % [
    dirname,
    basename,
    Time.now.to_i.to_s(36),
    $$.to_s(36),
    rand(1<<32).to_s(36)
  ]
end

.try_n_times(n = 10) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mischacks.rb', line 127

def self.try_n_times n=10
  i = 0
  begin
    ret = yield
  rescue
    i += 1
    retry if i < n
    raise
  end

  ret
end