Permalink
Browse files

Store the tmp files in the output_directory/.work/ instead of /tmp/.

Improved command line client.
  • Loading branch information...
1 parent 6d9e9b2 commit 2bea2cc94049dc64036cfd60b8aeaa695697dffd @alloy committed Oct 28, 2008
Showing with 53 additions and 14 deletions.
  1. +3 −1 Rakefile
  2. +2 −2 bin/ruby-nzb
  3. +9 −2 lib/nzb.rb
  4. +6 −0 lib/nzb/connection.rb
  5. +1 −1 lib/nzb/file.rb
  6. +18 −2 test/file_test.rb
  7. +14 −6 test/nzb_test.rb
View
@@ -2,4 +2,6 @@ require 'rake/testtask'
Rake::TestTask.new do |t|
t.test_files = FileList["test/*_test.rb"]
-end
+end
+
+task :default => :test
View
@@ -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
@@ -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
View
@@ -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
@@ -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
View
@@ -33,6 +33,8 @@ def initialize(*args)
super
@ready = false
@data = ''
+
+ puts "Opening connection."
end
def ready?
@@ -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
View
@@ -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,
View
@@ -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'),
@@ -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')
@@ -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
View
@@ -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
@@ -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

0 comments on commit 2bea2cc

Please sign in to comment.