onceover-metrics 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '019dd3e865c6d0e8d4f75bbe7484dc1d863a8556a4a2e01e4da44df62d0c99a0'
4
+ data.tar.gz: 17767640afff186ea49deb5a0a337c8e43518f2287e1b5da9bab3b1bd69a3a20
5
+ SHA512:
6
+ metadata.gz: 1acfab7b85ee4f1afcf9bbd9f80fafe44848e15fe307bdcda29a92bb2c71578b95b9f368eec87f7677f9753e42b73b19d6f4db1a9091cc9b877878c0a5791d84
7
+ data.tar.gz: b0bbc8058d9ca0cfcd320838734f4790aade725a65bb7e10c70acdc41ac878d17cb87014c2ebc8dafdea189bf7eba47682f65e37b15794a65a06e45cbedbb65e
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in onceover-metrics.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,136 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ onceover-metrics (0.1.0)
5
+ onceover (~> 3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ backticks (1.0.2)
11
+ blockenspiel (0.5.0)
12
+ colored (1.2)
13
+ cri (2.15.3)
14
+ colored (~> 1.2)
15
+ deep_merge (1.2.1)
16
+ diff-lcs (1.3)
17
+ facter (2.5.1)
18
+ faraday (0.13.1)
19
+ multipart-post (>= 1.2, < 3)
20
+ faraday_middleware (0.12.2)
21
+ faraday (>= 0.7.4, < 1.0)
22
+ fast_gettext (1.1.2)
23
+ gettext (3.2.9)
24
+ locale (>= 2.0.5)
25
+ text (>= 1.3.0)
26
+ gettext-setup (0.30)
27
+ fast_gettext (~> 1.1.0)
28
+ gettext (>= 3.0.2)
29
+ locale
30
+ git (1.5.0)
31
+ hiera (3.5.0)
32
+ hocon (1.2.5)
33
+ httpclient (2.8.3)
34
+ json (2.2.0)
35
+ little-plugger (1.1.4)
36
+ locale (2.1.2)
37
+ log4r (1.1.10)
38
+ logging (2.2.2)
39
+ little-plugger (~> 1.1)
40
+ multi_json (~> 1.10)
41
+ metaclass (0.0.4)
42
+ minitar (0.8)
43
+ mocha (1.8.0)
44
+ metaclass (~> 0.0.1)
45
+ multi_json (1.13.1)
46
+ multipart-post (2.0.0)
47
+ onceover (3.12.1)
48
+ backticks (>= 1.0.2)
49
+ colored (~> 1.2)
50
+ cri (>= 2.6)
51
+ deep_merge (>= 1.0.0)
52
+ git
53
+ json (>= 1.8.2)
54
+ logging (>= 2.0.0)
55
+ parallel_tests (>= 2.0.0)
56
+ puppet (>= 3.4.0)
57
+ puppetlabs_spec_helper (>= 0.4.0)
58
+ r10k (>= 2.1.0)
59
+ rake (>= 10.0.0)
60
+ rspec (>= 3.0.0)
61
+ rspec-puppet (>= 2.4.0)
62
+ rspec_junit_formatter (>= 0.2.0)
63
+ table_print (>= 1.0.0)
64
+ versionomy (>= 0.5.0)
65
+ parallel (1.14.0)
66
+ parallel_tests (2.28.0)
67
+ parallel
68
+ pathspec (0.2.1)
69
+ puppet (6.3.0)
70
+ facter (> 2.0.1, < 4)
71
+ fast_gettext (~> 1.1.2)
72
+ hiera (>= 3.2.1, < 4)
73
+ httpclient (~> 2.8)
74
+ locale (~> 2.1)
75
+ multi_json (~> 1.10)
76
+ puppet-resource_api (~> 1.5)
77
+ semantic_puppet (~> 1.0)
78
+ puppet-lint (2.3.6)
79
+ puppet-resource_api (1.8.1)
80
+ hocon (>= 1.0)
81
+ puppet-syntax (2.4.3)
82
+ rake
83
+ puppet_forge (2.2.9)
84
+ faraday (>= 0.9.0, < 0.14.0)
85
+ faraday_middleware (>= 0.9.0, < 0.13.0)
86
+ gettext-setup (~> 0.11)
87
+ minitar
88
+ semantic_puppet (~> 1.0)
89
+ puppetlabs_spec_helper (2.13.1)
90
+ mocha (~> 1.0)
91
+ pathspec (~> 0.2.1)
92
+ puppet-lint (~> 2.0)
93
+ puppet-syntax (~> 2.0)
94
+ rspec-puppet (~> 2.0)
95
+ r10k (3.1.1)
96
+ colored (= 1.2)
97
+ cri (~> 2.15.1)
98
+ gettext-setup (~> 0.24)
99
+ log4r (= 1.1.10)
100
+ multi_json (~> 1.10)
101
+ puppet_forge (~> 2.2.8)
102
+ rake (10.5.0)
103
+ rspec (3.8.0)
104
+ rspec-core (~> 3.8.0)
105
+ rspec-expectations (~> 3.8.0)
106
+ rspec-mocks (~> 3.8.0)
107
+ rspec-core (3.8.0)
108
+ rspec-support (~> 3.8.0)
109
+ rspec-expectations (3.8.2)
110
+ diff-lcs (>= 1.2.0, < 2.0)
111
+ rspec-support (~> 3.8.0)
112
+ rspec-mocks (3.8.0)
113
+ diff-lcs (>= 1.2.0, < 2.0)
114
+ rspec-support (~> 3.8.0)
115
+ rspec-puppet (2.7.3)
116
+ rspec
117
+ rspec-support (3.8.0)
118
+ rspec_junit_formatter (0.4.1)
119
+ rspec-core (>= 2, < 4, != 2.12.0)
120
+ semantic_puppet (1.0.2)
121
+ table_print (1.5.6)
122
+ text (1.3.1)
123
+ versionomy (0.5.0)
124
+ blockenspiel (~> 0.5)
125
+
126
+ PLATFORMS
127
+ ruby
128
+
129
+ DEPENDENCIES
130
+ bundler (~> 1.15)
131
+ onceover-metrics!
132
+ rake (~> 10.0)
133
+ rspec (~> 3.0)
134
+
135
+ BUNDLED WITH
136
+ 1.16.6
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Onceover::Metrics
2
+
3
+ This is an example plugin for [Onceover](https://github.com/dylanratcliffe/onceover), _The gateway drug to automated infrastructure testing with Puppet_
4
+
5
+ ## Installation
6
+
7
+ Onceover detects plugins in all gems named `onceover-*`. Your plugin is then responsible for registering itself and setting up new commands, etc.
8
+
9
+ This example plugin can be installed by adding it to your `Gemfile` or by running the following command:
10
+
11
+ ```shell
12
+ $ gem install onceover-metrics
13
+ ```
14
+
15
+ ## Usage
16
+ Onceover provides plugins with built-in support for help and argument processing. Here's how to run this example:
17
+
18
+ **Built-in help**
19
+
20
+ ```shell
21
+ $ onceover run metrics --help
22
+ ```
23
+
24
+ **Default execution**
25
+
26
+ ```shell
27
+ $ onceover run metrics
28
+ ```
29
+
30
+ **Option processing**
31
+
32
+ ```shell
33
+ $ onceover run metrics --name Wednesday
34
+ INFO -> Hello, Wednesday!
35
+ ```
36
+
37
+ ## Integration highlights
38
+ * [Onceover compatibility definition](https://github.com/declarativesystems/onceover-metrics/blob/master/onceover-metrics.gemspec#L27)
39
+ * [Library self-registration](https://github.com/declarativesystems/onceover-metrics/blob/master/lib/onceover/metrics.rb#L2)
40
+ * [Command definition](https://github.com/declarativesystems/onceover-metrics/blob/master/lib/onceover/metrics/cli.rb#L9)
41
+ * [Command self-registration](https://github.com/declarativesystems/onceover-metrics/blob/master/lib/onceover/metrics/cli.rb#L34)
42
+
43
+
44
+ ## Development
45
+
46
+ Finished (for now) - hopefully this makes writing your own plugins for Onceover easier :)
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/declarativesystems/onceover-metrics.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "onceover/metrics"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ require "onceover/metrics/version"
2
+ require "onceover/metrics/cli"
3
+
4
+ class Onceover
5
+ module Metrics
6
+ end
7
+ end
8
+
9
+
@@ -0,0 +1,33 @@
1
+ # Create a class to hold the new command definition. The class defined should
2
+ # match the file we are contained in.
3
+ require 'onceover/metrics/metrics'
4
+ class Onceover
5
+ module Metrics
6
+ class CLI
7
+
8
+ # Static method defining the new command to be added
9
+ def self.command
10
+ @cmd ||= Cri::Command.define do
11
+ name 'metrics'
12
+ usage 'metrics [--format NAME]'
13
+ summary "Metrics plugin for Onceover"
14
+ description <<-DESCRIPTION
15
+ Output some handy code metrics so you can guage the size of your Puppet code
16
+ DESCRIPTION
17
+
18
+ option nil, :format, 'Format - json, csv or text', :argument => :optional, default: "text"
19
+ flag nil, :detailed, 'Output per-class stats in text mode', :argument => :optional, default: false
20
+
21
+ run do |opts, args, cmd|
22
+ Onceover::Metrics::Metrics.run opts
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ # Register the new command with onceover. The method you add must match your
31
+ # own code
32
+ Onceover::CLI::Run.command.add_command(Onceover::Metrics::CLI.command)
33
+
@@ -0,0 +1,278 @@
1
+ require 'yaml'
2
+ require 'csv'
3
+ require 'deep_merge'
4
+
5
+ class Onceover
6
+ module Metrics
7
+ module Metrics
8
+ PUPPETFILE = "Puppetfile"
9
+ ENVIRONMENT_CONF = "environment.conf"
10
+ TOTALS_KEY = :totals
11
+ PUPPETFILE_KEY = :puppetfile
12
+ SITE_MODULES_KEY = :site_modules
13
+ HIERA_KEY = :hiera
14
+ YAML_KEY = :yaml
15
+ FILES_KEY = :files
16
+ TEMPLATES_KEY = :templates
17
+ LINES_KEY = :lines
18
+ COMMENTS_KEY = :comments
19
+ MANIFESTS_KEY = :manifests
20
+ LIBS_KEY = :libs
21
+ MODULES_KEY = :modules
22
+ CODE_KEY = :code
23
+ HIERA_FILES_KEY = :hiera_yaml_files
24
+ HIERA_KEYS_UNIQUE = :hiera_keys_unique
25
+ HIERA_TOP_LEVEL_KEYS = :top_level_keys
26
+
27
+
28
+ def self.puppetfile
29
+ data = File.readlines(PUPPETFILE)
30
+ mod_count = data.grep(/^\s*mod /).size
31
+
32
+ # inline definition barfs on keyname in variable
33
+ stats = {}
34
+ stats = {
35
+ TOTALS_KEY => {
36
+ MODULES_KEY => mod_count,
37
+ }
38
+ }
39
+
40
+ stats
41
+ end
42
+
43
+ def self.modpath_dirs
44
+ dirs = []
45
+ modulepath_line = File.readlines(ENVIRONMENT_CONF).grep(/^\s*modulepath\s*=/)
46
+ modulepath_value = modulepath_line[0].split(/=/)[1]
47
+ modulepath_value.split(":").reject { |e|
48
+ ! (e =~ /\w+/)
49
+ }.each { |e|
50
+ dirs << e.strip
51
+ }
52
+
53
+ dirs
54
+ end
55
+
56
+ def self.code_stats
57
+ # per module+file stats
58
+ stats = {
59
+ # agregate stats
60
+ TOTALS_KEY => {
61
+ LINES_KEY => 0,
62
+ COMMENTS_KEY => 0,
63
+ CODE_KEY => 0,
64
+ MANIFESTS_KEY => 0,
65
+ TEMPLATES_KEY => 0,
66
+ FILES_KEY => 0,
67
+ LIBS_KEY => 0,
68
+ MODULES_KEY => 0,
69
+ },
70
+
71
+ # per module scan path stats
72
+ CODE_KEY => {
73
+ SITE_MODULES_KEY => {},
74
+ },
75
+ }
76
+
77
+ # each scan path
78
+ modpath_dirs.each { |modpath_dir|
79
+ # ..but a scan path element may not exist...
80
+ if Dir.exist? modpath_dir
81
+ stats[CODE_KEY][SITE_MODULES_KEY][modpath_dir] = {}
82
+
83
+ Dir.entries(modpath_dir).select { |mod_dir|
84
+ File.directory? File.join(modpath_dir, mod_dir) and !(mod_dir =='.' || mod_dir == '..')
85
+ }.each do |mod_dir|
86
+ # each module dir
87
+ stats[CODE_KEY][SITE_MODULES_KEY][modpath_dir][mod_dir] = {
88
+ MANIFESTS_KEY => {},
89
+ }
90
+
91
+
92
+ Dir.chdir "#{modpath_dir}/#{mod_dir}" do
93
+ # each puppet module
94
+
95
+ Dir.chdir "manifests" do
96
+ # each puppet manifest
97
+ Dir["**/*.pp"].each { |pp_file|
98
+ data = File.readlines(pp_file)
99
+ lines = data.size
100
+ comments = data.grep(/^\s*#/).size
101
+ code = lines - comments
102
+ stats[CODE_KEY][SITE_MODULES_KEY][modpath_dir][mod_dir][MANIFESTS_KEY][pp_file] = {
103
+ LINES_KEY => lines,
104
+ COMMENTS_KEY => comments,
105
+ CODE_KEY => code,
106
+ }
107
+
108
+ # update the global total counts
109
+ stats[TOTALS_KEY][LINES_KEY] += lines
110
+ stats[TOTALS_KEY][COMMENTS_KEY] += comments
111
+ stats[TOTALS_KEY][CODE_KEY] += code
112
+ stats[TOTALS_KEY][MANIFESTS_KEY] += 1
113
+ }
114
+ end
115
+
116
+ stats[CODE_KEY][SITE_MODULES_KEY][modpath_dir][mod_dir][TOTALS_KEY] = {
117
+ TEMPLATES_KEY => (Dir["templates/**/*"].count { |file| File.file?(file)}),
118
+ LIBS_KEY => (Dir["lib/**/*"].count { |file| File.file?(file)}),
119
+ FILES_KEY => (Dir["files/**/*"].count { |file| File.file?(file)}),
120
+ }
121
+
122
+ stats[TOTALS_KEY][TEMPLATES_KEY] += stats[CODE_KEY][SITE_MODULES_KEY][modpath_dir][mod_dir][TOTALS_KEY][TEMPLATES_KEY]
123
+ stats[TOTALS_KEY][LIBS_KEY] += stats[CODE_KEY][SITE_MODULES_KEY][modpath_dir][mod_dir][TOTALS_KEY][LIBS_KEY]
124
+ stats[TOTALS_KEY][FILES_KEY] += stats[CODE_KEY][SITE_MODULES_KEY][modpath_dir][mod_dir][TOTALS_KEY][FILES_KEY]
125
+ stats[TOTALS_KEY][MODULES_KEY] += 1
126
+ end
127
+ end
128
+ end
129
+ }
130
+
131
+ stats
132
+
133
+ end
134
+
135
+ # assumes hiera data in /data
136
+ def self.hiera
137
+ stats = {
138
+ HIERA_KEY => {},
139
+ TOTALS_KEY => {},
140
+ HIERA_KEY => {
141
+ YAML_KEY => {},
142
+ },
143
+ }
144
+ total_lines = 0
145
+ yaml_files = Dir["data/**/*.yaml"]
146
+
147
+ # parse each file to count top-level keys
148
+ top_level_keys = []
149
+ yaml_files.each do |yaml_file|
150
+
151
+ # count lines that are whitespace or comments
152
+ lines = File.readlines(yaml_file).reject{|e| e =~ /^\s*(\s*|#.*)$/}.length
153
+
154
+ data = YAML.load_file(yaml_file)
155
+ stats[HIERA_KEY][YAML_KEY][yaml_file] = {
156
+ LINES_KEY => lines,
157
+ }
158
+
159
+ total_lines += lines
160
+ if data
161
+ # there may be files with no keys...
162
+ stats[HIERA_KEY][YAML_KEY][yaml_file][HIERA_TOP_LEVEL_KEYS] = data.keys.length
163
+ top_level_keys << data.keys
164
+ end
165
+
166
+ end
167
+
168
+ stats[TOTALS_KEY] = {
169
+ HIERA_FILES_KEY => yaml_files.length,
170
+ HIERA_KEYS_UNIQUE => top_level_keys.uniq.length,
171
+ LINES_KEY => total_lines,
172
+ }
173
+
174
+ stats
175
+ end
176
+
177
+
178
+ def self.print_table(h, indent="")
179
+ col_width = 50 - indent.length
180
+ h.each do |k,v|
181
+ kf = k.to_s.gsub(/_/, ' ')
182
+ printf("#{indent}%-#{col_width}s %10s\n", kf, v)
183
+ end
184
+ end
185
+
186
+ def self.format_report(stats, detailed)
187
+ if detailed
188
+ puts "----- [Hiera] -----"
189
+ stats[HIERA_KEY][YAML_KEY].each do |yaml_file, data|
190
+ puts yaml_file
191
+ print_table(data, " ")
192
+ end
193
+ puts ""
194
+ end
195
+
196
+ puts "----- [Puppet Code] -----"
197
+ stats[CODE_KEY][SITE_MODULES_KEY].each do |modpath, modpath_data|
198
+ # each path element
199
+ puts "#{modpath}"
200
+
201
+ modpath_data.each do | mod_name, mod_data |
202
+ # each module
203
+ indent = " "
204
+ puts "#{indent}#{mod_name}"
205
+
206
+ indent = " "
207
+ # per-module metrics
208
+ print_table(mod_data[TOTALS_KEY], indent)
209
+
210
+ # per-manifest stats
211
+ print_table({MANIFESTS_KEY => mod_data[MANIFESTS_KEY].length}, indent)
212
+
213
+ if detailed
214
+ mod_data[MANIFESTS_KEY].each { |pp_file, stats|
215
+
216
+ indent = " "
217
+ puts "#{indent}#{pp_file}"
218
+
219
+ indent = " "
220
+ print_table(stats, indent)
221
+ }
222
+ end
223
+ end
224
+ end
225
+ puts ""
226
+
227
+ puts "***** [TOTALS] *****"
228
+ stats[TOTALS_KEY].each do |k,v|
229
+ puts k
230
+ print_table(v, " ")
231
+ end
232
+
233
+
234
+ end
235
+
236
+ def self.run(opts)
237
+ puppetfile = puppetfile()
238
+ code_stats = code_stats()
239
+ hiera = hiera()
240
+
241
+ stats = {
242
+ CODE_KEY => code_stats[CODE_KEY],
243
+ HIERA_KEY => hiera[HIERA_KEY],
244
+ TOTALS_KEY => {
245
+ PUPPETFILE_KEY => puppetfile[TOTALS_KEY],
246
+ CODE_KEY => code_stats[TOTALS_KEY],
247
+ HIERA_KEY => hiera[TOTALS_KEY],
248
+ TOTALS_KEY => {
249
+ LINES_KEY => code_stats[TOTALS_KEY][LINES_KEY] + hiera[TOTALS_KEY][LINES_KEY],
250
+ MODULES_KEY => puppetfile[TOTALS_KEY][MODULES_KEY] + code_stats[TOTALS_KEY][MODULES_KEY]
251
+ }
252
+ },
253
+ }
254
+
255
+ if opts[:format] == "json"
256
+ pretty_json = JSON.pretty_generate(stats)
257
+ puts pretty_json
258
+ elsif opts[:format] == "csv"
259
+
260
+ csv_string = CSV.generate do |csv|
261
+ stats[TOTALS_KEY].each { |k,v|
262
+ csv << [k]
263
+ v.to_a.each {|elem| csv << elem}
264
+ csv << []
265
+ }
266
+ end
267
+
268
+ puts csv_string
269
+ else
270
+ # human mode
271
+ format_report(stats, opts[:detailed])
272
+ end
273
+
274
+ end
275
+ end
276
+ end
277
+ end
278
+
@@ -0,0 +1,5 @@
1
+ class Onceover
2
+ module Metrics
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "onceover/metrics/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "onceover-metrics"
8
+ spec.version = Onceover::Metrics::VERSION
9
+ spec.authors = ["Declarative Systems"]
10
+ spec.email = ["sales@declarativesystems.com"]
11
+ spec.license = "Apache-2.0"
12
+
13
+ spec.summary = %q{Example plugin for onceover}
14
+ spec.homepage = "https://github.com/declarativesystems/onceover-metrics"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.15"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+
27
+ spec.add_runtime_dependency 'onceover', '~> 3'
28
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onceover-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Declarative Systems
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: onceover
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ description:
70
+ email:
71
+ - sales@declarativesystems.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/onceover/metrics.rb
85
+ - lib/onceover/metrics/cli.rb
86
+ - lib/onceover/metrics/metrics.rb
87
+ - lib/onceover/metrics/version.rb
88
+ - onceover-metrics.gemspec
89
+ homepage: https://github.com/declarativesystems/onceover-metrics
90
+ licenses:
91
+ - Apache-2.0
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.7.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Example plugin for onceover
113
+ test_files: []