meta_bookworm 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +161 -0
- data/lib/bookworm/configuration.rb +53 -0
- data/lib/bookworm/crawler.rb +69 -0
- data/lib/bookworm/exceptions.rb +20 -0
- data/lib/bookworm/infer_base_classes.rb +80 -0
- data/lib/bookworm/infer_engine.rb +45 -0
- data/lib/bookworm/keys.rb +77 -0
- data/lib/bookworm/knowledge_base.rb +117 -0
- data/lib/bookworm/load_hack.rb +21 -0
- data/lib/bookworm/report_builder.rb +93 -0
- data/lib/bookworm/reports/AllReferencedRecipes.rb +35 -0
- data/lib/bookworm/reports/AllRoleDescriptions.rb +25 -0
- data/lib/bookworm/reports/CookbookDependencyDot.rb +30 -0
- data/lib/bookworm/reports/DynamicRecipeInclusion.rb +29 -0
- data/lib/bookworm/reports/LeafCookbooks.rb +30 -0
- data/lib/bookworm/reports/LibraryDefinedModulesAndClassConstants.rb +27 -0
- data/lib/bookworm/reports/MissingReferencedRecipes.rb +61 -0
- data/lib/bookworm/reports/NoParsedRuby.rb +46 -0
- data/lib/bookworm/reports/NotReferencedRecipes.rb +37 -0
- data/lib/bookworm/reports/RecipesAssigningConstants.rb +27 -0
- data/lib/bookworm/reports/RoleRecipeEntrypoints.rb +30 -0
- data/lib/bookworm/reports/RoleReferencedRoles.rb +30 -0
- data/lib/bookworm/rules/ExplicitMetadataDepends.rb +22 -0
- data/lib/bookworm/rules/IncludeRecipeDynamic.rb +24 -0
- data/lib/bookworm/rules/IncludeRecipeLiterals.rb +37 -0
- data/lib/bookworm/rules/LibraryDefinedClassConstants.rb +22 -0
- data/lib/bookworm/rules/LibraryDefinedModuleConstants.rb +22 -0
- data/lib/bookworm/rules/NoParsedRuby.rb +30 -0
- data/lib/bookworm/rules/RecipeConstantAssignments.rb +22 -0
- data/lib/bookworm/rules/RoleDescription.rb +22 -0
- data/lib/bookworm/rules/RoleExplicitRoles.rb +29 -0
- data/lib/bookworm/rules/RoleName.rb +22 -0
- data/lib/bookworm/rules/RoleRunList.rb +22 -0
- data/lib/bookworm/rules/RoleRunListRecipes.rb +29 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 74d40cd4a2e5f8abe5e81fb20d6af96989dcafd9ee1af5c88a1fa11b9ddd2500
|
4
|
+
data.tar.gz: 99dddd29f96343f7ebcda55fed4c920ec7b7ba1a738afd937bb2959f3c9d1bb6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 492fe8db8c559b94eaf225beb5db4eb42727fa2fbe1dd2080ffeac92ea9d65446a40f73e15ab6648d23513947fe55a2c678cb652dfb6d5f79e66ba76f3f76909
|
7
|
+
data.tar.gz: 6a0fd3a1b370cfa909d6a4f37e9b6f2f04c738a288f66f7b2cd81e021d4eaf0f65667c77ee21a9143b6523c4ea61a5500a699a5272fc0c2ff13d55337188b477
|
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,161 @@
|
|
1
|
+
fb_bookworm
|
2
|
+
===========
|
3
|
+
|
4
|
+
[![Continuous Integration](https://github.com/facebook/bookworm/workflows/Continuous%20Integration/badge.svg?event=push)](https://github.com/facebook/bookworm/actions?query=workflow%3A%22Continuous+Integration%22)
|
5
|
+
|
6
|
+
Bookworm is a program that gleans context from a Chef/Ruby codebase, which
|
7
|
+
recognizes that Ruby source files in different directories have different
|
8
|
+
semantic meaning to a larger program (ie Chef)
|
9
|
+
|
10
|
+
It currently runs on top of the Chef Workstation Ruby, although there is
|
11
|
+
nothing preventing running bookworm on vanilla Ruby using bundler, etc.
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
|
16
|
+
Bookworm is designed to be installed via Chef cookbook as well as running
|
17
|
+
directly from the cookbook. This assumes you have at least [Chef Workstation
|
18
|
+
20](https://www.chef.io/downloads/tools/workstation) installed. If you run this
|
19
|
+
cookbook in a Chef run, it will also install Bookworm into
|
20
|
+
`/usr/local/lib/bookworm` (with a shell script launcher at
|
21
|
+
`/usr/local/bin/bookworm`)
|
22
|
+
|
23
|
+
```
|
24
|
+
# If you're running directly from the cookbook
|
25
|
+
cd files/default/bookworm
|
26
|
+
|
27
|
+
# Get the program flags
|
28
|
+
$ ./bookworm.rb -h
|
29
|
+
Usage: bookworm.rb [options]
|
30
|
+
--report CLASS Give the (class) name of the report you'd like
|
31
|
+
--list-reports Get the (class) names of available reports
|
32
|
+
--list-rules Get the (class) names of available inference rules
|
33
|
+
|
34
|
+
Debugging options:
|
35
|
+
--irb-config-step Open IRB REPL after loading configuration
|
36
|
+
--irb-crawl-step Open IRB REPL after crawler has run
|
37
|
+
--irb-infer-step Open IRB REPL after inference has run
|
38
|
+
--irb-report-step Open IRB REPL after report is generated
|
39
|
+
|
40
|
+
# Get a list of reports that can be run
|
41
|
+
$ bookworm --list-reports
|
42
|
+
AllReferencedRecipes Determines all recipes that are directly referenced by all roles
|
43
|
+
AllRoleDescriptions Basic report to show extraction of description from roles
|
44
|
+
...
|
45
|
+
|
46
|
+
# Run a report
|
47
|
+
$ bookworm --report AllRoleDescriptions
|
48
|
+
```
|
49
|
+
|
50
|
+
Configuring Bookworm
|
51
|
+
--------------------
|
52
|
+
|
53
|
+
Bookworm currently checks 2 different places for configuration,
|
54
|
+
`~/.bookworm.yml` and `/usr/local/etc/bookworm/configuration.rb`
|
55
|
+
|
56
|
+
### ~/.bookworm.yml
|
57
|
+
|
58
|
+
A typical bookworm YAML configuration would look something like this:
|
59
|
+
|
60
|
+
```yaml
|
61
|
+
# A basic chef layout
|
62
|
+
source_dirs:
|
63
|
+
cookbook_dirs:
|
64
|
+
- /absolute/path/to/cookbooks
|
65
|
+
- /absolute/path/to/some/other/cookbooks
|
66
|
+
role_dirs:
|
67
|
+
- /absolute/path/to/role/files
|
68
|
+
debug: false # Additional debug information
|
69
|
+
```
|
70
|
+
|
71
|
+
### /usr/local/etc/bookworm/configuration.rb
|
72
|
+
|
73
|
+
If you need to programmatically determine what your `source_dirs` are (ie if
|
74
|
+
you don't know what directories Bookworm will be running on ahead of time), you
|
75
|
+
can use `/usr/local/etc/bookworm/configuration.rb` to set `DEFAULT_SOURCE_DIRS`
|
76
|
+
and `DEFAULT_DEBUG`. This might be overkill for your needs, and
|
77
|
+
`~/.bookworm.yml` is probably your best bet.
|
78
|
+
|
79
|
+
Glossary
|
80
|
+
--------
|
81
|
+
|
82
|
+
### Bookworm key
|
83
|
+
|
84
|
+
A bookworm key is a type of file, and all the information that should be
|
85
|
+
necessary to handle that file throughout the Bookworm pipeline. By
|
86
|
+
encapsulating the "what" and the "how" of a file here, adding new types of
|
87
|
+
files to Bookworm shouldn't involve more than adding a new key.
|
88
|
+
|
89
|
+
Of note, a 'metakey' isn't a file - it could be a concept or a group of files.
|
90
|
+
This is particularly useful with Chef code, since the notion of a 'cookbook' is
|
91
|
+
several things, and rather than doing backflips trying to match information to
|
92
|
+
specific files within a cookbook directory, it's much simpler to say "this
|
93
|
+
cookbook provides this resource/class/attribute."
|
94
|
+
|
95
|
+
### Crawler
|
96
|
+
|
97
|
+
The crawler is what transforms whatever the key finds into a navigable Ruby
|
98
|
+
representation of the file (for Ruby files, this would be an AST representation
|
99
|
+
of the source file as generated by the rubocop-ast gem).
|
100
|
+
|
101
|
+
### KnowledgeBase
|
102
|
+
|
103
|
+
The KnowledgeBase is the singleton object that holds all Ruby representations
|
104
|
+
of the crawled files, as well as all information derived by the Bookworm rules.
|
105
|
+
|
106
|
+
### Rule
|
107
|
+
|
108
|
+
A 'rule' in Bookworm is an auto-generated class that takes the crawled
|
109
|
+
representation of a file and extracts specific information about it. If you
|
110
|
+
want to know about 3 unrelated patterns in a source file, consider using (or
|
111
|
+
writing, if they don't exist yet) 3 rules. Small rules encourage re-use in
|
112
|
+
reports, and since rules can reference information about a file derived from
|
113
|
+
*other* rules, this is a good place to do any heavy lifting when figuring out
|
114
|
+
what's going on with your keys.
|
115
|
+
|
116
|
+
### InferEngine
|
117
|
+
|
118
|
+
The InferEngine runs the necessary rules against each key that was crawled.
|
119
|
+
|
120
|
+
### Reports
|
121
|
+
|
122
|
+
The report takes the information that you've extracted from the codebase, and
|
123
|
+
pulls it together into a human or machine-readable representation. While this
|
124
|
+
is a good place to store specific logic for representation, it doesn't hurt to
|
125
|
+
ask 'how do I delegate the hard work to rules' - think of reports as the glue
|
126
|
+
that pulls together a bunch of derived facts into something nice that you can
|
127
|
+
hand to your boss ;-)
|
128
|
+
|
129
|
+
Guiding design principles
|
130
|
+
-------------------------
|
131
|
+
|
132
|
+
- Easy and fast iteration
|
133
|
+
- Writing a Bookworm rule or report should be as easy as grabbing bash and grep
|
134
|
+
- Profiling and debugging hooks should be in strategic spots to make it easy to debug
|
135
|
+
- Each new rule or report unlocks the potential for new discoveries
|
136
|
+
- It should be easy to use existing work, so that building reports is just a matter of grabbing existing rules (or other reports)
|
137
|
+
- Zero-cost additions, so you only analyze/execute the report (and supporting
|
138
|
+
rules) that are needed
|
139
|
+
- A slow rule shouldn't matter unless you're actually using it
|
140
|
+
- Accept that copy-paste is going to happen with rules and reports, and make that simple.
|
141
|
+
|
142
|
+
Implementation notes - Why Rubocop for AST generation
|
143
|
+
-----------------------------------------------------
|
144
|
+
|
145
|
+
Because we wanted to use something that was already in Chef Workstation, the
|
146
|
+
two choices were Ripper or Parser a la RuboCop (ruby_parser uses racc which has
|
147
|
+
a C extension, but no sexp pattern matcher that I know of).
|
148
|
+
|
149
|
+
Ripper is fast, but the sexp output is kind of nasty, and cleaning that up
|
150
|
+
could be a big timesuck. Since the larger Ruby/Chef community has a bit more
|
151
|
+
familiarity with Parser/RuboCop's node pattern matching, it'd be better to stay
|
152
|
+
with that for now (there's no reason this couldn't be migrated later with
|
153
|
+
helpers to translate patterns). Work could also be done to speed up RuboCop
|
154
|
+
(ractor and async support could go a long way here).
|
155
|
+
This repo contains attribute-driven-API cookbooks maintained by Facebook. It's
|
156
|
+
a large chunk of what we refer to as our "core cookbooks."
|
157
|
+
|
158
|
+
License
|
159
|
+
-------
|
160
|
+
|
161
|
+
See the LICENSE file in this directory
|
@@ -0,0 +1,53 @@
|
|
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
|
+
#
|
16
|
+
module Bookworm
|
17
|
+
class Configuration
|
18
|
+
attr :source_dirs, :debug
|
19
|
+
attr_reader :system_contrib_dir
|
20
|
+
|
21
|
+
# Allow for programmatic configuration override
|
22
|
+
SYSTEM_CONFIGURATION_RUBY_FILE = '/usr/local/etc/bookworm/configuration.rb'.freeze
|
23
|
+
SYSTEM_CONTRIB_DIR = '/usr/local/etc/bookworm/contrib'.freeze
|
24
|
+
|
25
|
+
def initialize
|
26
|
+
begin
|
27
|
+
load SYSTEM_CONFIGURATION_RUBY_FILE
|
28
|
+
rescue LoadError
|
29
|
+
# puts "No configuration found at #{SYSTEM_CONFIGURATION_RUBY_FILE}"
|
30
|
+
config = {}
|
31
|
+
end
|
32
|
+
begin
|
33
|
+
config = YAML.load_file "#{Dir.home}/.bookworm.yml"
|
34
|
+
rescue StandardError
|
35
|
+
config = {}
|
36
|
+
end
|
37
|
+
|
38
|
+
@system_contrib_dir = SYSTEM_CONTRIB_DIR
|
39
|
+
if Bookworm::Configuration.const_defined?(:DEFAULT_SOURCE_DIRS)
|
40
|
+
@source_dirs ||= DEFAULT_SOURCE_DIRS
|
41
|
+
end
|
42
|
+
if Bookworm::Configuration.const_defined?(:DEFAULT_DEBUG)
|
43
|
+
@debug ||= DEFAULT_DEBUG
|
44
|
+
end
|
45
|
+
|
46
|
+
@system_contrib_dir = config['system_contrib_dir'] if config['system_contrib_dir']
|
47
|
+
@source_dirs = config['source_dirs'] if config['source_dirs']
|
48
|
+
@debug = config['debug'] if config['debug']
|
49
|
+
|
50
|
+
@source_dirs ||= []
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,69 @@
|
|
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
|
+
#
|
16
|
+
require 'rubocop'
|
17
|
+
require 'parser/current'
|
18
|
+
|
19
|
+
# TODO(dcrosby) Bookworm key should determine which AST parser is chosen. This
|
20
|
+
# would allow multiple AST parsers (ie ripper) and a way of ingesting non-Ruby
|
21
|
+
# files like JSON/YAML
|
22
|
+
|
23
|
+
module Bookworm
|
24
|
+
class Crawler
|
25
|
+
attr_reader :processed_files
|
26
|
+
|
27
|
+
def initialize(config, keys: [])
|
28
|
+
@config = config
|
29
|
+
@intake_queue = {}
|
30
|
+
@processed_files = {}
|
31
|
+
|
32
|
+
# TODO(dcrosby) add messages to verbose mode
|
33
|
+
keys.each do |key|
|
34
|
+
generate_file_queue(key)
|
35
|
+
process_paths(key)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def generate_file_queue(key)
|
42
|
+
v = BOOKWORM_KEYS[key]
|
43
|
+
@intake_queue[key] = @config.source_dirs[v['source_dirs']].
|
44
|
+
map { |d| Dir.glob("#{d}/#{v['glob_pattern']}") }.flatten
|
45
|
+
end
|
46
|
+
|
47
|
+
def process_paths(key)
|
48
|
+
queue = @intake_queue[key]
|
49
|
+
processed_files = {}
|
50
|
+
until queue.empty?
|
51
|
+
path = queue.pop
|
52
|
+
processed_files[path] = generate_ast(File.read(path))
|
53
|
+
end
|
54
|
+
@processed_files[key] = processed_files
|
55
|
+
end
|
56
|
+
|
57
|
+
# In order to keep rules from barfing on a nil value (when no AST is
|
58
|
+
# generated at all from eg. an empty source code file), we supply a
|
59
|
+
# single node called bookworm_found_nil. It's a magic value that is
|
60
|
+
# 'unique enough' for our purposes
|
61
|
+
EMPTY_RUBOCOP_AST = ::RuboCop::AST::Node.new('bookworm_found_nil').freeze
|
62
|
+
def generate_rubocop_ast(code)
|
63
|
+
::RuboCop::ProcessedSource.new(code, RUBY_VERSION.to_f)&.ast ||
|
64
|
+
EMPTY_RUBOCOP_AST
|
65
|
+
end
|
66
|
+
|
67
|
+
alias generate_ast generate_rubocop_ast
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,20 @@
|
|
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
|
+
#
|
16
|
+
module Bookworm
|
17
|
+
# ClassLoadError is where Bookworm tries to autogenerate a class from a file
|
18
|
+
# and cannot
|
19
|
+
class ClassLoadError < RuntimeError; end
|
20
|
+
end
|
@@ -0,0 +1,80 @@
|
|
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
|
+
#
|
16
|
+
require 'rubocop'
|
17
|
+
require 'pathname'
|
18
|
+
|
19
|
+
module Bookworm
|
20
|
+
class InferRule
|
21
|
+
class << self
|
22
|
+
{
|
23
|
+
'description' => '',
|
24
|
+
'keys' => [],
|
25
|
+
}.each do |attribute, default_value|
|
26
|
+
instance_variable_set("@#{attribute}".to_sym, default_value)
|
27
|
+
define_method(attribute.to_sym) do |val = nil|
|
28
|
+
instance_variable_set("@#{attribute}", val) unless val.nil?
|
29
|
+
instance_variable_get("@#{attribute}")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
extend RuboCop::NodePattern::Macros
|
35
|
+
def initialize(metadata)
|
36
|
+
@metadata = metadata
|
37
|
+
output
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_a
|
41
|
+
[]
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_h
|
45
|
+
{}
|
46
|
+
end
|
47
|
+
|
48
|
+
def default_output
|
49
|
+
:to_a
|
50
|
+
end
|
51
|
+
|
52
|
+
def output
|
53
|
+
send(default_output)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Initializing constant for Bookworm::InferRules
|
58
|
+
module InferRules; end
|
59
|
+
|
60
|
+
def self.load_rule_class(name, dir: '')
|
61
|
+
f = File.read "#{dir}/#{name.to_sym}.rb"
|
62
|
+
::Bookworm::InferRules.const_set(name.to_sym, ::Class.new(::Bookworm::InferRule))
|
63
|
+
::Bookworm::InferRules.const_get(name.to_sym).class_eval(f)
|
64
|
+
rescue StandardError
|
65
|
+
raise Bookworm::ClassLoadError
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.load_rules_dir(dir)
|
69
|
+
files = Dir.glob("#{dir}/*.rb")
|
70
|
+
files.each do |f|
|
71
|
+
name = Pathname(f).basename.to_s.gsub('.rb', '')
|
72
|
+
begin
|
73
|
+
Bookworm.load_rule_class name, :dir => dir
|
74
|
+
rescue Bookworm::ClassLoadError
|
75
|
+
puts "Unable to load rule #{f}"
|
76
|
+
exit(false)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,45 @@
|
|
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
|
+
#
|
16
|
+
require 'bookworm/knowledge_base'
|
17
|
+
require 'bookworm/infer_base_classes'
|
18
|
+
|
19
|
+
module Bookworm
|
20
|
+
# The InferEngine class takes a KnowledgeBase object, and then runs the given
|
21
|
+
# rules against the files within each bookworm key in the KnowledgeBase that
|
22
|
+
# the rule uses.
|
23
|
+
class InferEngine
|
24
|
+
def initialize(knowledge_base, rules = [])
|
25
|
+
@kb = knowledge_base
|
26
|
+
|
27
|
+
rules.each do |rule|
|
28
|
+
process_rule(rule)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def process_rule(rule)
|
33
|
+
klass = Bookworm::InferRules.const_get(rule)
|
34
|
+
klass.keys.each do |key|
|
35
|
+
@kb[key].each do |name, metadata|
|
36
|
+
@kb[key][name][rule] = klass.new(metadata).output
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def knowledge_base
|
42
|
+
@kb
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|