Class: Ajimi::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/ajimi/checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Checker

Returns a new instance of Checker.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ajimi/checker.rb', line 8

def initialize(config)
  @config = config
  @source = @config[:source]
  @target = @config[:target]
  @check_root_path = @config[:check_root_path]
  @pruned_paths = @config[:pruned_paths] || []
  @ignored_paths = @config[:ignored_paths] || []
  @ignored_contents = @config[:ignored_contents] || {}
  @pending_paths = @config[:pending_paths] || []
  @pending_contents = @config[:pending_contents] || {}
  @enable_check_contents = @config[:enable_check_contents] || false
  @limit_check_contents = @config[:limit_check_contents] || 0
  @find_max_depth = @config[:find_max_depth]
  @verbose = @config[:verbose] || false
end

Instance Attribute Details

#diff_contents_cacheObject

Returns the value of attribute diff_contents_cache.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def diff_contents_cache
  @diff_contents_cache
end

#diffsObject

Returns the value of attribute diffs.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def diffs
  @diffs
end

#enable_check_contentsObject

Returns the value of attribute enable_check_contents.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def enable_check_contents
  @enable_check_contents
end

#ignored_by_contentObject

Returns the value of attribute ignored_by_content.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def ignored_by_content
  @ignored_by_content
end

#ignored_by_pathObject

Returns the value of attribute ignored_by_path.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def ignored_by_path
  @ignored_by_path
end

#pending_by_contentObject

Returns the value of attribute pending_by_content.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def pending_by_content
  @pending_by_content
end

#pending_by_pathObject

Returns the value of attribute pending_by_path.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def pending_by_path
  @pending_by_path
end

#resultObject

Returns the value of attribute result.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def result
  @result
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def source
  @source
end

#targetObject

Returns the value of attribute target.



5
6
7
# File 'lib/ajimi/checker.rb', line 5

def target
  @target
end

Instance Method Details

#checkObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ajimi/checker.rb', line 24

def check
  puts_verbose "Start ajimi check with options: #{@config}\n"

  puts_verbose "Finding...: #{@source.host}\n"
  @source_find = @source.find(@check_root_path, @find_max_depth, @pruned_paths)

  puts_verbose "Finding...: #{@target.host}\n"
  @target_find = @target.find(@check_root_path, @find_max_depth, @pruned_paths)

  puts_verbose "Checking diff entries...\n"
  @diffs = diff_entries(@source_find, @target_find)

  puts_verbose "Checking ignored_paths and pending_paths...\n"
  @diffs = filter_ignored_and_pending_paths(@diffs, @ignored_paths, @pending_paths)

  if @enable_check_contents
    puts_verbose "Checking ignored_contents and pending_contents...\n"
    @diffs = filter_ignored_and_pending_contents(@diffs, @ignored_contents, @pending_contents, @limit_check_contents)
  end

  puts_verbose "Diffs empty?: #{@diffs.empty?}\n"
  puts_verbose "\n"

  @result = @diffs.empty?
end

#diff_entries(source_find, target_find) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ajimi/checker.rb', line 54

def diff_entries(source_find, target_find)
  diffs = ::Diff::LCS.diff(source_find, target_find)
  diffs.map do |diff|
    diff.map do |change|
      ::Diff::LCS::Change.new(
        change.action,
        change.position,
        Ajimi::Server::Entry.parse(change.element)
      )
    end
  end
end

#diff_file(path) ⇒ Object



156
157
158
159
160
# File 'lib/ajimi/checker.rb', line 156

def diff_file(path)
  source_file = @source.cat_or_md5sum(path)
  target_file = @target.cat_or_md5sum(path)
  diffs = ::Diff::LCS.diff(source_file, target_file)
end

#diff_file_countObject



206
207
208
# File 'lib/ajimi/checker.rb', line 206

def diff_file_count
  union_set_diff_file_paths(@diffs).count
end

#filter_diff_file(diffs, filter_pattern = nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ajimi/checker.rb', line 162

def filter_diff_file(diffs, filter_pattern = nil)
  if filter_pattern.nil?
    diffs
  else
    diffs.map do |diff|
      diff.reject do |change|
        change.element.match filter_pattern
      end
    end
  end
end

#filter_ignored_and_pending_contents(diffs, ignored_contents, pending_contents, limit_check_contents) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ajimi/checker.rb', line 91

def filter_ignored_and_pending_contents(diffs, ignored_contents, pending_contents, limit_check_contents)
  @ignored_by_content = []
  @pending_by_content = []
  @diff_contents_cache = ""
  
  diff_files = product_set_file_paths(diffs)
  diff_files = diff_files.slice(0, limit_check_contents) if limit_check_contents > 0
  diff_files.each do |file|
    diff_file_result = diff_file(file)
    ignored_diff_file_result = filter_diff_file(diff_file_result, ignored_contents[file])
    if ignored_diff_file_result.flatten.empty?
      @ignored_by_content << file
    else
      pending_diff_file_result = filter_diff_file(ignored_diff_file_result, pending_contents[file])
      if pending_diff_file_result.flatten.empty?
        @pending_by_content << file
      else
        @diff_contents_cache << "--- #{@source.host}: #{file}\n"
        @diff_contents_cache << "+++ #{@target.host}: #{file}\n"
        @diff_contents_cache << "\n"
        pending_diff_file_result.each do |diff|
          diff.map do |change|
            @diff_contents_cache << change.action + " " + change.position.to_s + " " + change.element.to_s + "\n"
          end
        end
      end
    end
  end
  diffs = remove_entry_from_diffs(diffs, @ignored_by_content + @pending_by_content)
end

#filter_ignored_and_pending_paths(diffs, ignored_paths, pending_paths) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ajimi/checker.rb', line 67

def filter_ignored_and_pending_paths(diffs, ignored_paths, pending_paths)
  if !ignored_paths.empty?
    @ignored_by_path = filter_paths(diffs, ignored_paths)
    diffs = remove_entry_from_diffs(diffs, @ignored_by_path)
  end
  
  if !pending_paths.empty?
    @pending_by_path = filter_paths(diffs, pending_paths)
    diffs = remove_entry_from_diffs(diffs, @pending_by_path)
  end
  diffs
end

#filter_paths(diffs, filter_paths = []) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/ajimi/checker.rb', line 80

def filter_paths(diffs, filter_paths = [])
  filtered = []
  union_regexp_pattern = Regexp.union(filter_paths)
  diffs.each do |diff|
    diff.each do |change|
      filtered <<  change.element.path if change.element.path.match union_regexp_pattern
    end
  end
  filtered.uniq.sort
end

#ignored_by_content_file_countObject



198
199
200
# File 'lib/ajimi/checker.rb', line 198

def ignored_by_content_file_count
  @ignored_by_content ? @ignored_by_content.count : 0
end

#ignored_by_path_file_countObject



190
191
192
# File 'lib/ajimi/checker.rb', line 190

def ignored_by_path_file_count
  @ignored_by_path ? @ignored_by_path.count : 0
end

#pending_by_content_file_countObject



202
203
204
# File 'lib/ajimi/checker.rb', line 202

def pending_by_content_file_count
  @pending_by_content ? @pending_by_content.count : 0
end

#pending_by_path_file_countObject



194
195
196
# File 'lib/ajimi/checker.rb', line 194

def pending_by_path_file_count
  @pending_by_path ? @pending_by_path.count : 0
end

#product_set_file_paths(diffs) ⇒ Object



151
152
153
154
# File 'lib/ajimi/checker.rb', line 151

def product_set_file_paths(diffs)
  minus, plus = split_diff_file_paths(diffs)
  (minus & plus).sort
end

#puts_verbose(message) ⇒ Object



50
51
52
# File 'lib/ajimi/checker.rb', line 50

def puts_verbose(message)
  puts message if @config[:verbose]
end

#remove_entry_from_diffs(diffs, remove_list) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/ajimi/checker.rb', line 174

def remove_entry_from_diffs(diffs, remove_list)
  diffs.map do |diff|
    diff.reject do |change|
      remove_list.include? change.element.path
    end
  end
end

#source_file_countObject



182
183
184
# File 'lib/ajimi/checker.rb', line 182

def source_file_count
  @source_find ? @source_find.count : 0
end

#split_diff_file_paths(diffs) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ajimi/checker.rb', line 133

def split_diff_file_paths(diffs)
  return [],[] if diffs.flatten.empty?
  minus = []
  plus = []
  diffs.each do |diff|
    diff.each do |change|
      minus << change.element.path if change.action == "-" && change.element.file?
      plus << change.element.path if change.action == "+" && change.element.file?
    end
  end
  return minus, plus
end

#target_file_countObject



186
187
188
# File 'lib/ajimi/checker.rb', line 186

def target_file_count
  @target_find ? @target_find.count : 0
end

#union_set_diff_file_paths(diffs) ⇒ Object



146
147
148
149
# File 'lib/ajimi/checker.rb', line 146

def union_set_diff_file_paths(diffs)
  minus, plus = split_diff_file_paths(diffs)
  (minus | plus).sort
end

#uniq_diff_file_paths(diffs) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/ajimi/checker.rb', line 122

def uniq_diff_file_paths(diffs)
  return [] if diffs.flatten.empty?
  diff_file_paths = []
  diffs.each do |diff|
    diff.each do |change|
      diff_file_paths << change.element.path if change.element.file?
    end
  end
  diff_file_paths.uniq.sort
end