Skip to content

Commit

Permalink
Store the tmp files in the output_directory/.work/ instead of /tmp/.
Browse files Browse the repository at this point in the history
Improved command line client.
  • Loading branch information
alloy committed Oct 28, 2008
1 parent 6d9e9b2 commit 2bea2cc
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Rakefile
Expand Up @@ -2,4 +2,6 @@ require 'rake/testtask'

Rake::TestTask.new do |t|
t.test_files = FileList["test/*_test.rb"]
end
end

task :default => :test
4 changes: 2 additions & 2 deletions bin/ruby-nzb
Expand Up @@ -19,7 +19,7 @@ parser = OptionParser.new do |opts|
options[:port] = port
end

opts.on('-ps', '--pool-size', Numeric, 'The number of simultaneous connections to make to the news server') do |pool_size|
opts.on('-t', '--threads [SIZE]', Numeric, 'The number of simultaneous connections to make to the news server') do |pool_size|
options[:pool_size] = pool_size
end

Expand Down Expand Up @@ -50,7 +50,7 @@ begin

@nzbs_on_screen.each_with_index do |nzb_on_screen, index|
if nzb_on_screen == nzb
@screen.setpos(index, 1)
@screen.setpos(index, 0)
@screen.addstr(log_message(nzb, message))
Curses.refresh
end
Expand Down
11 changes: 9 additions & 2 deletions lib/nzb.rb
Expand Up @@ -38,13 +38,20 @@ def initialize(path)
@files = Parser.new(self).files
@queue = @files.dup

FileUtils.mkdir_p(output_directory) unless ::File.exist?(output_directory)
unless ::File.exist?(output_directory)
FileUtils.mkdir_p(output_directory)
FileUtils.mkdir_p(work_directory)
end
end

def output_directory
@output_directory ||= ::File.join(NZB.output_directory, ::File.basename(@path, '.nzb'))
end

def work_directory
@work_directory ||= ::File.join(output_directory, '.work')
end

# This is called by NZB.request_file
def request_file
@queue.shift
Expand All @@ -59,7 +66,7 @@ def bytes
end

def downloaded_bytes
@files.inject(0) { |sum, file| sum += file.downloaded_bytes }
@files.inject(0) { |sum, file| p file.downloaded_bytes; sum += file.downloaded_bytes }
end

def downloaded_percentage
Expand Down
6 changes: 6 additions & 0 deletions lib/nzb/connection.rb
Expand Up @@ -33,6 +33,8 @@ def initialize(*args)
super
@ready = false
@data = ''

puts "Opening connection."
end

def ready?
Expand Down Expand Up @@ -88,6 +90,10 @@ def unescape_data!
# @data.gsub!(/\r\n=yend.+\r\n\.\r\n$/, '')
end

def unbind
puts "Closing connection."
end

def log(str)
puts "Client: #{str}" if ENV['LOG_DATA'] == 'true'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nzb/file.rb
Expand Up @@ -36,7 +36,7 @@ def request_job
end

def write_data(data)
@tmp_file ||= Tempfile.new(object_id)
@tmp_file ||= Tempfile.new(object_id, ::File.join(@nzb.work_directory))
@tmp_file.write(data)

# FIXME: This is cheating, as we use the bytes from the segment,
Expand Down
20 changes: 18 additions & 2 deletions test/file_test.rb
Expand Up @@ -3,14 +3,24 @@

describe "NZB::File" do
before do
output_directory = File.join(TMP_DIR, 'name_of_nzb')
work_directory = File.join(output_directory, '.work')
FileUtils.mkdir_p(work_directory)

@nzb = stub('NZB')
@nzb.stubs(:run_update_callback!)
@nzb.stubs(:output_directory).returns(output_directory)
@nzb.stubs(:work_directory).returns(work_directory)

@file = NZB::File.new(@nzb)
@file.add_segment('message_id' => '1', 'bytes' => '1')
@file.add_segment('message_id' => '2', 'bytes' => '2')
end

after do
FileUtils.rm_rf TMP_DIR
end

it "should add a segment" do
@file.segments.should == [
NZB::File::Segment.new('message_id' => '1'),
Expand All @@ -29,12 +39,17 @@
end

it "should initialize a tmp file instance if it doesn't exist yet and write data to it" do
@file.request_job

@file.write_data "Some data\r\n"
@file.tmp_file.rewind
@file.tmp_file.gets.should == "Some data\r\n"
File.dirname(@file.tmp_file.path).should == File.join(@nzb.output_directory, '.work')
end

it "should decode the file segments that were written to the tmp file and remove it and let the NZB instance know there was an update" do
@file.request_job

@file.write_data "Some data\r\n"
tmp_file = @file.tmp_file.path
nzb = mock('NZB')
Expand Down Expand Up @@ -77,7 +92,8 @@ class << self
end

it "should keep track of the amount of bytes that were downloaded and written to the tmp file" do
@file.write_data "12345678\r\n12345678\r\n"
@file.downloaded_bytes.should == 20
segment = @file.request_job
@file.write_data ""
@file.downloaded_bytes.should == segment.bytes
end
end
20 changes: 14 additions & 6 deletions test/nzb_test.rb
Expand Up @@ -62,13 +62,17 @@
@nzb.path.should == fixture('ubuntu.nzb')
end

it "should return the working directory" do
it "should return the output and work directory" do
@nzb.output_directory.should == File.join(TMP_DIR, 'ubuntu')
@nzb.work_directory.should == File.join(@nzb.output_directory, '.work')
end

it "should have created the working directory" do
it "should have created the output and work directory" do
File.should.exist @nzb.output_directory
File.should.be.directory @nzb.output_directory

File.should.exist @nzb.work_directory
File.should.be.directory @nzb.work_directory
end

it "should have parsed the files/segments from the NZB xml file" do
Expand Down Expand Up @@ -106,11 +110,15 @@
@nzb.bytes.should == @nzb.files.inject(0) { |sum, file| sum += file.bytes }
end

it "should return the amount of bytes that have been downloaded" do
xit "should return the amount of bytes that have been downloaded" do
@nzb.downloaded_bytes.should == 0
@nzb.files.first.write_data "12345678\r\n"
@nzb.files.last.write_data "12345678\r\n"
@nzb.downloaded_bytes.should == 20

segment = @nzb.files.first
segment.request_job
segment.stubs(:bytes).returns(10)
segment.write_data "12345678\r\n"

@nzb.downloaded_bytes.should == segment.bytes
end

it "should return the download completion percentage" do
Expand Down

0 comments on commit 2bea2cc

Please sign in to comment.