hdfs-viking 0.0.1

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.
@@ -0,0 +1,101 @@
1
+ module Viking
2
+ module FileUtils
3
+
4
+ def self.cd(dirname, &block)
5
+ if block
6
+ Viking::Dir.chdir(dirname, &block)
7
+ else
8
+ Viking::Dir.chdir(dirname)
9
+ end
10
+ end
11
+
12
+ def self.chdir(dirname, &block)
13
+ cd(dirname, &block)
14
+ end
15
+
16
+ def self.chmod(mode, list)
17
+ list = (list.is_a? Array) ? list : [list]
18
+ list.each do |path|
19
+ Viking::File.chmod(mode, path)
20
+ end
21
+ end
22
+
23
+ def self.chown(user, group, list)
24
+ list = (list.is_a? Array) ? list : [list]
25
+ list.each do |dir|
26
+ Viking::File.chown(user, group, dir)
27
+ end
28
+ end
29
+
30
+ def self.getwd
31
+ Viking::Dir.getwd
32
+ end
33
+
34
+ def self.makedirs(list)
35
+ list = (list.is_a? Array) ? list : [list]
36
+ list.each do |dir|
37
+ Viking::Dir.mkdir(dir)
38
+ end
39
+ end
40
+
41
+ def self.mkdir(list)
42
+ makedirs(list)
43
+ end
44
+
45
+ def self.move(src, dst)
46
+ Viking::File.move(src, dst)
47
+ end
48
+
49
+ def self.mv(src, dst)
50
+ move(src, dst)
51
+ end
52
+
53
+ def self.pwd
54
+ getwd
55
+ end
56
+
57
+ def self.remove(list)
58
+ list = (list.is_a? Array) ? list : [list]
59
+ list.each do |dir|
60
+ remove_file(dir)
61
+ end
62
+ end
63
+
64
+ def self.remove_dir(path)
65
+ Viking::Dir.delete(path)
66
+ end
67
+
68
+ def self.remove_entry(path)
69
+ rm_r(path)
70
+ end
71
+
72
+ def self.remove_file(path)
73
+ Viking::File.delete(path)
74
+ end
75
+
76
+ def self.rm(list)
77
+ remove(list)
78
+ end
79
+
80
+ def self.rm_r(list)
81
+ list = (list.is_a? Array) ? list : [list]
82
+ list.each do |path|
83
+ Viking.client.delete(Path.new(path), true)
84
+ end
85
+ end
86
+
87
+ def self.rmdir(list)
88
+ list = (list.is_a? Array) ? list : [list]
89
+ list.each do |dir|
90
+ remove_dir(dir)
91
+ end
92
+ end
93
+
94
+ def self.touch(list)
95
+ list = (list.is_a? Array) ? list : [list]
96
+ list.each do |path|
97
+ Viking.client.create_new_file(Path.new(path))
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module Viking
2
+ VERSION = "0.0.1"
3
+ end
data/lib/viking.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "lock_jar"
2
+
3
+ LockJar.load
4
+
5
+ java_import java.net.URI
6
+ java_import org.apache.hadoop.fs.FileSystem
7
+ java_import org.apache.hadoop.hdfs.DistributedFileSystem
8
+ java_import org.apache.hadoop.conf.Configuration
9
+ java_import org.apache.hadoop.fs.Path
10
+ java_import org.apache.hadoop.fs.permission.FsPermission
11
+
12
+ module Viking
13
+
14
+ def self.configure(config)
15
+ hostname = config[:host]
16
+ port = config[:port]
17
+
18
+ path = URI.new("hdfs://#{hostname}:#{port}")
19
+
20
+ @client = DistributedFileSystem.new
21
+ @client.initialize__method(path, Configuration.new)
22
+ end
23
+
24
+ def self.client
25
+ @client ||= FileSystem.get_local(Configuration.new)
26
+ end
27
+ end
28
+
29
+ require "viking/version"
30
+ require 'viking/file'
31
+ require 'viking/dir'
32
+ require 'viking/fileutilz'
@@ -0,0 +1,114 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/viking.rb', __FILE__)
4
+
5
+ class TestDirClassMethods < MiniTest::Unit::TestCase
6
+
7
+ TMP_DIR = "/tmp/viking"
8
+ TEST_FILE_1 = "test_file_1.rb"
9
+ TEST_FILE_2 = "test_file_2.rb"
10
+ TEST_FILE_PATH_1 = "#{TMP_DIR}/#{TEST_FILE_1}"
11
+ TEST_FILE_PATH_2 = "#{TMP_DIR}/#{TEST_FILE_2}"
12
+
13
+ def setup
14
+ FileUtils.mkpath(TMP_DIR) unless File.exists?(TMP_DIR)
15
+ FileUtils.touch(TEST_FILE_PATH_1)
16
+ FileUtils.touch(TEST_FILE_PATH_2)
17
+ File.write(TEST_FILE_PATH_2, "test")
18
+ end
19
+
20
+ def teardown
21
+ FileUtils.remove_dir(TMP_DIR) if File.exists?(TMP_DIR)
22
+ end
23
+
24
+ def test_chdir
25
+ assert 0 == Viking::Dir.chdir(TMP_DIR)
26
+ assert Viking::Dir.pwd == TMP_DIR
27
+
28
+ Viking::Dir.chdir("/tmp") do |path|
29
+ assert "/tmp" == path
30
+ assert "/tmp" == Viking::Dir.pwd
31
+ end
32
+
33
+ assert Viking::Dir.pwd == TMP_DIR
34
+
35
+ assert 0 == Viking::Dir.chdir(Dir.pwd)
36
+ end
37
+
38
+ def test_delete
39
+ FileUtils.mkpath("#{TMP_DIR}/empty")
40
+
41
+ assert_raises(Java::JavaIo::IOException) { Viking::Dir.delete(TMP_DIR) }
42
+ assert_raises(Errno::ENOENT) { Viking::Dir.delete("#{TMP_DIR}/does-not-exists") }
43
+
44
+ assert Dir.exist?("#{TMP_DIR}/empty")
45
+ assert 0 == Viking::Dir.delete("#{TMP_DIR}/empty")
46
+ assert !Dir.exist?("#{TMP_DIR}/empty")
47
+ end
48
+
49
+ def test_entries
50
+ assert_raises(Errno::ENOENT) { Viking::Dir.delete("#{TMP_DIR}/does-not-exists") }
51
+ assert_raises(Errno::ENOENT) { Viking::Dir.delete(TEST_FILE_PATH_1) }
52
+
53
+ assert [TEST_FILE_1, TEST_FILE_2] == Viking::Dir.entries(TMP_DIR)
54
+ end
55
+
56
+ def test_exist?
57
+ assert Viking::Dir.exist?(TMP_DIR)
58
+ assert !Viking::Dir.exist?(TEST_FILE_PATH_1)
59
+ assert !Viking::Dir.exist?("#{TMP_DIR}/does-not-exists")
60
+ end
61
+
62
+ def test_exists?
63
+ assert Viking::Dir.exists?(TMP_DIR)
64
+ assert !Viking::Dir.exists?(TEST_FILE_PATH_1)
65
+ assert !Viking::Dir.exists?("#{TMP_DIR}/does-not-exists")
66
+ end
67
+
68
+ def test_foreach
69
+ file_names = []
70
+ Viking::Dir.foreach(TMP_DIR) do |file_name|
71
+ file_names << file_name
72
+ end
73
+ assert [TEST_FILE_1, TEST_FILE_2] == file_names
74
+ end
75
+
76
+ def test_getwd
77
+ assert Dir.pwd == Viking::Dir.getwd
78
+ end
79
+
80
+ def test_home
81
+ assert Dir.home == Viking::Dir.home
82
+ end
83
+
84
+ def test_mkdir
85
+ assert !Dir.exist?("#{TMP_DIR}/new/folder/struct")
86
+ Viking::Dir.mkdir("#{TMP_DIR}/new/folder/struct")
87
+ assert Dir.exist?("#{TMP_DIR}/new/folder/struct")
88
+ end
89
+
90
+ def test_open
91
+ assert Viking::Dir.open(TMP_DIR).instance_of? Viking::Dir
92
+ assert TMP_DIR == Viking::Dir.open(TMP_DIR).path
93
+
94
+ Viking::Dir.open(TMP_DIR) do |dir|
95
+ assert dir.instance_of? Viking::Dir
96
+ assert TMP_DIR == dir.path
97
+ end
98
+ end
99
+
100
+ def test_pwd
101
+ assert Dir.pwd == Viking::Dir.pwd
102
+ end
103
+
104
+ def test_rmdir
105
+ FileUtils.mkpath("#{TMP_DIR}/empty")
106
+
107
+ assert_raises(Java::JavaIo::IOException) { Viking::Dir.delete(TMP_DIR) }
108
+ assert_raises(Errno::ENOENT) { Viking::Dir.delete("#{TMP_DIR}/does-not-exists") }
109
+
110
+ assert Dir.exist?("#{TMP_DIR}/empty")
111
+ assert 0 == Viking::Dir.delete("#{TMP_DIR}/empty")
112
+ assert !Dir.exist?("#{TMP_DIR}/empty")
113
+ end
114
+ end
@@ -0,0 +1,72 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/viking.rb', __FILE__)
4
+
5
+ class TestDirInstanceMethods < MiniTest::Unit::TestCase
6
+
7
+ TMP_DIR = "/tmp/viking"
8
+ TEST_FILE_1 = "test_file_1.rb"
9
+ TEST_FILE_2 = "test_file_2.rb"
10
+ TEST_FILE_PATH_1 = "#{TMP_DIR}/#{TEST_FILE_1}"
11
+ TEST_FILE_PATH_2 = "#{TMP_DIR}/#{TEST_FILE_2}"
12
+
13
+ def setup
14
+ FileUtils.mkpath(TMP_DIR) unless File.exists?(TMP_DIR)
15
+ FileUtils.touch(TEST_FILE_PATH_1)
16
+ FileUtils.touch(TEST_FILE_PATH_2)
17
+ File.write(TEST_FILE_PATH_2, "test")
18
+ end
19
+
20
+ def teardown
21
+ FileUtils.remove_dir(TMP_DIR) if File.exists?(TMP_DIR)
22
+ end
23
+
24
+ def test_close
25
+ d = Viking::Dir.new(TMP_DIR)
26
+ d.read
27
+ d.close
28
+ assert_raises(IOError) { d.read }
29
+ assert_raises(IOError) { d.close }
30
+ end
31
+
32
+ def test_each
33
+ d = Viking::Dir.new(TMP_DIR)
34
+
35
+ entries = []
36
+ d.each do |entry|
37
+ entries << entry
38
+ end
39
+
40
+ assert [TEST_FILE_1, TEST_FILE_2] == entries
41
+ end
42
+
43
+ def test_path
44
+ d = Viking::Dir.new(TMP_DIR)
45
+ assert TMP_DIR == d.path
46
+ end
47
+
48
+ def test_read
49
+ d = Viking::Dir.new(TMP_DIR)
50
+
51
+ assert TEST_FILE_1 == d.read
52
+ assert TEST_FILE_2 == d.read
53
+ assert d.read.nil?
54
+ assert d.read.nil?
55
+ end
56
+
57
+ def test_rewind
58
+ d = Viking::Dir.new(TMP_DIR)
59
+
60
+ assert TEST_FILE_1 == d.read
61
+ assert TEST_FILE_2 == d.read
62
+ assert d.read.nil?
63
+ assert d.read.nil?
64
+
65
+ d.rewind
66
+
67
+ assert TEST_FILE_1 == d.read
68
+ assert TEST_FILE_2 == d.read
69
+ assert d.read.nil?
70
+ assert d.read.nil?
71
+ end
72
+ end
@@ -0,0 +1,115 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/viking.rb', __FILE__)
4
+
5
+ class TestFileClassMethods < MiniTest::Unit::TestCase
6
+
7
+ TMP_DIR = "/tmp/viking"
8
+ TEST_FILE_1 = "test_file_1.rb"
9
+ TEST_FILE_2 = "test_file_2.rb"
10
+ TEST_FILE_PATH_1 = "#{TMP_DIR}/#{TEST_FILE_1}"
11
+ TEST_FILE_PATH_2 = "#{TMP_DIR}/#{TEST_FILE_2}"
12
+
13
+ def setup
14
+ FileUtils.mkpath(TMP_DIR) unless File.exists?(TMP_DIR)
15
+ FileUtils.touch(TEST_FILE_PATH_1)
16
+ FileUtils.touch(TEST_FILE_PATH_2)
17
+ File.write(TEST_FILE_PATH_2, "test")
18
+ end
19
+
20
+ def teardown
21
+ FileUtils.remove_dir(TMP_DIR) if File.exists?(TMP_DIR)
22
+ end
23
+
24
+ def test_absolute_path
25
+ same_level = "foo.rb"
26
+ up_level = "../../foo.rb"
27
+ up_down_level = "../bar/biz/foo.rb"
28
+
29
+ assert File.absolute_path(same_level) == Viking::File.absolute_path(same_level)
30
+ assert File.absolute_path(up_level) == Viking::File.absolute_path(up_level)
31
+ assert File.absolute_path(up_down_level) == Viking::File.absolute_path(up_down_level)
32
+ end
33
+
34
+ def test_basename
35
+ assert TEST_FILE_1 == Viking::File.basename(TEST_FILE_PATH_1)
36
+ assert TEST_FILE_2.sub(".rb", "") == Viking::File.basename(TEST_FILE_PATH_2, ".rb")
37
+ end
38
+
39
+ def test_chmod
40
+ assert 1 == Viking::File.chmod(777, TEST_FILE_PATH_1)
41
+ assert 33545 == File.stat(TEST_FILE_PATH_1).mode
42
+ end
43
+
44
+ def test_chown
45
+ assert 1 == Viking::File.chown(ENV["USER"], nil, TEST_FILE_PATH_1)
46
+ end
47
+
48
+ def test_delete
49
+ assert 2 == Viking::File.delete(TEST_FILE_PATH_1, TEST_FILE_PATH_2)
50
+ assert_raises(IOError) { Viking::File.delete(TMP_DIR) }
51
+ end
52
+
53
+ def test_directory?
54
+ assert Viking::File.directory?(TMP_DIR)
55
+ assert !Viking::File.directory?(TEST_FILE_PATH_1)
56
+ end
57
+
58
+ def test_exist?
59
+ assert Viking::File.exists?(TEST_FILE_PATH_1)
60
+ assert Viking::File.exists?(TMP_DIR)
61
+ assert !Viking::File.exists?("#{TEST_FILE_PATH_1}foobar")
62
+ end
63
+
64
+ def test_exists?
65
+ assert Viking::File.exists?(TEST_FILE_PATH_1)
66
+ assert Viking::File.exists?(TMP_DIR)
67
+ assert !Viking::File.exists?("#{TEST_FILE_PATH_1}foobar")
68
+ end
69
+
70
+ def test_file?
71
+ assert !Viking::File.file?(TMP_DIR)
72
+ assert Viking::File.file?(TEST_FILE_PATH_1)
73
+ end
74
+
75
+ def test_ftype
76
+ assert 'directory' == Viking::File.ftype(TMP_DIR)
77
+ assert 'file' == Viking::File.ftype(TEST_FILE_PATH_1)
78
+ end
79
+
80
+ def test_move
81
+ Viking::File.move(TEST_FILE_PATH_1, "#{TEST_FILE_PATH_1}.new")
82
+
83
+ assert File.exists?("#{TEST_FILE_PATH_1}.new")
84
+ assert !File.exists?(TEST_FILE_PATH_1)
85
+ end
86
+
87
+ def test_open
88
+ f = Viking::File.open(TEST_FILE_PATH_1) do |file|
89
+ file
90
+ end
91
+
92
+ assert f.instance_of? Viking::File
93
+ end
94
+
95
+ def test_rename
96
+ Viking::File.rename(TEST_FILE_PATH_1, "#{TEST_FILE_PATH_1}.new")
97
+
98
+ assert File.exists?("#{TEST_FILE_PATH_1}.new")
99
+ assert !File.exists?(TEST_FILE_PATH_1)
100
+ end
101
+
102
+ def test_size
103
+ assert 4 == Viking::File.size(TEST_FILE_PATH_2)
104
+ assert_raises(Java::JavaIo::FileNotFoundException) { Viking::File.size("#{TMP_DIR}/does_not_exists") }
105
+ end
106
+
107
+ def test_size?
108
+ assert 4 == Viking::File.size?(TEST_FILE_PATH_2)
109
+ assert Viking::File.size?("#{TMP_DIR}/does_not_exists").nil?
110
+ end
111
+
112
+ def test_split
113
+ assert [TMP_DIR, TEST_FILE_1] == Viking::File.split(TEST_FILE_PATH_1)
114
+ end
115
+ end
@@ -0,0 +1,142 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/viking.rb', __FILE__)
4
+
5
+ class TestFileInstanceMethods < MiniTest::Unit::TestCase
6
+
7
+ TMP_DIR = "/tmp/viking"
8
+ TEST_FILE_1 = "test_file_1.rb"
9
+ TEST_FILE_2 = "test_file_2.rb"
10
+ TEST_FILE_PATH_1 = "#{TMP_DIR}/#{TEST_FILE_1}"
11
+ TEST_FILE_PATH_2 = "#{TMP_DIR}/#{TEST_FILE_2}"
12
+
13
+ CONTENT = "line1\nline2\nline3\n"
14
+
15
+ def setup
16
+ FileUtils.mkpath(TMP_DIR) unless File.exists?(TMP_DIR)
17
+ FileUtils.touch(TEST_FILE_PATH_2)
18
+ File.write(TEST_FILE_PATH_2, CONTENT)
19
+ end
20
+
21
+ def teardown
22
+ FileUtils.remove_dir(TMP_DIR) if File.exists?(TMP_DIR)
23
+ end
24
+
25
+ def file1
26
+ Viking::File.new(TEST_FILE_PATH_1)
27
+ end
28
+
29
+ def file2
30
+ Viking::File.new(TEST_FILE_PATH_2)
31
+ end
32
+
33
+ def test_path
34
+ assert TEST_FILE_PATH_1 == file1.path
35
+ end
36
+
37
+ def test_size
38
+ assert CONTENT.bytes.to_a.size == file2.size
39
+ end
40
+
41
+ def test_close
42
+ f = file2
43
+ assert !f.closed?
44
+
45
+ f.close
46
+ assert f.closed?
47
+ end
48
+
49
+ def test_each_line
50
+ lines = []
51
+ file2.each_line do |line|
52
+ lines << line
53
+ end
54
+ assert CONTENT == lines.join("")
55
+ end
56
+
57
+ def test_each_char
58
+ chars = []
59
+ file2.each_char do |char|
60
+ chars << char
61
+ end
62
+ assert chars == CONTENT.bytes.to_a
63
+ end
64
+
65
+ def test_eof
66
+ f = file2
67
+ assert !f.eof
68
+
69
+ f.getc
70
+ assert !f.eof
71
+
72
+ f.read
73
+ assert f.eof
74
+
75
+ f.close
76
+ end
77
+
78
+ def test_eof?
79
+ f = file2
80
+ assert !f.eof?
81
+
82
+ (CONTENT.bytes.to_a.size - 1).times { f.getc }
83
+ assert !f.eof
84
+
85
+ f.getc
86
+ assert f.eof
87
+
88
+ f.close
89
+ end
90
+
91
+ def test_getc
92
+ f = file2
93
+ assert CONTENT.bytes.first == f.getc
94
+ f.close
95
+ end
96
+
97
+ def test_gets
98
+ f = file2
99
+ assert "#{CONTENT.split("\n").first}\n" == file2.gets("\n", nil)
100
+ f.close
101
+
102
+ f = file2
103
+ assert CONTENT[0,10] == f.gets(nil, 10)
104
+ f.close
105
+
106
+ f = file2
107
+ assert "#{CONTENT.split("\n").first}\n" == f.gets("\n", 10)
108
+ f.close
109
+ end
110
+
111
+ def test_putc
112
+ f = file1
113
+ f.putc("a")
114
+ f.close
115
+ assert File.read(TEST_FILE_PATH_1) == "a"
116
+
117
+ File.delete(TEST_FILE_PATH_1)
118
+
119
+ f = file1
120
+ f.putc("a".bytes.first)
121
+ f.close
122
+ assert File.read(TEST_FILE_PATH_1) == "a"
123
+ end
124
+
125
+ def test_puts
126
+ f = file1
127
+ f.puts("foo", "bar\n", "biz")
128
+ f.close
129
+ assert File.read(TEST_FILE_PATH_1) == "foo\nbar\nbiz\n"
130
+ end
131
+
132
+ def test_read
133
+ assert CONTENT == file2.read
134
+ end
135
+
136
+ def test_write
137
+ f = file1
138
+ f.write("foobar")
139
+ f.close
140
+ assert File.read(TEST_FILE_PATH_1) == "foobar"
141
+ end
142
+ end