file_copy 0.0.2 → 1.0.0

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.
@@ -1,66 +1,68 @@
1
1
  require 'net/ssh'
2
2
  require 'net/sftp'
3
3
 
4
- module BBFS
5
- module FileCopy
4
+ require 'log'
5
+ require 'params'
6
6
 
7
- # Creates ssh connection, assumes username and password
8
- # or without password but with ssh keys.
9
- def ssh_connect(username, password, server)
10
- username = (username and username.length > 0) ? username : ENV['USER']
11
- password = (password and password.length > 0) ? password : nil
12
- port = 22 # 22 is a standart SSH port
13
- raise "Undefined server" unless server
14
- p "Trying to connect(ssh): #{username}, #{password}, #{server}."
15
- if (username and password)
16
- Net::SSH.start(server, username,
17
- :password => password,
18
- :port => port)
19
- elsif (username)
20
- Net::SSH.start(server, username,
21
- :port => port)
22
- else
23
- raise "Undefined username"
24
- end
25
- end
26
- module_function :ssh_connect
27
-
28
- # Simply copy map files using sftp server on dest_server.
29
- # Note: stfp.upload support parallel uploads - default is 2.
30
- # TODO(kolman): packing files, all, not all, determine by part of file.
31
- def sftp_copy(username, password, server, files_map)
32
- ssh = FileCopy.ssh_connect(username, password, server)
33
- ssh.sftp.connect do |sftp|
34
- uploads = files_map.map { |from,to|
35
- remote_dir = File.dirname(to)
36
- sftp_mkdir_recursive(sftp, File.dirname(to))
37
- p "Copying #{from} to #{to}"
38
- sftp.upload(from, to)
39
- }
40
- uploads.each { |u| u.wait }
41
- p "Done."
42
- end # ssh.sftp.connect
43
- end # def initialize
44
- module_function :sftp_copy
7
+ module FileCopy
45
8
 
46
- def sftp_mkdir_recursive(sftp, path)
47
- dir_stat = nil
48
- begin
49
- p "Stat remote dir: #{path}."
50
- dir_stat = sftp.stat!(path).directory?
51
- p "Stat result #{dir_stat}."
52
- rescue Net::SFTP::StatusException
53
- end
54
- if !dir_stat
55
- p "Directory does not exists: #{path}."
56
- sftp_mkdir_recursive sftp, File.dirname(path)
57
- sftp.mkdir!(path)
58
- end
9
+ # Creates ssh connection, assumes username and password
10
+ # or without password but with ssh keys.
11
+ def ssh_connect(username, password, server)
12
+ username = (username and username.length > 0) ? username : ENV['USER']
13
+ password = (password and password.length > 0) ? password : nil
14
+ port = 22 # 22 is a standart SSH port
15
+ raise "Undefined server" unless server
16
+ Log.info "Trying to connect(ssh): #{username}, #{password}, #{server}, #{port}."
17
+ if (username and password)
18
+ Net::SSH.start(server, username,
19
+ :password => password,
20
+ :port => port)
21
+ elsif (username)
22
+ Net::SSH.start(server, username,
23
+ :port => port)
24
+ else
25
+ raise "Undefined username"
59
26
  end
60
- module_function :sftp_mkdir_recursive
61
-
27
+ end
28
+ module_function :ssh_connect
62
29
 
30
+ # Simply copy map files using sftp server on dest_server.
31
+ # Note: stfp.upload support parallel uploads - default is 2.
32
+ # TODO(kolman): packing files, all, not all, determine by part of file.
33
+ def sftp_copy(username, password, server, files_map)
34
+ ssh = FileCopy.ssh_connect(username, password, server)
35
+ ssh.sftp.connect do |sftp|
36
+ uploads = files_map.map { |from,to|
37
+ remote_dir = File.dirname(to)
38
+ sftp_mkdir_recursive(sftp, File.dirname(to))
39
+ Log.info "Copying #{from} to #{to}"
40
+ sftp.upload(from, to)
41
+ }
42
+ uploads.each { |u| u.wait }
43
+ Log.info "Done."
44
+ end # ssh.sftp.connect
45
+ end # def initialize
46
+ module_function :sftp_copy
63
47
 
48
+ def sftp_mkdir_recursive(sftp, path)
49
+ dir_stat = nil
50
+ begin
51
+ Log.info "Stat remote dir: #{path}."
52
+ dir_stat = sftp.stat!(path).directory?
53
+ Log.info "Stat result #{dir_stat}."
54
+ rescue Net::SFTP::StatusException
55
+ end
56
+ if !dir_stat
57
+ Log.info "Directory does not exists: #{path}."
58
+ sftp_mkdir_recursive sftp, File.dirname(path)
59
+ Log.info "Making dir #{path}."
60
+ response = sftp.mkdir!(path)
61
+ Log.info "Making dir ok:#{response.ok?}."
62
+ end
64
63
  end
64
+ module_function :sftp_mkdir_recursive
65
+
65
66
  end
66
67
 
68
+
@@ -0,0 +1,4 @@
1
+ module FileCopy
2
+ VERSION = "1.0.0"
3
+ end
4
+
data/lib/file_copy.rb CHANGED
@@ -1,7 +1,4 @@
1
- require_relative 'file_copy/copy'
1
+ require 'file_copy/copy'
2
2
 
3
- module BBFS
4
- module FileCopy
5
- VERSION = "0.0.1"
6
- end
3
+ module FileCopy
7
4
  end
@@ -0,0 +1,54 @@
1
+ require 'rspec'
2
+
3
+ require_relative '../../lib/file_copy/copy.rb'
4
+
5
+ module FileCopy
6
+ module Spec
7
+
8
+ describe 'FileCopy::ssh_connect' do
9
+ it 'should raise error when username not specified' do
10
+ ENV.stub(:[]).with(any_args()).and_return(nil)
11
+ expect { FileCopy::ssh_connect(nil, nil, 'a server') }.to raise_error "Undefined username"
12
+ end
13
+
14
+ it 'should raise error when server not specified' do
15
+ expect { FileCopy::ssh_connect('kuku', nil, nil) }.to raise_error "Undefined server"
16
+ end
17
+
18
+ it 'should try to connect if username is set explicitly' do
19
+ Net::SSH.should_receive(:start).with(any_args())
20
+ FileCopy::ssh_connect('kuku', nil, 'a server')
21
+ end
22
+ it 'should try to connect if username is set via ENV variable' do
23
+ Net::SSH.should_receive(:start).with(any_args())
24
+ ENV.stub(:[]).with("USER").and_return('kuku')
25
+ FileCopy::ssh_connect(nil, nil, 'a server')
26
+ end
27
+ end
28
+
29
+ # TODO(kolman): Bad test, should rewrite and understand how to write test correctly.
30
+ describe 'FileCopy::sftp_copy' do
31
+ it 'call upload with correct files' do
32
+ ssh_connection = double('Net::SSH::Connection')
33
+ sftp_session = double('Net::SFTP::Session')
34
+ uploader = double('Operations::Upload')
35
+ sftp_attributes = double('Attributes')
36
+
37
+ # Stubbing sftp.
38
+ FileCopy.stub(:ssh_connect).and_return(ssh_connection)
39
+ ssh_connection.stub(:sftp).and_return(sftp_session)
40
+ sftp_session.stub(:connect).and_yield(sftp_session)
41
+ sftp_session.stub(:stat!).and_return(sftp_attributes)
42
+ sftp_attributes.stub(:directory?).and_return(true)
43
+ uploader.stub(:wait).and_return(true, true)
44
+
45
+ # Test file uploaded
46
+ sftp_session.should_receive(:upload).with('a', 'b').and_return(uploader)
47
+ sftp_session.should_receive(:upload).with('c', 'd').and_return(uploader)
48
+ FileCopy::sftp_copy(nil, nil, nil, { 'a' => 'b', 'c' => 'd' })
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_copy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,18 +9,51 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-11 00:00:00.000000000Z
13
- dependencies: []
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: log
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: params
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
14
46
  description: Library for copying files to remote servers.
15
- email: kolmanv@gmail.com
47
+ email: bbfsdev@gmail.com
16
48
  executables: []
17
49
  extensions: []
18
50
  extra_rdoc_files: []
19
51
  files:
20
52
  - lib/file_copy.rb
21
53
  - lib/file_copy/copy.rb
22
- - test/file_copy/copy_spec.rb
23
- homepage: http://github.com/kolmanv/bbfs
54
+ - lib/file_copy/version.rb
55
+ - spec/file_copy/copy_spec.rb
56
+ homepage: http://github.com/bbfsdev/bbfs
24
57
  licenses: []
25
58
  post_install_message:
26
59
  rdoc_options: []
@@ -40,9 +73,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
73
  version: '0'
41
74
  requirements: []
42
75
  rubyforge_project:
43
- rubygems_version: 1.8.15
76
+ rubygems_version: 1.8.24
44
77
  signing_key:
45
78
  specification_version: 3
46
79
  summary: Library for copying files remotely.
47
80
  test_files:
48
- - test/file_copy/copy_spec.rb
81
+ - spec/file_copy/copy_spec.rb
@@ -1,47 +0,0 @@
1
- require_relative '../../lib/file_copy/copy.rb'
2
-
3
- module BBFS
4
- module FileCopy
5
- module Spec
6
-
7
- describe 'FileCopy::ssh_connect' do
8
- it 'should raise error when username not specified' do
9
- ENV.stub(:[]).with(any_args()).and_return(nil)
10
- expect { FileCopy::ssh_connect(nil, nil, 'a server') }.should raise_error "Undefined username"
11
- end
12
-
13
- it 'should raise error when server not specified' do
14
- expect { FileCopy::ssh_connect('kuku', nil, nil) }.should raise_error "Undefined server"
15
- end
16
-
17
- it 'should try to connect if username is set explicitly' do
18
- Net::SSH.should_receive(:start).with(any_args())
19
- FileCopy::ssh_connect('kuku', nil, 'a server')
20
- end
21
- it 'should try to connect if username is set via ENV variable' do
22
- Net::SSH.should_receive(:start).with(any_args())
23
- ENV.stub(:[]).with("USER").and_return('kuku')
24
- FileCopy::ssh_connect(nil, nil, 'a server')
25
- end
26
- end
27
-
28
- # TODO(kolman): Very bad test, should rewrite and understand how to
29
- # write test correctly.
30
- describe 'FileCopy::sftp_copy' do
31
- it 'should copy files' do
32
- ssh_connection = double('Net::SSH::Connection')
33
- FileCopy.should_receive(:ssh_connect).with(any_args()).and_return(ssh_connection)
34
- sftp_session = double('Net::SFTP::Session')
35
- ssh_connection.should_receive(:sftp).with(any_args()).and_return(sftp_session)
36
- sftp_session.should_receive(:connect).with(any_args()).and_yield(sftp_session)
37
- uploader = double('Operations::Upload')
38
- uploader.should_receive(:wait).with(any_args()).and_return(true, true)
39
- sftp_session.should_receive(:upload).with('a', 'b').and_return(uploader)
40
- sftp_session.should_receive(:upload).with('c', 'd').and_return(uploader)
41
- FileCopy::sftp_copy(nil, nil, nil, { 'a' => 'b', 'c' => 'd' })
42
- end
43
- end
44
-
45
- end
46
- end
47
- end