sambala 0.9.2 → 0.9.3

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.
@@ -46,6 +46,7 @@
46
46
  # :title:Sambala
47
47
 
48
48
  class Sambala
49
+ require 'logger'
49
50
  require 'pty'
50
51
  require 'expect'
51
52
  require 'rubygems'
@@ -53,6 +54,7 @@ class Sambala
53
54
 
54
55
  require 'sambala_gardener'
55
56
  include Gardener
57
+ @@log_level = Logger::WARN; @@log_output = STDERR
56
58
 
57
59
  # The +new+ class method initializes Sambala.
58
60
  # === Parameters
@@ -69,7 +71,8 @@ class Sambala
69
71
  # :password => 'eggman',
70
72
  # :threads => 2 )
71
73
  def initialize(options={:domain => '', :host => '', :share => '', :user => '', :password => '', :threads => 1})
72
- begin
74
+ init_logger
75
+ begin
73
76
  options[:threads] = 4 if options[:threads] > 4
74
77
  options[:init_timeout] = options[:threads]
75
78
  @options = options; gardener_ok
@@ -270,24 +273,46 @@ class Sambala
270
273
  def volume
271
274
  execute('volume' ,'', false)[1]
272
275
  end
273
-
274
- # The +local+ methods allow shell command execution on the local machine.
275
- # === Parameters
276
- # * _command_ = the shell comand to be executed locally
277
- # === Interactive Returns
278
- # * _string_ = the normal return message of the command
276
+
277
+ # The +queue_waiting+ method returns the number of waiting task in queue
279
278
  # === Example
280
- # samba.local('mkdir mydir')
281
- def local(command)
282
- execute('!', command, false)[1]
279
+ # samba.queue_waiting # => 3
280
+ def queue_waiting
281
+ @gardener.growth(:seed)
283
282
  end
284
-
285
- # The +queue_results+ methods collect a done queue items results
283
+
284
+ # The +queue_processing+ method returns an array containing the tasks actually processing
285
+ # === Example
286
+ # samba.queue_processing # => [[1, "put myFile"],[2, "put lib/sambala.rb sambala.rb"]]
287
+ def queue_processing
288
+ results = @gardener.harvest(:sprout)
289
+ results.map { |result| [result[:id], result[:seed]] }
290
+ end
291
+
292
+ # The +queue_completed+ method returns an array containing the task that have completed
293
+ def queue_completed
294
+ parse_results(@gardener.harvest(:crop))
295
+ end
296
+
297
+ # The +queue_empty?+ method return true if there are no jobs in queue
298
+ # === Example
299
+ # samba.queue_empty? # false
300
+ def queue_empty?
301
+ @gardener.growth(:empty)
302
+ end
303
+
304
+ # The queue_done? method return true if all jobs have finished processing
305
+ # === Example
306
+ # samba.queue_done? # true
307
+ def queue_done?
308
+ @gardener.growth(:finished)
309
+ end
310
+
311
+ # The +queue_results+ methods wait for all queued items to finish and returns them.
286
312
  # === Example
287
313
  # result = samba.queue_results
288
314
  def queue_results
289
- crop = @gardener.harvest(:full_crop)
290
- crop.map! { |result| [ result[:success], result[:seed], result[:message] ] }
315
+ parse_results(@gardener.harvest(:full_crop))
291
316
  end
292
317
 
293
318
  # The +progress+ method returns a progress ratio indicator from 0.00 to 1.00
@@ -302,7 +327,34 @@ class Sambala
302
327
  # samba.close
303
328
  def close
304
329
  result = @gardener.close
330
+ # $log_sambala.close
305
331
  result.values.map { |queue| queue.empty? }.uniq.size == 1 ? true : false
306
332
  end
333
+
334
+ def Sambala::log_output=(out)
335
+ @@log_output = out
336
+ end
337
+
338
+ def Sambala::log_level=(level)
339
+ @@log_level = case level
340
+ when :debug
341
+ Logger::DEBUG
342
+ when :info
343
+ Logger::INFO
344
+ when :warn
345
+ Logger::WARN
346
+ when :error
347
+ Logger::ERROR
348
+ when :fatal
349
+ Logger::FATAL
350
+ end
351
+ end
352
+
353
+ private
354
+
355
+ def init_logger
356
+ $log_sambala = Logger.new(@@log_output)
357
+ $log_sambala.level = @@log_level
358
+ end
307
359
 
308
360
  end
@@ -46,6 +46,7 @@ class Sambala
46
46
  # result = execute_all('mask','match*') # => true
47
47
  def execute_all(command,data)
48
48
  result = @gardener.seed_all("#{command} #{data}")
49
+ $log_sambala.debug("execute_all result") {"#{result.inspect}"}
49
50
  bools = result.map { |row| row[:success] }
50
51
  bools.uniq.size == 1 ? true : false
51
52
  end
@@ -59,6 +60,7 @@ class Sambala
59
60
  def exec_interactive(command,data)
60
61
  id = @gardener.seed("#{command} #{data}")
61
62
  result = @gardener.harvest(:one,id)
63
+ $log_sambala.debug("exec_interactive result") {"#{result.inspect}"}
62
64
  return result[:success], result[:message]
63
65
  end
64
66
 
@@ -67,10 +69,17 @@ class Sambala
67
69
  # * _command_ = the command as a string
68
70
  # * _data_ = the command argument as a string
69
71
  # === Example
70
- # result = exec_queue('get','aFile.txt') # => [true,true]
72
+ # result = exec_queue('get','aFile.txt') # => [true,1]
71
73
  def exec_queue(command,data)
72
- @gardener.seed("#{command} #{data}").integer? ? [true,true] : [false,false]
74
+ result = @gardener.seed("#{command} #{data}")
75
+ $log_sambala.debug("exec_queue result") {"#{result.inspect}"}
76
+ result.integer? ? [true,result] : [false,result]
73
77
  end
78
+
79
+ # The +parse_results+ method map the gardener's return hash values to an array
80
+ def parse_results(results)
81
+ results.map { |result| [result[:id], result[:success], result[:seed], result[:message]] }
82
+ end
74
83
 
75
84
  # The +gardener_ok+ method does the +init_gardener+ invocation, degrading the options parameters until
76
85
  # it initializes, or raise SmbInitError exception after 4 try.
@@ -85,11 +94,11 @@ class Sambala
85
94
  init.map! { |result| result[:success] }
86
95
  throw :gardener if init.uniq.size == 1 and init[0] == true
87
96
  rescue Timeout::Error
88
- $! = "Slugish Network or Server?"
97
+ $log_sambala.error("Having problem setting the smb client... TRY #{num}")
89
98
  end
90
99
  kill_gardener_and_incr
91
100
  end
92
- $! = "All Attemps Failed, Gardener could not be initiated" if $!.nil?
101
+ $log_sambala.fatal("All Attemps Failed, Gardener could not be initiated")
93
102
  raise SmbInitError.exception("Couldn't set smbclient properly (#{$!.to_s})")
94
103
  end
95
104
  @posix_support = posix?(@init_status[0][:message])
@@ -98,7 +107,8 @@ class Sambala
98
107
  # The +init_gardener+ method initialize a gardener class object
99
108
  def init_gardener
100
109
  @gardener = Abundance.gardener(:rows => @options[:threads], :init_timeout => @options[:init_timeout]) do
101
- PTY.spawn("smbclient //#{@options[:host]}/#{@options[:share]} #{@options[:password]} -W #{@options[:domain]} -U #{@options[:user]}") do |r,w,pid|
110
+
111
+ PTY.spawn("smbclient //#{@options[:host]}/#{@options[:share]} #{@options[:password]} -W #{@options[:domain]} -U #{@options[:user]}") do |r,w,pid|
102
112
  w.sync = true
103
113
  $expect_verbose = false
104
114
  catch :init do
@@ -112,32 +122,47 @@ class Sambala
112
122
  end
113
123
  end
114
124
  Abundance.grow do |seed|
115
- w.print "#{seed.sprout}\r"
125
+ w.print "#{seed.sprout}\r"; $log_sambala.debug("smbclient") {"sprout: -- #{seed.sprout} --"}
116
126
  catch :result do
127
+ iter = 1
117
128
  loop do
118
129
  r.expect(/.*\xD\xAsmb: [\x5C]*\w*[\x5C]+\x3E.*/) do |text|
130
+ $log_sambala.debug("smbclient") {"expect: -- #{text} --"}
119
131
  if text != nil
120
132
  msg = text[0]
121
133
 
122
134
  msg.gsub!(/smb: \w*\x5C\x3E\s*$/, '')
123
135
  msg.gsub!(/^\s*#{seed.sprout}/, '')
124
- msg.lstrip!
125
-
136
+ msg.lstrip!; $log_sambala.debug("smbclient") {"msg: -- #{msg} --"}
137
+
126
138
  success = case seed.sprout
127
139
  when /^put/
128
140
  msg['putting'].nil? ? false : true
129
141
  else
130
- msg['NT_STATUS'].nil? ? true : false
142
+ if msg['NT_STATUS']
143
+ false
144
+ elsif msg['timed out'] || msg['Server stopped']
145
+ false
146
+ else
147
+ true
148
+ end
131
149
  end
132
-
150
+
133
151
  seed.crop(success, msg)
134
152
  throw :result
153
+ elsif iter > 20
154
+ $log_sambala.warn("Failed to #{seed.sprout}")
155
+ seed.crop(false, "Failed to #{seed.sprout}")
156
+ throw :result
157
+ else
158
+ iter += 1
135
159
  end
136
160
  end
137
161
  end
138
162
  end
139
163
  end
140
164
  end
165
+
141
166
  end
142
167
  end
143
168
 
@@ -1,9 +1,10 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'logger'
2
3
  require 'test/unit'
3
4
  require 'sambala'
4
5
 
5
6
  TESTFILE = 'sambala_test'
6
- TESTDIR = 'sambala_temp'
7
+ TESTDIR = 'sambala_test_dir'
7
8
  WELCOME = <<TITLE
8
9
  .|'''.| '|| '||
9
10
  ||.. ' .... .. .. .. || ... .... || ....
@@ -14,16 +15,27 @@ WELCOME = <<TITLE
14
15
 
15
16
  /////////////////////////////////////////////////////////////
16
17
  TITLE
18
+ THREADS = 4
19
+
20
+ if ARGV[0].nil?
21
+ LOG_LEVEL = :warn
22
+ else
23
+ LOG_LEVEL = ARGV[0].to_sym
24
+ end
17
25
 
18
26
  class TestSambalaMain < Test::Unit::TestCase
19
27
 
20
28
  def setup
29
+ setup_logger(STDERR,LOG_LEVEL)
21
30
  check_smbclient_presence
22
31
  get_samba_param_from_input
23
32
  init_sambala
33
+ put_marker
34
+ Dir.mkdir(TESTDIR)
24
35
  end
25
36
 
26
37
  def test_main
38
+ @log_test.info("Testing sample SMB operations...")
27
39
  ls_one = check_ls
28
40
  check_mkdir(TESTDIR)
29
41
  check_exist(TESTDIR)
@@ -31,20 +43,58 @@ class TestSambalaMain < Test::Unit::TestCase
31
43
 
32
44
  ls_two = check_ls
33
45
  assert(ls_one != ls_two)
46
+
34
47
  check_lcd_put_get
48
+ check_queue
35
49
 
36
50
  check_cd('..')
37
51
  check_rmdir(TESTDIR)
38
52
  end
39
53
 
40
54
  def teardown
41
- @samba.rmdir(TESTDIR) if @samba.exist?(TESTDIR)
55
+ back_to_marker
56
+ @log_test.info("All Test Done!!! Tearing Down!!!")
57
+ @samba.rmdir(TESTDIR).to_s if @samba.exist?(TESTDIR)
58
+ @log_test.debug("remote directory clean")
42
59
  close = @samba.close
43
60
  assert(close)
44
- Dir.rmdir(TESTDIR) if File.exist?(TESTDIR)
61
+ @log_test.debug("samba client closed")
62
+ FileUtils.remove_dir(TESTDIR) if File.exist?(TESTDIR)
63
+ @log_test.debug("local directory clean")
64
+ @log_test.close
65
+ puts "\nBEWARE, if test fails you may have to clean up your server and the test directory,\na folder named #{TESTDIR} may be left after exiting... Sorry!"
45
66
  end
46
67
 
47
68
  private
69
+
70
+ def setup_logger(out,level)
71
+ Sambala::log_level = level
72
+ @log_test = Logger.new(out)
73
+ case level
74
+ when :debug
75
+ @log_test.level = Logger::DEBUG
76
+ when :info
77
+ @log_test.level = Logger::INFO
78
+ when :warn
79
+ @log_test.level = Logger::WARN
80
+ when :error
81
+ @log_test.level = Logger::ERROR
82
+ when :fatal
83
+ @log_test.level = Logger::FATAL
84
+ else
85
+ @log_test.level = Logger::UNKNOWN
86
+ end
87
+ end
88
+
89
+ def put_marker
90
+ result = @samba.put(:from => 'test/' + File.basename(__FILE__), :to => File.basename(__FILE__))
91
+ assert_equal(true, result[0], "Putting initial path marker failed: #{result[1]}")
92
+ end
93
+
94
+ def back_to_marker
95
+ @samba.cd('..') unless @samba.exist?(File.basename(__FILE__))
96
+ @samba.del(File.basename(__FILE__))
97
+ end
48
98
 
49
99
  def check_smbclient_presence
50
100
  answer = `smbclient --help`
@@ -75,7 +125,7 @@ class TestSambalaMain < Test::Unit::TestCase
75
125
  :share => @share,
76
126
  :user => @user,
77
127
  :password => @password,
78
- :threads => 1)
128
+ :threads => THREADS)
79
129
  puts "Connection successfull,\nnow proceding with test:"
80
130
  end
81
131
 
@@ -83,8 +133,7 @@ class TestSambalaMain < Test::Unit::TestCase
83
133
  result = @samba.ls
84
134
  assert_not_nil(result)
85
135
  result_alias = @samba.dir
86
- assert_not_nil(result)
87
- assert(result == result_alias)
136
+ assert_not_nil(result_alias)
88
137
  return result
89
138
  end
90
139
 
@@ -109,48 +158,115 @@ class TestSambalaMain < Test::Unit::TestCase
109
158
  end
110
159
 
111
160
  def check_lcd_put_get
112
- before = @samba.local('ls')
113
- @samba.local("mkdir #{TESTDIR}")
114
- after = @samba.local('ls')
115
- assert(before != after)
116
-
161
+ @log_test.debug("cd in testdir")
117
162
  re = @samba.lcd(TESTDIR)
118
163
  assert_equal(true,re)
119
- before2 = @samba.local('ls')
120
- @samba.local("touch #{TESTFILE}")
121
- after2 = @samba.local('ls')
122
- assert(before2 != after2)
123
164
 
165
+ @log_test.debug("making local test file")
166
+ f = File.new("#{TESTDIR}/#{TESTFILE}",'w')
167
+ f.puts "test file"
168
+ f.close
169
+ assert(File.exist?("#{TESTDIR}/#{TESTFILE}"))
124
170
 
171
+ @log_test.debug("putting file")
125
172
  re = @samba.put(:from => TESTFILE, :to => TESTFILE)
126
173
  assert_kind_of(Array,re)
127
174
  assert_equal(true,re[0])
128
175
 
129
- check_exist(TESTFILE)
176
+ @log_test.debug("deleting local test file")
177
+ File.delete("#{TESTDIR}/#{TESTFILE}")
178
+ assert(!File.exist?("#{TESTDIR}/#{TESTFILE}"))
130
179
 
131
- @samba.local("rm #{TESTFILE}")
132
- after2_2 = @samba.local('ls')
133
- assert(before2 == after2_2)
180
+ check_exist(TESTFILE)
134
181
 
182
+ @log_test.debug("getting test file")
135
183
  re = @samba.get(:from => TESTFILE, :to => TESTFILE)
136
184
  assert_kind_of(Array,re)
137
185
  assert_equal(true,re[0])
138
186
 
187
+ @log_test.debug("delete remote test file")
139
188
  re = @samba.del(TESTFILE)
140
189
  assert_equal(true,re)
141
190
 
142
- after2_3 = @samba.local('ls')
143
- assert(after2 == after2_3)
144
- @samba.local("rm #{TESTFILE}")
191
+ @log_test.debug("check local test file")
192
+ assert(File.exist?("#{TESTDIR}/#{TESTFILE}"))
193
+ File.delete("#{TESTDIR}/#{TESTFILE}")
194
+ assert(!File.exist?("#{TESTDIR}/#{TESTFILE}"))
145
195
 
146
196
  re = @samba.lcd('..')
147
197
  assert_equal(true,re)
148
- @samba.local("rmdir #{TESTDIR}")
149
- after = @samba.local('ls')
150
- assert(before == after)
198
+
151
199
  end
152
200
 
153
-
154
-
201
+ def check_queue
202
+ jobs = 300
203
+ @log_test.info("Testing queue... (be patient, this will take a couple minutes)")
204
+ assert_equal(true, @samba.queue_empty?)
205
+ assert_equal(true, @samba.queue_done?)
206
+ assert_equal(0,@samba.queue_waiting)
207
+
208
+ files = Array.new(jobs) { |id| "file_" + id.to_s }
209
+ content = "01" * 10000
210
+ files.each do |file|
211
+ f = File.new("#{TESTDIR}/#{file}",'w')
212
+ f.puts content
213
+ f.close
214
+ end
215
+
216
+ re = @samba.lcd(TESTDIR)
217
+ assert_equal(true,re)
218
+
219
+ files.each do |file|
220
+ @samba.put(:from => file, :to => file, :queue => true)
221
+ end
222
+
223
+ assert_equal(false, @samba.queue_empty?)
224
+ assert_equal(false, @samba.queue_done?)
225
+ assert(@samba.queue_waiting > 0)
226
+
227
+ result = @samba.queue_processing
228
+ @log_test.debug("queue processing result is: #{result.inspect}")
229
+ assert_kind_of(Array,result)
230
+ assert_kind_of(Array,result[0])
231
+ assert_equal(2,result[0].size)
232
+ assert_kind_of(Integer,result[0][0])
233
+ assert_kind_of(String,result[0][1])
234
+
235
+ result = @samba.queue_completed
236
+ @log_test.debug("queue completed results is: #{result.inspect}")
237
+ assert_kind_of(Array,result)
238
+ assert_kind_of(Array,result[0])
239
+ assert_equal(4,result[0].size)
240
+ assert_kind_of(Integer,result[0][0])
241
+ assert(result[0][1] == true || result[0][1] == false)
242
+ assert_kind_of(String,result[0][2])
243
+ assert_kind_of(String,result[0][3])
244
+
245
+ more_result = @samba.queue_results
246
+ @log_test.debug("more result: #{more_result.inspect}")
247
+ assert_kind_of(Array,more_result)
248
+ assert_kind_of(Array,more_result[0])
249
+ assert_equal(4,more_result[0].size)
250
+ assert_kind_of(Integer,more_result[0][0])
251
+ assert(more_result[0][1] == true || more_result[0][1] == false)
252
+ assert_kind_of(String,more_result[0][2])
253
+ assert_kind_of(String,more_result[0][3])
254
+
255
+ total_result = result + more_result
256
+ assert_equal(jobs,total_result.size)
257
+
258
+ assert_equal(true, @samba.queue_empty?)
259
+ assert_equal(true, @samba.queue_done?)
260
+ assert_equal(0,@samba.queue_waiting)
261
+
262
+ files.each do |file|
263
+ del = @samba.del(file)
264
+ @log_test.debug("remote delete: #{del.inspect}")
265
+ File.delete("#{TESTDIR}/#{file}")
266
+ end
267
+
268
+ re = @samba.lcd('..')
269
+ assert_equal(true,re)
270
+ end
155
271
 
156
272
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sambala
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Louis-Philippe Perron
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-03 00:00:00 -05:00
12
+ date: 2009-02-09 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.3.2
23
+ version: 1.3.4
24
24
  version:
25
25
  description:
26
26
  email: lp@spiralix.org