rbbt-util 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rbbt-util.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'rbbt'
2
+ require 'rbbt/util/open'
3
+ require 'rbbt/util/cmd'
4
+ require 'rbbt/util/tmpfile'
5
+ require 'rbbt/util/filecache'
6
+ require 'rbbt/util/tsv'
7
+ require 'rbbt/util/cachehelper'
8
+ require 'rbbt/util/misc'
9
+
10
+ FileCache.cachedir = Rbbt.cachedir
11
+ Open.cachedir = File.join(Rbbt.cachedir, 'open-remote/')
12
+ TmpFile.tmpdir = File.join(Rbbt.tmpdir)
13
+ TSV.cachedir = File.join(Rbbt.cachedir, 'tsv_cache')
data/lib/rbbt.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'rbbt/util/pkg_config'
2
+ require 'rbbt/util/pkg_data'
3
+ require 'rbbt/util/pkg_software'
4
+ require 'rbbt/util/open'
5
+ require 'rbbt/util/tmpfile'
6
+ require 'rbbt/util/filecache'
7
+
8
+ module Rbbt
9
+ extend PKGConfig
10
+ extend PKGData
11
+ extend PKGSoftware
12
+
13
+ self.load_cfg(%w(tmpdir cachedir datadir))
14
+ end
15
+
@@ -0,0 +1,257 @@
1
+ #vim: ft=sh
2
+
3
+ SOFTWARE_DIR="$RBBT_SOFTWARE_DIR"
4
+ OPT_DIR="$SOFTWARE_DIR/opt"; [ -d $OPT_SRC_DIR ] || mkdir -p $OPT_SRC_DIR
5
+ OPT_BIN_DIR="$OPT_DIR/bin"; [ -d $OPT_BIN_DIR ] || mkdir -p $OPT_BIN_DIR
6
+ OPT_SRC_DIR="$SOFTWARE_DIR/src"; [ -d $OPT_SRC_DIR ] || mkdir -p $OPT_SRC_DIR
7
+ OPT_SCM_DIR="$SOFTWARE_DIR/scm"; [ -d $OPT_SCM_DIR ] || mkdir -p $OPT_SCM_DIR
8
+ OPT_JAR_DIR="$OPT_DIR/jars"; [ -d $OPT_JAR_DIR ] || mkdir -p $OPT_JAR_DIR
9
+ OPT_BUILD_DIR="$SOFTWARE_DIR/.build"; [ -d $OPT_BUILD_DIR ] || mkdir -p $OPT_BUILD_DIR
10
+
11
+ #source "$HOME/config/bash/_utility_functions"
12
+
13
+ function expand_path(){
14
+ name=$(basename $1)
15
+ dir=$(dirname $1)
16
+
17
+ echo "$(cd $dir && pwd)/$name"
18
+ }
19
+
20
+
21
+ #{{{ GET PKG OPT_BUILD_DIR
22
+
23
+ # From SRC
24
+ get_pkg(){
25
+ local name="$1"
26
+ local url="$2"
27
+
28
+ if [ ! -f "$OPT_SRC_DIR/$name.pkg" ]; then
29
+ wget "$url" -O "$OPT_SRC_DIR/$name.pkg"
30
+ fi
31
+ }
32
+
33
+ uncompress_pkg(){
34
+ local name="$1"
35
+ local pkg="$OPT_SRC_DIR/$name.pkg"
36
+
37
+ local old_pwd="`expand_path $(pwd)`"
38
+ clean_build
39
+
40
+ cd "$OPT_BUILD_DIR"
41
+
42
+ (tar xvfz $pkg || tar xvfj $pkg || unzip $pkg || echo "Error decompressing") 2> /dev/null
43
+
44
+ cd "$old_pwd"
45
+ }
46
+
47
+ get_src(){
48
+ local name="$1"
49
+ local url="$2"
50
+
51
+ get_pkg "$name" "$url"
52
+ uncompress_pkg "$name"
53
+ }
54
+
55
+ # From SNV
56
+ get_svn(){
57
+ local name="$1"
58
+ local url="$2"
59
+
60
+ local old_pwd="`expand_path $(pwd)`"
61
+
62
+ cd $OPT_SCM_DIR
63
+ if [ -d "$name" ]; then
64
+ cd "$name"
65
+ svn update
66
+ else
67
+ git checkout "$url" "$name"
68
+ fi
69
+
70
+ clean_build
71
+
72
+ cd $OPT_BUILD_DIR
73
+ ln -s "$OPT_SCM_DIR/$name" "$name"
74
+
75
+ cd "$old_pwd"
76
+
77
+ }
78
+
79
+ # From GIT
80
+ get_git(){
81
+ local name="$1"
82
+ local url="$2"
83
+
84
+ local old_pwd="`expand_path $(pwd)`"
85
+
86
+ cd $OPT_SCM_DIR
87
+ if [ -d "$name" ]; then
88
+ cd "$name"
89
+ git stash
90
+ git pull origin master
91
+ git rebase master
92
+ git stash apply
93
+ else
94
+ git clone "$url" "$name"
95
+ fi
96
+
97
+ clean_build
98
+
99
+ cd $OPT_BUILD_DIR
100
+ ln -s "$OPT_SCM_DIR/$name" "$name"
101
+
102
+ cd "$old_pwd"
103
+ }
104
+
105
+
106
+ #{{{ BUILD PKG in OPT_BUILD_DIR
107
+
108
+ clean_build(){
109
+ rm -Rf $OPT_BUILD_DIR/*
110
+ }
111
+
112
+ build_dir(){
113
+ echo $OPT_BUILD_DIR/`ls $OPT_BUILD_DIR |head -n 1`
114
+ }
115
+
116
+ compile(){
117
+ local name=$1; shift
118
+ local extra="$@"
119
+ [ -f Makefile ] && make && make install
120
+ [ -f build.xml ] && ant
121
+ [ -f setup.rb ] && ruby -Ilib/ setup.rb --prefix="$(opt_dir "$name")" $extra
122
+ [ -f setup.py ] && python setup.py build && python setup.py install --user && clean_build
123
+ }
124
+
125
+ build(){
126
+ local name=$1; shift
127
+ local extra="$@"
128
+
129
+ echo "Building $name"
130
+
131
+ local old_pwd="`expand_path $(pwd)`"
132
+ cd "`build_dir`"
133
+ echo `pwd`
134
+ ls
135
+
136
+ if [ -f aclocal.m4 ]; then
137
+ autoconf
138
+ fi
139
+
140
+ if [ -f autogen.sh ]; then
141
+ ./autogen.sh
142
+ fi
143
+
144
+ if [ -f aclocal.m4 ]; then
145
+ autoconf
146
+ fi
147
+
148
+ if [ -f configure ]; then
149
+ ./configure --prefix="$(opt_dir "$name")" $extra
150
+ compile "$name" $extra
151
+ setup "$name"
152
+ clean_build
153
+ else
154
+ compile "$name" $extra
155
+ move_opt "$name"
156
+ setup "$name"
157
+ clean_build
158
+ fi
159
+
160
+ cd "$old_pwd"
161
+ }
162
+
163
+ #{{{ SETUP
164
+
165
+ versioned?(){
166
+ local name="$1"
167
+ echo $name | grep ':' &>/dev/null
168
+ }
169
+
170
+ opt_dir(){
171
+ local name="$1"
172
+ if versioned? "$name"; then
173
+ local real_name=`echo $1 | sed 's/:.*//'`
174
+ local version=`echo $1 | sed 's/.*://'`
175
+ echo "$OPT_DIR/$real_name/$version"
176
+ else
177
+ echo "$OPT_DIR/$name"
178
+ fi
179
+ }
180
+
181
+ move_opt(){
182
+ local name="$1"
183
+ local pkg_dir="`opt_dir \"$name\"`"
184
+
185
+ mkdir -p $(dirname "$pkg_dir")
186
+ mv "`build_dir`" "$pkg_dir"
187
+ }
188
+
189
+ setup(){
190
+ local name="$1"
191
+ local pkg_dir="`opt_dir \"$name\"`"
192
+
193
+ if versioned? "$name"; then
194
+ rm -f "$(dirname $pkg_dir)/current"
195
+ ln -sf "$pkg_dir" "$(dirname $pkg_dir)/current"
196
+ pkg_dir="$(dirname $pkg_dir)/current"
197
+ fi
198
+
199
+ if [ -d "$pkg_dir/bin/" ]; then
200
+ local old_pwd="`expand_path $(pwd)`"
201
+ cd "$OPT_DIR/bin"
202
+
203
+ for exe in `ls "$pkg_dir/bin/"`;do
204
+ rm -f ./$exe
205
+ ln -s "$pkg_dir/bin/$exe" . 2>/dev/null
206
+ done
207
+
208
+ cd "$old_pwd"
209
+ fi
210
+
211
+ ([ -d "$pkg_dir/lib" ] && add2file "$OPT_DIR/.ld-paths" "$pkg_dir/lib")
212
+ ([ -d "$pkg_dir/lib/pkgconfig" ] && add2file "$OPT_DIR/.pkgconfig-paths" "$pkg_dir/lib/pkgconfig")
213
+ }
214
+
215
+ #{{{ HELPERS
216
+
217
+ install_src(){
218
+ local name="$1"
219
+ local url="$2"
220
+ shift;shift;
221
+ local extra="$@"
222
+
223
+ get_pkg "$name" "$url"
224
+ uncompress_pkg "$name"
225
+ build "$name" "$extra"
226
+ }
227
+
228
+ install_git(){
229
+ local name="$1"
230
+ local url="$2"
231
+ shift;shift;
232
+ local extra="$@"
233
+
234
+ get_git "$name" "$url"
235
+ build "$name" $extra
236
+ }
237
+
238
+ install_jar(){
239
+ local name="$1"
240
+ local url="$2"
241
+
242
+ [ -d "$OPT_DIR/$name/" ] || mkdir -p "$OPT_DIR/$name/"
243
+ wget "$url" -O "$OPT_DIR/$name/$name.jar"
244
+ ln -s "$OPT_DIR/$name/$name.jar" "$OPT_JAR_DIR/$name.jar"
245
+ }
246
+
247
+ jar2bin(){
248
+ local name="$1";shift
249
+ local extra="$@"
250
+
251
+ echo "#!/bin/bash" > $OPT_DIR/bin/$name
252
+ echo "java $extra -jar '$OPT_JAR_DIR/$name.jar'" >> $OPT_DIR/bin/$name
253
+
254
+ chmod +x $OPT_DIR/bin/$name
255
+ }
256
+
257
+ # vim: ft=sh
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/cmd'
3
+ require 'test/unit'
4
+
5
+ class TestCmd < Test::Unit::TestCase
6
+
7
+ def test_cmd_option_string
8
+ assert_equal("--user-agent firefox", CMD.process_cmd_options("--user-agent" => "firefox"))
9
+ assert_equal("--user-agent=firefox", CMD.process_cmd_options("--user-agent=" => "firefox"))
10
+ assert_equal("-q", CMD.process_cmd_options("-q" => true))
11
+ assert_equal("", CMD.process_cmd_options("-q" => nil))
12
+ assert_equal("", CMD.process_cmd_options("-q" => false))
13
+
14
+ assert(CMD.process_cmd_options("--user-agent" => "firefox", "-q" => true) =~ /--user-agent firefox/)
15
+ assert(CMD.process_cmd_options("--user-agent" => "firefox", "-q" => true) =~ /-q/)
16
+ end
17
+
18
+ def test_cmd
19
+ assert_equal("test\n", CMD.cmd("echo '{opt}' test").read)
20
+ assert_equal("test", CMD.cmd("echo '{opt}' test", "-n" => true).read)
21
+ assert_equal("test2\n", CMD.cmd("cut", "-f" => 2, "-d" => '" "', :in => "test1 test2").read)
22
+ end
23
+
24
+ def test_pipe
25
+ assert_equal("test\n", CMD.cmd("echo '{opt}' test", :pipe => true).read)
26
+ assert_equal("test", CMD.cmd("echo '{opt}' test", "-n" => true, :pipe => true).read)
27
+ assert_equal("test2\n", CMD.cmd("cut", "-f" => 2, "-d" => '" "', :in => "test1 test2", :pipe => true).read)
28
+ end
29
+
30
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt'
3
+ require 'rbbt/util/data_module'
4
+ require 'test/unit'
5
+ require 'fileutils'
6
+
7
+ module DataTest
8
+ extend DataModule
9
+
10
+ def self.salute(name)
11
+ "Hello #{name}"
12
+ end
13
+
14
+ World = with_key("world")
15
+ end
16
+
17
+ class TestDataModule < Test::Unit::TestCase
18
+
19
+ SHAREDIR = File.join(PKGData.sharedir_for_file(__FILE__), 'install', 'DataTest')
20
+
21
+ def setup
22
+ FileUtils.mkdir_p SHAREDIR
23
+ File.open(File.join(SHAREDIR, 'Rakefile'), 'w') do |f|
24
+ f.puts "task :file1 do |t| File.open(t.name, 'w') do |f| f.write 'File 1' end end"
25
+ end
26
+ end
27
+
28
+ def test_module
29
+ assert_equal "File 1", Open.read(DataTest.file1).chomp
30
+ assert_equal "Hello world", DataTest.salute("world")
31
+ assert_equal "Hello world", DataTest.salute("world")
32
+ assert_equal "Hello world", DataTest::with_key("world").salute
33
+ assert_equal "Hello world", DataTest::World.salute
34
+ FileUtils.rm_rf File.join(Rbbt.datadir, 'DataTest')
35
+ end
36
+
37
+ def test_method_missing
38
+ assert_raise NoMethodError do DataTest.missing end
39
+ end
40
+
41
+ def teardown
42
+ FileUtils.rm_rf SHAREDIR
43
+ end
44
+ end
45
+
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/excel2tsv'
3
+
4
+ class TestTSV < Test::Unit::TestCase
5
+ def test_tsv2excel
6
+ tsv = TSV.excel2tsv(test_datafile('Test.xls'), :header => true)
7
+ assert_equal 'Id', tsv.key_field
8
+ end
9
+ end
10
+
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/filecache'
3
+ require 'rbbt-util'
4
+ require 'test/unit'
5
+
6
+ class TestFileCache < Test::Unit::TestCase
7
+
8
+ def test_path
9
+ assert_equal(File.join(Rbbt.cachedir,'3','2','1','123.ext'), FileCache.path('123.ext'))
10
+ end
11
+
12
+ def test_add_read
13
+ filename = '123.ext'
14
+ content = 'test'
15
+
16
+ FileCache.add(filename, content)
17
+ assert_equal(content, File.open(FileCache.path(filename)).read)
18
+ assert_equal(content, FileCache.get(filename).read)
19
+
20
+ FileCache.del(filename)
21
+ end
22
+
23
+ def test_add_io
24
+ filename = '123.ext'
25
+ content =<<-EOF
26
+ test test test
27
+ test test test
28
+ EOF
29
+
30
+ FileCache.add(filename, StringIO.new(content))
31
+ assert_equal(content, File.open(FileCache.path(filename)).read)
32
+
33
+ FileCache.del(filename)
34
+ end
35
+
36
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/misc'
3
+ require 'test/unit'
4
+
5
+ class TestMisc < Test::Unit::TestCase
6
+
7
+ def test_string2hash
8
+ assert(Misc.string2hash("--user-agent=firefox").include? "--user-agent")
9
+ assert(Misc.string2hash(":true")[:true] == true)
10
+ assert(Misc.string2hash("true")["true"] == true)
11
+ assert(Misc.string2hash("a=1")["a"] == 1)
12
+ assert(Misc.string2hash("a=b")["a"] == 'b')
13
+ assert(Misc.string2hash("a=b#c=d#:h=j")["c"] == 'd')
14
+ assert(Misc.string2hash("a=b#c=d#:h=j")[:h] == 'j')
15
+ end
16
+
17
+ def test_named_array
18
+ a = NamedArray.name([1,2,3,4], %w(a b c d))
19
+ assert_equal(1, a['a'])
20
+ end
21
+
22
+ end
@@ -0,0 +1,89 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/open'
3
+ require 'rbbt/util/tmpfile'
4
+ require 'test/unit'
5
+ require 'iconv'
6
+
7
+ class TestOpen < Test::Unit::TestCase
8
+
9
+ def test_wget
10
+ assert(Misc.fixutf8(Open.wget('http://google.com', :quiet => true).read) =~ /html/)
11
+ end
12
+
13
+ def test_nice
14
+ nice = 0.5
15
+
16
+ Open.wget('http://google.com', :quiet => true, :nice => nice).read =~ /html/
17
+ t = Time.now
18
+ Open.wget('http://google.com', :quiet => true, :nice => nice).read =~ /html/
19
+ assert(Time.now - t + 0.5 >= nice)
20
+
21
+ Open.wget('http://google.com', :quiet => true, :nice => nice, :nice_key => 1).read =~ /html/
22
+ Open.wget('http://google.com', :quiet => true, :nice => nice, :nice_key => 2).read =~ /html/
23
+ t = Time.now
24
+ Open.wget('http://google.com', :quiet => true, :nice => nice, :nice_key => 1).read =~ /html/
25
+ assert(Time.now - t + 0.5 >= nice)
26
+ end
27
+
28
+ def test_remote?
29
+ assert(Open.remote?('http://google.com'))
30
+ assert(! Open.remote?('~/.bashrc'))
31
+ end
32
+
33
+ def test_open
34
+ assert(Open.read('http://google.com', :quiet => true) =~ /html/)
35
+ end
36
+
37
+ def test_read
38
+ content =<<-EOF
39
+ 1
40
+ 2
41
+ 3
42
+ 4
43
+ EOF
44
+ TmpFile.with_file(content) do |file|
45
+ sum = 0
46
+ Open.read file do |line| sum += line.to_i end
47
+ assert_equal(1 + 2 + 3 + 4, sum)
48
+ assert_equal(content, Open.read(file))
49
+ end
50
+ end
51
+
52
+ def test_read_grep
53
+ content =<<-EOF
54
+ 1
55
+ 2
56
+ 3
57
+ 4
58
+ EOF
59
+ TmpFile.with_file(content) do |file|
60
+ sum = 0
61
+ Open.read(file, :grep => '^1\|3') do |line| sum += line.to_i end
62
+ assert_equal(1 + 3, sum)
63
+ end
64
+
65
+ TmpFile.with_file(content) do |file|
66
+ sum = 0
67
+ Open.read(file, :grep => ["1","3"]) do |line| sum += line.to_i end
68
+ assert_equal(1 + 3, sum)
69
+ end
70
+
71
+ end
72
+
73
+ def test_gzip
74
+ content =<<-EOF
75
+ 1
76
+ 2
77
+ 3
78
+ 4
79
+ EOF
80
+ TmpFile.with_file(content) do |file|
81
+ `gzip #{file}`
82
+ assert_equal(content, Open.read(file + '.gz'))
83
+ FileUtils.rm file + '.gz'
84
+ end
85
+ end
86
+
87
+
88
+ end
89
+
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/simpleDSL'
3
+ require 'test/unit'
4
+
5
+ class TestClass
6
+ include SimpleDSL
7
+
8
+ def action(name, *args, &block)
9
+ @actions ||= {}
10
+ @actions[name] = args.first
11
+ end
12
+
13
+ def actions
14
+ @actions
15
+ end
16
+ end
17
+
18
+
19
+ class TestDSL < Test::Unit::TestCase
20
+ def setup
21
+ @parser = TestClass.new
22
+ @parser.load_config(:action) do
23
+ action1 "Hello"
24
+ action2 "Good bye"
25
+ end
26
+ end
27
+
28
+ def test_actions
29
+ assert_equal({:action1=>"Hello", :action2=>"Good bye"}, @parser.actions)
30
+ end
31
+
32
+ def test_method_missing
33
+ assert_raise(NoMethodError){@parser.cues}
34
+ end
35
+
36
+ def test_config
37
+ config = <<-EOC
38
+ action1("Hello")
39
+ action2("Good bye")
40
+ EOC
41
+
42
+ begin
43
+ assert(@parser.config(:action) == config)
44
+ rescue SimpleDSL::NoRuby2Ruby
45
+ end
46
+ end
47
+
48
+ def test_parse
49
+ @parser.parse :action do
50
+ action3 "Back again"
51
+ end
52
+
53
+ assert_equal({:action1 =>"Hello", :action2 =>"Good bye", :action3 =>"Back again"}, @parser.actions)
54
+ end
55
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/simpleopt'
3
+ require 'test/unit'
4
+
5
+ class TestSOPT < Test::Unit::TestCase
6
+
7
+ def test_cmd_option_string
8
+ assert_equal("--tsv-options", SOPT.parse("-h--help:-to--tsv-options")["tsv-options"][:long])
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/tmpfile'
3
+ require 'rbbt/util/tc_hash'
4
+
5
+ class TestTCHash < Test::Unit::TestCase
6
+ def test_each
7
+ TmpFile.with_file do |f|
8
+ t = TCHash.new f
9
+ t["1"] = 2
10
+ t["2"] = 3
11
+
12
+ t.collect do |k,v|
13
+ puts k
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+ require 'rbbt/util/tmpfile'
3
+ require 'test/unit'
4
+
5
+ class TestTmpFile < Test::Unit::TestCase
6
+
7
+ def test_tmp_file
8
+ assert(TmpFile.tmp_file("test") =~ /tmp\/test\d+$/)
9
+ end
10
+
11
+ def test_do_tmp_file
12
+ content = "Hello World!"
13
+ TmpFile.with_file(content) do |file|
14
+ assert_equal content, File.open(file).read
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+