Module: Util

Extended by:
Util
Included in:
ChkBuild::Build, Util
Defined in:
lib/misc/util.rb

Instance Method Summary collapse

Instance Method Details

#atomic_make_file(filename, content) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/misc/util.rb', line 207

def atomic_make_file(filename, content)
  tmp = nil
  i = 0
  begin
    tmp = "#{filename}.tmp#{i}"
    f = File.open(tmp, File::WRONLY|File::CREAT|File::TRUNC|File::EXCL)
  rescue Errno::EEXIST
    i += 1
    retry
  end
  f << content
  f.close
  File.rename tmp, filename
end


192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/misc/util.rb', line 192

def force_link(old, new)
  i = 0
  tmp = new
  begin
    File.link old, tmp
  rescue Errno::EEXIST
    i += 1
    tmp = "#{new}.tmp#{i}"
    retry
  end
  if tmp != new
    File.rename tmp, new
  end
end

#mkcd(dir) ⇒ Object



149
150
151
152
# File 'lib/misc/util.rb', line 149

def mkcd(dir)
  FileUtils.mkpath dir
  Dir.chdir dir
end

#product(*args) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/misc/util.rb', line 126

def product(*args)
  if block_given?
    product_each(*args) {|vs| yield vs }
  else
    r = []
    product_each(*args) {|vs| r << vs }
    r
  end
end

#product_each(*args) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/misc/util.rb', line 136

def product_each(*args)
  if args.empty?
    yield []
  else
    arg, *rest = args
    arg.each {|v|
      product_each(*rest) {|vs|
        yield [v, *vs]
      }
    }
  end
end

#resource_limit(resource, val) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/misc/util.rb', line 166

def resource_limit(resource, val)
  if Symbol === resource
    begin
      resource = Process.const_get(resource)
    rescue NameError
      return
    end
  end
  cur_limit, max_limit = Process.getrlimit(resource)
  if max_limit < val
    val = max_limit
  end
  Process.setrlimit(resource, val, val)
end

#resource_unlimit(resource) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/misc/util.rb', line 154

def resource_unlimit(resource)
  if Symbol === resource
    begin
      resource = Process.const_get(resource)
    rescue NameError
      return
    end
  end
  cur_limit, max_limit = Process.getrlimit(resource)
  Process.setrlimit(resource, max_limit, max_limit)
end

#rproduct(*args) ⇒ Object

Util.rproduct(ary1, ary2, …)

Util.rproduct([1,2],[3,4]) #=> [[1, 3], [2, 3], [1, 4], [2, 4]]


116
117
118
119
120
121
122
123
124
# File 'lib/misc/util.rb', line 116

def rproduct(*args)
  if block_given?
    product_each(*args.reverse) {|vs| yield vs.reverse }
  else
    r = []
    product_each(*args.reverse) {|vs| r << vs.reverse }
    r
  end
end

#sha256_digest_file(filename) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/misc/util.rb', line 181

def sha256_digest_file(filename)
  d = Digest::SHA256.new
  open(filename) {|f|
    buf = ""
    while f.read(4096, buf)
      d << buf
    end
  }
  "sha256:#{d.hexdigest}"
end

#simple_hostnameObject



229
230
231
# File 'lib/misc/util.rb', line 229

def simple_hostname
  Socket.gethostname.sub(/\..*/, '')
end

#with_tempfile(content) {|t| ... } ⇒ Object

:yield: tempfile

Yields:

  • (t)


222
223
224
225
226
227
# File 'lib/misc/util.rb', line 222

def with_tempfile(content) # :yield: tempfile
  t = Tempfile.new("chkbuild")
  t << content
  t.sync
  yield t
end