Module: ALib::Util
- Defined in:
- lib/alib-0.4.0/util.rb
Overview
the utility module is a namespace for things which otherwise wouldn’t have a home. the methods of Util can be used as module methods or included into a class and then used as instance OR class methods
Class Method Summary collapse
-
.append_features(c) ⇒ Object
–}}}.
-
.export(*syms) ⇒ Object
–{{{.
Instance Method Summary collapse
-
#alive(pid) ⇒ Object
(also: #alive?)
returns true if pid is running, false otherwise.
-
#argv_split(argv) ⇒ Object
pop of options from an arglist.
-
#atoi(s, opts = {}) ⇒ Object
convert a string to an integer.
- #atomic_copy(src, dst) ⇒ Object
-
#attempt(label = 'attempt') ⇒ Object
is aborted.
-
#btrace(e) ⇒ Object
format exception backtrace as string.
- #camel_case(string) ⇒ Object
- #child_object ⇒ Object
-
#columnize(buf, opts = {}) ⇒ Object
option (default 0).
-
#constant_get(hierachy) ⇒ Object
a better const_get.
- #curl(uri, opts = {}) ⇒ Object
-
#defval(var, opts = {}) ⇒ Object
returning the option default, or nil.
-
#delopt(opt, hash, default = nil) ⇒ Object
(also: #delete_opt, #extract_opt, #extractopt, #xopt)
returning the value or, if key is not found, nil or default.
-
#emsg(e) ⇒ Object
format exception as Logger class does - no backtrace.
-
#erreq(a, b) ⇒ Object
determine equality of two exceptions.
-
#errmsg(e) ⇒ Object
format exception as Logger class does - with backtrace.
-
#escape(s, char, esc) ⇒ Object
new copy of s with any occurances of char escaped with esc.
-
#escape!(s, char, esc) ⇒ Object
escapes any occurances of char in s with esc modifying s inplace.
-
#exec(*args, &block) ⇒ Object
an quiet exec.
- #expand(string, opts = {}) ⇒ Object
-
#expand!(string, vars = {}) ⇒ Object
expand variables in a string destructively (to the string).
-
#find(*a, &b) ⇒ Object
shortcut to Find2.
-
#find2(*a, &b) ⇒ Object
shortcut to Find2.
-
#fork(*args, &block) ⇒ Object
a quiet fork.
-
#getopt(opt, hash, default = nil) ⇒ Object
(also: #get_opt)
returning the value or, if key is not found, nil or default.
-
#give_up!(label = 'attempt') ⇒ Object
(also: #giveup!, #give_up)
see attempt.
-
#hashify(*hashes) ⇒ Object
collect n hashed into one hash - later keys overried earlier keys.
-
#hasopt(opt, hash, default = false) ⇒ Object
(also: #has_opt, #hasopt?, #has_opt?)
see getopt.
-
#hms(s) ⇒ Object
explode a.
-
#host ⇒ Object
lookup, and cache, the host (first bit of hostname quad).
-
#hostname ⇒ Object
lookup, and cache, the hostname.
-
#klass ⇒ Object
self.class.
-
#klass_stamp(hierachy, *a, &b) ⇒ Object
creates a class by class name.
-
#maim(pid, opts = {}) ⇒ Object
and 4 respectively.
-
#mcp(obj) ⇒ Object
marshal’d ‘deep’ copy of obj.
-
#md ⇒ Object
declare multi-dimensional arrays as in md.
-
#optfilter(*list) ⇒ Object
pull out options from arglist.
- #parse_timespec(spec) ⇒ Object
-
#prognam ⇒ Object
the basename of $0.
-
#prune ⇒ Object
shortcut to Find2.prune.
-
#rangemod(n, ir) ⇒ Object
bin a integer into a int range using modulo logic.
-
#realpath(path) ⇒ Object
File::expand_path + link resolution.
-
#require_version(version) ⇒ Object
require_version ‘1.8.0’.
-
#singleton_class(&b) ⇒ Object
evaluates block in singleton scope or simply returns singleton_class.
- #snake_case(string) ⇒ Object
-
#splitpath(path) ⇒ Object
split a path into dirname, basename, extension.
-
#stamptime(string, opts = {}) ⇒ Object
TODO - 1.8.4 parses usec but 1.8.2 does not.
-
#strtod(s, opts = {}) ⇒ Object
convert a string to an integer of any base.
-
#system(*args, &block) ⇒ Object
a quiet system.
-
#timestamp(arg = Time::now) ⇒ Object
nospace option - useful for constructing filenames.
-
#tmpdir(*argv) ⇒ Object
generate a temporary directory.
-
#tmpnam(*argv) ⇒ Object
basename.
-
#try_again!(label = 'attempt') ⇒ Object
(also: #try_again, #again!)
see attempt.
-
#uncache(file) ⇒ Object
make best effort to invalidate any inode caching done by nfs clients.
- #unindent(s) ⇒ Object
-
#unindent!(s) ⇒ Object
find natural left margin of a here-doc and un-indent it.
-
#unzip(path, z_pat = nil) ⇒ Object
(also: #gunzip)
unzip a file - return unzipped name.
-
#unzipped(path, z_pat = %r/\.(?:z|gz)$/io) ⇒ Object
handle a pathname after unzipping (iff needed).
-
#unzipped_name(path, z_pat = nil) ⇒ Object
return the unzipped name of a path.
- #version_cmp(a, b) ⇒ Object
-
#which_ruby ⇒ Object
determine path of current ruby interpreter (argv in c).
-
#zip(path, z_ext = nil) ⇒ Object
(also: #gzip)
zip a file - return zipped name.
-
#zipped?(path, z_pat = %r/\.(?:z|gz)$/io) ⇒ Boolean
(also: #gzipped?, #zipped)
guess if a file is zipped based on pathname.
-
#zipped_name(path, z_ext = nil) ⇒ Object
return the zipped name of a path.
Class Method Details
.append_features(c) ⇒ Object
–}}}
19 20 21 22 23 24 |
# File 'lib/alib-0.4.0/util.rb', line 19 def append_features c #--{{{ super c.extend self #--}}} end |
.export(*syms) ⇒ Object
–{{{
10 11 12 13 14 15 16 17 18 |
# File 'lib/alib-0.4.0/util.rb', line 10 def export(*syms) #--{{{ syms.each do |sym| sym = "#{ sym }".intern module_function sym public sym end #--}}} end |
Instance Method Details
#alive(pid) ⇒ Object Also known as: alive?
returns true if pid is running, false otherwise
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/alib-0.4.0/util.rb', line 191 def alive pid #--{{{ pid = Integer("#{ pid }") begin Process::kill 0, pid true rescue Errno::ESRCH false end #--}}} end |
#argv_split(argv) ⇒ Object
pop of options from an arglist
744 745 746 747 748 749 750 |
# File 'lib/alib-0.4.0/util.rb', line 744 def argv_split(argv) #--{{{ args = argv opts = Hash === args.last ? args.pop : {} [args, opts] #--}}} end |
#atoi(s, opts = {}) ⇒ Object
convert a string to an integer
876 877 878 879 880 |
# File 'lib/alib-0.4.0/util.rb', line 876 def atoi(s, opts = {}) #--{{{ strtod("#{ s }".gsub(%r/^0+/,''), 'base' => 10) #--}}} end |
#atomic_copy(src, dst) ⇒ Object
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 |
# File 'lib/alib-0.4.0/util.rb', line 1059 def atomic_copy src, dst #--{{{ f, fu = File, FileUtils src_dirname, src_basename = f.split src dst = f.join dst, src_basename if test ?d, dst dst_dirname, dst_basename = f.split dst tmp = f.join dst_dirname, ".#{ src_basename }.#{ hostname }.tmp" begin fu.cp_r src, tmp, :preserve => true begin fu.mv tmp, dst rescue uncache tmp rescue nil uncache dst rescue nil sleep 42 fu.mv tmp, dst end ensure fu.rm_f tmp end dst #--}}} end |
#attempt(label = 'attempt') ⇒ Object
is aborted. calls to attempt may be nested.
647 648 649 650 651 652 653 654 |
# File 'lib/alib-0.4.0/util.rb', line 647 def attempt label = 'attempt' #--{{{ ret = nil n_attempts = 0 loop{ break unless catch("#{ label }"){ ret = yield(n_attempts += 1) } == 'try_again' } ret #--}}} end |
#btrace(e) ⇒ Object
format exception backtrace as string
452 453 454 455 456 |
# File 'lib/alib-0.4.0/util.rb', line 452 def btrace e #--{{{ (e.backtrace or []).join("\n") #--}}} end |
#camel_case(string) ⇒ Object
1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'lib/alib-0.4.0/util.rb', line 1049 def camel_case string #--{{{ return string if string =~ %r/[A-Z]/ and string !~ %r/_/ words = string.strip.split %r/\s*_+\s*/ words.map!{|w| w.downcase.sub(%r/^./){|c| c.upcase}} words.join #--}}} end |
#child_object ⇒ Object
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 |
# File 'lib/alib-0.4.0/util.rb', line 1089 def child_object #--{{{ r, w = IO.pipe IO.popen('-') do |pipe| if pipe w.close buf = pipe.read Process.wait unless $? == 0 loaded = Marshal.load r.read case loaded when Exception raise loaded else klass, , backtrace = loaded e = klass.new e.set_backtrace backtrace raise e end end Marshal.load(buf) else r.close begin print(Marshal.dump(yield)) rescue Exception => e dumped = begin Marshal.dump e # we can't do this in ruby 1.8.1 rescue Marshal.dump [e.class, e., e.backtrace] end w.write dumped exit! 42 end end end ensure r.close rescue nil #--}}} end |
#columnize(buf, opts = {}) ⇒ Object
option (default 0)
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/alib-0.4.0/util.rb', line 598 def columnize buf, opts = {} #--{{{ width = getopt 'width', opts, 80 indent = getopt 'indent', opts indent = Fixnum === indent ? (' ' * indent) : "#{ indent }" column = [] words = buf.split %r/\s+/o row = "#{ indent }" while((word = words.shift)) if((row.size + word.size) < (width - 1)) row << word else column << row row = "#{ indent }" row << word end row << ' ' unless row.size == (width - 1) end column << row unless row.strip.empty? column.join "\n" #--}}} end |
#constant_get(hierachy) ⇒ Object
a better const_get
681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/alib-0.4.0/util.rb', line 681 def constant_get(hierachy) #--{{{ ancestors = hierachy.split(%r/::/) parent = Object while((child = ancestors.shift)) klass = parent.const_get child parent = klass end klass #--}}} end |
#curl(uri, opts = {}) ⇒ Object
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 |
# File 'lib/alib-0.4.0/util.rb', line 1132 def curl uri, opts = {} #--{{{ retries = begin;(getopt 'retries', opts, 42).to_i;rescue NoMethodError;0;end wait = Float(getopt('wait', opts, 2)) begin uri = URI === uri ? uri : URI::parse(uri.to_s) req = Net::HTTP::Get.new(uri.path || 'index.html') res = Net::HTTP::start(uri.host, uri.port){|http| http.request(req)} res.body rescue Timeout::Error, Errno::ECONNREFUSED => e if retries > 0 retries -= 1 #Kernel::warn(errmsg(e)) sleep wait retry else raise end end #--}}} end |
#defval(var, opts = {}) ⇒ Object
returning the option default, or nil
627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/alib-0.4.0/util.rb', line 627 def defval var, opts = {} #--{{{ default = getopt 'default', opts, nil c = getopt 'class', opts, klass d0, d1 = "default_#{ var }".intern, "#{ var }".intern c0, c1 = "DEFAULT_#{ var }".upcase.intern, "#{ var }".upcase.intern [d0, d1].each{|x| return(c.send(x)) if c.respond_to?(x)} [c0, c1].each{|x| return(c.const_get(x)) if c.const_defined?(x)} return default #--}}} end |
#delopt(opt, hash, default = nil) ⇒ Object Also known as: delete_opt, extract_opt, extractopt, xopt
returning the value or, if key is not found, nil or default
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/alib-0.4.0/util.rb', line 164 def delopt opt, hash, default = nil #--{{{ keys = opt.respond_to?('each') ? opt : [opt] keys.each do |key| return hash.delete(key) if hash.has_key? key key = "#{ key }" return hash.delete(key) if hash.has_key? key key = key.intern return hash.delete(key) if hash.has_key? key end return default #--}}} end |
#emsg(e) ⇒ Object
format exception as Logger class does - no backtrace
443 444 445 446 447 |
# File 'lib/alib-0.4.0/util.rb', line 443 def emsg e #--{{{ "#{ e.message } - (#{ e.class })" #--}}} end |
#erreq(a, b) ⇒ Object
determine equality of two exceptions
470 471 472 473 474 475 476 |
# File 'lib/alib-0.4.0/util.rb', line 470 def erreq a, b #--{{{ a.class == b.class and a. == b. and a.backtrace == b.backtrace #--}}} end |
#errmsg(e) ⇒ Object
format exception as Logger class does - with backtrace
461 462 463 464 465 |
# File 'lib/alib-0.4.0/util.rb', line 461 def errmsg e #--{{{ emsg(e) << "\n" << btrace(e) #--}}} end |
#escape(s, char, esc) ⇒ Object
new copy of s with any occurances of char escaped with esc
371 372 373 374 375 |
# File 'lib/alib-0.4.0/util.rb', line 371 def escape s, char, esc #--{{{ escape! "#{ s }", char, esc #--}}} end |
#escape!(s, char, esc) ⇒ Object
escapes any occurances of char in s with esc modifying s inplace
358 359 360 361 362 363 364 365 366 |
# File 'lib/alib-0.4.0/util.rb', line 358 def escape! s, char, esc #--{{{ # re = %r/([#{ 0x5c.chr << esc }]*)#{ char }/ re = %r/([#{ Regexp::quote esc }]*)#{ Regexp::quote char }/ s.gsub!(re) do (($1.size % 2 == 0) ? ($1 << esc) : $1) + char end #--}}} end |
#exec(*args, &block) ⇒ Object
an quiet exec
395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/alib-0.4.0/util.rb', line 395 def exec(*args, &block) #--{{{ begin verbose = $VERBOSE $VERBOSE = nil Kernel::exec(*args, &block) ensure $VERBOSE = verbose end #--}}} end |
#expand(string, opts = {}) ⇒ Object
957 958 959 960 961 |
# File 'lib/alib-0.4.0/util.rb', line 957 def string, opts = {} #--{{{ string.dup, opts #--}}} end |
#expand!(string, vars = {}) ⇒ Object
expand variables in a string destructively (to the string)
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 |
# File 'lib/alib-0.4.0/util.rb', line 936 def string, vars = {} #--{{{ loop do changed = false vars.each do |var, value| var.gsub! %r/[^a-zA-Z0-9_]/, '' [ %r/\$#{ var }\b/, %r/\@#{ var }\b/, %r/\${\s*#{ var }\s*}/, %r/\@{\s*#{ var }\s*}/ ].each do |pat| changed = string.gsub! pat, "#{ value }" end end break unless changed end string #--}}} end |
#find(*a, &b) ⇒ Object
shortcut to Find2
705 706 707 708 709 710 |
# File 'lib/alib-0.4.0/util.rb', line 705 def find(*a, &b) #--{{{ a << '.' if a.empty? ALib::Find2::find(*a, &b) #--}}} end |
#find2(*a, &b) ⇒ Object
shortcut to Find2
715 716 717 718 719 720 |
# File 'lib/alib-0.4.0/util.rb', line 715 def find2(*a, &b) #--{{{ a << '.' if a.empty? ALib::Find2::find2(*a, &b) #--}}} end |
#fork(*args, &block) ⇒ Object
a quiet fork
380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/alib-0.4.0/util.rb', line 380 def fork(*args, &block) #--{{{ begin verbose = $VERBOSE $VERBOSE = nil Process::fork(*args, &block) ensure $VERBOSE = verbose end #--}}} end |
#getopt(opt, hash, default = nil) ⇒ Object Also known as: get_opt
returning the value or, if key is not found, nil or default
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/alib-0.4.0/util.rb', line 116 def getopt opt, hash, default = nil #--{{{ keys = opt.respond_to?('each') ? opt : [opt] keys.each do |key| return hash[key] if hash.has_key? key key = "#{ key }" return hash[key] if hash.has_key? key key = key.intern return hash[key] if hash.has_key? key end return default #--}}} end |
#give_up!(label = 'attempt') ⇒ Object Also known as: giveup!, give_up
see attempt
670 671 672 673 674 |
# File 'lib/alib-0.4.0/util.rb', line 670 def give_up! label = 'attempt' #--{{{ throw "#{ label }", 'give_up' #--}}} end |
#hashify(*hashes) ⇒ Object
collect n hashed into one hash - later keys overried earlier keys
106 107 108 109 110 |
# File 'lib/alib-0.4.0/util.rb', line 106 def hashify(*hashes) #--{{{ hashes.inject(accum={}){|accum,hash| accum.update hash} #--}}} end |
#hasopt(opt, hash, default = false) ⇒ Object Also known as: has_opt, hasopt?, has_opt?
see getopt
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/alib-0.4.0/util.rb', line 138 def hasopt opt, hash, default = false #--{{{ keys = opt.respond_to?('each') ? opt : [opt] keys.each do |key| return key if hash.has_key? key key = "#{ key }" return key if hash.has_key? key key = key.intern return key if hash.has_key? key end return default #--}}} end |
#hms(s) ⇒ Object
explode a
925 926 927 928 929 930 931 |
# File 'lib/alib-0.4.0/util.rb', line 925 def hms s #--{{{ h, s = s.divmod 3600 m, s = s.divmod 60 [h.to_i, m.to_i, s] #--}}} end |
#host ⇒ Object
lookup, and cache, the host (first bit of hostname quad)
434 435 436 437 438 |
# File 'lib/alib-0.4.0/util.rb', line 434 def host #--{{{ @__host__ ||= hostname.gsub(%r/\..*$/o,'') #--}}} end |
#hostname ⇒ Object
lookup, and cache, the hostname
425 426 427 428 429 |
# File 'lib/alib-0.4.0/util.rb', line 425 def hostname #--{{{ @__hostname__ ||= Socket::gethostname #--}}} end |
#klass ⇒ Object
self.class
73 74 75 76 77 |
# File 'lib/alib-0.4.0/util.rb', line 73 def klass #--{{{ Module === self ? self : self.class #--}}} end |
#klass_stamp(hierachy, *a, &b) ⇒ Object
creates a class by class name
696 697 698 699 700 |
# File 'lib/alib-0.4.0/util.rb', line 696 def klass_stamp(hierachy, *a, &b) #--{{{ constant_get(hierachy)::new(*a, &b) #--}}} end |
#maim(pid, opts = {}) ⇒ Object
and 4 respectively
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/alib-0.4.0/util.rb', line 211 def maim(pid, opts = {}) #--{{{ sigs = getopt 'signals', opts, %w(SIGTERM SIGQUIT SIGKILL) suspend = getopt 'suspend', opts, 4 pid = Integer("#{ pid }") existed = false sigs.each do |sig| begin Process::kill(sig, pid) existed = true rescue Errno::ESRCH unless existed return nil else return true end end return true unless alive?(pid) sleep suspend return true unless alive?(pid) end return(not alive?(pid)) #--}}} end |
#mcp(obj) ⇒ Object
marshal’d ‘deep’ copy of obj
64 65 66 67 68 |
# File 'lib/alib-0.4.0/util.rb', line 64 def mcp obj #--{{{ Marshal.load(Marshal.dump(obj)) #--}}} end |
#md ⇒ Object
declare multi-dimensional arrays as in md
976 977 978 979 980 981 |
# File 'lib/alib-0.4.0/util.rb', line 976 def md #--{{{ @__md__ ||= lambda{|*ds| Array::new(ds.shift||0).map{md[*ds] unless ds.empty?}} #--}}} end |
#optfilter(*list) ⇒ Object
pull out options from arglist
734 735 736 737 738 739 |
# File 'lib/alib-0.4.0/util.rb', line 734 def optfilter(*list) #--{{{ args, opts = [ list ].flatten.partition{|item| not Hash === item} [args, hashify(*opts)] #--}}} end |
#parse_timespec(spec) ⇒ Object
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 |
# File 'lib/alib-0.4.0/util.rb', line 1008 def parse_timespec spec #--{{{ ret = nil pat = %r/(\d+(?:\.\d+)?)\s*([sSmMhHdD][^\d]*)?/ begin "#{ spec }".scan(pat) do |m| n = Float m[0] unit = m[1] if unit factor = case unit when %r/^m/i 60 when %r/^h/i 60 * 60 when %r/^d/i 60 * 60 * 24 else 1 end n *= factor end ret ||= 0.0 ret += n end rescue raise "bad time spec <#{ spec }>" end ret #--}}} end |
#prognam ⇒ Object
the basename of $0
55 56 57 58 59 |
# File 'lib/alib-0.4.0/util.rb', line 55 def prognam #--{{{ File::basename $0 #--}}} end |
#prune ⇒ Object
shortcut to Find2.prune
725 726 727 728 729 |
# File 'lib/alib-0.4.0/util.rb', line 725 def prune #--{{{ ALib::Find2.prune #--}}} end |
#rangemod(n, ir) ⇒ Object
bin a integer into a int range using modulo logic
885 886 887 888 889 890 891 892 893 894 895 896 |
# File 'lib/alib-0.4.0/util.rb', line 885 def rangemod n, ir #--{{{ a, b = Integer(ir.first), Integer(ir.last) a, b = b, a if a >= b exclusive = ir.exclude_end? size = b - a size += 1 unless exclusive offset = a - 0 x = (n + offset).modulo size x += offset #--}}} end |
#realpath(path) ⇒ Object
File::expand_path + link resolution
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/alib-0.4.0/util.rb', line 92 def realpath path #--{{{ path = File:: "#{ path }" begin Pathname.new(path).realpath.to_s rescue Errno::ENOENT, Errno::ENOTDIR path end #--}}} end |
#require_version(version) ⇒ Object
require_version ‘1.8.0’
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/alib-0.4.0/util.rb', line 31 def require_version version #--{{{ major, minor, teeny = "#{ version }".split(%r/\./o).map{|n| Integer(n)} required = "#{ major }.#{ minor }.#{ teeny }" _major, _minor, _teeny = %w( MAJOR MINOR TEENY ).map{|k| Integer(::Config::CONFIG[k])} actual = "#{ _major }.#{ _minor }.#{ _teeny }" unless _major > major or _major == major and _minor >= minor STDERR.puts("=" * 79) STDERR.puts "this program requires a ruby version >= <#{ required }>" STDERR.puts STDERR.puts "you are currenlty running ruby version <#{ actual }>" STDERR.puts STDERR.puts "possible problems which could cause this are:" STDERR.puts " - improper PATH environment variable setting" STDERR.puts " - ruby > <#{ required }> has not been installed" STDERR.puts("=" * 79) exit 1 end #--}}} end |
#singleton_class(&b) ⇒ Object
evaluates block in singleton scope or simply returns singleton_class
82 83 84 85 86 87 88 |
# File 'lib/alib-0.4.0/util.rb', line 82 def singleton_class &b #--{{{ sc = class << self; self; end b ? sc.module_eval(&b) : sc #--}}} end |
#snake_case(string) ⇒ Object
1041 1042 1043 1044 1045 1046 |
# File 'lib/alib-0.4.0/util.rb', line 1041 def snake_case string #--{{{ return string unless string =~ %r/[A-Z]/ string.reverse.scan(%r/[A-Z]+|[^A-Z]*[A-Z]+?/).reverse.map{|word| word.reverse.downcase}.join '_' #--}}} end |
#splitpath(path) ⇒ Object
split a path into dirname, basename, extension
755 756 757 758 759 760 761 |
# File 'lib/alib-0.4.0/util.rb', line 755 def splitpath path #--{{{ path = "#{ path }" dirname, basename = File::split path [ dirname, (%r/^([^\.]*)(.*)$/).match(basename)[1,2] ].flatten #--}}} end |
#stamptime(string, opts = {}) ⇒ Object
TODO - 1.8.4 parses usec but 1.8.2 does not. handle differently.
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 |
# File 'lib/alib-0.4.0/util.rb', line 284 def stamptime string, opts = {} #--{{{ tz = string[ %r/-\d\d:\d\d\s*$/ ] time = nil if opts.empty? or tz u = string[%r/\.\d+/] string[%r/\.\d+/] = '' if u time = Time::parse string time += u.to_f if u else local = getopt 'local', opts, false string = "#{ string }" pat = %r/^\s*(\d\d\d\d)-(\d\d)-(\d\d)[\s_tT]+(\d\d):(\d\d):(\d\d)(?:.(\d+))?\s*$/o match = pat.match string raise ArgumentError, "<#{ string.inspect }>" unless match yyyy,mm,dd,h,m,s,u = match.to_a[1..-1].map{|m| (m || 0).to_i} if local time = Time::local yyyy,mm,dd,h,m,s,u else time = Time::gm yyyy,mm,dd,h,m,s,u end end return time #--}}} end |
#strtod(s, opts = {}) ⇒ Object
convert a string to an integer of any base
858 859 860 861 862 863 864 865 866 867 868 869 870 871 |
# File 'lib/alib-0.4.0/util.rb', line 858 def strtod(s, opts = {}) #--{{{ base = getopt 'base', opts, 10 case base when 2 s = "0b#{ s }" unless s =~ %r/^0b\d/ when 8 s = "0#{ s }" unless s =~ %r/^0\d/ when 16 s = "0x#{ s }" unless s =~ %r/^0x\d/ end Integer s #--}}} end |
#system(*args, &block) ⇒ Object
a quiet system
410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/alib-0.4.0/util.rb', line 410 def system(*args, &block) #--{{{ begin verbose = $VERBOSE $VERBOSE = nil Kernel::system(*args, &block) ensure $VERBOSE = verbose end #--}}} end |
#timestamp(arg = Time::now) ⇒ Object
nospace option - useful for constructing filenames
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 |
# File 'lib/alib-0.4.0/util.rb', line 241 def arg = Time::now #--{{{ if Time === arg arg.iso8601 2 else opts = case arg when Hash opts = arg when Time {'time' => arg} else raise ArgumentError, "#{ arg.inspect } (#{ arg.class })" end time = getopt 'time', opts, Time::now local = getopt 'local', opts, false nospace = getopt('nospace', opts, getopt('no_space', opts, false)) dateonly = getopt('dateonly', opts, getopt('date_only', opts, false)) time = time.utc unless local usec = "#{ time.usec }" usec << ('0' * (6 - usec.size)) if usec.size < 6 stamp = unless dateonly time.strftime('%Y-%m-%d %H:%M:%S.') << usec else time.strftime('%Y-%m-%d') end if nospace spc = TrueClass === nospace ? 'T' : "#{ nospace }" stamp.gsub! %r/\s+/, spc end stamp end #--}}} end |
#tmpdir(*argv) ⇒ Object
generate a temporary directory
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/alib-0.4.0/util.rb', line 531 def tmpdir(*argv) #--{{{ args, opts = argv_split argv retries = getopt 'retries', opts, 42 turd = getopt 'turd', opts dir = args.first || getopt(%w(dir base prefix), opts) dir ||= ENV['ALIB_TMP'] || ENV['ALIB_TEMP'] dir ||= '.' opts['dir'] = dir d = nil retries.times do td = tmpnam(opts) break if ((d = FileUtils::mkdir_p td rescue nil)) end raise "surpassed max retries <#{ retries }>" unless d d = File:: d delete = lambda do Dir::glob(File::join(d, "**")).each{|e| FileUtils::rm_rf e} FileUtils::rm_rf d end if block_given? cwd = Dir::pwd begin Dir::chdir d yield [cwd,d] ensure Dir::chdir cwd if cwd delete.call unless turd end else at_exit &delete unless turd return d end #--}}} end |
#tmpnam(*argv) ⇒ Object
basename
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/alib-0.4.0/util.rb', line 482 def tmpnam(*argv) #--{{{ args, opts = argv_split argv dirname = argv.shift || getopt(%w(dir base prefix), opts, '.') seed = getopt 'seed', opts, prognam reap = getopt 'reap', opts, true dirname = File. dirname seed = seed.gsub(%r/[^0-9a-zA-Z]/,'_').gsub(%r/\s+/, '') host = hostname.gsub(%r/\./, '_') if reap begin baseglob = "%s__*__*__*__%s" % [ host, seed ] glob = File.join(dirname, baseglob) host_re = %r/^#{ host }$/ candidates = Dir[glob] candidates.each do |candidate| basename = File.basename candidate parts = basename.split %r/__/, 5 if parts[0] =~ host_re pid = Integer parts[1] unless alive? pid FileUtils.rm_rf candidate end end end rescue => e warn(errmsg(e)) rescue nil end end basename = "%s__%s__%s__%s__%s" % [ host, Process::pid, ('nospace' => true), rand, seed, ] File.join(dirname, basename) #--}}} end |
#try_again!(label = 'attempt') ⇒ Object Also known as: try_again, again!
see attempt
659 660 661 662 663 |
# File 'lib/alib-0.4.0/util.rb', line 659 def try_again! label = 'attempt' #--{{{ throw "#{ label }", 'try_again' #--}}} end |
#uncache(file) ⇒ Object
make best effort to invalidate any inode caching done by nfs clients
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/alib-0.4.0/util.rb', line 573 def uncache file #--{{{ refresh = nil begin is_a_file = File === file path = (is_a_file ? file.path : file.to_s) stat = (is_a_file ? file.stat : File::stat(file.to_s)) refresh = tmpnam(File::dirname(path)) File::link path, refresh rescue File::symlink path, refresh File::chmod stat.mode, path File::utime stat.atime, stat.mtime, path open(File::dirname(path)){|d| d.fsync rescue nil} ensure begin File::unlink refresh if refresh rescue Errno::ENOENT end end #--}}} end |
#unindent(s) ⇒ Object
999 1000 1001 1002 1003 1004 1005 |
# File 'lib/alib-0.4.0/util.rb', line 999 def unindent s #--{{{ s = "#{ s }" unindent! s s #--}}} end |
#unindent!(s) ⇒ Object
find natural left margin of a here-doc and un-indent it
986 987 988 989 990 991 992 993 994 995 996 |
# File 'lib/alib-0.4.0/util.rb', line 986 def unindent! s #--{{{ indent = nil s.each do |line| next if line =~ %r/^\s*$/ indent = line[%r/^\s*/] and break end s.gsub! %r/^#{ indent }/, "" if indent indent ? s : nil #--}}} end |
#unzip(path, z_pat = nil) ⇒ Object Also known as: gunzip
unzip a file - return unzipped name
816 817 818 819 820 821 822 823 824 825 826 827 828 |
# File 'lib/alib-0.4.0/util.rb', line 816 def unzip path, z_pat = nil #--{{{ z_pat ||= %r/\.(?:z|gz)$/io spawn "gzip --force --decompress #{ path }" if zipped?(path, z_pat) #spawn "gzip --force --decompress #{ path }" uzn = unzipped_name path, z_pat if block_given? yield uzn else uzn end #--}}} end |
#unzipped(path, z_pat = %r/\.(?:z|gz)$/io) ⇒ Object
handle a pathname after unzipping (iff needed)
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/alib-0.4.0/util.rb', line 766 def unzipped path, z_pat = %r/\.(?:z|gz)$/io #--{{{ zipped = zipped?(path, z_pat) unless zipped yield path else unzipped = unzip path begin yield unzipped ensure zip unzipped end end #--}}} end |
#unzipped_name(path, z_pat = nil) ⇒ Object
return the unzipped name of a path
835 836 837 838 839 840 |
# File 'lib/alib-0.4.0/util.rb', line 835 def unzipped_name path, z_pat = nil #--{{{ z_pat ||= %r/\.(?:z|gz)$/io path.gsub z_pat, '' #--}}} end |
#version_cmp(a, b) ⇒ Object
310 311 312 313 314 315 |
# File 'lib/alib-0.4.0/util.rb', line 310 def version_cmp a, b #--{{{ to_v = lambda{|v| v.to_s.scan(%r/\d+/).map{|c| Integer c}} to_v[a] <=> to_v[b] #--}}} end |
#which_ruby ⇒ Object
determine path of current ruby interpreter (argv in c)
966 967 968 969 970 971 |
# File 'lib/alib-0.4.0/util.rb', line 966 def which_ruby #--{{{ c = ::Config::CONFIG File::join(c['bindir'], c['ruby_install_name']) << c['EXEEXT'] #--}}} end |
#zip(path, z_ext = nil) ⇒ Object Also known as: gzip
zip a file - return zipped name
785 786 787 788 789 790 791 792 793 794 795 796 797 798 |
# File 'lib/alib-0.4.0/util.rb', line 785 def zip path, z_ext = nil #--{{{ z_ext ||= '.gz' z_ext.gsub! %r/^\s*\.+/, '.' spawn "gzip --suffix #{ z_ext } --force #{ path }" zipped = "#{ path }#{ z_ext }" raise "could not create <#{ zipped }>" unless test ?e, zipped if block_given? yield zipped else zipped end #--}}} end |
#zipped?(path, z_pat = %r/\.(?:z|gz)$/io) ⇒ Boolean Also known as: gzipped?, zipped
guess if a file is zipped based on pathname
845 846 847 848 849 |
# File 'lib/alib-0.4.0/util.rb', line 845 def zipped? path, z_pat = %r/\.(?:z|gz)$/io #--{{{ path =~ z_pat #--}}} end |
#zipped_name(path, z_ext = nil) ⇒ Object
return the zipped name of a path
805 806 807 808 809 810 811 |
# File 'lib/alib-0.4.0/util.rb', line 805 def zipped_name path, z_ext = nil #--{{{ z_ext ||= '.gz' z_ext.gsub! %r/^\s*\.+/, '.' "#{ path }#{ z_ext }" #--}}} end |