sambala 0.8.8 → 0.8.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sambala.rb CHANGED
@@ -43,7 +43,7 @@ class Sambala
43
43
  # * :password = the password to log into the server
44
44
  # * :threads = how many parallel operations you want initiated, !!! higher than 4 at you own risk !!!
45
45
  # === Example
46
- # sam = Sambala.new( :domain => 'NTDOMAIN',
46
+ # samba = Sambala.new( :domain => 'NTDOMAIN',
47
47
  # :host => 'sambaserver',
48
48
  # :share => 'sambashare',
49
49
  # :user => 'walrus',
@@ -52,27 +52,23 @@ class Sambala
52
52
  def initialize(options={:domain => '', :host => '', :share => '', :user => '', :password => '', :threads => 1})
53
53
  begin
54
54
  options[:threads] = 4 if options[:threads] > 4
55
- options[:init_timeout] = options[:threads] * 2
55
+ options[:init_timeout] = options[:threads] * 1
56
56
  @options = options; gardener_ok
57
- rescue SmbInitError
58
- raise SmbInitError.exception("Failed smbclient initialisation")
59
57
  rescue
60
58
  @gardener.close unless @gardener.nil? || @gardener.class != 'Gardener'
61
59
  raise RuntimeError.exception("Unknown Process Failed!!")
62
60
  end
63
61
  end
64
- # The +cd+ instance method takes only one argument, the path to which you wish to change directory
65
- # Its one of the only implemented command where queue mode is not available, for the simple reason that
66
- # when queued operations are executed in parallel, one does not control which command will get executed first,
67
- # making a queued +cd+ operation very dangerous.
62
+
63
+ # The +cd+ instance method takes only one argument, the path to which you wish to change directory
68
64
  # === Parameters
69
65
  # * :to = the path to change directory to
70
66
  # === Interactive Returns
71
67
  # * _boolean_ = confirms if +cd+ operation completed successfully
72
68
  # === Example
73
- # sam.cd(:to => 'aFolder/anOtherFolder/') # => true
69
+ # samba.cd(:to => 'aFolder/anOtherFolder/') # => true
74
70
  def cd(opts={:to => ''})
75
- execute('cd', opts[:to], false)[0]
71
+ execute_all('cd', opts[:to])
76
72
  end
77
73
 
78
74
  # The +du+ instance does exactly what _du_ usually does: estimates file space usage.
@@ -81,7 +77,7 @@ class Sambala
81
77
  # === Interactive Returns
82
78
  # * _string_ = +du+ command results
83
79
  # === Example
84
- # puts sam.du # => 34923 blocks of size 2097152. 27407 blocks available
80
+ # puts samba.du # => 34923 blocks of size 2097152. 27407 blocks available
85
81
  # Total number of bytes: 59439077
86
82
  def du(opts={:queue=>false})
87
83
  execute('du', '', opts[:queue])[1]
@@ -94,12 +90,12 @@ class Sambala
94
90
  # === Interactive Returns
95
91
  # * _boolean_ = confirms if +del+ operation completed successfully
96
92
  # === Example
97
- # sam.del(:mask => 'aFile') # => true
93
+ # samba.del(:mask => 'aFile') # => true
98
94
  def del(opts={:mask => nil, :queue=>false})
99
95
  execute('del', opts[:mask], opts[:queue])[0]
100
96
  end
101
-
102
- # The maexist? instance method is borrowed from Ruby File Class idiome.
97
+ alias rm del
98
+ # The exist? instance method is borrowed from Ruby File Class idiome.
103
99
  # It is used to test the presence of files or directories on the server
104
100
  # === Parameters
105
101
  # * :mask = the mask matching the file or directory to look for.
@@ -107,7 +103,7 @@ class Sambala
107
103
  # === Interactive Returns
108
104
  # * _boolean_ = confirm the presence of a matching file or directory
109
105
  # === Example
110
- # sam.exist?(:mask => 'aFile') # => true
106
+ # samba.exist?(:mask => 'aFile') # => true
111
107
  def exist?(opts={:mask => nil, :queue => false})
112
108
  execute('ls', opts[:mask], opts[:queue])[0]
113
109
  end
@@ -121,24 +117,21 @@ class Sambala
121
117
  # === Interactive Returns
122
118
  # _array_ = [ _booleanSuccess_, _getResultMessage_ ]
123
119
  # === Example
124
- # sam.get(:from => 'aFile.txt') # => [true, "getting file \\aFile.txt.rb of size 3877 as test.rb (99.6 kb/s) (average 89.9 kb/s)\r\n"]
120
+ # samba.get(:from => 'aFile.txt') # => [true, "getting file \\aFile.txt.rb of size 3877 as test.rb (99.6 kb/s) (average 89.9 kb/s)\r\n"]
125
121
  def get(opts={:from => nil, :to => nil, :queue => false})
126
122
  opts[:to].nil? ? strng = opts[:from] : strng = opts[:from] + ' ' + opts[:to]
127
123
  execute('get', strng, opts[:queue])
128
124
  end
129
125
 
130
- # The +lcd+ instance method changes the current working directory on the local machine to the directory specified.
131
- # Its one of the only implemented command where queue mode is not available, for the simple reason that
132
- # when queued operations are executed in parallel, one does not control which command will get executed first,
133
- # making a queued +lcd+ operation very dangerous.
126
+ # The +lcd+ instance method changes the current working directory on the local machine to the directory specified.
134
127
  # === Parameters
135
128
  # * :to = the path to change directory to
136
129
  # === Interactive Returns
137
130
  # * _boolean_ = confirms if +cd+ operation completed successfully
138
131
  # === Example
139
- # sam.lcd(:to => 'aLocalFolder/anOtherFolder/') # => true
132
+ # samba.lcd(:to => 'aLocalFolder/anOtherFolder/') # => true
140
133
  def lcd(opts={:to => ''})
141
- execute('lcd', opts[:to], false)[0]
134
+ execute_all('lcd', opts[:to])
142
135
  end
143
136
 
144
137
  # The +lowercase+ method toggles lowercasing of filenames for the get command.
@@ -147,7 +140,7 @@ class Sambala
147
140
  # === Interactive Returns
148
141
  # * _boolean_ = confirms if +lowercase+ operation completed successfully
149
142
  # === Example
150
- # sam.lowercase # => true # toggle from files all UPPERCASE to all lowercase
143
+ # samba.lowercase # => true # toggle from files all UPPERCASE to all lowercase
151
144
  def lowercase
152
145
  execute_all('lowercase' ,'')
153
146
  end
@@ -159,7 +152,7 @@ class Sambala
159
152
  # === Interactive Returns
160
153
  # * _string_ = containing +ls+ command results
161
154
  # === Example
162
- # sam.ls # => genpi.rb A 81 Mon Nov 17 22:12:40 2008
155
+ # samba.ls # => genpi.rb A 81 Mon Nov 17 22:12:40 2008
163
156
  # 34923 blocks of size 2097152. 27407 blocks available
164
157
  def ls(opts={:mask => nil, :queue=>false})
165
158
  execute('ls' ,opts[:mask], opts[:queue])[1]
@@ -172,7 +165,7 @@ class Sambala
172
165
  # === Parameters
173
166
  # * :mask = the matching filter
174
167
  # === Example
175
- # sam.mask(:mask => 'filter*') # => true
168
+ # samba.mask(:mask => 'filter*') # => true
176
169
  def mask(opts={:mask => nil})
177
170
  execute_all('mask' ,opts[:mask])
178
171
  end
@@ -185,7 +178,7 @@ class Sambala
185
178
  # === Interactive Returns
186
179
  # _array_ = [ _booleanSuccess_, _mgetResultMessage_ ]
187
180
  # === Example
188
- # sam.mget(:mask => 'file*') # => [true, "getting file \\file_new.txt of size 3877 as file_new.txt (99.6 kb/s) (average 89.9 kb/s)\r\n"]
181
+ # samba.mget(:mask => 'file*') # => [true, "getting file \\file_new.txt of size 3877 as file_new.txt (99.6 kb/s) (average 89.9 kb/s)\r\n"]
189
182
  def mget(opts={:mask => nil, :queue => false})
190
183
  execute('mget' ,opts[:mask], opts[:queue])
191
184
  end
@@ -197,7 +190,7 @@ class Sambala
197
190
  # === Interactive Returns
198
191
  # * _boolean_ = confirms if +mkdir+ operation completed successfully
199
192
  # === Example
200
- # sam.mkdir(:path => 'aFolder/aNewFolder') # => true
193
+ # samba.mkdir(:path => 'aFolder/aNewFolder') # => true
201
194
  def mkdir(opts={:path => '', :queue => false})
202
195
  execute('mkdir' ,opts[:path], opts[:queue])[0]
203
196
  end
@@ -211,7 +204,7 @@ class Sambala
211
204
  # === Interactive Returns
212
205
  # _array_ = [ _booleanSuccess_, _mputResultMessage_ ]
213
206
  # === Example
214
- # sam.mput(:mask => 'file*') # => [true, "putting file \\file_new.txt of size 1004 as file_new.txt (65.4 kb/s) (average 65.4 kb/s)\r\n"]
207
+ # samba.mput(:mask => 'file*') # => [true, "putting file \\file_new.txt of size 1004 as file_new.txt (65.4 kb/s) (average 65.4 kb/s)\r\n"]
215
208
  def mput(opts={:mask => nil, :queue => false})
216
209
  execute('mput' ,opts[:mask], opts[:queue])
217
210
  end
@@ -225,7 +218,7 @@ class Sambala
225
218
  # === Interactive Returns
226
219
  # _array_ = [ _booleanSuccess_, _putResultMessage_ ]
227
220
  # === Example
228
- # sam.put(:from => 'aLocalFile.txt') # => [false, "aLocalFile.txt does not exist\r\n"]
221
+ # samba.put(:from => 'aLocalFile.txt') # => [false, "aLocalFile.txt does not exist\r\n"]
229
222
 
230
223
  def put(opts={:from => nil, :to => nil, :queue => false})
231
224
  opts[:to].nil? ? strng = opts[:from] : strng = opts[:from] + ' ' + opts[:to]
@@ -237,10 +230,22 @@ class Sambala
237
230
  # === Interactive Returns
238
231
  # * _boolean_ = confirms if +mkdir+ operation completed successfully
239
232
  # === Example
240
- # sam.recurse # => true
233
+ # samba.recurse # => true
241
234
  def recurse
242
235
  execute_all('recurse' ,'')
243
236
  end
237
+
238
+ # The +rmdir+ method deletes the specified directory
239
+ # === Parameters
240
+ # * :path = the relative path to the directory to be deleted
241
+ # * :queue = sets queue processing mode. Defaults to interactive mode when no option given.
242
+ # === Interactive Returns
243
+ # * _boolean_ = confirms if +rmdir+ operation completed successfully
244
+ # === Example
245
+ # samba.rmdir(:path => 'mydir') # => true
246
+ def rmdir(opts={:path=>nil,:queue=>false})
247
+ execute('rmdir' ,opts[:path], opts[:queue])[0]
248
+ end
244
249
 
245
250
  # The +volume+ method returns remote volume information.
246
251
  # === Parameters
@@ -248,14 +253,14 @@ class Sambala
248
253
  # === Interactive Returns
249
254
  # * _string_ = containing +volume+ command results
250
255
  # === Example
251
- # sam.volume # => "Volume: |geminishare| serial number 0x6d723053"
256
+ # samba.volume # => "Volume: |geminishare| serial number 0x6d723053"
252
257
  def volume(opts={:queue=>false})
253
258
  execute('volume' ,'', opts[:queue])[1]
254
259
  end
255
260
 
256
261
  # The +queue_results+ methods collect a done queue items results
257
262
  # === Example
258
- # result = sam.queue_results
263
+ # result = samba.queue_results
259
264
  def queue_results
260
265
  crop = @gardener.harvest(:full_crop)
261
266
  crop.map! { |result| [ result[:success], result[:seed], result[:message] ] }
@@ -263,14 +268,14 @@ class Sambala
263
268
 
264
269
  # The +progress+ method returns a progress ratio indicator from 0.00 to 1.00
265
270
  # === Example
266
- # progress = sam.progress # => 0.75
271
+ # progress = samba.progress # => 0.75
267
272
  def progress
268
273
  @gardener.growth(:progress)
269
274
  end
270
275
 
271
276
  # The +close+ method safely end the smbclient session
272
277
  # === Example
273
- # sam.close
278
+ # samba.close
274
279
  def close
275
280
  result = @gardener.close
276
281
  result.values.map { |queue| queue.empty? }.uniq.size == 1 ? true : false
@@ -11,6 +11,7 @@ class Sambala
11
11
  #
12
12
  # :title:Sambala::Gardener
13
13
  module Gardener
14
+ require 'timeout'
14
15
 
15
16
  # The +execute+ method splits the execution according to the operation mode: queue or interactive.
16
17
  # === Parameters
@@ -31,7 +32,6 @@ class Sambala
31
32
  # === Example
32
33
  # result = execute_all('mask','match*') # => true
33
34
  def execute_all(command,data)
34
- sleep 1
35
35
  result = @gardener.seed_all("#{command} #{data}")
36
36
  bools = result.map { |row| row[:success] }
37
37
  bools.uniq.size == 1 ? true : false
@@ -65,11 +65,15 @@ class Sambala
65
65
  init = []
66
66
  catch :gardener do
67
67
  4.times do |num|
68
- init_gardener; init = @init_status = @gardener.init_status
69
- init.map! { |result| result[:success] }
70
- throw :gardener if init.uniq.size == 1 and init[0] == true
71
- @gardener.close; @gardener = nil
72
- @options[:threads] -= 1 unless @options[:threads] == 1; @options[:init_timeout] += 1
68
+ init_gardener; sleep 1
69
+ begin
70
+ Timeout.timeout(2) { @init_status = @gardener.init_status }
71
+ init = @init_status
72
+ init.map! { |result| result[:success] }
73
+ throw :gardener if init.uniq.size == 1 and init[0] == true
74
+ rescue Timeout::Error
75
+ end
76
+ kill_gardener_and_incr
73
77
  end
74
78
  raise SmbInitError.exception("Couldn't set smbclient properly")
75
79
  end
@@ -81,7 +85,6 @@ class Sambala
81
85
  PTY.spawn("smbclient //#{@options[:host]}/#{@options[:share]} #{@options[:password]} -W #{@options[:domain]} -U #{@options[:user]}") do |r,w,pid|
82
86
  w.sync = true
83
87
  $expect_verbose = false
84
-
85
88
  catch :init do
86
89
  loop do
87
90
  r.expect(/.*\xD\xAsmb:[ \x5C]*\x3E.*/) do |text|
@@ -92,15 +95,14 @@ class Sambala
92
95
  end
93
96
  end
94
97
  end
95
-
96
98
  Abundance.grow do |seed|
97
99
  w.print "#{seed.sprout}\r"
98
100
  catch :result do
99
101
  loop do
100
- r.expect(/.*\xD\xAsmb: \w*[\x5C]*\x3E.*/) do |text|
102
+ r.expect(/.*\xD\xAsmb: [\x5C]*\w*[\x5C]+\x3E.*/) do |text|
101
103
  if text != nil
102
104
  msg = text[0]
103
-
105
+
104
106
  msg.gsub!(/smb: \w*\x5C\x3E\s*$/, '')
105
107
  msg.gsub!(/^\s*#{seed.sprout}/, '')
106
108
  msg.lstrip!
@@ -122,6 +124,20 @@ class Sambala
122
124
  end
123
125
  end
124
126
  end
127
+
128
+ private
129
+
130
+ def kill_gardener_and_incr
131
+ begin
132
+ Timeout.timeout(2) { @gardener.close }
133
+ rescue Timeout::Error
134
+ pids = @gardener.rows_pids; pids << @gardener.garden_pid
135
+ pids.each { |pid| Process.kill('HUP', pid)}
136
+ end
137
+ @gardener = nil
138
+ @options[:threads] -= 1 unless @options[:threads] == 1; @options[:init_timeout] += 1
139
+ end
140
+
125
141
  end
126
142
 
127
143
  end
@@ -0,0 +1,98 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'test/unit'
3
+ require 'sambala'
4
+
5
+ TESTDIR = 'sambala_test'
6
+
7
+ class TestSambalaMain < Test::Unit::TestCase
8
+
9
+ def setup
10
+ check_smbclient_presence
11
+ get_samba_param_from_input
12
+ init_sambala
13
+ end
14
+
15
+ def test_main
16
+ ls_one = check_ls
17
+ check_mkdir(TESTDIR)
18
+ check_exist(TESTDIR)
19
+ check_cd(TESTDIR)
20
+ ls_two = check_ls
21
+ assert(ls_one != ls_two)
22
+ # check_exist
23
+ check_cd('..')
24
+ check_rmdir(TESTDIR)
25
+ end
26
+
27
+ def teardown
28
+ close = @samba.close
29
+ assert(close)
30
+ end
31
+
32
+ private
33
+
34
+ def check_smbclient_presence
35
+ answer = `smbclient --help`
36
+ assert(answer.include?('Usage'), "No 'smbclient' tool was found on this computer,\nPlease install 'smbclient' and try again.")
37
+ end
38
+
39
+ def get_samba_param_from_input
40
+ puts "I will need you to input some working Samba connection settings..."
41
+ print "\n"; sleep 1
42
+ print "host name or IP: "
43
+ @host = $stdin.gets.chomp
44
+ print "share name: "
45
+ @share = $stdin.gets.chomp
46
+ print "domain: "
47
+ @domain = $stdin.gets.chomp
48
+ print "user: "
49
+ @user = $stdin.gets.chomp
50
+ print "password: "
51
+ @password = $stdin.gets.chomp
52
+ puts "I will now try to connect to #{@share.to_s} for the purpose of testing sambala..."
53
+ print "\n"
54
+ end
55
+
56
+ def init_sambala
57
+ @samba = Sambala.new( :domain => @domain,
58
+ :host => @host,
59
+ :share => @share,
60
+ :user => @user,
61
+ :password => @password,
62
+ :threads => 1)
63
+ puts "Connection successfull,\nnow proceding with test:"
64
+ end
65
+
66
+ def check_ls
67
+ result = @samba.ls
68
+ assert_not_nil(result)
69
+ result_alias = @samba.dir
70
+ assert_not_nil(result)
71
+ assert(result == result_alias)
72
+ return result
73
+ end
74
+
75
+ def check_cd(path)
76
+ cd = @samba.cd(:to => path)
77
+ assert_equal(true,cd)
78
+ end
79
+
80
+ def check_exist(path)
81
+ exist = @samba.exist?(:mask => path)
82
+ assert_equal(true,exist)
83
+ end
84
+
85
+ def check_mkdir(path)
86
+ re = @samba.mkdir(:path => path)
87
+ assert_equal(true,re)
88
+ end
89
+
90
+ def check_rmdir(path)
91
+ re = @samba.rmdir(:path => path)
92
+ assert_equal(true,re)
93
+ end
94
+
95
+
96
+
97
+
98
+ 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.8.8
4
+ version: 0.8.9
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-01-18 00:00:00 -05:00
12
+ date: 2009-01-27 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.2.6
23
+ version: 1.3.1
24
24
  version:
25
25
  description:
26
26
  email: lp@spiralix.org
@@ -33,7 +33,7 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - lib/sambala.rb
35
35
  - lib/sambala_gardener.rb
36
- - lib/test_sambala2.rb
36
+ - test/tc_sambala_main.rb
37
37
  has_rdoc: true
38
38
  homepage: http://sambala.rubyforge.org/
39
39
  post_install_message:
@@ -60,5 +60,5 @@ rubygems_version: 1.2.0
60
60
  signing_key:
61
61
  specification_version: 2
62
62
  summary: ruby samba client, interactive smbclient commands session and multi-threaded smb transfer queued mode
63
- test_files: []
64
-
63
+ test_files:
64
+ - test/tc_sambala_main.rb
data/lib/test_sambala2.rb DELETED
@@ -1,72 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
- # require 'test/unit'
4
- # require 'rubygems'
5
- require 'sambala'
6
-
7
- puts "init"
8
- begin
9
- # b = Sambala.new( :domain => 'TCSMTLPROD',
10
- # :host => 'modulagampc',
11
- # :share => 'testBkp',
12
- # :user => 'perronlo',
13
- # :password => 'lpMOD1135',
14
- # :threads => 4 )
15
- # rescue Sambala::SmbInitError
16
- # raise RuntimeError.exception("Couldn't initialise Sambala")
17
- # end
18
-
19
- b = Sambala.new( :domain => '',
20
- :host => 'spiralgemini',
21
- :share => 'geminishare',
22
- :user => 'leelasheel',
23
- :password => 'goLEELAubu',
24
- :threads => 2 )
25
- rescue Sambala::SmbInitError
26
- raise RuntimeError.exception("Couldn't initialise Sambala")
27
- end
28
-
29
- puts "init Sambala done"
30
- puts "sambala object: #{b.inspect}"
31
- b.lowercase
32
- puts "done lowercase"
33
- # l = b.ls
34
- l = b.ls(:mask => 'gen*')
35
- puts "progress is: #{b.progress}"
36
- puts "ls: #{l.inspect}"
37
- # pu = b.put(:from => 'shoe.rb')
38
- # puts "progress is: #{b.progress}"
39
- # puts "put: #{pu.inspect}"
40
-
41
- # g = b.get(:from => 'inin.rb')
42
- # puts "get: #{g.inspect}"
43
- v = b.volume
44
- puts "progress is: #{b.progress}"
45
- puts "volume: #{v.inspect}"
46
-
47
- b.recurse
48
- d = b.du
49
- puts "progress is: #{b.progress}"
50
- puts "du: #{d.inspect}"
51
- # c = b.close
52
- # puts "CCCCCCCCCC is: #{c.inspect}"
53
-
54
- lsqueue = b.ls(:queue => true)
55
- puts "lsqueue: #{lsqueue.inspect} #{lsqueue.class}"
56
- # b.put(:from => 'ore.rb', :queue => true)
57
- puts "progress is: #{b.progress}"
58
- # b.get(:from => 'TEST.rb', :queue => true)
59
- b.volume(:queue => true)
60
- puts "progress is: #{b.progress}"
61
- b.du(:queue => true)
62
- puts "progress is: #{b.progress}"
63
-
64
- res = b.queue_results
65
- puts "results is: #{res.inspect}"
66
- # sleep 1
67
- # b.recurse
68
- # l = b.ls
69
- # puts "ls: #{l.inspect}"
70
-
71
- c = b.close
72
- puts "CCCCCCCCCC is: #{c.inspect}"