directory_watcher 0.1.1 → 0.1.2
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.
- data/lib/directory_watcher.rb +33 -34
- metadata +2 -2
data/lib/directory_watcher.rb
CHANGED
@@ -164,8 +164,6 @@ class DirectoryWatcher
|
|
164
164
|
#
|
165
165
|
Event = Struct.new :type, :path
|
166
166
|
|
167
|
-
FileInfo = Struct.new :mtime, :size, :stable # :nodoc:
|
168
|
-
|
169
167
|
#
|
170
168
|
# call-seq:
|
171
169
|
# DirectoryWatcher.new( directory, options )
|
@@ -206,6 +204,7 @@ class DirectoryWatcher
|
|
206
204
|
|
207
205
|
@files = (opts[:pre_load] ? scan_files : Hash.new)
|
208
206
|
@events = []
|
207
|
+
@thread = nil
|
209
208
|
end
|
210
209
|
|
211
210
|
#
|
@@ -371,17 +370,17 @@ class DirectoryWatcher
|
|
371
370
|
# scan_files
|
372
371
|
#
|
373
372
|
# Using the configured glob pattern, scan the directory for all files and
|
374
|
-
# return a hash with the filenames as keys and +
|
375
|
-
# values. The +
|
373
|
+
# return a hash with the filenames as keys and +File::Stat+ objects as the
|
374
|
+
# values. The +File::Stat+ objects contain the mtime and size of the file.
|
376
375
|
#
|
377
376
|
def scan_files
|
378
377
|
files = {}
|
379
378
|
@glob.each do |glob|
|
380
379
|
Dir.glob(glob).each do |fn|
|
381
380
|
begin
|
382
|
-
|
383
|
-
|
384
|
-
|
381
|
+
stat = File.stat fn
|
382
|
+
next unless stat.file?
|
383
|
+
files[fn] = stat
|
385
384
|
rescue SystemCallError; end
|
386
385
|
end
|
387
386
|
end
|
@@ -403,15 +402,14 @@ class DirectoryWatcher
|
|
403
402
|
start = Time.now.to_f
|
404
403
|
|
405
404
|
files = scan_files
|
406
|
-
keys = [files.keys, @files.keys]
|
407
|
-
common = keys.first & keys.last
|
405
|
+
keys = [files.keys, @files.keys] # current files, previous files
|
408
406
|
|
409
407
|
find_added(files, *keys)
|
410
|
-
find_modified(files,
|
408
|
+
find_modified(files, *keys)
|
411
409
|
find_removed(*keys)
|
412
410
|
|
413
411
|
notify_observers
|
414
|
-
@files = files
|
412
|
+
@files = files # store the current file list for the next iteration
|
415
413
|
|
416
414
|
nap_time = @interval - (Time.now.to_f - start)
|
417
415
|
sleep nap_time if nap_time > 0
|
@@ -424,9 +422,7 @@ class DirectoryWatcher
|
|
424
422
|
#
|
425
423
|
# Taking the list of current files, _cur_, and the list of files found
|
426
424
|
# previously, _prev_, figure out which files have been added and generate
|
427
|
-
# a new file added event for each.
|
428
|
-
# from this method. If no files have been added, the returned array is
|
429
|
-
# empty.
|
425
|
+
# a new file added event for each.
|
430
426
|
#
|
431
427
|
def find_added( files, cur, prev )
|
432
428
|
added = cur - prev
|
@@ -443,9 +439,7 @@ class DirectoryWatcher
|
|
443
439
|
#
|
444
440
|
# Taking the list of current files, _cur_, and the list of files found
|
445
441
|
# previously, _prev_, figure out which files have been removed and
|
446
|
-
# generate a new file removed event for each.
|
447
|
-
# an array from this method. If no files have been removed, the returned
|
448
|
-
# array is empty.
|
442
|
+
# generate a new file removed event for each.
|
449
443
|
#
|
450
444
|
def find_removed( cur, prev )
|
451
445
|
removed = prev - cur
|
@@ -455,33 +449,32 @@ class DirectoryWatcher
|
|
455
449
|
|
456
450
|
#
|
457
451
|
# call-seq:
|
458
|
-
# find_modified( files,
|
452
|
+
# find_modified( files, cur, prev )
|
459
453
|
#
|
460
|
-
# Taking the list of
|
461
|
-
#
|
462
|
-
# Generate a new file modified event for each
|
463
|
-
# looking at the stable count in the _files_ hash,
|
464
|
-
# have become stable since being added or modified.
|
465
|
-
# event for each stabilized file.
|
466
|
-
# this method. If there are no events, the array is empty.
|
454
|
+
# Taking the list of current files, _cur_, and the list of files found
|
455
|
+
# previously, _prev_, find those that are common between them and determine
|
456
|
+
# if any have been modified. Generate a new file modified event for each
|
457
|
+
# modified file. Also, by looking at the stable count in the _files_ hash,
|
458
|
+
# figure out if any files have become stable since being added or modified.
|
459
|
+
# Generate a new stable event for each stabilized file.
|
467
460
|
#
|
468
|
-
def find_modified( files,
|
469
|
-
|
470
|
-
|
461
|
+
def find_modified( files, cur, prev )
|
462
|
+
(cur & prev).each do |key|
|
463
|
+
cur_stat, prev_stat = files[key], @files[key]
|
471
464
|
|
472
465
|
# if the modification time or the file size differs from the last
|
473
466
|
# time it was seen, then create a :modified event
|
474
|
-
if
|
467
|
+
if (cur_stat <=> prev_stat) != 0 or cur_stat.size != prev_stat.size
|
475
468
|
@events << Event.new(:modified, key)
|
476
|
-
|
469
|
+
cur_stat.stable = @stable
|
477
470
|
|
478
471
|
# otherwise, if the count is not nil see if we need to create a
|
479
472
|
# :stable event
|
480
|
-
elsif !
|
481
|
-
|
482
|
-
if
|
473
|
+
elsif !prev_stat.stable.nil?
|
474
|
+
cur_stat.stable = prev_stat.stable - 1
|
475
|
+
if cur_stat.stable == 0
|
483
476
|
@events << Event.new(:stable, key)
|
484
|
-
|
477
|
+
cur_stat.stable = nil
|
485
478
|
end
|
486
479
|
end
|
487
480
|
end
|
@@ -507,4 +500,10 @@ class DirectoryWatcher
|
|
507
500
|
|
508
501
|
end # class DirectoryWatcher
|
509
502
|
|
503
|
+
# :stopdoc:
|
504
|
+
# We need to add a 'stable' attribute to the File::Stat object
|
505
|
+
class File::Stat
|
506
|
+
attr_accessor :stable
|
507
|
+
end
|
508
|
+
|
510
509
|
# EOF
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: directory_watcher
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2006-11-
|
6
|
+
version: 0.1.2
|
7
|
+
date: 2006-11-26 00:00:00 -07:00
|
8
8
|
summary: A class for watching files within a directory and generating events when those files change
|
9
9
|
require_paths:
|
10
10
|
- lib
|