Module: MiscHacks

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

Defined Under Namespace

Modules: ExceptionMixin, IOMixin Classes: ChildError, Password, Random

Constant Summary collapse

VERSION =
File.read(File.dirname(__FILE__)+'/../VERSION').chomp
RANDOM =
Random.instance

Class Method Summary collapse

Class Method Details

.catching_exit(final_proc, fallthrough_status) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mischacks.rb', line 55

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



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

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

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



75
76
77
# File 'lib/mischacks.rb', line 75

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

.fork_and_checkObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/mischacks.rb', line 44

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



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
113
114
115
# File 'lib/mischacks.rb', line 79

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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mischacks.rb', line 31

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(&:to_s))
    end
  end

  nil
end

.tempname_for(path) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mischacks.rb', line 117

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



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

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