file_tree_profiler 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/file_tree_profiler.rb +19 -0
- data/lib/file_tree_profiler/data_file.rb +3 -0
- data/lib/file_tree_profiler/dir_file.rb +3 -2
- data/lib/file_tree_profiler/merge.rb +48 -0
- data/lib/file_tree_profiler/merge/pairing.rb +66 -0
- data/lib/file_tree_profiler/version.rb +1 -1
- data/spec/example_folders/differents/profile_a/foo2/bar.txt +1 -0
- data/spec/example_folders/differents/profile_a/foo2/baz/foo.txt +1 -0
- data/spec/example_folders/differents/profile_a/foo2/foo.txt +1 -0
- data/spec/example_folders/differents/profile_c/foo2/baz.txt +1 -0
- data/spec/example_folders/differents/profile_c/foo2/baz/foo.txt +1 -0
- data/spec/example_folders/differents/profile_c/foo2/foo.txt +1 -0
- data/spec/lib/file_tree_profiler/merge/files_spec.rb +56 -0
- data/spec/lib/file_tree_profiler/profile_spec.rb +39 -0
- metadata +18 -2
data/lib/file_tree_profiler.rb
CHANGED
@@ -4,10 +4,29 @@ require 'file_tree_profiler/file'
|
|
4
4
|
require 'file_tree_profiler/data_file'
|
5
5
|
require 'file_tree_profiler/dir_file'
|
6
6
|
require 'file_tree_profiler/profile'
|
7
|
+
require 'file_tree_profiler/merge/pairing'
|
8
|
+
require 'file_tree_profiler/merge'
|
7
9
|
require 'file_tree_profiler/export/csv'
|
8
10
|
require 'file_tree_profiler/export/sql'
|
9
11
|
|
10
12
|
module FileTreeProfiler
|
13
|
+
def self.monitor_report(*args)
|
14
|
+
@@monitor.report(*args) if monitoring?
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.monitoring?
|
18
|
+
defined?(@@monitoring) && @@monitoring == true
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.with_monitoring(monitor)
|
22
|
+
@@monitor = monitor
|
23
|
+
@@monitoring = true
|
24
|
+
yield
|
25
|
+
ensure
|
26
|
+
@@monitor = false
|
27
|
+
@@monitoring = false
|
28
|
+
end
|
29
|
+
|
11
30
|
def self.profile path
|
12
31
|
Profile.new path
|
13
32
|
end
|
@@ -11,10 +11,11 @@ module FileTreeProfiler
|
|
11
11
|
# collects all children as DirFile or DataFile objects
|
12
12
|
# and is invoked on each collected DirFile object
|
13
13
|
def walk
|
14
|
+
FileTreeProfiler.monitor_report(:profile, :dir, path)
|
14
15
|
@children = []
|
15
|
-
Dir.foreach(
|
16
|
+
Dir.foreach(path) do |entry|
|
16
17
|
next if (entry == '..' || entry == '.')
|
17
|
-
full_path = ::File.join(
|
18
|
+
full_path = ::File.join(path, entry)
|
18
19
|
if ::File.directory?(full_path)
|
19
20
|
children.push DirFile.new(self, entry)
|
20
21
|
else
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module FileTreeProfiler
|
2
|
+
class Merge
|
3
|
+
attr_reader :source_profile, :target_profile, :pairings
|
4
|
+
|
5
|
+
def initialize source_profile, target_profile
|
6
|
+
@source_profile = source_profile
|
7
|
+
@target_profile = target_profile
|
8
|
+
@pairings = {}
|
9
|
+
merge :source, source_profile.root
|
10
|
+
merge :target, target_profile.root
|
11
|
+
|
12
|
+
pairings.each do |relative_path, f|
|
13
|
+
FileTreeProfiler.monitor_report :merge_finalize, relative_path
|
14
|
+
if parent_relative_path = f.parent_relative_path
|
15
|
+
if parent = self[parent_relative_path]
|
16
|
+
f.status_leaf = parent.status != f.status
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def add scope, file
|
23
|
+
key = file.relative_path
|
24
|
+
pairings[key] ||= Pairing.new
|
25
|
+
pairings[key].add scope, file
|
26
|
+
end
|
27
|
+
|
28
|
+
def merge scope, dir_file
|
29
|
+
FileTreeProfiler.monitor_report :merge_map, :dir, dir_file.path
|
30
|
+
add scope, dir_file
|
31
|
+
dir_file.children.each do |child|
|
32
|
+
if child.class == DirFile
|
33
|
+
merge scope, child
|
34
|
+
else
|
35
|
+
add scope, child
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def [](relative_path)
|
41
|
+
pairings[relative_path]
|
42
|
+
end
|
43
|
+
|
44
|
+
def size
|
45
|
+
pairings.size
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module FileTreeProfiler
|
2
|
+
class Merge
|
3
|
+
class Pairing
|
4
|
+
EQUAL = 1
|
5
|
+
DIFFERENT = 2
|
6
|
+
ONLY_SOURCE = 3
|
7
|
+
ONLY_TARGET = 4
|
8
|
+
|
9
|
+
attr_accessor :status_leaf
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@status_leaf = false
|
13
|
+
end
|
14
|
+
|
15
|
+
def add scope, file
|
16
|
+
@files ||= {}
|
17
|
+
@files[scope] = file
|
18
|
+
end
|
19
|
+
|
20
|
+
def source
|
21
|
+
@files[:source]
|
22
|
+
end
|
23
|
+
|
24
|
+
def target
|
25
|
+
@files[:target]
|
26
|
+
end
|
27
|
+
|
28
|
+
def name
|
29
|
+
any.name
|
30
|
+
end
|
31
|
+
|
32
|
+
def relative_path
|
33
|
+
any.relative_path
|
34
|
+
end
|
35
|
+
|
36
|
+
def parent_relative_path
|
37
|
+
if relative_path == '/'
|
38
|
+
nil
|
39
|
+
else
|
40
|
+
::File.dirname(relative_path)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def any
|
45
|
+
(source || target)
|
46
|
+
end
|
47
|
+
|
48
|
+
def status
|
49
|
+
@status ||= begin
|
50
|
+
if source && target
|
51
|
+
if source.checksum == target.checksum
|
52
|
+
EQUAL
|
53
|
+
else
|
54
|
+
DIFFERENT
|
55
|
+
end
|
56
|
+
elsif source
|
57
|
+
ONLY_SOURCE
|
58
|
+
else
|
59
|
+
ONLY_TARGET
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
bar.txt
|
@@ -0,0 +1 @@
|
|
1
|
+
foo.txt
|
@@ -0,0 +1 @@
|
|
1
|
+
foo.txt
|
@@ -0,0 +1 @@
|
|
1
|
+
baz.txt
|
@@ -0,0 +1 @@
|
|
1
|
+
foo.txt
|
@@ -0,0 +1 @@
|
|
1
|
+
different
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FileTreeProfiler::Merge::Pairing do
|
4
|
+
let(:source) do
|
5
|
+
mock(:source).tap do |source|
|
6
|
+
source.stub(:checksum).and_return('a1')
|
7
|
+
source.stub(:name).and_return('foo')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:target) do
|
12
|
+
mock(:target).tap do |source|
|
13
|
+
source.stub(:checksum).and_return('a1')
|
14
|
+
source.stub(:name).and_return('foo')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context :equal do
|
19
|
+
before do
|
20
|
+
subject.add :source, source
|
21
|
+
subject.add :target, target
|
22
|
+
end
|
23
|
+
|
24
|
+
its(:name) { should == 'foo' }
|
25
|
+
its(:status) { should == described_class::EQUAL }
|
26
|
+
end
|
27
|
+
|
28
|
+
context :different do
|
29
|
+
before do
|
30
|
+
source.stub(:checksum).and_return('b2')
|
31
|
+
subject.add :source, source
|
32
|
+
subject.add :target, target
|
33
|
+
end
|
34
|
+
|
35
|
+
its(:name) { should == 'foo' }
|
36
|
+
its(:status) { should == described_class::DIFFERENT }
|
37
|
+
end
|
38
|
+
|
39
|
+
context :only_target do
|
40
|
+
before do
|
41
|
+
subject.add :target, target
|
42
|
+
end
|
43
|
+
|
44
|
+
its(:name) { should == 'foo' }
|
45
|
+
its(:status) { should == described_class::ONLY_TARGET }
|
46
|
+
end
|
47
|
+
|
48
|
+
context :only_source do
|
49
|
+
before do
|
50
|
+
subject.add :source, source
|
51
|
+
end
|
52
|
+
|
53
|
+
its(:name) { should == 'foo' }
|
54
|
+
its(:status) { should == described_class::ONLY_SOURCE }
|
55
|
+
end
|
56
|
+
end
|
@@ -35,6 +35,45 @@ describe FileTreeProfiler::Profile do
|
|
35
35
|
context :partially do
|
36
36
|
specify { profile_a.root.children.first.name.should == profile_c.root.children.first.name }
|
37
37
|
specify { profile_a.root.children.first.checksum.should == profile_c.root.children.first.checksum }
|
38
|
+
|
39
|
+
context :merge do
|
40
|
+
let(:merge) { FileTreeProfiler::Merge.new(profile_a, profile_c) }
|
41
|
+
subject { merge }
|
42
|
+
its(:size) { should == 11 }
|
43
|
+
|
44
|
+
specify do
|
45
|
+
subject.pairings.map{|k,v| k}.should == \
|
46
|
+
["/", "/foo", "/foo/bar.txt", "/foo/foo.txt", "/foo2", "/foo2/bar.txt", "/foo2/baz", "/foo2/baz/foo.txt", "/foo2/foo.txt", "/foo2/baz.txt", "/partially_different.txt"]
|
47
|
+
end
|
48
|
+
|
49
|
+
shared_examples_for :pairing do |relative_path, expected_status, expected_status_leaf|
|
50
|
+
expected_status = {
|
51
|
+
:equal => FileTreeProfiler::Merge::Pairing::EQUAL,
|
52
|
+
:different => FileTreeProfiler::Merge::Pairing::DIFFERENT,
|
53
|
+
:only_target => FileTreeProfiler::Merge::Pairing::ONLY_TARGET,
|
54
|
+
:only_source => FileTreeProfiler::Merge::Pairing::ONLY_SOURCE
|
55
|
+
}[expected_status] || raise("unknown status = '#{expected_status}'")
|
56
|
+
context relative_path do
|
57
|
+
subject { merge[relative_path] }
|
58
|
+
its(:relative_path) { should == relative_path }
|
59
|
+
its(:parent_relative_path) { should == ::File.dirname(relative_path) }
|
60
|
+
its(:name) { should == ::File.basename(relative_path) }
|
61
|
+
its(:class) { should == FileTreeProfiler::Merge::Pairing }
|
62
|
+
its(:status) { should == expected_status }
|
63
|
+
its(:status_leaf) { should == expected_status_leaf }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
include_examples :pairing, '/foo', :equal, true
|
68
|
+
include_examples :pairing, '/foo/bar.txt', :equal, false
|
69
|
+
include_examples :pairing, '/foo/foo.txt', :equal, false
|
70
|
+
include_examples :pairing, '/partially_different.txt', :only_target, true
|
71
|
+
include_examples :pairing, '/foo2/baz', :equal, true
|
72
|
+
include_examples :pairing, '/foo2/baz/foo.txt', :equal, false
|
73
|
+
include_examples :pairing, '/foo2/bar.txt', :only_source, true
|
74
|
+
include_examples :pairing, '/foo2/baz.txt', :only_target, true
|
75
|
+
include_examples :pairing, '/foo2/foo.txt', :different, false
|
76
|
+
end
|
38
77
|
end
|
39
78
|
end
|
40
79
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_tree_profiler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sequel
|
@@ -82,6 +82,8 @@ files:
|
|
82
82
|
- lib/file_tree_profiler/export/csv.rb
|
83
83
|
- lib/file_tree_profiler/export/sql.rb
|
84
84
|
- lib/file_tree_profiler/file.rb
|
85
|
+
- lib/file_tree_profiler/merge.rb
|
86
|
+
- lib/file_tree_profiler/merge/pairing.rb
|
85
87
|
- lib/file_tree_profiler/profile.rb
|
86
88
|
- lib/file_tree_profiler/version.rb
|
87
89
|
- spec/example_folders/a/a.txt
|
@@ -91,9 +93,15 @@ files:
|
|
91
93
|
- spec/example_folders/b/bb.txt
|
92
94
|
- spec/example_folders/differents/profile_a/foo/bar.txt
|
93
95
|
- spec/example_folders/differents/profile_a/foo/foo.txt
|
96
|
+
- spec/example_folders/differents/profile_a/foo2/bar.txt
|
97
|
+
- spec/example_folders/differents/profile_a/foo2/baz/foo.txt
|
98
|
+
- spec/example_folders/differents/profile_a/foo2/foo.txt
|
94
99
|
- spec/example_folders/differents/profile_b/foo/bar.txt
|
95
100
|
- spec/example_folders/differents/profile_c/foo/bar.txt
|
96
101
|
- spec/example_folders/differents/profile_c/foo/foo.txt
|
102
|
+
- spec/example_folders/differents/profile_c/foo2/baz.txt
|
103
|
+
- spec/example_folders/differents/profile_c/foo2/baz/foo.txt
|
104
|
+
- spec/example_folders/differents/profile_c/foo2/foo.txt
|
97
105
|
- spec/example_folders/differents/profile_c/partially_different.txt
|
98
106
|
- spec/example_folders/equals/profile_a/bar.txt
|
99
107
|
- spec/example_folders/equals/profile_a/foo/foo.txt
|
@@ -106,6 +114,7 @@ files:
|
|
106
114
|
- spec/lib/file_tree_profiler/data_file_spec.rb
|
107
115
|
- spec/lib/file_tree_profiler/dir_file_spec.rb
|
108
116
|
- spec/lib/file_tree_profiler/export/sql_export_spec.rb
|
117
|
+
- spec/lib/file_tree_profiler/merge/files_spec.rb
|
109
118
|
- spec/lib/file_tree_profiler/profile_spec.rb
|
110
119
|
- spec/lib/file_tree_profiler_spec.rb
|
111
120
|
- spec/spec_helper.rb
|
@@ -143,9 +152,15 @@ test_files:
|
|
143
152
|
- spec/example_folders/b/bb.txt
|
144
153
|
- spec/example_folders/differents/profile_a/foo/bar.txt
|
145
154
|
- spec/example_folders/differents/profile_a/foo/foo.txt
|
155
|
+
- spec/example_folders/differents/profile_a/foo2/bar.txt
|
156
|
+
- spec/example_folders/differents/profile_a/foo2/baz/foo.txt
|
157
|
+
- spec/example_folders/differents/profile_a/foo2/foo.txt
|
146
158
|
- spec/example_folders/differents/profile_b/foo/bar.txt
|
147
159
|
- spec/example_folders/differents/profile_c/foo/bar.txt
|
148
160
|
- spec/example_folders/differents/profile_c/foo/foo.txt
|
161
|
+
- spec/example_folders/differents/profile_c/foo2/baz.txt
|
162
|
+
- spec/example_folders/differents/profile_c/foo2/baz/foo.txt
|
163
|
+
- spec/example_folders/differents/profile_c/foo2/foo.txt
|
149
164
|
- spec/example_folders/differents/profile_c/partially_different.txt
|
150
165
|
- spec/example_folders/equals/profile_a/bar.txt
|
151
166
|
- spec/example_folders/equals/profile_a/foo/foo.txt
|
@@ -158,6 +173,7 @@ test_files:
|
|
158
173
|
- spec/lib/file_tree_profiler/data_file_spec.rb
|
159
174
|
- spec/lib/file_tree_profiler/dir_file_spec.rb
|
160
175
|
- spec/lib/file_tree_profiler/export/sql_export_spec.rb
|
176
|
+
- spec/lib/file_tree_profiler/merge/files_spec.rb
|
161
177
|
- spec/lib/file_tree_profiler/profile_spec.rb
|
162
178
|
- spec/lib/file_tree_profiler_spec.rb
|
163
179
|
- spec/spec_helper.rb
|