uatu 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/uatu +42 -0
  2. data/lib/uatu.rb +80 -0
  3. metadata +58 -0
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'uatu'
4
+ require 'optparse'
5
+
6
+ options = {
7
+ cmd: false,
8
+ script: false,
9
+ path: '.',
10
+ pattern: false,
11
+ exclude: false,
12
+ time: false,
13
+ }
14
+ OptionParser.new do |opts|
15
+ opts.banner= "Usage: uatu "
16
+
17
+ opts.on('-c CMD', 'Command to run') do |cmd|
18
+ options[:cmd] = cmd
19
+ end
20
+ opts.on('-s SCRIPT', 'Script to run') do |script|
21
+ options[:script] = File.realpath(script)
22
+ end
23
+ opts.on('-b BASE', 'Path to watch default: "."') do |path|
24
+ options[:path] = path
25
+ end
26
+ opts.on('-p', '--patern PATTERN', 'Only trigger when file matches PATTRN') do |pattern|
27
+ options[:pattern] = pattern
28
+ end
29
+ opts.on('-x', '--exclude PATTERN', 'Exclude some files') do |exclude|
30
+ options[:exclude] = exclude
31
+ end
32
+ opts.on('-w', '--wait TIME', 'Minimum time between triggers') do |time|
33
+ begin
34
+ options[:time] = time.to_i
35
+ rescue
36
+ puts "Error: '%s' is not a integer" % [time.to_s]
37
+ exit 255
38
+ end
39
+ end
40
+ end.parse!
41
+
42
+ Uatu.run(options)
@@ -0,0 +1,80 @@
1
+ require 'fssm'
2
+
3
+ class Uatu
4
+
5
+ def self.run(options)
6
+ new(options).run!
7
+ end
8
+
9
+ def initialize(options)
10
+ @options = options
11
+ end
12
+
13
+ def run!
14
+ unless @options[:cmd] or @options[:script]
15
+ puts "'-s SCRIPT' or '-c CMD' arguments required !"
16
+ exit 1
17
+ end
18
+
19
+ puts "Watcher started"
20
+ @last_update = Time.now
21
+ FSSM.monitor(@options[:path]) do |mon|
22
+ mon.update &(trigger_block :update)
23
+ mon.delete &(trigger_block :delete)
24
+ mon.create &(trigger_block :create)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def trigger_block(evt)
31
+ proc { |base, rel|
32
+ trigger base, rel, evt
33
+ }
34
+ end
35
+
36
+ def trigger(base, rel, evt)
37
+ path = File.realpath(rel, base)
38
+ return if exclude? path
39
+ return unless time_elapsed?
40
+
41
+ puts "Changed %s" % [rel]
42
+
43
+ if include? path
44
+ run_script path if @options[:script]
45
+ run_command path if @options[:cmd]
46
+ @last_update = Time.now
47
+ end
48
+ end
49
+
50
+ def exclude? (path)
51
+ return false unless @options[:exclude]
52
+ File.fnmatch(@options[:exclude], path)
53
+ end
54
+
55
+ def include? (path)
56
+ return true unless @options[:pattern]
57
+ File.fnmatch(@options[:pattern], path)
58
+ end
59
+
60
+ def time_elapsed?
61
+ return true unless @options[:time]
62
+
63
+ @last_update + @options[:time] <= Time.now
64
+ end
65
+
66
+ def run_script(change_path)
67
+ puts "Triggered '%s'" % [@options[:script]]
68
+ fork do
69
+ exec("%s %s" % [@options[:script], change_path])
70
+ end
71
+ end
72
+
73
+ def run_command(change_path)
74
+ puts "Triggered '%s'" % [@options[:cmd]]
75
+ fork do
76
+ exec(@options[:cmd] % [change_path])
77
+ end
78
+ end
79
+
80
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uatu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy David
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fssm
16
+ requirement: &9321440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.2.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *9321440
25
+ description: ! "\n A simple gem to trigger a script upon file system change\n "
26
+ email: uatu@jeremydavid.ch
27
+ executables:
28
+ - uatu
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - bin/uatu
33
+ - lib/uatu.rb
34
+ homepage: http://github.com/ltouroumov/uatu
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.11
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Script trigger upon changes
58
+ test_files: []