fb_bookworm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/README.md +161 -0
  4. data/lib/bookworm/configuration.rb +53 -0
  5. data/lib/bookworm/crawler.rb +69 -0
  6. data/lib/bookworm/exceptions.rb +20 -0
  7. data/lib/bookworm/infer_base_classes.rb +80 -0
  8. data/lib/bookworm/infer_engine.rb +45 -0
  9. data/lib/bookworm/keys.rb +77 -0
  10. data/lib/bookworm/knowledge_base.rb +117 -0
  11. data/lib/bookworm/load_hack.rb +21 -0
  12. data/lib/bookworm/report_builder.rb +93 -0
  13. data/lib/bookworm/reports/AllReferencedRecipes.rb +35 -0
  14. data/lib/bookworm/reports/AllRoleDescriptions.rb +25 -0
  15. data/lib/bookworm/reports/CookbookDependencyDot.rb +30 -0
  16. data/lib/bookworm/reports/DynamicRecipeInclusion.rb +29 -0
  17. data/lib/bookworm/reports/LeafCookbooks.rb +30 -0
  18. data/lib/bookworm/reports/LibraryDefinedModulesAndClassConstants.rb +27 -0
  19. data/lib/bookworm/reports/MissingReferencedRecipes.rb +61 -0
  20. data/lib/bookworm/reports/NoParsedRuby.rb +46 -0
  21. data/lib/bookworm/reports/NotReferencedRecipes.rb +37 -0
  22. data/lib/bookworm/reports/RecipesAssigningConstants.rb +27 -0
  23. data/lib/bookworm/reports/RoleRecipeEntrypoints.rb +30 -0
  24. data/lib/bookworm/reports/RoleReferencedRoles.rb +30 -0
  25. data/lib/bookworm/rules/ExplicitMetadataDepends.rb +22 -0
  26. data/lib/bookworm/rules/IncludeRecipeDynamic.rb +24 -0
  27. data/lib/bookworm/rules/IncludeRecipeLiterals.rb +37 -0
  28. data/lib/bookworm/rules/LibraryDefinedClassConstants.rb +22 -0
  29. data/lib/bookworm/rules/LibraryDefinedModuleConstants.rb +22 -0
  30. data/lib/bookworm/rules/NoParsedRuby.rb +30 -0
  31. data/lib/bookworm/rules/RecipeConstantAssignments.rb +22 -0
  32. data/lib/bookworm/rules/RoleDescription.rb +22 -0
  33. data/lib/bookworm/rules/RoleExplicitRoles.rb +29 -0
  34. data/lib/bookworm/rules/RoleName.rb +22 -0
  35. data/lib/bookworm/rules/RoleRunList.rb +22 -0
  36. data/lib/bookworm/rules/RoleRunListRecipes.rb +29 -0
  37. metadata +93 -0
@@ -0,0 +1,30 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Determines all recipes that are directly referenced by all roles'
16
+ needs_rules ['RoleRunListRecipes']
17
+
18
+ def to_a
19
+ buffer = Set.new
20
+ @kb.roles.each do |_, metadata|
21
+ metadata['RoleRunListRecipes'].each do |recipe|
22
+ buffer << recipe
23
+ end
24
+ end
25
+ buffer.sort.to_a
26
+ end
27
+
28
+ def output
29
+ to_a
30
+ end
@@ -0,0 +1,30 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Determine roles which are directly referenced by other roles'
16
+ needs_rules ['RoleExplicitRoles']
17
+
18
+ def to_a
19
+ buffer = Set.new
20
+ @kb.roles.each do |_, metadata|
21
+ metadata['RoleExplicitRoles'].each do |role|
22
+ buffer << role
23
+ end
24
+ end
25
+ buffer.sort.to_a
26
+ end
27
+
28
+ def output
29
+ to_a
30
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Extract depends usage from a cookbook\'s metadata.rb'
16
+ keys ['metadatarb']
17
+
18
+ def_node_search :explicit_depends, '`(send nil? :depends (str $_))'
19
+
20
+ def to_a
21
+ explicit_depends(@metadata['ast']).to_a.uniq
22
+ end
@@ -0,0 +1,24 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Extracts recipes that do not use include_recipe with a string literal'
16
+ keys ['recipe']
17
+
18
+ def_node_search :include_recipe_dynamic, '`(send nil? :include_recipe $_)'
19
+
20
+ def output
21
+ include_recipe_dynamic(@metadata['ast']).any? do |x|
22
+ !(x.is_a?(RuboCop::AST::StrNode) && x.str_type?)
23
+ end
24
+ end
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Extracts recipes that are used by include_recipe with string literals'
16
+ keys ['recipe']
17
+
18
+ def_node_search :include_recipe_string_literals, '`(send nil? :include_recipe (str $_))'
19
+
20
+ def to_a
21
+ arr = []
22
+ include_recipe_string_literals(@metadata['ast']).each do |x|
23
+ arr << x
24
+ end
25
+ return [] if arr.empty?
26
+ arr.map! do |x|
27
+ if x.start_with?('::')
28
+ "#{@metadata['cookbook']}#{x}"
29
+ elsif !x.include?('::')
30
+ "#{x}::default"
31
+ else
32
+ x
33
+ end
34
+ end
35
+ arr.uniq!
36
+ arr.sort!
37
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Get all class constants - it does *not* qualify the namespace'
16
+ keys ['library']
17
+
18
+ def_node_search :defined_class_constants, '`(class (const nil? $_) ...)'
19
+
20
+ def to_a
21
+ defined_class_constants(@metadata['ast']).to_a.uniq
22
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Get all module constants - it does *not* qualify the namespace'
16
+ keys ['library']
17
+
18
+ def_node_search :defined_module_constants, '`(module (const nil? $_) ...)'
19
+
20
+ def to_a
21
+ defined_module_constants(@metadata['ast']).to_a.uniq
22
+ end
@@ -0,0 +1,30 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Ruby files which are empty, comments-only, ' +
16
+ 'or unparseable by RuboCop'
17
+ keys %w{
18
+ attribute
19
+ library
20
+ metadatarb
21
+ provider
22
+ recipe
23
+ resource
24
+ role
25
+ }
26
+
27
+ # See note in crawler.rb about EMPTY_RUBOCOP_AST constant
28
+ def output
29
+ @metadata['ast'] == Bookworm::Crawler::EMPTY_RUBOCOP_AST
30
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Extract constants that are defined in recipes'
16
+ keys ['recipe']
17
+
18
+ def_node_search :constants_assigned, '`(casgn nil? $_ ...)'
19
+
20
+ def to_a
21
+ constants_assigned(@metadata['ast']).to_a.uniq.sort
22
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Scrapes description from role file'
16
+ keys ['role']
17
+
18
+ def_node_matcher :role_description, '`(send nil? :description (str $_))'
19
+
20
+ def output
21
+ role_description @metadata['ast']
22
+ end
@@ -0,0 +1,29 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Scrapes explicit roles in a role files run list'
16
+ keys ['role']
17
+
18
+ def_node_matcher :role_run_list, '`(send nil? :run_list (str $_)*)'
19
+
20
+ def output
21
+ arr = role_run_list @metadata['ast']
22
+ roles = []
23
+ arr&.each do |item|
24
+ roles << item if item.start_with? 'role['
25
+ end
26
+ # TODO(dcrosby) better regex here
27
+ roles.map! { |x| x.gsub(/^role\[/, '') }
28
+ roles.map { |x| x.gsub(/\]$/, '') }
29
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Scrapes name from role file'
16
+ keys ['role']
17
+
18
+ def_node_matcher :role_name, '`(send nil? :name (str $_))'
19
+
20
+ def output
21
+ role_name @metadata['ast']
22
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Scrapes run list from role file'
16
+ keys ['role']
17
+
18
+ def_node_matcher :role_run_list, '`(send nil? :run_list (str $_)*)'
19
+
20
+ def output
21
+ role_run_list @metadata['ast']
22
+ end
@@ -0,0 +1,29 @@
1
+ # Copyright (c) 2022-present, Meta Platforms, Inc. and affiliates
2
+ # All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ description 'Scrapes run list recipes from role file'
16
+ keys ['role']
17
+
18
+ def_node_matcher :role_run_list, '`(send nil? :run_list (str $_)*)'
19
+
20
+ def output
21
+ arr = role_run_list @metadata['ast']
22
+ recipes = []
23
+ arr&.each do |item|
24
+ recipes << item unless item.start_with? 'role['
25
+ end
26
+
27
+ recipes.map! { |x| x.start_with?('recipe[') ? x.match(/recipe\[(.*)\]/)[1] : x }
28
+ recipes.map { |x| x.include?('::') ? x : "#{x}::default" }
29
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fb_bookworm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Crosby
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.25'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.25'
27
+ description: Program to build context around Chef cookbook code
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files:
32
+ - README.md
33
+ - LICENSE
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - lib/bookworm/configuration.rb
38
+ - lib/bookworm/crawler.rb
39
+ - lib/bookworm/exceptions.rb
40
+ - lib/bookworm/infer_base_classes.rb
41
+ - lib/bookworm/infer_engine.rb
42
+ - lib/bookworm/keys.rb
43
+ - lib/bookworm/knowledge_base.rb
44
+ - lib/bookworm/load_hack.rb
45
+ - lib/bookworm/report_builder.rb
46
+ - lib/bookworm/reports/AllReferencedRecipes.rb
47
+ - lib/bookworm/reports/AllRoleDescriptions.rb
48
+ - lib/bookworm/reports/CookbookDependencyDot.rb
49
+ - lib/bookworm/reports/DynamicRecipeInclusion.rb
50
+ - lib/bookworm/reports/LeafCookbooks.rb
51
+ - lib/bookworm/reports/LibraryDefinedModulesAndClassConstants.rb
52
+ - lib/bookworm/reports/MissingReferencedRecipes.rb
53
+ - lib/bookworm/reports/NoParsedRuby.rb
54
+ - lib/bookworm/reports/NotReferencedRecipes.rb
55
+ - lib/bookworm/reports/RecipesAssigningConstants.rb
56
+ - lib/bookworm/reports/RoleRecipeEntrypoints.rb
57
+ - lib/bookworm/reports/RoleReferencedRoles.rb
58
+ - lib/bookworm/rules/ExplicitMetadataDepends.rb
59
+ - lib/bookworm/rules/IncludeRecipeDynamic.rb
60
+ - lib/bookworm/rules/IncludeRecipeLiterals.rb
61
+ - lib/bookworm/rules/LibraryDefinedClassConstants.rb
62
+ - lib/bookworm/rules/LibraryDefinedModuleConstants.rb
63
+ - lib/bookworm/rules/NoParsedRuby.rb
64
+ - lib/bookworm/rules/RecipeConstantAssignments.rb
65
+ - lib/bookworm/rules/RoleDescription.rb
66
+ - lib/bookworm/rules/RoleExplicitRoles.rb
67
+ - lib/bookworm/rules/RoleName.rb
68
+ - lib/bookworm/rules/RoleRunList.rb
69
+ - lib/bookworm/rules/RoleRunListRecipes.rb
70
+ homepage: https://github.com/facebook/bookworm
71
+ licenses:
72
+ - Apache-2.0
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.5.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.4.10
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: fb_bookworm
93
+ test_files: []