forked from ruby/chkbuild
/
util.rb
480 lines (437 loc) · 9.94 KB
/
util.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# util.rb - utilities
#
# Copyright (C) 2006-2012 Tanaka Akira <akr@fsij.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# 3. The name of the author may not be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'fileutils'
require 'socket'
require "etc"
require "digest/sha2"
require "fcntl"
require "tempfile"
require "find"
require "pathname"
require "rbconfig"
require "stringio"
require "erb"
include ERB::Util
def tp(obj)
open("/dev/tty", "w") {|f| f.puts obj.inspect }
end
def tpp(obj)
require 'pp'
open("/dev/tty", "w") {|f| PP.pp(obj, f) }
end
if "".respond_to? :encode
def h(str)
str.encode("US-ASCII", Encoding.find("locale"), :invalid=>:replace, :undef=>:replace, :xml=>:text)
end
end
def ha(str)
'"' + h(str) + '"'
end
module Kernel
if !nil.respond_to?(:funcall)
if nil.respond_to?(:fcall)
alias funcall fcall
else
alias funcall send
end
end
end
class String
def lastline
if pos = rindex(?\n)
self[(pos+1)..-1]
else
self
end
end
if !"".respond_to?(:start_with?)
def start_with?(arg, *rest)
[arg, *rest].any? {|prefix|
prefix.length <= self.length && prefix == self[0, prefix.length]
}
end
end
end
unless File.respond_to? :identical?
def File.identical?(filename1, filename2)
test(?-, filename1, filename2)
end
end
unless STDIN.respond_to? :close_on_exec?
class IO
def close_on_exec?
self.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC != 0
end
def close_on_exec=(v)
flags = self.fcntl(Fcntl::F_GETFD)
if v
flags |= Fcntl::FD_CLOEXEC
else
flags &= ~Fcntl::FD_CLOEXEC
end
self.fcntl(Fcntl::F_SETFD, flags)
v
end
end
end
unless RbConfig.respond_to? :ruby
def RbConfig.ruby
File.join(
RbConfig::CONFIG["bindir"],
RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"]
)
end
end
class Tempfile
def gather_each(arg)
if Regexp === arg
regexp = arg
arg = lambda {|e| regexp =~ e; $& }
end
prev_value = prev_elts = nil
self.each {|e|
v = arg.call(e)
if prev_value == nil
if v == nil
yield [e]
else
prev_value = v
prev_elts = [e]
end
else
if v == nil
yield prev_elts
yield [e]
prev_value = prev_elts = nil
elsif prev_value == v
prev_elts << e
else
yield prev_elts
prev_value = v
prev_elts = [e]
end
end
}
if prev_value != nil
yield prev_elts
end
end
end
module Util
extend Util # similar to module_function but instance methods are public.
# Util.rproduct(ary1, ary2, ...)
#
# Util.rproduct([1,2],[3,4]) #=> [[1, 3], [2, 3], [1, 4], [2, 4]]
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
def product(*args)
if block_given?
product_each(*args) {|vs| yield vs }
else
r = []
product_each(*args) {|vs| r << vs }
r
end
end
def product_each(*args)
if args.empty?
yield []
else
arg, *rest = args
arg.each {|v|
product_each(*rest) {|vs|
yield [v, *vs]
}
}
end
end
def mkcd(dir, &b)
FileUtils.mkpath dir
Dir.chdir(dir, &b)
end
def search_command(c, path=ENV['PATH'])
path.split(/:/).each {|d|
f = File.join(d, c)
if File.file?(f) && File.executable?(f)
return f
end
}
nil
end
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
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
def atomic_make_file(filename)
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
yield f
f.close
File.rename tmp, filename
end
def atomic_make_compressed_file(filename)
atomic_make_file(filename) {|f|
Zlib::GzipWriter.wrap(f) {|z|
yield z
z.finish
}
}
end
def compress_file(src, dst)
Zlib::GzipWriter.wrap(open(dst, "w")) {|z|
open(src) {|f|
FileUtils.copy_stream(f, z)
}
}
end
def with_stdouterr(io)
sync_stdout = STDOUT.sync
save_stdout = STDOUT.dup
save_stderr = STDERR.dup
STDOUT.reopen(io)
STDERR.reopen(io)
begin
yield
ensure
STDOUT.reopen(save_stdout)
STDERR.reopen(save_stderr)
STDOUT.sync = sync_stdout
STDERR.sync = true
end
end
def with_tempfile(content) # :yield: tempfile
t = Tempfile.new("chkbuild")
t << content
t.sync
yield t
end
def with_templog(dir, basename)
n = 1
begin
name = "#{dir}/#{basename}#{n}"
f = File.open(name, File::RDWR|File::CREAT|File::EXCL)
rescue Errno::EEXIST
n += 1
retry
end
begin
yield name, f
ensure
f.close
end
end
def simple_hostname
Socket.gethostname.sub(/\..*/, '')
end
def format_elapsed_time(seconds)
res = "%.1fs" % seconds
m, s = seconds.divmod(60)
h, m = m.divmod(60)
if h != 0
res << " = %dh %dm %.1fs" % [h, m, s]
elsif m != 0
res << " = %dm %.1fs" % [m, s]
end
res
end
def merge_opts(opts_list)
h = {}
max = Hash.new(0)
opts_list.each {|opts|
opts.each {|k, v|
if /_\?\z/ =~ k.to_s
base = $`
n = (max[base] += 1)
k = "#{base}_#{n}".intern
end
if /_(\d+)\z/ =~ k.to_s
base = $`
max[base] = $1.to_i
end
if !h.include?(k)
h[k] = v
end
}
}
h
end
def opts2allsuffixes(opts)
opts2aryparam(opts, :suffix).flatten
end
def opts2funsuffixes(opts)
opts2allsuffixes(opts).reject {|s| /\A-/ =~ s }
end
def numstrkey(str)
k = []
str.scan(/(\d+)|\D+/) {
if $1
k << [0, $1.to_i, $&]
else
k << [1, $&]
end
}
k
end
def numstrsort(ary)
ary.sort_by {|s| Util.numstrkey(s) }
end
def opts2aryparam(opts, name)
template_list = opts.fetch(name, [])
return [template_list] unless Array === template_list
re = /\A#{Regexp.escape name.to_s}_/
pairs = []
opts.each {|k, v|
if re =~ k.to_s
pairs << [$', v]
end
}
result = []
template_list.each {|e|
if Symbol === e
e = e.to_s
if /_\?\z/ =~ e
base = $`
re2 = /\A#{Regexp.escape base.to_s}_/
ps, pairs = pairs.partition {|k, v| re2 =~ k }
else
ps, pairs = pairs.partition {|k, v| e == k }
end
ps = ps.sort_by {|k, v| Util.numstrkey(k) }
ps.each {|k, v|
v = [v] unless Array === v
result.concat v
}
else
result << e
end
}
pairs = pairs.sort_by {|k, v| Util.numstrkey(k) }
pairs.each {|k, v|
v = [v] unless Array === v
result.concat v
}
result
end
def opts2nullablearyparam(opts, name)
return opts2aryparam(opts, name) if opts.has_key?(name)
re = /\A#{Regexp.escape name.to_s}_/
return opts2aryparam(opts, name) if opts.any? {|k, v| re =~ k.to_s }
return nil
end
def opts2hashparam(opts, name)
h = {}
re = /\A#{Regexp.escape name.to_s}_/
opts.each {|k, v|
if re =~ k.to_s
h[$'] = v
end
}
h
end
end
module Find
def stable_find(*paths) # :yield: path
block_given? or return enum_for(__method__, *paths)
paths.collect!{|d| raise Errno::ENOENT unless File.exist?(d); d.dup}
while file = paths.shift
catch(:prune) do
yield file.dup.taint
begin
s = File.lstat(file)
rescue Errno::ENOENT
next
end
begin
if s.directory? then
fs = Dir.entries(file)
fs.sort!
fs.reverse!
for f in fs
next if f == "." or f == ".."
if File::ALT_SEPARATOR and file =~ /^(?:[\/\\]|[A-Za-z]:[\/\\]?)$/ then
f = file + f
elsif file == "/" then
f = "/" + f
else
f = File.join(file, f)
end
paths.unshift f.untaint
end
end
rescue Errno::ENOENT, Errno::EACCES
end
end
end
end
module_function :stable_find
end
class Pathname # * Find *
def stable_find(&block) # :yield: pathname
if @path == '.'
Find.stable_find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
else
Find.stable_find(@path) {|f| yield self.class.new(f) }
end
end
end