Class: Videoreg::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/videoreg/util.rb

Class Method Summary collapse

Class Method Details

.amqp_urlObject

Extracts the connection string for the rabbitmq service from the service information provided by Cloud Foundry in an environment variable.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/videoreg/util.rb', line 29

def amqp_url
  services = JSON.parse(ENV['VCAP_SERVICES'], :symbolize_names => true)
  url = services.values.map do |srvs|
    srvs.map do |srv|
      if srv[:label] =~ /^rabbitmq-/
        srv[:credentials][:url]
      else
        []
      end
    end
  end.flatten!.first
end

.humanize(secs) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/videoreg/util.rb', line 43

def humanize(secs)
  [[60, :sec], [60, :min], [24, :hr], [1000, :days]].map { |count, name|
    if secs > 0
      secs, n = secs.divmod(count)
      "#{n.to_i}#{name}"
    end
  }.compact.reverse.join(' ')
end

.proc_alive?(pid) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/videoreg/util.rb', line 16

def proc_alive?(pid)
  return false unless pid
  begin
    Process.kill(0, pid)
    true
  rescue Errno::ESRCH
    false
  end
end

.udevinfo(maxcount) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/videoreg/util.rb', line 53

def udevinfo(maxcount)
  res = []
  maxcount.times do |dnum|
    devpath_parts = nil
    Open4::popen4("udevadm info --query all --name video#{dnum} | grep DEVPATH") do |pid, stdin, stdout, stderr|
      devpath = stdout.read.match(/DEVPATH=(.*)\n$/)
      unless devpath.nil?
        devpath_parts = devpath[1].match(/(\/.*\/)(usb\d+)\/(\d*-\d*)((?:\/.*)?\/video4linux\/)(video(\d+))/)
      end
    end
    unless devpath_parts.nil?
      res << {
          :prefix => devpath_parts[1],
          :usbhub => devpath_parts[2],
          :usbport => devpath_parts[3],
          :postfix => devpath_parts[4],
          :device => "/dev/#{devpath_parts[5]}",
          :devnum => devpath_parts[6]
      }
    end
  end
  res
end

.which(cmd) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/videoreg/util.rb', line 5

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    }
  end
  nil
end