rbbt-util 2.1.0 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/bin/rbbt_query.rb +63 -0
  2. data/lib/rbbt-util.rb +5 -5
  3. data/lib/rbbt.rb +2 -11
  4. data/lib/rbbt/util/cmd.rb +1 -1
  5. data/lib/rbbt/util/fix_width_table.rb +9 -3
  6. data/lib/rbbt/util/log.rb +23 -7
  7. data/lib/rbbt/util/misc.rb +121 -15
  8. data/lib/rbbt/util/open.rb +14 -4
  9. data/lib/rbbt/util/persistence.rb +52 -21
  10. data/lib/rbbt/util/rake.rb +108 -21
  11. data/lib/rbbt/util/resource.rb +338 -0
  12. data/lib/rbbt/util/simpleDSL.rb +1 -1
  13. data/lib/rbbt/util/simpleopt.rb +1 -1
  14. data/lib/rbbt/util/task.rb +340 -0
  15. data/lib/rbbt/util/tc_hash.rb +19 -2
  16. data/lib/rbbt/util/tsv.rb +15 -10
  17. data/lib/rbbt/util/tsv/accessor.rb +16 -7
  18. data/lib/rbbt/util/tsv/attach.rb +220 -17
  19. data/lib/rbbt/util/tsv/index.rb +6 -1
  20. data/lib/rbbt/util/tsv/manipulate.rb +4 -5
  21. data/lib/rbbt/util/tsv/parse.rb +45 -21
  22. data/lib/rbbt/util/tsv/resource.rb +74 -0
  23. data/lib/rbbt/util/workflow.rb +99 -75
  24. data/test/rbbt/util/test_filecache.rb +2 -2
  25. data/test/rbbt/util/test_misc.rb +7 -2
  26. data/test/rbbt/util/test_persistence.rb +40 -5
  27. data/test/rbbt/util/test_resource.rb +92 -0
  28. data/test/rbbt/util/test_task.rb +118 -0
  29. data/test/rbbt/util/test_tsv.rb +5 -1
  30. data/test/rbbt/util/test_workflow.rb +77 -62
  31. data/test/rbbt/util/tsv/test_attach.rb +95 -7
  32. data/test/rbbt/util/tsv/test_index.rb +0 -1
  33. data/test/rbbt/util/tsv/test_manipulate.rb +20 -0
  34. data/test/rbbt/util/tsv/test_resource.rb +9 -0
  35. data/test/test_helper.rb +10 -0
  36. data/test/test_rbbt.rb +2 -37
  37. metadata +16 -18
  38. data/lib/rbbt/util/data_module.rb +0 -93
  39. data/lib/rbbt/util/path.rb +0 -155
  40. data/lib/rbbt/util/pkg_config.rb +0 -78
  41. data/lib/rbbt/util/pkg_data.rb +0 -119
  42. data/lib/rbbt/util/pkg_software.rb +0 -145
  43. data/test/rbbt/util/test_data_module.rb +0 -50
  44. data/test/rbbt/util/test_path.rb +0 -10
  45. data/test/rbbt/util/test_pkg_data.rb +0 -129
  46. data/test/test_pkg.rb +0 -28
@@ -0,0 +1,74 @@
1
+ require 'rbbt/util/resource'
2
+
3
+ module Resource
4
+ module Path
5
+ def tsv(type = nil, options = {})
6
+ if options.empty? and Hash === type
7
+ options, type = type, nil
8
+ end
9
+
10
+ tsv = TSV.new self, type, options
11
+ tsv.namespace ||= namespace
12
+ tsv
13
+ end
14
+
15
+ def namespace_or_dirname
16
+ namespace || File.basename(File.dirname(self))
17
+ end
18
+
19
+ def index(options = {})
20
+ TSV.index self, options
21
+ end
22
+
23
+ def open(options = {})
24
+ produce
25
+ Open.open(self.find, options)
26
+ end
27
+
28
+ def read(options = {})
29
+ produce
30
+ Open.read(self.find, options)
31
+ end
32
+
33
+ def fields(sep = nil, header_hash = nil)
34
+ produce
35
+ key, fields, options, line = TSV.parse_header(self.open, sep, header_hash)
36
+ namespace = options[:namespace] if options.include? namespace
37
+ fields.collect{|f| f.extend TSV::Field; f.namespace = namespace || namespace_or_dirname ;f}
38
+ end
39
+
40
+ def all_fields(sep = nil, header_hash = nil)
41
+ produce
42
+ key, fields, options, line = TSV.parse_header(self.open, sep, header_hash)
43
+ namespace = options[:namespace] if options.include? namespace
44
+ [key,fields].flatten.collect{|f| f.extend TSV::Field; f.namespace = namespace || namespace_or_dirname ;f}
45
+ end
46
+
47
+ def fields_in_namespace(sep = nil, header_hash = nil)
48
+ produce
49
+ TSV.parse_header(self.open, sep, header_hash)[1].collect{|f| f.extend TSV::Field; f.namespace = namespace ;f}.select{|f| f.namespace == namespace}
50
+ end
51
+
52
+ def all_namespace_fields(namespace, sep = /\t/, header_hash = "#")
53
+ produce
54
+ key_field, fields = TSV.parse_header(self.open, sep, header_hash).values_at(0, 1).flatten.collect{|f| f.extend TSV::Field; f.namespace = namespace; f}.select{|f| f.namespace == namespace}
55
+ end
56
+
57
+ def identifier_files
58
+ dir = self.find.sub(self,'')
59
+ if dir.nil? or dir.empty?
60
+ path = File.join(File.dirname(self.find), 'identifiers')
61
+ path.extend Path
62
+ path.pkg_module = pkg_module
63
+ if path.exists?
64
+ [path]
65
+ else
66
+ []
67
+ end
68
+ else
69
+ identifier_files = Misc.find_files_back_to(self.find, 'identifiers', dir)
70
+ return identifier_files.collect{|f| Resource::Path.path(f)}
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,90 +1,114 @@
1
- require 'rake'
2
- require 'rbbt/util/rake'
3
-
1
+ require 'rbbt/util/resource'
2
+ require 'rbbt/util/task'
3
+ require 'rbbt/util/persistence'
4
4
  module WorkFlow
5
- module Runner
6
- def data
7
- $_workflow_prereq
5
+ def self.extended(base)
6
+ class << base
7
+ attr_accessor :tasks, :jobdir, :dangling_options, :dangling_option_descriptions,
8
+ :dangling_option_types, :dangling_option_defaults, :dangling_dependencies, :last_task
8
9
  end
9
10
 
10
- def input
11
- $_workflow_input
12
- end
11
+ base.extend Resource
12
+ base.lib_dir = Resource.caller_base_dir if base.class == Object
13
+ base.tasks = {}
14
+ base.jobdir = (File.exists?(base.var.find(:lib)) ? base.var.find(:lib) : base.var.find)
15
+ base.clear_dangling
16
+ end
13
17
 
14
- $_workflow_default_persistence = :string
15
- def default_persistence
16
- $_workflow_default_persistence
18
+ def local_persist(*args, &block)
19
+ argsv = *args
20
+ options = argsv.pop
21
+ if Hash === options
22
+ options.merge!(:persistence_dir => cache.find(:lib))
23
+ argsv.push options
24
+ else
25
+ argsv.push options
26
+ argsv.push({:persistence_dir => cache.find(:lib)})
17
27
  end
28
+ Persistence.persist(*argsv, &block)
29
+ end
18
30
 
19
- def default_persistence=(type)
20
- $_workflow_default_persistence = type
21
- end
31
+ def clear_dangling
32
+ @dangling_options = []
33
+ @dangling_option_descriptions = {}
34
+ @dangling_option_types = {}
35
+ @dangling_option_defaults = {}
36
+ @dangling_dependencies = nil
37
+ end
38
+
39
+ def task_option(*args)
40
+ name, description, type, default = *args
41
+ @dangling_options << name if name
42
+ @dangling_option_descriptions[name] = description if description
43
+ @dangling_option_types[name] = type if type
44
+ @dangling_option_defaults[name] = default if default
45
+ end
46
+
47
+ def task_dependencies(dependencies)
48
+ dependencies = [dependencies] unless Array === dependencies
49
+ @dangling_dependencies = dependencies
50
+ end
51
+
52
+ def process_dangling
53
+ res = [
54
+ @dangling_options,
55
+ Hash[*@dangling_options.zip(@dangling_option_descriptions.values_at(*@dangling_options)).flatten],
56
+ Hash[*@dangling_options.zip(@dangling_option_types.values_at(*@dangling_options)).flatten],
57
+ Hash[*@dangling_options.zip(@dangling_option_defaults.values_at(*@dangling_options)).flatten],
58
+ @dangling_dependencies || @last_task,
59
+ ]
22
60
 
23
- def step(step_name, options = nil)
24
- dependencies, options = case
25
- when ((String === options or Symbol === options) and %w(string marshal tsv tsv_string).include? options.to_s)
26
- [nil, {:persistence_type => options}]
27
- when Hash === options
28
- [nil, options]
29
- else
30
- [options, {}]
31
- end
32
-
33
- options = Misc.add_defaults options, :persistence_type => default_persistence
34
- persistence_type = Misc.process_options options, :persistence_type
35
- dependencies = Misc.process_options options, :dependencies if options.include? :dependencies
36
-
37
- re = Regexp.new(/(?:^|\/)#{Regexp.quote step_name.to_s}\/.*$/)
38
-
39
- @last_step = nil unless defined? @last_step
40
- @last_persistence_type = nil unless defined? @last_persistence_type
41
-
42
- if dependencies.nil? && ! @last_step.nil?
43
- dependencies = @last_step
44
- end
45
- @last_step = step_name
46
-
47
- # Generate the Hash definition
48
- rule_def = case
49
- when dependencies.nil?
50
- re
51
- when String === dependencies || Symbol === dependencies
52
- {re => lambda{|filename| filename.sub(step_name.to_s, dependencies.to_s) }}
53
- when Array === dependencies
54
- {re => lambda{|filename| dependencies.collect{|dep| filename.sub(step_name.to_s, dep.to_s) } }}
55
- when Proc === dependencies
56
- {re => dependencies}
57
- end
58
-
59
- @last_step = step_name
60
- last_persistence_type, @last_persistence_type = @last_persistence_type, persistence_type
61
-
62
- rule rule_def do |t|
63
- Persistence.persist(t.name, "", persistence_type, :persistence_file => t.name) do
64
- $_workflow_prereq = case
65
- when (t.prerequisites.nil? or (Array === t.prerequisites and t.prerequisites.empty?))
66
- nil
67
- else
68
- Persistence.persist(t.prerequisites.first, "", last_persistence_type, :persistence_file => t.prerequisites.first) do
69
- raise "Error, this file should be produced already"
70
- end
71
- end
72
- yield
73
- end
74
- end
61
+ clear_dangling
62
+ res
63
+ end
64
+
65
+ def task(name, &block)
66
+ if Hash === name
67
+ persistence = name.values.first
68
+ name = name.keys.first
69
+ else
70
+ persistence = :marshal
75
71
  end
72
+
73
+ options, option_descriptions, option_types, option_defaults, dependencies = process_dangling
74
+ option_descriptions.delete_if do |k,v| v.nil? end
75
+ option_types.delete_if do |k,v| v.nil? end
76
+ option_defaults.delete_if do |k,v| v.nil? end
77
+ task = Task.new name, persistence, options, option_descriptions, option_types, option_defaults, self, dependencies, self, &block
78
+ tasks[name] = task
79
+ @last_task = task
76
80
  end
77
81
 
78
- def self.run(file = :default, workflow_input = nil, &block)
79
- $_workflow_input = workflow_input
80
- RakeHelper.run("Runtime", file) do
81
- yield
82
+ def job(task, jobname, *args)
83
+ task = tasks[task]
84
+ raise "Task #{ task } not found" if task.nil?
85
+
86
+ all_options, option_descriptions, option_types, option_defaults = task.recursive_options
87
+
88
+ non_optional_arguments = all_options.reject{|option| option_defaults.include? option}
89
+ run_options = nil
90
+
91
+ case
92
+ when args.length == non_optional_arguments.length
93
+ run_options = Hash[*non_optional_arguments.zip(args).flatten].merge option_defaults
94
+ when args.length == non_optional_arguments.length + 1
95
+ optional_args = args.pop
96
+ run_options = option_defaults.
97
+ merge(optional_args).
98
+ merge(Hash[*non_optional_arguments.zip(args).flatten])
99
+ else
100
+ raise "Number of non optional arguments (#{non_optional_arguments * ', '}) does not match given (#{args.flatten * ", "})"
82
101
  end
102
+
103
+ task.job(jobname, run_options)
83
104
  end
84
105
 
85
- def self.load(wf_file, file = :default, workflow_input = nil)
86
- $_workflow_input = workflow_input
87
- RakeHelper.run(wf_file, file)
106
+ def run(*args)
107
+ job(*args).run
88
108
  end
89
- end
90
109
 
110
+ def load_job(taskname, job_id)
111
+ tasks[taskname].load(job_id)
112
+ end
113
+
114
+ end
@@ -1,12 +1,12 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
- require 'rbbt/util/filecache'
3
2
  require 'rbbt-util'
3
+ require 'rbbt/util/filecache'
4
4
  require 'test/unit'
5
5
 
6
6
  class TestFileCache < Test::Unit::TestCase
7
7
 
8
8
  def test_path
9
- assert_equal(File.join(Rbbt.cachedir,'3','2','1','123.ext'), FileCache.path('123.ext'))
9
+ assert_equal(File.join(Rbbt.var.cache.filecache.find(:user),'3','2','1','123.ext'), FileCache.path('123.ext'))
10
10
  end
11
11
 
12
12
  def test_add_read
@@ -71,8 +71,13 @@ This is an example file. Entries are separated by Entry
71
71
  hash = {:a => /test/}
72
72
  assert_equal({}, Misc.string2hash(Misc.hash2string(hash)))
73
73
 
74
-
75
-
76
74
  end
77
75
 
76
+ def test_merge
77
+ a = [[1],[2]]
78
+ a = NamedArray.name a, %w(1 2)
79
+ a.merge [3,4]
80
+ assert_equal [1,3], a[0]
81
+ end
82
+
78
83
  end
@@ -10,7 +10,7 @@ class TestPersistence < Test::Unit::TestCase
10
10
  assert File.exists? f
11
11
  assert_equal string, Open.read(f)
12
12
 
13
- rm f
13
+ FileUtils.rm f
14
14
  end
15
15
  end
16
16
 
@@ -22,7 +22,7 @@ class TestPersistence < Test::Unit::TestCase
22
22
  assert_equal object, YAML.load(File.open(f))
23
23
  assert_equal YAML.dump(object), Open.read(f)
24
24
 
25
- rm f
25
+ FileUtils.rm f
26
26
  end
27
27
  end
28
28
 
@@ -34,7 +34,7 @@ class TestPersistence < Test::Unit::TestCase
34
34
  assert_equal object, Marshal.load(File.open(f))
35
35
  assert_equal Marshal.dump(object), Open.read(f)
36
36
 
37
- rm f
37
+ FileUtils.rm f
38
38
  end
39
39
  end
40
40
 
@@ -42,7 +42,7 @@ class TestPersistence < Test::Unit::TestCase
42
42
  object = {:a => 1, :b => 2}
43
43
  TmpFile.with_file do |f|
44
44
  Persistence.persist("token_file", :Test, :tsv_extra, :persistence_file => f) do
45
- [object, {:fields => ["Number"], :key_field => "Letter", :type => :list, :filename => "foo"}]
45
+ [object, {:fields => ["Number"], :key_field => "Letter", :type => :single, :cast => :to_i, :filename => "foo"}]
46
46
  end
47
47
 
48
48
  assert File.exists? f
@@ -51,7 +51,7 @@ class TestPersistence < Test::Unit::TestCase
51
51
  assert_equal 1, new["a"]
52
52
  assert_equal "Letter", new.key_field
53
53
 
54
- rm f
54
+ FileUtils.rm f
55
55
  end
56
56
  end
57
57
 
@@ -117,5 +117,40 @@ row2 A B Id3
117
117
  end
118
118
  end
119
119
 
120
+ def test_integer
121
+ content =<<-EOF
122
+ #Id ValueA
123
+ row1 1
124
+ row2 2
125
+ EOF
126
+
127
+ TmpFile.with_file(content) do |filename|
128
+ tsv = TSV.new(filename, :single, :sep => /\s+/, :cast => :to_i, :persistence => true)
129
+ assert_equal 1, tsv["row1"]
130
+ end
131
+ end
132
+
133
+ def test_integer
134
+ content =<<-EOF
135
+ row1 1 2 3 4 5
136
+ row2 2 4 6 8
137
+ EOF
138
+
139
+ TmpFile.with_file(content) do |filename|
140
+ tsv = TSV.new(filename, :flat, :sep => /\s+/, :cast => :to_i, :persistence => true)
141
+ assert_equal [1,2,3,4,5], tsv["row1"]
142
+ end
143
+ end
144
+
145
+ def test_persist_dir
146
+ string = Persistence.persist("Test", :Test, :string, :persistence_dir => Rbbt.tmp.test.persistence) do
147
+ "Test"
148
+ end
149
+
150
+ assert Dir.glob(Rbbt.tmp.test.persistence.find + '*').length == 1
151
+
152
+ assert_equal "Test", string
153
+ end
154
+
120
155
  end
121
156
 
@@ -0,0 +1,92 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../..', 'test_helper.rb')
2
+ require 'rbbt/util/resource'
3
+
4
+
5
+ module Rbbt
6
+ extend Resource
7
+
8
+ tmp.test_string.define_as_string "Test String"
9
+ tmp.url.define_as_url "http://www.ruby-lang.org/es/"
10
+ tmp.Rakefile.define_as_string <<-EOF
11
+ file 'foo' do |t|
12
+ Open.write(t.name, "Test String")
13
+ end
14
+ EOF
15
+
16
+ tmp.work.define_as_rake tmp.Rakefile.find.produce
17
+
18
+ tmp.test.install.xclip.define_as_string <<-EOF
19
+ name="xclip:0.12"
20
+ url="http://downloads.sourceforge.net/project/xclip/xclip/0.12/xclip-0.12.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fxclip%2F&ts=1286472387&use_mirror=sunet"
21
+
22
+ install_src "$name" "$url"
23
+ EOF
24
+
25
+ FileUtils.chmod 0770, tmp.test.install.xclip.produce
26
+
27
+ software.opt.xclip.define_as_install tmp.test.install.xclip.find
28
+ end
29
+
30
+ Open.cachedir = Rbbt.tmp.cache.find :user
31
+
32
+ module Phgx
33
+ extend Resource
34
+ end
35
+
36
+
37
+ class TestResource < Test::Unit::TestCase
38
+ def test_methods
39
+ assert Resource.methods.include?("resources")
40
+ assert ! Resource.methods.include?("pkgdir")
41
+ assert ! Phgx.methods.include?("resources")
42
+ assert Phgx.methods.include?("pkgdir")
43
+
44
+ end
45
+ def test_resolve
46
+ assert_equal File.join(ENV['HOME'], '.rbbt/etc/foo'), Resource.resolve('etc/foo', '', :user)
47
+ assert_equal File.join(ENV['HOME'], '.phgx/etc/foo'), Resource.resolve('etc/foo', 'phgx', :user)
48
+
49
+ assert_equal File.join('/', 'etc/foo'), Resource.resolve('etc/foo', '', :global)
50
+ assert_equal File.join('/', 'etc/phgx/foo'), Resource.resolve('etc/foo', 'phgx', :global)
51
+
52
+ assert_equal File.join('/usr/local', 'etc/foo'), Resource.resolve('etc/foo', '', :local)
53
+ assert_equal File.join('/usr/local', 'etc/phgx/foo'), Resource.resolve('etc/foo', 'phgx', :local)
54
+
55
+ assert_equal File.expand_path(File.join(File.dirname(File.expand_path(__FILE__)), '../../../', 'etc/foo')), Resource.resolve('etc/foo', '', :lib)
56
+ assert_equal File.expand_path(File.join(File.dirname(File.expand_path(__FILE__)), '../../../', 'etc/foo')), Resource.resolve('etc/foo', 'phgx', :lib)
57
+
58
+ assert_equal File.join(ENV['HOME'], '.rbbt/etc/foo'), Resource.resolve('etc/foo', '')
59
+ assert_equal File.join(ENV['HOME'], '.phgx/etc/foo'), Resource.resolve('etc/foo', 'phgx')
60
+ end
61
+
62
+ def test_base
63
+ assert_equal Rbbt, Rbbt.base
64
+ end
65
+
66
+ def test_path
67
+ assert_equal File.join(ENV['HOME'], '.rbbt/etc/foo'), Rbbt.etc.foo.find
68
+ assert_equal File.join(ENV['HOME'], '.rbbt/etc/foo'), Rbbt.etc.foo.find(:user)
69
+ assert_equal File.join(ENV['HOME'], '.phgx/etc/foo'), Phgx.etc.foo.find
70
+ assert_equal File.join(ENV['HOME'], '.phgx/etc/foo'), Phgx.etc.foo.find(:user)
71
+ end
72
+
73
+ def test_libdir
74
+ assert File.exists? Rbbt.share.lib.R["util.R"].find :lib
75
+ assert File.exists? Rbbt.share.lib.R["util.R"].find
76
+ end
77
+
78
+ def test_define
79
+ begin
80
+ assert_equal "Test String", Rbbt.tmp.test_string.read
81
+ assert_equal "Test String", Rbbt.tmp.work.foo.read
82
+ ensure
83
+ FileUtils.rm Rbbt.tmp.test_string.find if File.exists? Rbbt.tmp.test_string.find
84
+ FileUtils.rm Rbbt.tmp.url.find if File.exists? Rbbt.tmp.url.find
85
+ end
86
+ end
87
+
88
+ def test_install
89
+ assert File.exists?(Rbbt.software.opt.xclip.produce)
90
+ end
91
+ end
92
+