-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathanalyzer_base.rb
100 lines (81 loc) · 2.36 KB
/
analyzer_base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true
require "cc/engine/analyzers/parser_error"
require "cc/engine/analyzers/parser_base"
module CC
module Engine
module Analyzers
class Base
RESCUABLE_ERRORS = [
::CC::Engine::Analyzers::ParserError,
::Errno::ENOENT,
::Racc::ParseError,
::RubyParser::SyntaxError,
::RuntimeError,
].freeze
BASE_POINTS = 1_500_000
def initialize(engine_config:)
@engine_config = engine_config
end
def run(file)
if (skip_reason = skip?(file))
$stderr.puts("Skipping file #{file} because #{skip_reason}")
else
process_file(file)
end
rescue => ex
if RESCUABLE_ERRORS.map { |klass| ex.instance_of?(klass) }.include?(true)
$stderr.puts("Skipping file #{file} due to exception (#{ex.class}): #{ex.message}\n#{ex.backtrace.join("\n")}")
else
$stderr.puts("#{ex.class} error occurred processing file #{file}: aborting.")
raise ex
end
end
def files
file_list.files
end
def filters
engine_config.filters_for(language)
end
def language
self.class::LANGUAGE
end
def mass_threshold
engine_config.mass_threshold_for(language) || self.class::DEFAULT_MASS_THRESHOLD
end
def count_threshold
engine_config.count_threshold_for(language)
end
def calculate_points(mass)
overage = mass - mass_threshold
base_points + (overage * points_per_overage)
end
def transform_sexp(sexp)
sexp
end
private
attr_reader :engine_config
def base_points
self.class::BASE_POINTS
end
def points_per_overage
self.class::POINTS_PER_OVERAGE
end
def process_file(_path)
raise NoMethodError, "Subclass must implement `process_file`"
end
def file_list
@_file_list ||= ::CC::Engine::Analyzers::FileList.new(
engine_config: engine_config,
patterns: engine_config.patterns_for(
language,
self.class::PATTERNS,
),
)
end
def skip?(_path)
nil
end
end
end
end
end