vfs 0.3.15 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,183 +0,0 @@
1
- require 'tempfile'
2
-
3
- module Vfs
4
- module Storages
5
- class Local
6
- class Writer
7
- def initialize out; @out = out end
8
-
9
- def write data; @out.write data end
10
- end
11
-
12
- module LocalVfsHelper
13
- DEFAULT_BUFFER = 1000 * 1024
14
-
15
- attr_writer :buffer
16
- def buffer
17
- @buffer || DEFAULT_BUFFER
18
- end
19
-
20
- #
21
- # Attributes
22
- #
23
- def attributes path
24
- path = with_root path
25
-
26
- stat = ::File.stat path
27
- attrs = {}
28
- attrs[:file] = !!stat.file?
29
- attrs[:dir] = !!stat.directory?
30
-
31
- # attributes special for file system
32
- attrs[:created_at] = stat.ctime
33
- attrs[:updated_at] = stat.mtime
34
- attrs[:size] = stat.size if attrs[:file]
35
- attrs
36
- rescue Errno::ENOENT
37
- nil
38
- end
39
-
40
- def set_attributes path, attrs
41
- # TODO2 set attributes
42
- not_implemented
43
- end
44
-
45
-
46
- #
47
- # File
48
- #
49
- def read_file path, &block
50
- path = with_root path
51
- ::File.open path, 'r' do |is|
52
- while buff = is.gets(self.buffer || DEFAULT_BUFFER)
53
- block.call buff
54
- end
55
- end
56
- end
57
-
58
- def write_file original_path, append, &block
59
- path = with_root original_path
60
-
61
- # TODO2 Performance lost, extra call to check file existence
62
- raise "can't write, entry #{original_path} already exist!" if !append and ::File.exist?(path)
63
-
64
- option = append ? 'a' : 'w'
65
- ::File.open path, option do |out|
66
- block.call Writer.new(out)
67
- end
68
- end
69
-
70
- def delete_file path
71
- path = with_root path
72
- ::File.delete path
73
- end
74
-
75
- # def move_file from, to
76
- # FileUtils.mv from, to
77
- # end
78
-
79
-
80
- #
81
- # Dir
82
- #
83
- def create_dir path
84
- path = with_root path
85
- ::Dir.mkdir path
86
- end
87
-
88
- def delete_dir original_path
89
- path = with_root original_path
90
-
91
- # TODO2 Performance lost, extra call to check file existence
92
- raise "can't delete file (#{original_path})!" if ::File.file?(path)
93
-
94
- ::FileUtils.rm_r path
95
- end
96
-
97
- def each_entry path, query, &block
98
- path = with_root path
99
-
100
- if query
101
- path_with_trailing_slash = path == '/' ? path : "#{path}/"
102
- ::Dir["#{path_with_trailing_slash}#{query}"].each do |absolute_path|
103
- relative_path = absolute_path.sub path_with_trailing_slash, ''
104
- # TODO2 Performance loss
105
- if ::File.directory? absolute_path
106
- block.call relative_path, :dir
107
- else
108
- block.call relative_path, :file
109
- end
110
- end
111
- else
112
- ::Dir.foreach path do |relative_name|
113
- next if relative_name == '.' or relative_name == '..'
114
- if ::File.directory? "#{path}/#{relative_name}"
115
- block.call relative_name, :dir
116
- else
117
- block.call relative_name, :file
118
- end
119
- end
120
- end
121
- end
122
-
123
- # def efficient_dir_copy from, to, override
124
- # return false if override # FileUtils.cp_r doesn't support this behaviour
125
- #
126
- # from.storage.open_fs do |from_fs|
127
- # to.storage.open_fs do |to_fs|
128
- # if from_fs.local? and to_fs.local?
129
- # FileUtils.cp_r from.path, to.path
130
- # true
131
- # else
132
- # false
133
- # end
134
- # end
135
- # end
136
- # end
137
-
138
- #
139
- # Other
140
- #
141
- def local?; true end
142
-
143
- def tmp &block
144
- path = "/tmp/#{rand(10**6)}"
145
- # tmp_dir = "#{::Dir.tmpdir}/#{rand(10**6)}"
146
- if block
147
- begin
148
- ::FileUtils.mkdir_p with_root(path)
149
- block.call path
150
- ensure
151
- ::FileUtils.rm_r with_root(path) if ::File.exist? with_root(path)
152
- end
153
- else
154
- ::FileUtils.mkdir_p with_root(path)
155
- path
156
- end
157
- end
158
-
159
- def to_s; '' end
160
-
161
- protected
162
- def root
163
- @root || raise('root not defined!')
164
- end
165
-
166
- def with_root path
167
- path == '/' ? root : root + path
168
- end
169
- end
170
-
171
- include LocalVfsHelper
172
-
173
- def initialize root = ''
174
- @root = root
175
- end
176
-
177
- def open &block
178
- block.call self if block
179
- end
180
- def close; end
181
- end
182
- end
183
- end
@@ -1,161 +0,0 @@
1
- require 'rspec_ext'
2
- require 'ruby_ext'
3
-
4
- shared_examples_for 'vfs storage basic' do
5
- it 'should respond to :local?' do
6
- @storage.should respond_to(:local?)
7
- end
8
-
9
- it "should provide open method" do
10
- @storage.open
11
- @storage.open{'result'}.should == 'result'
12
- end
13
- end
14
-
15
- shared_examples_for 'vfs storage attributes basic' do
16
- it 'should have root dir' do
17
- attrs = @storage.attributes('/')
18
- attrs[:dir].should be_true
19
- attrs[:file].should be_false
20
- end
21
-
22
- it "attributes should return nil if there's no entry" do
23
- @storage.attributes('/non_existing_entry').should be_nil
24
- end
25
- end
26
-
27
- shared_examples_for 'vfs storage files' do
28
- it "file attributes" do
29
- @storage.attributes('/file').should be_nil
30
-
31
- @storage.write_file('/file', false){|w| w.write 'something'}
32
- attrs = @storage.attributes('/file')
33
- attrs[:file].should be_true
34
- attrs[:dir].should be_false
35
- end
36
-
37
- it "read, write, append" do
38
- # write
39
- @storage.write_file('/file', false){|w| w.write 'something'}
40
- @storage.attributes('/file')[:file].should == true
41
-
42
- # read
43
- data = ""
44
- @storage.read_file('/file'){|buff| data << buff}
45
- data.should == 'something'
46
-
47
- # append
48
- @storage.write_file('/file', true){|w| w.write ' another'}
49
- data = ""
50
- @storage.read_file('/file'){|buff| data << buff}
51
- data.should == 'something another'
52
- end
53
-
54
- it "delete_file" do
55
- @storage.write_file('/file', false){|w| w.write 'something'}
56
- @storage.attributes('/file')[:file].should be_true
57
- @storage.delete_file('/file')
58
- @storage.attributes('/file').should be_nil
59
- end
60
- end
61
-
62
- shared_examples_for 'vfs storage full attributes for files' do
63
- it "attributes for files" do
64
- @storage.write_file('/file', false){|w| w.write 'something'}
65
- attrs = @storage.attributes('/file')
66
- attrs[:file].should be_true
67
- attrs[:dir].should be_false
68
- attrs[:created_at].class.should == Time
69
- attrs[:updated_at].class.should == Time
70
- attrs[:size].should == 9
71
- end
72
- end
73
-
74
- shared_examples_for 'vfs storage dirs' do
75
- it "directory crud" do
76
- @storage.attributes('/dir').should be_nil
77
-
78
- @storage.create_dir('/dir')
79
- attrs = @storage.attributes('/dir')
80
- attrs[:file].should be_false
81
- attrs[:dir].should be_true
82
-
83
- @storage.delete_dir('/dir')
84
- @storage.attributes('/dir').should be_nil
85
- end
86
-
87
- it 'should delete not-empty directories' do
88
- @storage.create_dir('/dir')
89
- @storage.create_dir('/dir/dir2')
90
- @storage.write_file('/dir/dir2/file', false){|w| w.write 'something'}
91
- @storage.attributes('/dir').should_not be_nil
92
-
93
- @storage.delete_dir('/dir')
94
- @storage.attributes('/dir').should be_nil
95
- end
96
-
97
- it 'each' do
98
- -> {@storage.each_entry('/not_existing_dir', nil){|path, type| list[path] = type}}.should raise_error
99
-
100
- @storage.create_dir '/dir'
101
- @storage.create_dir('/dir/dir2')
102
- @storage.write_file('/dir/file', false){|w| w.write 'something'}
103
-
104
- list = {}
105
- @storage.each_entry('/dir', nil){|path, type| list[path] = type}
106
- list.should == {'dir2' => :dir, 'file' => :file}
107
- end
108
-
109
- # it "upload_directory & download_directory" do
110
- # upload_path_check = "#{@remote_path}/dir2/file"
111
- # check_attributes upload_path_check, nil
112
- # @storage.upload_directory(@from_local, @remote_path)
113
- # check_attributes upload_path_check, file: true, dir: false
114
- #
115
- # download_path_check = "#{@to_local}/dir2/file"
116
- # File.exist?(download_path_check).should be_false
117
- # @storage.download_directory(@remote_path, @to_local)
118
- # File.exist?(download_path_check).should be_true
119
- # end
120
- end
121
-
122
- shared_examples_for 'vfs storage query' do
123
- it 'each with query' do
124
- @storage.create_dir '/dir'
125
- @storage.create_dir('/dir/dir_a')
126
- @storage.create_dir('/dir/dir_b')
127
- @storage.write_file('/dir/file_a', false){|w| w.write 'something'}
128
-
129
- list = {}
130
- @storage.each_entry('/dir', '*_a'){|path, type| list[path] = type}
131
- list.should == {'dir_a' => :dir, 'file_a' => :file}
132
- end
133
- end
134
-
135
- shared_examples_for 'vfs storage full attributes for dirs' do
136
- it "attributes for dirs" do
137
- @storage.create_dir('/dir')
138
- attrs = @storage.attributes('/dir')
139
- attrs[:file].should be_false
140
- attrs[:dir].should be_true
141
- attrs[:created_at].class.should == Time
142
- attrs[:updated_at].class.should == Time
143
- attrs.should_not include(:size)
144
- end
145
- end
146
-
147
- shared_examples_for 'vfs storage tmp dir' do
148
- it "tmp dir" do
149
- dir = @storage.tmp
150
- @storage.attributes(dir).should_not be_nil
151
- @storage.delete_dir dir
152
- @storage.attributes(dir).should be_nil
153
-
154
- dir = nil
155
- @storage.tmp do |tmp_dir|
156
- dir = tmp_dir
157
- @storage.attributes(dir).should_not be_nil
158
- end
159
- @storage.attributes(dir).should be_nil
160
- end
161
- end
data/lib/vfs/support.rb DELETED
@@ -1,2 +0,0 @@
1
- require 'fileutils'
2
- require 'set'