vfs 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/Rakefile +2 -2
  2. data/lib/vfs.rb +18 -0
  3. data/lib/vfs/entries/dir.rb +272 -0
  4. data/lib/vfs/entries/entry.rb +127 -0
  5. data/lib/vfs/entries/file.rb +185 -0
  6. data/lib/vfs/entries/universal_entry.rb +24 -0
  7. data/lib/vfs/entry_proxy.rb +38 -0
  8. data/lib/vfs/error.rb +4 -0
  9. data/lib/vfs/integration/string.rb +19 -0
  10. data/lib/vfs/path.rb +125 -0
  11. data/lib/vfs/storages/hash_fs.rb +194 -0
  12. data/lib/vfs/storages/local.rb +138 -0
  13. data/lib/vfs/storages/specification.rb +127 -0
  14. data/lib/vfs/support.rb +2 -0
  15. data/readme.md +110 -14
  16. data/spec/container_spec.rb +31 -0
  17. data/spec/dir_spec.rb +224 -0
  18. data/spec/entry_spec.rb +24 -0
  19. data/spec/file_spec.rb +189 -0
  20. data/spec/path_spec.rb +121 -0
  21. data/spec/spec_helper.rb +2 -9
  22. data/spec/storages/hash_fs_spec.rb +10 -0
  23. data/spec/storages/local_spec.rb +10 -0
  24. data/spec/universal_entry_spec.rb +42 -0
  25. metadata +25 -23
  26. data/lib/old/ssh.rb +0 -11
  27. data/lib/rsh.rb +0 -19
  28. data/lib/rsh/box.rb +0 -182
  29. data/lib/rsh/box/marks.rb +0 -29
  30. data/lib/rsh/drivers/abstract.rb +0 -15
  31. data/lib/rsh/drivers/local.rb +0 -48
  32. data/lib/rsh/drivers/ssh.rb +0 -147
  33. data/lib/rsh/gems.rb +0 -2
  34. data/lib/rsh/support.rb +0 -30
  35. data/spec/abstract_driver.rb +0 -82
  36. data/spec/abstract_driver/dir/dir2/file +0 -0
  37. data/spec/abstract_driver/local_file +0 -1
  38. data/spec/box_spec.rb +0 -109
  39. data/spec/box_spec/dir/dir2/file +0 -0
  40. data/spec/box_spec/local_file +0 -1
  41. data/spec/config.example.yml +0 -4
  42. data/spec/config.yml +0 -5
  43. data/spec/local_driver_spec.rb +0 -9
  44. data/spec/ssh_driver_spec.rb +0 -15
data/lib/old/ssh.rb DELETED
@@ -1,11 +0,0 @@
1
- class Ssh
2
- attr_reader :config
3
-
4
-
5
- def setstat(path, options)
6
- remote do |sh, sftp|
7
- sftp.setstat! path, options
8
- end
9
- end
10
-
11
- end
data/lib/rsh.rb DELETED
@@ -1,19 +0,0 @@
1
- raise 'ruby 1.9.2 or higher required!' if RUBY_VERSION < '1.9.2'
2
-
3
- require 'rsh/gems'
4
-
5
- require 'open3'
6
-
7
- require 'net/ssh'
8
- require 'net/sftp'
9
-
10
- %w(
11
- support
12
-
13
- drivers/abstract
14
- drivers/local
15
- drivers/ssh
16
-
17
- box/marks
18
- box
19
- ).each{|f| require "rsh/#{f}"}
data/lib/rsh/box.rb DELETED
@@ -1,182 +0,0 @@
1
- module Rsh
2
- class Box
3
- include Marks
4
-
5
- attr_accessor :options
6
-
7
- def initialize options = {}
8
- @options = options
9
- options[:host] ||= 'localhost'
10
- end
11
-
12
- def driver
13
- unless @driver
14
- klass = options[:host] == 'localhost' ? Drivers::Local : Drivers::Ssh
15
- @driver = klass.new options
16
- end
17
- @driver
18
- end
19
-
20
- def local_driver
21
- @local_driver ||= Drivers::Local.new
22
- end
23
-
24
- def bulk &b
25
- driver.bulk &b
26
- end
27
-
28
- def upload_file from_local_path, to_remote_path, options = {}
29
- bulk do
30
- raise "file '#{from_local_path}' not exists!" unless local_driver.file_exist? from_local_path
31
- if driver.file_exist?(to_remote_path)
32
- if options[:override]
33
- driver.remove_file to_remote_path
34
- else
35
- raise "file '#{to_remote_path}' already exists!"
36
- end
37
- end
38
- driver.upload_file from_local_path, to_remote_path
39
- end
40
- end
41
-
42
- def download_file from_remote_path, to_local_path, options = {}
43
- bulk do
44
- raise "file #{from_remote_path} not exists!" unless driver.file_exist?(from_remote_path)
45
- if local_driver.file_exist? to_local_path
46
- if options[:override]
47
- local_driver.remove_file to_local_path
48
- else
49
- raise "file #{to_local_path} already exists!"
50
- end
51
- end
52
- driver.download_file from_remote_path, to_local_path
53
- end
54
- end
55
-
56
- def remove_file remote_file_path, options = {}
57
- bulk do
58
- if driver.file_exist? remote_file_path
59
- driver.remove_file remote_file_path
60
- else
61
- raise "file #{remote_file_path} not exists!" unless options[:silent]
62
- end
63
- end
64
- end
65
-
66
- def exist? remote_path
67
- driver.exist? remote_path
68
- end
69
-
70
- def file_exist? remote_file_path
71
- driver.file_exist? remote_file_path
72
- end
73
-
74
- def remove_directory remote_directory_path, options = {}
75
- bulk do
76
- if driver.directory_exist? remote_directory_path
77
- driver.remove_directory remote_directory_path
78
- else
79
- raise "directory #{remote_directory_path} not exists!" unless options[:silent]
80
- end
81
- end
82
- end
83
-
84
- def create_directory remote_path, options = {}
85
- bulk do
86
- if driver.directory_exist?(remote_path)
87
- if options[:override]
88
- driver.remove_directory remote_path
89
- driver.create_directory remote_path
90
- elsif options[:silent]
91
- # do nothing
92
- else
93
- raise "directory '#{remote_path}' already exists!"
94
- end
95
- else
96
- driver.create_directory remote_path
97
- end
98
- end
99
- end
100
-
101
- def directory_exist? remote_path
102
- driver.directory_exist? remote_path
103
- end
104
-
105
- def upload_directory from_local_path, to_remote_path, options = {}
106
- bulk do
107
- raise "directory '#{from_local_path}' not exists!" unless local_driver.directory_exist? from_local_path
108
- if driver.directory_exist?(to_remote_path)
109
- if options[:override]
110
- driver.remove_directory to_remote_path
111
- else
112
- raise "directory '#{to_remote_path}' already exists!"
113
- end
114
- end
115
- driver.upload_directory from_local_path, to_remote_path
116
- end
117
- end
118
-
119
- def download_directory from_remote_path, to_local_path, options = {}
120
- bulk do
121
- raise "directory #{from_remote_path} not exists!" unless driver.directory_exist?(from_remote_path)
122
- if local_driver.directory_exist? to_local_path
123
- if options[:override]
124
- local_driver.remove_directory to_local_path
125
- else
126
- raise "directory #{to_local_path} already exists!"
127
- end
128
- end
129
- driver.download_directory from_remote_path, to_local_path
130
- end
131
- end
132
-
133
- def with_tmp_dir &block
134
- bulk do
135
- tmp_dir = driver.generate_tmp_dir_name
136
- begin
137
- remove_directory tmp_dir if directory_exist? tmp_dir
138
- create_directory tmp_dir
139
- block.call
140
- ensure
141
- remove_directory tmp_dir if directory_exist? tmp_dir
142
- end
143
- end
144
- end
145
-
146
- def bash cmd, options = {}
147
- ignore_stderr = options.delete :ignore_stderr
148
- raise "invalid options :#{options.keys.join(', :')}" unless options.empty?
149
-
150
- code, stdout, stderr = exec cmd
151
- unless code == 0
152
- puts stdout
153
- puts stderr
154
- raise "can't execute '#{cmd}'!"
155
- end
156
- unless stderr.empty? or ignore_stderr
157
- puts stderr
158
- raise "stderr not empty for '#{cmd}'!"
159
- end
160
- stdout + stderr
161
- end
162
-
163
- def exec cmd
164
- driver.exec cmd
165
- end
166
-
167
- def home path = nil
168
- @home ||= bash('cd ~; pwd').gsub("\n", '')
169
- "#{@home}#{path}"
170
- end
171
-
172
- def inspect
173
- "<Box: #{options[:host]}>"
174
- end
175
- alias_method :to_s, :inspect
176
-
177
- protected
178
- def method_missing m, *a, &b
179
- driver.send m, *a, &b
180
- end
181
- end
182
- end
data/lib/rsh/box/marks.rb DELETED
@@ -1,29 +0,0 @@
1
- module Rsh
2
- module Marks
3
- def mark key
4
- ensure_mark_requrements!
5
- bash "touch #{marks_dir}/#{key}"
6
- end
7
-
8
- def has_mark? key
9
- ensure_mark_requrements!
10
- file_exist? "#{marks_dir}/#{key}"
11
- end
12
-
13
- def clear_marks
14
- bash "rm -r #{marks_dir}"
15
- end
16
-
17
- protected
18
- def marks_dir
19
- home "/.marks"
20
- end
21
-
22
- def ensure_mark_requrements!
23
- unless @ensure_mark_requrements
24
- create_directory marks_dir unless directory_exist? marks_dir
25
- @ensure_mark_requrements = true
26
- end
27
- end
28
- end
29
- end
@@ -1,15 +0,0 @@
1
- module Rsh
2
- module Drivers
3
- class Abstract
4
- attr_reader :options
5
-
6
- def initialize options = {}
7
- @options = options
8
- end
9
-
10
- def generate_tmp_dir_name
11
- "/tmp/ssh_tmp_dir_#{rand(10**6)}"
12
- end
13
- end
14
- end
15
- end
@@ -1,48 +0,0 @@
1
- module Rsh
2
- module Drivers
3
- class Local < Abstract
4
- def upload_file from_local_path, to_remote_path
5
- FileUtils.copy from_local_path, to_remote_path
6
- end
7
-
8
- def download_file from_remote_path, to_local_path
9
- FileUtils.copy from_remote_path, to_local_path
10
- end
11
-
12
- def exist? remote_file_path
13
- File.exist? remote_file_path
14
- end
15
-
16
- alias_method :directory_exist?, :exist?
17
- alias_method :file_exist?, :exist?
18
-
19
- def remove_file remote_file_path
20
- File.delete remote_file_path
21
- end
22
-
23
- def create_directory path
24
- Dir.mkdir path
25
- end
26
-
27
- def remove_directory path
28
- FileUtils.rm_r path
29
- end
30
-
31
- def upload_directory from_local_path, to_remote_path
32
- FileUtils.cp_r from_local_path, to_remote_path
33
- end
34
-
35
- def download_directory from_remote_path, to_local_path
36
- FileUtils.cp_r from_remote_path, to_local_path
37
- end
38
-
39
- def exec command
40
- code, stdout, stderr = Open3.popen3 command do |stdin, stdout, stderr, waitth|
41
- [waitth.value.to_i, stdout.read, stderr.read]
42
- end
43
-
44
- return code, stdout, stderr
45
- end
46
- end
47
- end
48
- end
@@ -1,147 +0,0 @@
1
- module Rsh
2
- module Drivers
3
- class Ssh < Abstract
4
- def initialize options = {}
5
- super
6
- raise "ssh options not provided!" unless options[:ssh]
7
- raise "invalid ssh options!" unless options[:ssh].is_a?(Hash)
8
- end
9
-
10
- def upload_file from_local_path, to_remote_path
11
- sftp.upload! from_local_path, fix_path(to_remote_path)
12
- end
13
-
14
- def download_file from_remote_path, to_local_path
15
- File.open to_local_path, "w" do |out|
16
- sftp.download! fix_path(from_remote_path), out #, :recursive => true
17
- end
18
- end
19
-
20
- def exist? remote_file_path
21
- begin
22
- fattrs = sftp.stat! fix_path(remote_file_path)
23
- fattrs.directory? or fattrs.file? or fattrs.symlink?
24
- rescue Net::SFTP::StatusException
25
- false
26
- end
27
- end
28
- alias_method :directory_exist?, :exist?
29
- alias_method :file_exist?, :exist?
30
-
31
- def remove_file remote_file_path
32
- sftp.remove! fix_path(remote_file_path)
33
- end
34
-
35
- def exec command
36
- remote do |ssh, sftp|
37
- # somehow net-ssh doesn't executes ~/.profile, so we need to execute it manually
38
- # command = ". ~/.profile && #{command}"
39
-
40
- stdout, stderr, code, signal = hacked_exec! ssh, command
41
-
42
- return code, stdout, stderr
43
- end
44
- end
45
-
46
- def open
47
- ssh_options = self.options[:ssh].clone
48
- host = options[:host] || raise('host not provided!')
49
- user = ssh_options.delete(:user) || raise('user not provied!')
50
- @ssh = Net::SSH.start(host, user, ssh_options)
51
- @sftp = @ssh.sftp.connect
52
- end
53
-
54
- def close
55
- @ssh.close
56
- # @sftp.close not needed
57
- @ssh, @sftp = nil
58
- end
59
-
60
- def create_directory path
61
- remote do |ssh, sftp|
62
- sftp.mkdir! path
63
- # exec "mkdir #{path}"
64
- end
65
- end
66
-
67
- def remove_directory path
68
- exec "rm -r #{path}"
69
- end
70
-
71
- def upload_directory from_local_path, to_remote_path
72
- remote do |ssh, sftp|
73
- sftp.upload! from_local_path, fix_path(to_remote_path)
74
- end
75
- end
76
-
77
- def download_directory from_remote_path, to_local_path
78
- remote do |ssh, sftp|
79
- sftp.download! fix_path(from_remote_path), to_local_path, :recursive => true
80
- end
81
- end
82
-
83
- protected
84
- def fix_path path
85
- path.sub(/^\~/, home)
86
- end
87
-
88
- def home
89
- unless @home
90
- command = 'cd ~; pwd'
91
- code, stdout, stderr = exec command
92
- raise "can't execute '#{command}'!" unless code == 0
93
- @home = stdout.gsub("\n", '')
94
- end
95
- @home
96
- end
97
-
98
- # taken from here http://stackoverflow.com/questions/3386233/how-to-get-exit-status-with-rubys-netssh-library/3386375#3386375
99
- def hacked_exec!(ssh, command, &block)
100
- stdout_data = ""
101
- stderr_data = ""
102
- exit_code = nil
103
- exit_signal = nil
104
-
105
- channel = ssh.open_channel do |channel|
106
- channel.exec(command) do |ch, success|
107
- raise "could not execute command: #{command.inspect}" unless success
108
-
109
- channel.on_data{|ch2, data| stdout_data << data}
110
- channel.on_extended_data{|ch2, type, data| stderr_data << data}
111
- channel.on_request("exit-status"){|ch,data| exit_code = data.read_long}
112
- channel.on_request("exit-signal"){|ch, data| exit_signal = data.read_long}
113
- end
114
- end
115
-
116
- channel.wait
117
-
118
- [stdout_data, stderr_data, exit_code, exit_signal]
119
- end
120
-
121
- def remote(&block)
122
- if @ssh
123
- block.call @ssh, @sftp
124
- else
125
- # Rails.logger.info "Connecting to remote Hadoop #{options[:user]}@#{options[:host]}"
126
- begin
127
- open
128
- block.call @ssh, @sftp
129
- ensure
130
- close
131
- end
132
-
133
- # Net::SSH.start options[:host], options[:user], :config => true do |ssh|
134
- # ssh.sftp.connect do |sftp|
135
- # begin
136
- # @ssh, @sftp = ssh, sftp
137
- # block.call @ssh, @sftp
138
- # ensure
139
- # @ssh, @sftp = nil
140
- # end
141
- # end
142
- # end
143
- end
144
- end
145
- end
146
- end
147
- end