codeclimate 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1bfaf5724cae591ae825c0be10218f072c65939e
4
- data.tar.gz: 03a588c5411b86d2e796efc4bfa78d08134f6d9d
3
+ metadata.gz: ed633d34c37b17d582f70edcf4e105f66125b66f
4
+ data.tar.gz: 196920672b0465ed7ad5b36c073eb6ed26f7721e
5
5
  SHA512:
6
- metadata.gz: 95ce4e1916df7d699aca2bac7e4a6c30160a7c47fd9003a5b1ca2bbe2e269fc49f3871ad003b6188b7ff085edc7e2131a57feb296eba4ab3f7c7bb39453afea5
7
- data.tar.gz: e9a307e4e88493b6429d6785cc4b1573ebb5a9c3341008e501cecc24bf0e5d3bd5c14904c2182dffea99e41901a68d42b4fdac2aafc6ec7d96dbe7242d708a88
6
+ metadata.gz: ed1f6e059f66b75db9c2ca1ec7a7f68de47344060addd304579a3403bee95410f5b6baac4d6520b9549d34234c9458103e4348cc89d640ec636ed88cfccf022a
7
+ data.tar.gz: 547ec662b6daac3235675a42b83677f6ba9e716bc05046c32f5ef4d33e9f1ec795a3bb52a8e52759d3672978cfd1f1b72ceb8830844b7a7f91bc82f2a465ccbb
@@ -2,8 +2,8 @@ require "yaml"
2
2
 
3
3
  module CC
4
4
  module Analyzer
5
+ # TODO: replace each use of this with CC::Yaml and remove it
5
6
  class Config
6
-
7
7
  def initialize(config_body)
8
8
  @config = YAML.safe_load(config_body) || {"engines"=> {} }
9
9
  @config["engines"] ||= {}
@@ -8,11 +8,11 @@ module CC
8
8
 
9
9
  TIMEOUT = 15 * 60 # 15m
10
10
 
11
- def initialize(name, metadata, code_path, config_json, label)
11
+ def initialize(name, metadata, code_path, config, label)
12
12
  @name = name
13
13
  @metadata = metadata
14
14
  @code_path = code_path
15
- @config_json = config_json
15
+ @config = config
16
16
  @label = label.to_s
17
17
  end
18
18
 
@@ -102,7 +102,7 @@ module CC
102
102
 
103
103
  def config_file
104
104
  path = File.join("/tmp/cc", SecureRandom.uuid)
105
- File.write(path, @config_json)
105
+ File.write(path, @config.to_json)
106
106
  path
107
107
  end
108
108
 
@@ -115,13 +115,7 @@ module CC
115
115
  end
116
116
 
117
117
  def output_filter
118
- @output_filter ||= EngineOutputFilter.new(config)
119
- end
120
-
121
- def config
122
- # N.B. there is no expected scenario where this would fail so a
123
- # parser-error rescue has been omitted intentionally
124
- JSON.parse(@config_json)
118
+ @output_filter ||= EngineOutputFilter.new(@config)
125
119
  end
126
120
 
127
121
  CommandFailure = Class.new(StandardError)
@@ -3,18 +3,29 @@ require "safe_yaml"
3
3
  module CC
4
4
  module Analyzer
5
5
  class EngineRegistry
6
- def initialize
6
+ def initialize(dev_mode = false)
7
7
  @path = File.expand_path("../../../../config/engines.yml", __FILE__)
8
8
  @config = YAML.safe_load_file(@path)
9
+ @dev_mode = dev_mode
9
10
  end
10
11
 
11
12
  def [](engine_name)
12
- @config[engine_name]
13
+ if dev_mode?
14
+ { "image" => "codeclimate/codeclimate-#{engine_name}:latest" }
15
+ else
16
+ @config[engine_name]
17
+ end
13
18
  end
14
19
 
15
20
  def list
16
21
  @config
17
22
  end
23
+
24
+ private
25
+
26
+ def dev_mode?
27
+ @dev_mode
28
+ end
18
29
  end
19
30
  end
20
31
  end
@@ -0,0 +1,80 @@
1
+ require "securerandom"
2
+
3
+ module CC
4
+ module Analyzer
5
+ class EnginesRunner
6
+ InvalidEngineName = Class.new(StandardError)
7
+ NoEnabledEngines = Class.new(StandardError)
8
+
9
+ def initialize(registry, formatter, source_dir, config, container_label = nil)
10
+ @registry = registry
11
+ @formatter = formatter
12
+ @source_dir = source_dir
13
+ @config = config
14
+ @container_label = container_label
15
+ end
16
+
17
+ def run
18
+ raise NoEnabledEngines if engines.empty?
19
+
20
+ Analyzer.logger.info("running #{engines.size} engines")
21
+
22
+ @formatter.started
23
+
24
+ engines.each { |engine| run_engine(engine) }
25
+
26
+ @formatter.finished
27
+ ensure
28
+ @formatter.close
29
+ end
30
+
31
+ private
32
+
33
+ def run_engine(engine)
34
+ Analyzer.logger.info("starting engine #{engine.name}")
35
+
36
+ Analyzer.statsd.time("engines.time") do
37
+ Analyzer.statsd.time("engines.names.#{engine.name}.time") do
38
+ @formatter.engine_running(engine) do
39
+ engine.run(@formatter)
40
+ end
41
+ end
42
+ end
43
+
44
+ Analyzer.logger.info("finished engine #{engine.name}")
45
+ end
46
+
47
+ def engines
48
+ @engines ||= enabled_engines.map do |name, config|
49
+ metadata = registry_lookup(name)
50
+ label = @container_label || SecureRandom.uuid
51
+ engine_config = config.merge!(exclude_paths: exclude_paths)
52
+
53
+ Engine.new(name, metadata, @source_dir, engine_config, label)
54
+ end
55
+ end
56
+
57
+ def enabled_engines
58
+ {}.tap do |ret|
59
+ @config.engines.each do |name, config|
60
+ if config.enabled?
61
+ ret[name] = config
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ def registry_lookup(engine_name)
68
+ if (metadata = @registry[engine_name])
69
+ metadata
70
+ else
71
+ raise InvalidEngineName, "unknown engine name: #{engine_name}"
72
+ end
73
+ end
74
+
75
+ def exclude_paths
76
+ PathPatterns.new(@config.exclude_paths || []).expanded
77
+ end
78
+ end
79
+ end
80
+ end
@@ -22,6 +22,9 @@ module CC
22
22
  def finished
23
23
  end
24
24
 
25
+ def close
26
+ end
27
+
25
28
  def failed(output)
26
29
  end
27
30
 
@@ -0,0 +1,26 @@
1
+ module CC
2
+ module Analyzer
3
+ class PathPatterns
4
+ def initialize(patterns, root = Dir.pwd)
5
+ @patterns = patterns
6
+ @root = root
7
+ end
8
+
9
+ def expanded
10
+ @expanded ||= expand
11
+ end
12
+
13
+ private
14
+
15
+ def expand
16
+ results = Dir.chdir(@root) do
17
+ @patterns.flat_map do |pattern|
18
+ Dir.glob(pattern)
19
+ end
20
+ end
21
+
22
+ results.sort.uniq
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/cc/analyzer.rb CHANGED
@@ -5,23 +5,31 @@ module CC
5
5
  autoload :Engine, "cc/analyzer/engine"
6
6
  autoload :EngineClient, "cc/analyzer/engine_client"
7
7
  autoload :EngineOutputFilter, "cc/analyzer/engine_output_filter"
8
- autoload :EngineProcess, "cc/analyzer/engine_process"
9
8
  autoload :EngineRegistry, "cc/analyzer/engine_registry"
9
+ autoload :EnginesRunner, "cc/analyzer/engines_runner"
10
10
  autoload :Filesystem, "cc/analyzer/filesystem"
11
11
  autoload :Formatters, "cc/analyzer/formatters"
12
12
  autoload :IssueSorter, "cc/analyzer/issue_sorter"
13
13
  autoload :LocationDescription,"cc/analyzer/location_description"
14
14
  autoload :NullConfig, "cc/analyzer/null_config"
15
+ autoload :PathPatterns, "cc/analyzer/path_patterns"
15
16
  autoload :SourceBuffer, "cc/analyzer/source_buffer"
16
17
  autoload :UnitName, "cc/analyzer/unit_name"
17
18
 
18
19
  class DummyStatsd
19
- def method_missing(*args)
20
+ def method_missing(*)
20
21
  yield if block_given?
21
22
  end
22
23
  end
23
24
 
24
- cattr_accessor :statsd
25
+ class DummyLogger
26
+ def method_missing(*)
27
+ yield if block_given?
28
+ end
29
+ end
30
+
31
+ cattr_accessor :statsd, :logger
25
32
  self.statsd = DummyStatsd.new
33
+ self.logger = DummyLogger.new
26
34
  end
27
35
  end
@@ -1,11 +1,9 @@
1
- require "securerandom"
2
-
3
1
  module CC
4
2
  module CLI
5
3
  class Analyze < Command
6
4
  include CC::Analyzer
7
5
 
8
- def initialize(args = [])
6
+ def initialize(_args = [])
9
7
  super
10
8
 
11
9
  process_args
@@ -13,23 +11,14 @@ module CC
13
11
 
14
12
  def run
15
13
  require_codeclimate_yml
16
- if engines.empty?
17
- fatal("No engines enabled. Add some to your .codeclimate.yml file!")
18
- end
19
-
20
- formatter.started
21
-
22
- engines.each do |engine|
23
- formatter.engine_running(engine) do
24
- engine.run(formatter)
25
- end
26
- end
27
14
 
28
- formatter.finished
29
- end
15
+ runner = EnginesRunner.new(registry, formatter, source_dir, config)
16
+ runner.run
30
17
 
31
- def dev_mode?
32
- !!@dev_mode
18
+ rescue EnginesRunner::InvalidEngineName => ex
19
+ fatal(ex.message)
20
+ rescue EnginesRunner::NoEnabledEngines
21
+ fatal("No enabled engines. Add some to your .codeclimate.yml file!")
33
22
  end
34
23
 
35
24
  private
@@ -47,71 +36,21 @@ module CC
47
36
  fatal(e.message)
48
37
  end
49
38
 
50
- def config
51
- @config ||= if filesystem.exist?(CODECLIMATE_YAML)
52
- config_body = filesystem.read_path(CODECLIMATE_YAML)
53
- config = Config.new(config_body)
54
- else
55
- config = NullConfig.new
56
- end
57
- end
58
-
59
- def engine_registry
60
- @engine_registry ||= EngineRegistry.new
61
- end
62
-
63
- def engine_config(engine_name)
64
- config.engine_config(engine_name).
65
- merge!(exclude_paths: exclude_paths).to_json
66
- end
67
-
68
- def exclude_paths
69
- if config.exclude_paths
70
- filesystem.files_matching(config.exclude_paths)
71
- else
72
- []
73
- end
74
- end
75
-
76
- def engines
77
- @engines ||= config.engine_names.map do |engine_name|
78
- entry = registry_entry(engine_name)
79
- if entry.nil?
80
- fatal("unknown engine name: #{engine_name}")
81
- else
82
- Engine.new(
83
- engine_name,
84
- entry,
85
- path,
86
- engine_config(engine_name),
87
- SecureRandom.uuid
88
- )
89
- end
90
- end.compact
39
+ def registry
40
+ EngineRegistry.new(@dev_mode)
91
41
  end
92
42
 
93
43
  def formatter
94
44
  @formatter ||= Formatters::PlainTextFormatter.new
95
45
  end
96
46
 
97
- def path
98
- ENV['CODE_PATH']
99
- end
100
-
101
- def registry_entry(engine_name)
102
- if @dev_mode
103
- dev_registry_entry(engine_name)
104
- else
105
- engine_registry[engine_name]
106
- end
47
+ def source_dir
48
+ ENV["CODE_PATH"]
107
49
  end
108
50
 
109
- def dev_registry_entry(engine_name)
110
- {
111
- "image"=>"codeclimate/codeclimate-#{engine_name}:latest"
112
- }
51
+ def config
52
+ CC::Yaml.parse(filesystem.read_path(CODECLIMATE_YAML))
113
53
  end
114
-
115
54
  end
116
55
  end
117
56
  end
data/lib/cc/cli.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "active_support"
2
2
  require "active_support/core_ext"
3
3
  require "cc/analyzer"
4
+ require "cc/yaml"
4
5
 
5
6
  module CC
6
7
  module CLI
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codeclimate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code Climate
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-23 00:00:00.000000000 Z
11
+ date: 2015-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -194,6 +194,7 @@ files:
194
194
  - lib/cc/analyzer/engine.rb
195
195
  - lib/cc/analyzer/engine_output_filter.rb
196
196
  - lib/cc/analyzer/engine_registry.rb
197
+ - lib/cc/analyzer/engines_runner.rb
197
198
  - lib/cc/analyzer/filesystem.rb
198
199
  - lib/cc/analyzer/formatters.rb
199
200
  - lib/cc/analyzer/formatters/formatter.rb
@@ -203,6 +204,7 @@ files:
203
204
  - lib/cc/analyzer/issue_sorter.rb
204
205
  - lib/cc/analyzer/location_description.rb
205
206
  - lib/cc/analyzer/null_config.rb
207
+ - lib/cc/analyzer/path_patterns.rb
206
208
  - lib/cc/analyzer/source_buffer.rb
207
209
  - lib/cc/analyzer/unit_name.rb
208
210
  - lib/cc/cli.rb