6 # miscellaneous useful functions
9 # turn a number of seconds into a human readable string, e.g
10 # 2 days, 3 hours, 18 minutes, 10 seconds
11 def Utils.secs_to_string(secs)
13 days = (secs / (60 * 60 * 24)).to_i
14 secs = secs % (60 * 60 * 24)
15 hours = (secs / (60 * 60)).to_i
16 secs = (secs % (60 * 60))
17 mins = (secs / 60).to_i
18 secs = (secs % 60).to_i
19 ret += "#{days} days, " if days > 0
20 ret += "#{hours} hours, " if hours > 0 || days > 0
21 ret += "#{mins} minutes and " if mins > 0 || hours > 0 || days > 0
22 ret += "#{secs} seconds"
27 def Utils.safe_exec(command, *args)
30 return p.readlines.join("\n")
36 puts "exec of #{command} led to exception: #{e.inspect}"
39 puts "exec of #{command} failed"
45 # returns a string containing the result of an HTTP GET on the uri
46 def Utils.http_get(uristr, readtimeout=8, opentimeout=4)
48 # ruby 1.7 or better needed for this (or 1.6 and debian unstable)
50 # (so we support the 1_1 api anyway, avoids problems)
52 uri = URI.parse uristr
55 query += "?#{uri.query}"
60 if(ENV['http_proxy'] && proxy_uri = URI.parse(ENV['http_proxy']))
61 proxy_host = proxy_uri.host
62 proxy_port = proxy_uri.port
66 http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port)
67 http.open_timeout = opentimeout
68 http.read_timeout = readtimeout
71 resp = http.get(query)
78 error "Utils.http_get exception: #{e.inspect}, while trying to get #{uristr}"