build-graph 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/build/version.rb DELETED
@@ -1,3 +0,0 @@
1
- module Build
2
- VERSION = "0.1.0"
3
- end
data/lib/build/walker.rb DELETED
@@ -1,175 +0,0 @@
1
-
2
- require 'set'
3
-
4
- require 'build/error'
5
-
6
- module Build
7
- # A walker walks over a graph and applies a task to each node.
8
- class Walker
9
- def initialize(graph, &task)
10
- @graph = graph
11
- @task = task
12
-
13
- @count = 0
14
-
15
- @outputs = {}
16
- @dirty = Set.new
17
-
18
- # Generate a list of dirty outputs, possibly a subset, if the build graph might generate additional nodes:
19
- @graph.nodes.each do |key, node|
20
- # For a given child, a list of any parents waiting on it.
21
- if node.dirty?
22
- @dirty << node
23
-
24
- @outputs[node] = []
25
-
26
- node.outputs.each do |output|
27
- @outputs[output] = []
28
- end
29
- end
30
- end
31
-
32
- @parents = {}
33
-
34
- # Failed output paths:
35
- @failed = Set.new
36
- end
37
-
38
- attr :graph
39
- attr :output
40
-
41
- attr_accessor :count
42
-
43
- attr :dirty
44
- attr :parents
45
-
46
- def task(*arguments)
47
- @task.call(self, *arguments)
48
- end
49
-
50
- def wait_on_paths(paths)
51
- edge = Edge.new
52
- failed = false
53
-
54
- paths.each do |path|
55
- if @outputs.include? path
56
- @outputs[path] << edge
57
-
58
- edge.increment!
59
- end
60
-
61
- if !failed and @failed.include?(path)
62
- failed = true
63
- end
64
- end
65
-
66
- edge.wait || failed
67
- end
68
-
69
- def wait_for_nodes(children)
70
- edge = Edge.new
71
-
72
- children.each do |child|
73
- if @dirty.include?(child)
74
- edge.increment!
75
-
76
- @parents[child] ||= []
77
- @parents[child] << edge
78
- end
79
- end
80
-
81
- edge.wait
82
- end
83
-
84
- def exit(node)
85
- @dirty.delete(node)
86
-
87
- # Fail outputs if the node failed:
88
- @failed += node.outputs if node.failed?
89
-
90
- # Clean the node's outputs:
91
- node.outputs.each do |path|
92
- if edges = @outputs.delete(path)
93
- edges.each{|edge| edge.traverse(node)}
94
- end
95
- end
96
-
97
- # Trigger the parent nodes:
98
- if parents = @parents.delete(node)
99
- parents.each{|edge| edge.traverse(node)}
100
- end
101
- end
102
- end
103
-
104
- # A task is a specific process and scope applied to a graph node.
105
- class Task
106
- def initialize(graph, walker, node)
107
- @graph = graph
108
- @node = node
109
- @walker = walker
110
-
111
- # If the execution of the node fails, this is where we save the error:
112
- @error = nil
113
-
114
- @children = []
115
- end
116
-
117
- attr :children
118
-
119
- def inputs
120
- @node.inputs
121
- end
122
-
123
- def outputs
124
- @node.outputs
125
- end
126
-
127
- # Derived task should override this function to provide appropriate behaviour.
128
- def visit
129
- wait_for_inputs
130
-
131
- # If all inputs were good, we can update the node.
132
- unless any_inputs_failed?
133
- begin
134
- #self.instance_eval(&update)
135
- yield
136
- rescue TransientError => error
137
- $stderr.puts Rainbow("Error: #{error.inspect}").red
138
- @error = error
139
- end
140
- end
141
-
142
- wait_for_children
143
- end
144
-
145
- def exit
146
- if @error || any_child_failed? || any_inputs_failed?
147
- @node.fail!
148
- elsif @pool
149
- @node.clean!
150
- end
151
-
152
- @walker.exit(@node)
153
-
154
- @walker.count += 1
155
- end
156
-
157
- protected
158
- def wait_for_inputs
159
- # Wait on any inputs, returns whether any inputs failed:
160
- @inputs_failed = @walker.wait_on_paths(@node.inputs)
161
- end
162
-
163
- def wait_for_children
164
- @walker.wait_for_nodes(@children)
165
- end
166
-
167
- def any_child_failed?
168
- @children.any?{|child| child.failed?}
169
- end
170
-
171
- def any_inputs_failed?
172
- @inputs_failed
173
- end
174
- end
175
- end
@@ -1,18 +0,0 @@
1
-
2
- #include <iostream>
3
- #include <vector>
4
-
5
- std::vector<int> get_numbers() {
6
- return {10, 20, 30, 40, 50, 60, 80};
7
- }
8
-
9
- int main (int argc, char ** argv)
10
- {
11
- auto numbers = get_numbers();
12
- std::sort(numbers.begin(), numbers.end());
13
-
14
- for (auto & number : numbers)
15
- std::cerr << number << "?" << std::endl;
16
-
17
- return 0;
18
- }
data/test/test_files.rb DELETED
@@ -1,74 +0,0 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'test/unit'
22
-
23
- require 'build/files'
24
-
25
- class TestFiles < Test::Unit::TestCase
26
- def test_inclusion
27
- # Glob all test files:
28
- glob = Build::Files::Glob.new(__dir__, "*.rb")
29
-
30
- assert glob.count > 0
31
-
32
- # Should include this file:
33
- assert glob.include?(__FILE__)
34
-
35
- # Glob should intersect self:
36
- assert glob.intersects?(glob)
37
- end
38
-
39
- def test_composites
40
- lib = File.join(__dir__, "../lib")
41
-
42
- test_glob = Build::Files::Glob.new(__dir__, "*.rb")
43
- lib_glob = Build::Files::Glob.new(lib, "*.rb")
44
-
45
- both = test_glob + lib_glob
46
-
47
- # List#roots is the generic accessor for Lists
48
- assert both.roots.include? test_glob.root
49
-
50
- # The composite should include both:
51
- assert both.include?(__FILE__)
52
- end
53
-
54
- def test_roots
55
- test_glob = Build::Files::Glob.new(__dir__, "*.rb")
56
-
57
- # Despite returning a String:
58
- assert test_glob.first.kind_of? String
59
-
60
- # We actually return a subclass which includes the root portion:
61
- assert_equal __dir__, test_glob.first.root
62
- end
63
-
64
- def test_renaming
65
- program_root = File.join(__dir__, "program")
66
- program_glob = Build::Files::Glob.new(program_root, "*.cpp")
67
-
68
- paths = program_glob.process do |path|
69
- path + ".o"
70
- end
71
-
72
- puts "object paths: #{paths} from program paths: #{program_glob.to_a}"
73
- end
74
- end
data/test/test_graph.rb DELETED
@@ -1,148 +0,0 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- # THE SOFTWARE.
20
-
21
- require 'test/unit'
22
-
23
- require 'build/graph'
24
- require 'build/files'
25
-
26
- require 'process/group'
27
- require 'fileutils'
28
-
29
- class TestGraph < Test::Unit::TestCase
30
- # The graph node is created once, so a graph has a fixed number of nodes, which store per-vertex state and connectivity.
31
- class Node < Build::Node
32
- def initialize(graph, inputs, outputs, &update)
33
- @update = update
34
-
35
- super(graph, inputs, outputs)
36
- end
37
-
38
- def apply!(scope)
39
- scope.instance_eval(&@update)
40
- end
41
-
42
- # This ensures that enclosed nodes are run if they are dirty. The top level node has no inputs or outputs by default, so children who become dirty wouldn't mark it as dirty and thus wouldn't be run.
43
- def requires_update?
44
- if outputs.count == 0
45
- return true
46
- else
47
- super
48
- end
49
- end
50
- end
51
-
52
- # The task is the context in which a vertex is updated. Because nodes may initially create other nodes, it is also responsible for looking up and creating new nodes.
53
- class Task < Build::Task
54
- def initialize(graph, walker, node, group = nil)
55
- super(graph, walker, node)
56
-
57
- @group = group
58
- end
59
-
60
- def wet?
61
- @group# and @node.dirty?
62
- end
63
-
64
- def process(inputs, outputs, &block)
65
- child_node = @graph.nodes.fetch([inputs, outputs]) do |key|
66
- @graph.nodes[key] = Node.new(@graph, inputs, outputs, &block)
67
- end
68
-
69
- @children << child_node
70
-
71
- # State saved in update!
72
- child_node.update!(@walker)
73
- end
74
-
75
- def run(*arguments)
76
- if wet?
77
- status = @group.spawn(*arguments)
78
-
79
- if status != 0
80
- raise RuntimeError.new(status)
81
- end
82
- end
83
- end
84
-
85
- def visit
86
- super do
87
- @node.apply!(self)
88
- end
89
- end
90
- end
91
-
92
- class Graph < Build::Graph
93
- def initialize(&block)
94
- @top = Node.new(self, Build::Files::NONE, Build::Files::NONE, &block)
95
-
96
- super()
97
- end
98
-
99
- attr_accessor :top
100
-
101
- def traverse!(walker)
102
- @top.update!(walker)
103
- end
104
-
105
- def build_graph!
106
- super do |walker, node|
107
- Task.new(self, walker, node)
108
- end
109
- end
110
-
111
- def update!
112
- group = Process::Group.new
113
-
114
- super do |walker, node|
115
- Task.new(self, walker, node, group)
116
- end
117
-
118
- group.wait
119
- end
120
- end
121
-
122
- def test_minimal_graph
123
- test_glob = Build::Files::Glob.new(__dir__, "*.rb")
124
- output_paths = Build::Files::Paths.new(__dir__, ["listing.txt"])
125
-
126
- FileUtils.rm_f output_paths.to_a
127
-
128
- graph = Graph.new do
129
- process test_glob, output_paths do
130
- run("ls", "-la", *test_glob, :out => output_paths.first)
131
- end
132
- end
133
-
134
- graph.update!
135
-
136
- mtime = File.mtime(output_paths.first)
137
-
138
- graph.update!
139
-
140
- assert_equal mtime, File.mtime(output_paths.first)
141
-
142
- FileUtils.rm_f output_paths.to_a
143
-
144
- #graph.nodes.each do |key, node|
145
- # puts "#{node.status} #{node.inspect}"
146
- #end
147
- end
148
- end