Skip to content

Commit

Permalink
Avoid error with too many open files.
Browse files Browse the repository at this point in the history
  • Loading branch information
evan committed Apr 13, 2008
1 parent 65d8c27 commit 32ebdc2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 22 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG
@@ -1,5 +1,7 @@

v0.2.1. Better error handling.
v0.2.2. Avoid error with too many open files.

v0.2.1. Better error handling; smarter genre choosing.

v0.2. Genre tagging; bugfixes.

Expand Down
21 changes: 3 additions & 18 deletions lib/sweeper.rb
Expand Up @@ -36,6 +36,8 @@ class Problem < RuntimeError; end
def initialize(options = {})
@dir = File.expand_path(options['dir'] || Dir.pwd)
@options = options
@outf = Tempfile.new("stdout")
@errf = Tempfile.new("stderr")
end

# Run the Sweeper according to the <tt>options</tt>.
Expand Down Expand Up @@ -149,7 +151,7 @@ def lookup(filename, tags = {})
# Lookup the basic metadata for an mp3 file. Accepts a pathname. Returns a tag hash.
def lookup_basic(filename)
Dir.chdir File.dirname(binary) do
response = silence { `./#{File.basename(binary)} #{filename.inspect}` }
response = `./#{File.basename(binary)} #{filename.inspect} 2> #{@errf.path}`
object = begin
XSD::Mapping.xml2obj(response)
rescue REXML::ParseException
Expand Down Expand Up @@ -246,21 +248,4 @@ def load(filename)
ID3Lib::Tag.new(filename, ID3Lib::V_ALL)
end

# Silence STDERR and STDOUT for the duration of the block.
def silence(outf = nil, errf = nil)
# This method is annoying.
outf, errf = Tempfile.new("stdout"), Tempfile.new("stderr")
out, err = $stdout.clone, $stderr.clone
$stdout.reopen(outf)
$stderr.reopen(errf)
begin
yield
ensure
$stdout.reopen(out)
$stderr.reopen(err)
outf.close; outf.unlink
errf.close; errf.unlink
end
end

end
4 changes: 1 addition & 3 deletions test/integration/sweeper_test.rb
Expand Up @@ -49,9 +49,7 @@ def test_read
end

def test_write
@s.silence do
@s.write(@found_many, @s.lookup(@found_many))
end
@s.write(@found_many, @s.lookup(@found_many))
assert_equal(
@s.lookup(@found_many),
@s.read(@found_many))
Expand Down

0 comments on commit 32ebdc2

Please sign in to comment.