broom 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.markdown +22 -0
- data/Rakefile +18 -0
- data/lib/broom.rb +56 -0
- metadata +67 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Daniel Johnston
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Broom
|
2
|
+
=====
|
3
|
+
A simple module to help monitor a directory for new files and perform an action
|
4
|
+
on each new file. Broom periodically sweeps a directory, caching each file and its
|
5
|
+
last modification time. After each sweep, Broom iterates through the cache and
|
6
|
+
yields only the files that have a current modification time that matches the cached
|
7
|
+
modification time. In the event of an error, Broom gracefully sweeps
|
8
|
+
the file to a failure dustpan. Else, the file is swept to a success dustpan.
|
9
|
+
|
10
|
+
Broom depends on the following directory structure, which can be modified
|
11
|
+
using the 'success_dir' and 'failure_dir' options.
|
12
|
+
|
13
|
+
/path/to/directory
|
14
|
+
/path/to/directory/_success
|
15
|
+
/path/to/directory/_failure
|
16
|
+
|
17
|
+
Examples
|
18
|
+
========
|
19
|
+
|
20
|
+
Broom.sweep('/tmp', :pattern => '*.pdf') do |file|
|
21
|
+
Resque.enqueue(Job, file)
|
22
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
$:.unshift(File.dirname(__FILE__))
|
5
|
+
require 'lib/broom'
|
6
|
+
|
7
|
+
desc "Publish gem and source."
|
8
|
+
task :publish => :build do
|
9
|
+
sh "gem push broom-#{Broom::VERSION}.gem"
|
10
|
+
sh "git tag v#{Broom::VERSION}"
|
11
|
+
sh "git push origin v#{Broom::VERSION}"
|
12
|
+
sh "git push origin master"
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Build gem."
|
16
|
+
task :build do
|
17
|
+
sh "gem build broom.gemspec"
|
18
|
+
end
|
data/lib/broom.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
Signal.trap('INT') do
|
4
|
+
$stdout.puts "throwing in the towel..."
|
5
|
+
exit
|
6
|
+
end
|
7
|
+
|
8
|
+
module Broom
|
9
|
+
|
10
|
+
VERSION = '0.1.1'
|
11
|
+
|
12
|
+
class Directory
|
13
|
+
|
14
|
+
attr_reader :path, :pattern, :failure_dir, :success_dir
|
15
|
+
|
16
|
+
def initialize(path, options={})
|
17
|
+
@path = File.expand_path(path)
|
18
|
+
@pattern = options[:pattern] || "*"
|
19
|
+
@failure_dir = options[:failure_dir] || File.join(@path, "_failure")
|
20
|
+
@success_dir = options[:success_dir] || File.join(@path, "_success")
|
21
|
+
[@path, @failure_dir, @success_dir].each do |dir|
|
22
|
+
FileUtils.mkdir(dir) unless File.exist?(dir)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def cache
|
27
|
+
files = {}
|
28
|
+
Dir.glob(File.join(@path, @pattern)).each { |file|
|
29
|
+
next if file == @success_dir || file == @failure_dir
|
30
|
+
files[file] = File.mtime(file)
|
31
|
+
}
|
32
|
+
files
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
extend self
|
38
|
+
|
39
|
+
def sweep(path, options={}, &block)
|
40
|
+
dir = Directory.new(path, options)
|
41
|
+
loop do
|
42
|
+
dir.cache.each do |file, modified_at|
|
43
|
+
next if modified_at != File.mtime(file)
|
44
|
+
begin
|
45
|
+
yield(file)
|
46
|
+
rescue Object => boom
|
47
|
+
FileUtils.mv(file, dir.failure_dir)
|
48
|
+
else
|
49
|
+
FileUtils.mv(file, dir.success_dir)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
sleep options[:sleep] || 1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: broom
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Daniel Johnston
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-08 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: " A small module for watching a directory for new files.\n"
|
22
|
+
email: dan@dj-agiledev.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- README.markdown
|
31
|
+
- LICENSE
|
32
|
+
- Rakefile
|
33
|
+
- lib/broom.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/djohnston/broom
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.7
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Simple file picker-upper
|
66
|
+
test_files: []
|
67
|
+
|