videoreg 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ module Videoreg
2
+ class Util
3
+
4
+ class << self
5
+ def which(cmd)
6
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
7
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
8
+ exts.each { |ext|
9
+ exe = "#{path}/#{cmd}#{ext}"
10
+ return exe if File.executable? exe
11
+ }
12
+ end
13
+ nil
14
+ end
15
+
16
+ def proc_alive?(pid)
17
+ return false unless pid
18
+ begin
19
+ Process.kill(0, pid)
20
+ true
21
+ rescue Errno::ESRCH
22
+ false
23
+ end
24
+ end
25
+
26
+ # Extracts the connection string for the rabbitmq service from the
27
+ # service information provided by Cloud Foundry in an environment
28
+ # variable.
29
+ def amqp_url
30
+ services = JSON.parse(ENV['VCAP_SERVICES'], :symbolize_names => true)
31
+ url = services.values.map do |srvs|
32
+ srvs.map do |srv|
33
+ if srv[:label] =~ /^rabbitmq-/
34
+ srv[:credentials][:url]
35
+ else
36
+ []
37
+ end
38
+ end
39
+ end.flatten!.first
40
+ end
41
+
42
+
43
+ def humanize(secs)
44
+ [[60, :sec], [60, :min], [24, :hr], [1000, :days]].map { |count, name|
45
+ if secs > 0
46
+ secs, n = secs.divmod(count)
47
+ "#{n.to_i}#{name}"
48
+ end
49
+ }.compact.reverse.join(' ')
50
+ end
51
+
52
+
53
+ def udevinfo(maxcount)
54
+ res = []
55
+ maxcount.times do |dnum|
56
+ devpath_parts = nil
57
+ Open4::popen4("udevadm info --query all --name video#{dnum} | grep DEVPATH") do |pid, stdin, stdout, stderr|
58
+ devpath = stdout.read.match(/DEVPATH=(.*)\n$/)
59
+ unless devpath.nil?
60
+ devpath_parts = devpath[1].match(/(\/.*\/)(usb\d+)\/(\d*-\d*)((?:\/.*)?\/video4linux\/)(video(\d+))/)
61
+ end
62
+ end
63
+ unless devpath_parts.nil?
64
+ res << {
65
+ :prefix => devpath_parts[1],
66
+ :usbhub => devpath_parts[2],
67
+ :usbport => devpath_parts[3],
68
+ :postfix => devpath_parts[4],
69
+ :device => "/dev/#{devpath_parts[5]}",
70
+ :devnum => devpath_parts[6]
71
+ }
72
+ end
73
+ end
74
+ res
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ require_relative '../lib/videoreg'
3
+ Dir["#{File.dirname(__FILE__)}/../lib/**/*.rb"].each { |f| require f }
4
+
5
+ RSpec.configure do |config|
6
+ config.color_enabled = true
7
+ config.formatter = 'documentation'
8
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../spec_helper'
2
+ require 'tempfile'
3
+
4
+
5
+ describe Videoreg::Lockfile do
6
+
7
+ it "should create lockfile " do
8
+
9
+ PID = 12345
10
+ tempfile = Tempfile.new('lockfile')
11
+ lock_path = tempfile.path
12
+ tempfile.unlink
13
+ g = Videoreg::Lockfile.new(lock_path)
14
+ g.lock(PID).should be_true
15
+ g.verify.should be_true
16
+ h = Videoreg::Lockfile.new(lock_path)
17
+ h.lock.should be_false
18
+ h.verify.should be_false
19
+ h.release.should be_false
20
+ g.verify.should be_true
21
+ g.lockcode.should == PID.to_s
22
+ g.release.should be_true
23
+ File.exists?(lock_path).should be_false
24
+ end
25
+
26
+ end
@@ -0,0 +1,33 @@
1
+ require_relative '../spec_helper'
2
+ require 'tmpdir'
3
+
4
+ describe Videoreg::Registrar do
5
+
6
+
7
+ it "should clean up old files" do
8
+
9
+ tmpdir = Pathname.new(Dir.tmpdir)
10
+ reg = Videoreg::Registrar.new { |c|
11
+ c.storage = tmpdir.to_s
12
+ c.store_max = 5
13
+ }
14
+
15
+ (reg.config.store_max + 10).times {
16
+ File.open(tmpdir.join("#{Time.now.to_i}.#{Time.now.usec}.avi").to_s, "w+") { |f| f.write('') }
17
+ }
18
+
19
+ lambda {
20
+ reg.clean_old_files!
21
+ }.should change {
22
+ Dir[tmpdir.join('*.avi').to_s].length
23
+ }.to(reg.config.store_max)
24
+
25
+
26
+ lambda {
27
+ reg.clean_old_files!
28
+ }.should_not change {
29
+ Dir[tmpdir.join('*.avi').to_s].length
30
+ }
31
+
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Videoreg::Registrar do
4
+
5
+ it "should be configurable" do
6
+
7
+ subject.configure do |c|
8
+ c.resolution = '640x480'
9
+ c.command = 'test command #{resolution}'
10
+ end
11
+
12
+ subject.resolution.should == '640x480'
13
+ subject.cmd.should == 'test command 640x480'
14
+
15
+ end
16
+ end
@@ -0,0 +1,38 @@
1
+ reg {
2
+ device '/dev/webcam0'
3
+ resolution '640x480'
4
+ fps 25
5
+ duration 60
6
+ filename '#{time}-webcam0.avi'
7
+ storage '/tmp/webcam0'
8
+ lockfile '/tmp/videoreg.webcam0.lock'
9
+ store_max 5
10
+ }
11
+
12
+ reg {
13
+ device '/dev/webcam1'
14
+ resolution '640x480'
15
+ fps 25
16
+ duration 60
17
+ filename '#{time}-webcam1.avi'
18
+ storage '/tmp/webcam1'
19
+ lockfile '/tmp/videoreg.webcam1.lock'
20
+ store_max 3
21
+ }
22
+
23
+ reg {
24
+ device '/dev/webcam2'
25
+ resolution '640x480'
26
+ fps 25
27
+ duration 60
28
+ filename '#{time}-webcam2.avi'
29
+ storage '/tmp/webcam2'
30
+ lockfile '/tmp/videoreg.webcam2.lock'
31
+ store_max 3
32
+ }
33
+
34
+
35
+ log_path '/tmp/videoreg.log'
36
+ pid_path '/tmp/videoreg.pid'
37
+ mq_queue 'com.ifree.videoreg.daemon'
38
+ mq_host 'localhost'
@@ -0,0 +1,22 @@
1
+ pid_path = '/tmp/videoreg.pid'
2
+ log_path = '/tmp/videoreg.log'
3
+
4
+ God.watch do |w|
5
+ w.name = "videoreg"
6
+ w.interval = 30.seconds
7
+ w.start = "videoreg -l #{log_path} -P #{pid_path} -c videoreg-sample-config.rb"
8
+ w.stop = "videoreg -l #{log_path} -P #{pid_path} -c videoreg-sample-config.rb -k"
9
+ w.start_grace = 15.seconds
10
+ w.restart_grace = 15.seconds
11
+ w.pid_file = pid_path
12
+ w.keepalive
13
+
14
+ w.behavior(:clean_pid_file)
15
+
16
+ w.start_if do |start|
17
+ start.condition(:process_running) do |c|
18
+ c.interval = 5.seconds
19
+ c.running = false
20
+ end
21
+ end
22
+ end
data/videoreg.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ require 'pathname'
2
+
3
+ require Pathname.new(File.dirname(File.expand_path(__FILE__))).join('lib/videoreg.rb')
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'videoreg'
7
+ s.version = Videoreg::VERSION
8
+ s.date = '2012-06-22'
9
+ s.authors = ["Ilya Sadykov"]
10
+ s.email = 'i.sadykov@i-free.com'
11
+ s.homepage = ""
12
+ s.summary = %q{Video registration script}
13
+ s.description = %q{Video Registration scripts. Allows to register continuously}
14
+
15
+ s.add_development_dependency 'rspec', '~> 2.10.0'
16
+ s.add_runtime_dependency 'dante'
17
+ s.add_runtime_dependency 'open4'
18
+ s.add_runtime_dependency 'god'
19
+ s.add_runtime_dependency 'amqp'
20
+ s.add_runtime_dependency 'json'
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
25
+ s.require_paths = %w(lib)
26
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: videoreg
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ilya Sadykov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.10.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.10.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: dante
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: open4
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: god
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: amqp
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: json
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Video Registration scripts. Allows to register continuously
111
+ email: i.sadykov@i-free.com
112
+ executables:
113
+ - videoreg
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - Gemfile.lock
120
+ - README.md
121
+ - Rakefile
122
+ - bin/videoreg
123
+ - dist/videoreg-0.1.gem
124
+ - install.sh
125
+ - lib/videoreg.rb
126
+ - lib/videoreg/base.rb
127
+ - lib/videoreg/config.rb
128
+ - lib/videoreg/lockfile.rb
129
+ - lib/videoreg/registrar.rb
130
+ - lib/videoreg/util.rb
131
+ - spec/spec_helper.rb
132
+ - spec/videoreg/lockfile_spec.rb
133
+ - spec/videoreg/registrar_spec.rb
134
+ - spec/videoreg_spec.rb
135
+ - videoreg-sample-config.rb
136
+ - videoreg-sample.god
137
+ - videoreg.gemspec
138
+ homepage: ''
139
+ licenses: []
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.25
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Video registration script
162
+ test_files: []