filewatch 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/gtail CHANGED
@@ -32,10 +32,7 @@ def main(args)
32
32
 
33
33
  tail = FileWatch::TailGlob.new
34
34
  ARGV.each do |path|
35
- tail.tail(path, :exclude => exclude_patterns) do |fullpath|
36
- p :new => fullpath
37
- end
38
-
35
+ tail.tail(path, :exclude => exclude_patterns)
39
36
  end
40
37
 
41
38
  buffer = BufferedTokenizer.new
@@ -1,6 +1,7 @@
1
- require "filewatch/namespace"
2
- require "filewatch/inotify/fd"
3
1
  require "ffi"
2
+ require "filewatch/inotify/fd"
3
+ require "filewatch/namespace"
4
+ require "filewatch/rubyfixes"
4
5
 
5
6
  class FileWatch::Inotify::Event < FFI::Struct
6
7
  layout :wd, :int,
@@ -1,10 +1,11 @@
1
1
  require "rubygems"
2
2
  require "ffi"
3
3
  require "fcntl"
4
+ require "filewatch/exception"
4
5
  require "filewatch/inotify/event"
5
6
  require "filewatch/namespace"
7
+ require "filewatch/rubyfixes"
6
8
  require "filewatch/stringpipeio"
7
- require "filewatch/exception"
8
9
 
9
10
  class FileWatch::Inotify::FD
10
11
  include Enumerable
@@ -0,0 +1,8 @@
1
+
2
+ # Ruby <= 1.8.6 doesn't have String#bytesize
3
+ # FFI 1.0.7 wants it. Monkeypatch time.
4
+ if !String.instance_methods.include?("bytesize")
5
+ class String
6
+ alias :bytesize :size
7
+ end
8
+ end
@@ -1,8 +1,10 @@
1
1
  require "filewatch/namespace"
2
2
  require "filewatch/exception"
3
3
  require "filewatch/watchglob"
4
+ require "logger"
4
5
 
5
6
  class FileWatch::TailGlob
7
+ attr_accessor :logger
6
8
 
7
9
  public
8
10
  def initialize
@@ -10,31 +12,46 @@ class FileWatch::TailGlob
10
12
 
11
13
  # hash of string path => File
12
14
  @files = {}
15
+ @globoptions = {}
13
16
  @options = {}
14
17
 
18
+ self.logger = Logger.new(STDERR)
15
19
  # hash of string path => action to take on EOF
16
- #@eof_actions = {}
17
20
  end # def initialize
18
21
 
22
+ def logger=(logger)
23
+ @logger = logger
24
+ @watch.logger = logger
25
+ end
26
+
19
27
  # Watch a path glob.
20
28
  #
21
29
  # Options is a hash of:
22
30
  # :exclude => array of globs to ignore.
23
31
  public
24
- def tail(path, options={}, &block)
32
+ def tail(glob, options={}, &block)
25
33
  what_to_watch = [ :create, :modify, :delete ]
26
34
 
35
+ # Setup a callback specific to tihs tail call for handling
36
+ # new files found; so we can attach options to each new file.
37
+ callback_set_options = lambda do |path|
38
+ @options[path] = options
39
+ end
40
+
27
41
  # Save glob options
28
- @options[path] = options
29
- @options[path][:new_follow_callback] = block
42
+ @globoptions[glob] = options
43
+
44
+ # callbacks can be an array of functions to call.
45
+ callbacks = [ callback_set_options ]
46
+ callbacks << block if block_given?
47
+ @globoptions[glob][:new_follow_callback] = callbacks
30
48
 
31
- @watch.watch(path, *what_to_watch) do |path|
49
+ @watch.watch(glob, *what_to_watch) do |path|
32
50
  # Save per-path options
33
- @options[path] = options
51
+ callback_set_options.call(path)
34
52
 
35
53
  # for each file found by the glob, open it.
36
54
  follow_file(path, :end)
37
-
38
55
  end # @watch.watch
39
56
  end # def watch
40
57
 
@@ -42,7 +59,7 @@ class FileWatch::TailGlob
42
59
  def follow_file(path, seek=:end)
43
60
  # Don't follow things that aren't files.
44
61
  if !File.file?(path)
45
- puts "Skipping follow on #{path}, File.file? == false"
62
+ @logger.info "Skipping follow on #{path}, File.file? == false"
46
63
  return
47
64
  end
48
65
 
@@ -50,14 +67,17 @@ class FileWatch::TailGlob
50
67
  if options.include?(:exclude)
51
68
  options[:exclude].each do |exclusion|
52
69
  if File.fnmatch?(exclusion, path)
53
- puts "Skipping #{path.inspect}, matches exclusion #{exclusion.inspect}"
70
+ @logger.info "Skipping #{path.inspect}, matches exclusion #{exclusion.inspect}"
54
71
  return
55
72
  end
56
73
  end
57
74
  end
58
75
 
59
76
  if options.include?(:new_follow_callback)
60
- options[:new_follow_callback].call(path)
77
+ options[:new_follow_callback].each do |callback|
78
+ #puts "Callback: #{callback.inspect}"
79
+ callback.call(path)
80
+ end
61
81
  end
62
82
 
63
83
  close(path) if @files.include?(path)
@@ -95,23 +115,25 @@ class FileWatch::TailGlob
95
115
 
96
116
  protected
97
117
  def file_action_modify(path, event, &block)
118
+ file = @files[path]
119
+ # Check if this path is in the exclude list.
120
+ # TODO(sissel): Is this check sufficient?
121
+ if file.nil?
122
+ @logger.info "Ignoring modify on '#{path}' - it's probably ignored'"
123
+ return
124
+ end
125
+
126
+ # Read until EOF, emitting each chunk read.
98
127
  loop do
99
- file = @files[path]
100
128
  begin
101
129
  data = file.sysread(4096)
102
130
  yield event.name, data
103
131
  rescue EOFError
104
132
  check_for_truncation_or_deletion(path, event, &block)
105
- #case @eof_actions[path]
106
- #when :reopen
107
- #puts "Reopening #{path} due to eof and new file"
108
- #reopen(path)
109
- #end
110
-
111
133
  break
112
134
  end
113
- end
114
- end
135
+ end # loop
136
+ end # def file_action_modify
115
137
 
116
138
  protected
117
139
  def check_for_truncation_or_deletion(path, event, &block)
@@ -3,6 +3,8 @@ require "filewatch/exception"
3
3
  require "filewatch/watch"
4
4
 
5
5
  class FileWatch::WatchGlob
6
+ attr_accessor :logger
7
+
6
8
  # This class exists to wrap inotify, kqueue, periodic polling, etc,
7
9
  # to provide you with a way to watch files and directories.
8
10
  #
@@ -11,6 +13,7 @@ class FileWatch::WatchGlob
11
13
  @watch = FileWatch::Watch.new
12
14
  @globdirs = []
13
15
  @globs = []
16
+ @logger = Logger.new(STDERR)
14
17
  end
15
18
 
16
19
  public
@@ -23,7 +26,7 @@ class FileWatch::WatchGlob
23
26
  paths.each do |path|
24
27
  begin
25
28
  next if watching.include?(path)
26
- puts "Watching #{path}"
29
+ @logger.info("Watching #{path}")
27
30
  @watch.watch(path, :create, :delete, :modify)
28
31
  watching << path
29
32
 
@@ -31,7 +34,7 @@ class FileWatch::WatchGlob
31
34
  # This allows initialization on new files found at start.
32
35
  yield path if block_given?
33
36
  rescue FileWatch::Exception => e
34
- $stderr.puts "Failed starting watch on #{path} - #{e}"
37
+ @logger.info("Failed starting watch on #{path} - #{e}")
35
38
  errors << e
36
39
  end
37
40
  end
@@ -47,12 +50,12 @@ class FileWatch::WatchGlob
47
50
  globprefix = File.join(splitpath[0 ... i])
48
51
  Dir.glob(globprefix).each do |path|
49
52
  next if watching.include?(path)
50
- p "Watching dir #{path}"
53
+ @logger.info("Watching dir: #{path.inspect}")
51
54
  @watch.watch(path, :create)
52
55
  @globdirs << path
53
- end
54
- end
55
- end
56
+ end # Dir.glob
57
+ end # if part.include?("*")
58
+ end # splitpath.each_with_index
56
59
  end # def watch
57
60
 
58
61
  # TODO(sissel): implement 'unwatch' or cancel?
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filewatch
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
4
+ hash: 31
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 4
10
+ version: 0.2.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jordan Sissel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-12 00:00:00 -07:00
18
+ date: 2011-04-13 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -41,17 +41,18 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
 
43
43
  files:
44
- - lib/filewatch/buftok.rb
45
- - lib/filewatch/exception.rb
44
+ - lib/filewatch/watchglob.rb
45
+ - lib/filewatch/watch.rb
46
+ - lib/filewatch/tailglob.rb
47
+ - lib/filewatch/namespace.rb
46
48
  - lib/filewatch/tail.rb
49
+ - lib/filewatch/rubyfixes.rb
50
+ - lib/filewatch/exception.rb
47
51
  - lib/filewatch/stringpipeio.rb
48
52
  - lib/filewatch/inotify/emhandler.rb
49
53
  - lib/filewatch/inotify/fd.rb
50
54
  - lib/filewatch/inotify/event.rb
51
- - lib/filewatch/watchglob.rb
52
- - lib/filewatch/watch.rb
53
- - lib/filewatch/namespace.rb
54
- - lib/filewatch/tailglob.rb
55
+ - lib/filewatch/buftok.rb
55
56
  - test/log4j/log4j.properties
56
57
  - test/log4j/LogTest.java
57
58
  - bin/gtail
@@ -86,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
87
  requirements: []
87
88
 
88
89
  rubyforge_project:
89
- rubygems_version: 1.3.7
90
+ rubygems_version: 1.6.0
90
91
  signing_key:
91
92
  specification_version: 3
92
93
  summary: filewatch - file watching for ruby