polyglot-piranha 0.3.34__cp312-cp312-macosx_10_13_universal2.whl
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.
- LICENSE +202 -0
- NOTICE +5 -0
- polyglot_piranha/__init__.py +5 -0
- polyglot_piranha/__init__.pyi +326 -0
- polyglot_piranha/polyglot_piranha.cpython-312-darwin.so +0 -0
- polyglot_piranha/py.typed +0 -0
- polyglot_piranha-0.3.34.dist-info/METADATA +125 -0
- polyglot_piranha-0.3.34.dist-info/RECORD +11 -0
- polyglot_piranha-0.3.34.dist-info/WHEEL +6 -0
- polyglot_piranha-0.3.34.dist-info/licenses/LICENSE +202 -0
- polyglot_piranha-0.3.34.dist-info/licenses/NOTICE +5 -0
LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner].
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
NOTICE
ADDED
@@ -0,0 +1,326 @@
|
|
1
|
+
# Copyright (c) 2023 Uber Technologies, Inc.
|
2
|
+
#
|
3
|
+
# <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
|
4
|
+
# except in compliance with the License. You may obtain a copy of the License at
|
5
|
+
# <p>http://www.apache.org/licenses/LICENSE-2.0
|
6
|
+
#
|
7
|
+
# <p>Unless required by applicable law or agreed to in writing, software distributed under the
|
8
|
+
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
9
|
+
# express or implied. See the License for the specific language governing permissions and
|
10
|
+
# limitations under the License.
|
11
|
+
|
12
|
+
from __future__ import annotations
|
13
|
+
from typing import List, Optional, Literal
|
14
|
+
|
15
|
+
# Languages that Piranha supports (see ./src/models/language.rs)
|
16
|
+
PiranhaLanguage = Literal["java", "kt", "kotlin", "go", "python", "swift", "typescript", "tsx", "thrift", "strings", "scm", "scala", "ruby", "yaml", "yml"]
|
17
|
+
|
18
|
+
|
19
|
+
def execute_piranha(piranha_argument: PiranhaArguments) -> list[PiranhaOutputSummary]:
|
20
|
+
"""
|
21
|
+
Executes piranha for the given `piranha_arguments` and returns `PiranhaOutputSummary` for each file analyzed by Piranha
|
22
|
+
Parameters
|
23
|
+
------------
|
24
|
+
piranha_arguments: Piranha Arguments
|
25
|
+
Configurations for piranha
|
26
|
+
Returns
|
27
|
+
------------
|
28
|
+
List of `PiranhaOutPutSummary`
|
29
|
+
"""
|
30
|
+
...
|
31
|
+
|
32
|
+
class PiranhaArguments:
|
33
|
+
"""
|
34
|
+
A class to capture Piranha's configurations
|
35
|
+
"""
|
36
|
+
|
37
|
+
def __init__(
|
38
|
+
self,
|
39
|
+
language: PiranhaLanguage,
|
40
|
+
paths_to_codebase: Optional[List[str]] = None,
|
41
|
+
include: Optional[List[str]] = None,
|
42
|
+
exclude: Optional[List[str]] = None,
|
43
|
+
substitutions: Optional[dict[str, str]] = None,
|
44
|
+
path_to_configurations: Optional[str] = None,
|
45
|
+
rule_graph: Optional[RuleGraph] = None,
|
46
|
+
code_snippet: Optional[str] = None,
|
47
|
+
dry_run: Optional[bool] = None,
|
48
|
+
cleanup_comments: Optional[bool] = None,
|
49
|
+
cleanup_comments_buffer: Optional[int] = None,
|
50
|
+
number_of_ancestors_in_parent_scope: Optional[int] = None,
|
51
|
+
delete_consecutive_new_lines: Optional[bool] = None,
|
52
|
+
global_tag_prefix: Optional[str] = "GLOBAL_TAG",
|
53
|
+
delete_file_if_empty: Optional[bool] = None,
|
54
|
+
path_to_output: Optional[str] = None,
|
55
|
+
allow_dirty_ast: Optional[bool] = None,
|
56
|
+
should_validate: Optional[bool] = None,
|
57
|
+
experiment_dyn: Optional[bool] = None,
|
58
|
+
path_to_custom_builtin_rules: Optional[str] = None,
|
59
|
+
):
|
60
|
+
"""
|
61
|
+
Constructs `PiranhaArguments`
|
62
|
+
|
63
|
+
Parameters
|
64
|
+
------------
|
65
|
+
language: PiranhaLanguage
|
66
|
+
the target language
|
67
|
+
paths_to_codebase: List[str]
|
68
|
+
Paths to source code folder or file
|
69
|
+
keyword arguments: _
|
70
|
+
substitutions (dict): Substitutions to instantiate the initial set of rules
|
71
|
+
path_to_configurations (str): Directory containing the configuration files - `piranha_arguments.toml`, `rules.toml`, and `edges.toml`
|
72
|
+
rule_graph (RuleGraph): The rule graph constructed via RuleGraph DSL
|
73
|
+
code_snippet (str): The input code snippet to transform
|
74
|
+
dry_run (bool): Disables in-place rewriting of code
|
75
|
+
cleanup_comments (bool): Enables deletion of associated comments
|
76
|
+
cleanup_comments_buffer (int): The number of lines to consider for cleaning up the comments
|
77
|
+
number_of_ancestors_in_parent_scope (int): The number of ancestors considered when PARENT rules
|
78
|
+
delete_consecutive_new_lines (bool): Replaces consecutive \ns with a \n
|
79
|
+
global_tag_prefix (str): the prefix for global tags
|
80
|
+
delete_file_if_empty (bool): User option that determines whether an empty file will be deleted
|
81
|
+
path_to_output (str): Path to the output json file
|
82
|
+
allow_dirty_ast (bool): Allows syntax errors in the input source code
|
83
|
+
path_to_custom_builtin_rules (str): If provided, the default built-in rules will be ignored
|
84
|
+
"""
|
85
|
+
...
|
86
|
+
|
87
|
+
class PiranhaOutputSummary:
|
88
|
+
"""
|
89
|
+
A class to represent Piranha's output
|
90
|
+
|
91
|
+
Attributes
|
92
|
+
----------
|
93
|
+
path: path to the file
|
94
|
+
content: content of the file after all the rewrites
|
95
|
+
matches: All the occurrences of "match-only" rules
|
96
|
+
rewrites: All the applied edits
|
97
|
+
"""
|
98
|
+
|
99
|
+
path: str
|
100
|
+
"path to the file"
|
101
|
+
|
102
|
+
original_content: str
|
103
|
+
"Original content of the file before any rewrites"
|
104
|
+
|
105
|
+
content: str
|
106
|
+
"Final content of the file after all the rewrites"
|
107
|
+
|
108
|
+
matches: list[tuple[str, Match]]
|
109
|
+
'All the occurrences of "match-only" rules'
|
110
|
+
|
111
|
+
rewrites: list[Edit]
|
112
|
+
"All the applied edits"
|
113
|
+
|
114
|
+
class Edit:
|
115
|
+
"""
|
116
|
+
A class to represent an edit performed by Piranha
|
117
|
+
|
118
|
+
Attributes
|
119
|
+
----------
|
120
|
+
p_match: The match representing the target site of the edit
|
121
|
+
replacement_string: The string to replace the substring encompassed by the match
|
122
|
+
matched_rule: The rule used for creating this match-replace
|
123
|
+
"""
|
124
|
+
|
125
|
+
p_match: Match
|
126
|
+
"The match representing the target site of the edit"
|
127
|
+
|
128
|
+
matched_rule: str
|
129
|
+
"The rule used for creating this match-replace"
|
130
|
+
|
131
|
+
replacement_string: str
|
132
|
+
"The string to replace the substring encompassed by the match"
|
133
|
+
|
134
|
+
class Match:
|
135
|
+
"""
|
136
|
+
A class to represent a match
|
137
|
+
|
138
|
+
Attributes
|
139
|
+
----------
|
140
|
+
matched_sting: Code snippet that matched
|
141
|
+
range: Range of the entire AST node captured by the match
|
142
|
+
matches: The mapping between tags and string representation of the AST captured
|
143
|
+
"""
|
144
|
+
|
145
|
+
matched_string: str
|
146
|
+
"Code snippet that matched"
|
147
|
+
|
148
|
+
range: Range
|
149
|
+
"Range of the entire AST node captured by the match"
|
150
|
+
|
151
|
+
matches: dict[str, str]
|
152
|
+
"The mapping between tags and string representation of the AST captured"
|
153
|
+
""
|
154
|
+
|
155
|
+
class Range:
|
156
|
+
"""A range of positions in a multi-line text document,
|
157
|
+
both in terms of bytes and of rows and columns.
|
158
|
+
"""
|
159
|
+
|
160
|
+
start_byte: int
|
161
|
+
end_byte: int
|
162
|
+
start_point: Point
|
163
|
+
end_point: Point
|
164
|
+
|
165
|
+
class Point:
|
166
|
+
row: int
|
167
|
+
column: int
|
168
|
+
|
169
|
+
class Filter:
|
170
|
+
"""A class to capture filters of a Piranha Rule"""
|
171
|
+
|
172
|
+
enclosing_node: TSQuery
|
173
|
+
"AST patterns that some ancestor node of the primary match should comply"
|
174
|
+
not_contains: list[TSQuery]
|
175
|
+
"AST patterns that SHOULD NOT match any subtree of node matching `enclosing_node` pattern"
|
176
|
+
contains: TSQuery
|
177
|
+
"AST pattern that SHOULD match subtrees of `enclosing_node`. " "Number of matches should be within the range of `at_least` and `at_most`."
|
178
|
+
at_least: int
|
179
|
+
"The minimum number of times the contains query should match in the enclosing node"
|
180
|
+
at_most: int
|
181
|
+
"The maximum number of times the contains query should match in the enclosing node"
|
182
|
+
child_count: int
|
183
|
+
"Number of named children under the primary matched node"
|
184
|
+
sibling_count: int
|
185
|
+
"Number of named siblings of the primary matched node"
|
186
|
+
def __init__(
|
187
|
+
self,
|
188
|
+
enclosing_node: Optional[str] = None,
|
189
|
+
not_enclosing_node: Optional[str] = None,
|
190
|
+
not_contains: list[str] = [],
|
191
|
+
contains: Optional[str] = None,
|
192
|
+
at_least: int = 1,
|
193
|
+
at_most: int = 4294967295, # u32::MAX
|
194
|
+
child_count: int = 4294967295, # u32::MAX
|
195
|
+
sibling_count: int = 4294967295, # u32::MAX
|
196
|
+
):
|
197
|
+
"""
|
198
|
+
Constructs `Filter`
|
199
|
+
|
200
|
+
Parameters
|
201
|
+
------------
|
202
|
+
enclosing_node: str
|
203
|
+
AST patterns that some ancestor node of the primary match should comply
|
204
|
+
not_contains: list[str]
|
205
|
+
AST patterns that should not match any subtree of node matching `enclosing_node` pattern
|
206
|
+
"""
|
207
|
+
...
|
208
|
+
|
209
|
+
class Rule:
|
210
|
+
"""A class to capture Piranha Rule"""
|
211
|
+
|
212
|
+
name: str
|
213
|
+
"Name of the rule"
|
214
|
+
query: TSQuery
|
215
|
+
"Tree-sitter query as string"
|
216
|
+
replace_node: str
|
217
|
+
"The tag corresponding to the node to be replaced"
|
218
|
+
replace_node_idx: str
|
219
|
+
"The i'th child of node corresponding to the replace_node tag will be replaced"
|
220
|
+
replace: str
|
221
|
+
"Replacement pattern"
|
222
|
+
groups: set[str]
|
223
|
+
"Group(s) to which the rule belongs"
|
224
|
+
holes: set[str]
|
225
|
+
"Holes that need to be filled, in order to instantiate a rule"
|
226
|
+
filters: set[Filter]
|
227
|
+
"Filters to test before applying a rule"
|
228
|
+
is_seed_rule: bool
|
229
|
+
"Marks a rule as a seed rule"
|
230
|
+
keep_comment_regexes: set[str]
|
231
|
+
"Make comment deleting optional"
|
232
|
+
|
233
|
+
def __init__(
|
234
|
+
self,
|
235
|
+
name: str,
|
236
|
+
query: Optional[str] = None,
|
237
|
+
replace_node: Optional[str] = None,
|
238
|
+
replace: Optional[str] = None,
|
239
|
+
groups: set[str] = set(),
|
240
|
+
holes: set[str] = set(),
|
241
|
+
filters: set[Filter] = set(),
|
242
|
+
is_seed_rule: bool = True,
|
243
|
+
keep_comment_regexes: Optional[set[str]] = None,
|
244
|
+
):
|
245
|
+
"""
|
246
|
+
Constructs `Rule`
|
247
|
+
|
248
|
+
Parameters
|
249
|
+
------------
|
250
|
+
name: str
|
251
|
+
Name of the rule
|
252
|
+
query: str
|
253
|
+
Tree-sitter query as string
|
254
|
+
replace_node: str
|
255
|
+
The tag corresponding to the node to be replaced
|
256
|
+
replace: str
|
257
|
+
Replacement pattern
|
258
|
+
groups: set[str]
|
259
|
+
Group(s) to which the rule belongs
|
260
|
+
holes: set[str]
|
261
|
+
Holes that need to be filled, in order to instantiate a rule
|
262
|
+
filters: set[Filter]
|
263
|
+
Filters to test before applying a rule
|
264
|
+
is_seed_rule: bool
|
265
|
+
Marks a rule as a seed rule
|
266
|
+
"""
|
267
|
+
...
|
268
|
+
|
269
|
+
class OutgoingEdges:
|
270
|
+
frm: str
|
271
|
+
"The source rule or group of rules"
|
272
|
+
to: list[str]
|
273
|
+
"The target edges or groups of edges"
|
274
|
+
scope: str
|
275
|
+
"The scope label for the edge"
|
276
|
+
|
277
|
+
def __init__(
|
278
|
+
self,
|
279
|
+
frm: str,
|
280
|
+
to: list[str],
|
281
|
+
scope: str,
|
282
|
+
):
|
283
|
+
"""
|
284
|
+
Constructs `OutgoingEdge`
|
285
|
+
|
286
|
+
Parameters
|
287
|
+
------------
|
288
|
+
frm: str
|
289
|
+
The source rule or group of rules
|
290
|
+
to: list[str]
|
291
|
+
The target edges or groups of edges
|
292
|
+
scope: str
|
293
|
+
The scope label for the edge
|
294
|
+
"""
|
295
|
+
...
|
296
|
+
|
297
|
+
class RuleGraph:
|
298
|
+
rules: list[Rule]
|
299
|
+
"The rules in the graph"
|
300
|
+
edges: list[OutgoingEdges]
|
301
|
+
"The edges in the graph"
|
302
|
+
graph: dict[str, list[tuple[str, str]]]
|
303
|
+
"The graph itself (as an adjacency list)"
|
304
|
+
|
305
|
+
def __init__(
|
306
|
+
self,
|
307
|
+
rules: list[Rule],
|
308
|
+
edges: list[OutgoingEdges],
|
309
|
+
):
|
310
|
+
"""
|
311
|
+
Constructs `OutgoingEdge`
|
312
|
+
|
313
|
+
Parameters
|
314
|
+
------------
|
315
|
+
rules: list[Rule]
|
316
|
+
The rules in the graph
|
317
|
+
edges: list[OutgoingEdges]
|
318
|
+
The edges in the graph
|
319
|
+
"""
|
320
|
+
...
|
321
|
+
|
322
|
+
class TSQuery:
|
323
|
+
"Captures a Tree sitter query"
|
324
|
+
def query(self):
|
325
|
+
"""The query"""
|
326
|
+
...
|
Binary file
|
File without changes
|
@@ -0,0 +1,125 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: polyglot_piranha
|
3
|
+
Version: 0.3.34
|
4
|
+
License-File: LICENSE
|
5
|
+
License-File: NOTICE
|
6
|
+
Summary: Polyglot Piranha is a library for performing structural find and replace with deep cleanup.
|
7
|
+
Keywords: refactoring,code update,structural find-replace,structural search and replace,structural search
|
8
|
+
Author: Uber Technologies Inc.
|
9
|
+
License: Apache-2.0
|
10
|
+
Requires-Python: >=3.8
|
11
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
12
|
+
Project-URL: homepage, https://github.com/uber/piranha
|
13
|
+
Project-URL: documentation, https://github.com/uber/piranha
|
14
|
+
Project-URL: repository, https://github.com/uber/piranha
|
15
|
+
|
16
|
+
# PolyglotPiranha
|
17
|
+
|
18
|
+
PolyglotPiranha is a lightweight code transformation toolset for automating large scale changes. At Uber, it is mostly used to clean up stale feature flags.
|
19
|
+
|
20
|
+
We only support languages that are used at Uber. We likely won't be able to add new languages in this repo. There are a number of forks (see https://github.com/uber/piranha/forks for a full list) that may provide additional features.
|
21
|
+
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
To install Polyglot Piranha, you can use it as a Python library or as a command-line tool.
|
26
|
+
|
27
|
+
### Python API
|
28
|
+
|
29
|
+
To install the Python API, run the following command:
|
30
|
+
|
31
|
+
```bash
|
32
|
+
pip install polyglot-piranha
|
33
|
+
```
|
34
|
+
|
35
|
+
### Command-line Interface
|
36
|
+
|
37
|
+
To install the command-line interface, follow these steps:
|
38
|
+
|
39
|
+
1. Install [Rust](https://www.rust-lang.org/tools/install)
|
40
|
+
2. Clone the repository: `git clone https://github.com/uber/piranha.git`
|
41
|
+
3. Navigate to the cloned directory: `cd piranha`
|
42
|
+
4. Build the project: `cargo build --release` (or `cargo build --release --no-default-features` for macOS)
|
43
|
+
5. The binary will be generated under `target/release`
|
44
|
+
|
45
|
+
## Example Usage
|
46
|
+
|
47
|
+
```python
|
48
|
+
from polyglot_piranha import execute_piranha, PiranhaArguments, Rule, RuleGraph, OutgoingEdges
|
49
|
+
|
50
|
+
# Original code snippet
|
51
|
+
code = """
|
52
|
+
if (obj.isLocEnabled() || x > 0) {
|
53
|
+
// do something
|
54
|
+
} else {
|
55
|
+
// do something else!
|
56
|
+
}
|
57
|
+
"""
|
58
|
+
|
59
|
+
# Define the rule to replace the method call
|
60
|
+
r1 = Rule(
|
61
|
+
name="replace_method",
|
62
|
+
query="cs :[x].isLocEnabled()", # cs indicates we are using concrete syntax
|
63
|
+
replace_node="*",
|
64
|
+
replace="true",
|
65
|
+
is_seed_rule=True
|
66
|
+
)
|
67
|
+
|
68
|
+
# Define the edges for the rule graph.
|
69
|
+
# In this case, boolean_literal_cleanup is already defined for java [see src/cleanup_rules]
|
70
|
+
edge = OutgoingEdges("replace_method", to=["boolean_literal_cleanup"], scope="parent")
|
71
|
+
|
72
|
+
# Create Piranha arguments
|
73
|
+
piranha_arguments = PiranhaArguments(
|
74
|
+
code_snippet=code,
|
75
|
+
language="java",
|
76
|
+
rule_graph=RuleGraph(rules=[r1], edges=[edge])
|
77
|
+
)
|
78
|
+
|
79
|
+
# Execute Piranha and print the transformed code
|
80
|
+
piranha_summary = execute_piranha(piranha_arguments)
|
81
|
+
print(piranha_summary[0].content)
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
## Documentation
|
86
|
+
|
87
|
+
For more examples and explanations of the toolset, please check our demos and extended [POLYGLOT_README.md](POLYGLOT_README.md) file.
|
88
|
+
|
89
|
+
|
90
|
+
## Feature Flags
|
91
|
+
|
92
|
+
|
93
|
+
Feature flags are commonly used to enable gradual rollout or experiment with new features. In a few cases, even after the purpose of the flag is accomplished, the code pertaining to the feature flag is not removed. We refer to such flags as stale flags. The presence of code pertaining to stale flags can have the following drawbacks:
|
94
|
+
- Unnecessary code clutter increases the overall complexity w.r.t maintenance resulting in reduced developer productivity
|
95
|
+
- The flags can interfere with other experimental flags (e.g., due to nesting under a flag that is always false)
|
96
|
+
- Presence of unused code in the source as well as the binary
|
97
|
+
- Stale flags can also cause bugs
|
98
|
+
|
99
|
+
PolyglotPiranha is a tool that can automatically refactor code related to stale flags. At a higher level, the input to the tool is the name of the flag and the expected behavior, after specifying a list of APIs related to flags in a properties file. Piranha will use these inputs to automatically refactor the code according to the expected behavior.
|
100
|
+
|
101
|
+
PolyglotPiranha (as of May 2022) is a common refactoring tool to support multiple languages and feature flag APIs.
|
102
|
+
For legacy language-specific implementations please check following [tag](https://github.com/uber/piranha/releases/tag/last-version-having-legacy-piranha).
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
A few additional links on Piranha:
|
107
|
+
|
108
|
+
- Research paper published at [PLDI 2024](https://dl.acm.org/doi/10.1145/3656429) on PolyglotPiranha.
|
109
|
+
- A technical [report](report.pdf) detailing our experiences with using Piranha at Uber.
|
110
|
+
- A [blogpost](https://eng.uber.com/piranha/) presenting more information on Piranha.
|
111
|
+
- 6 minute [video](https://www.youtube.com/watch?v=V5XirDs6LX8&feature=emb_logo) overview of Piranha.
|
112
|
+
|
113
|
+
## Support
|
114
|
+
|
115
|
+
If you have any questions on how to use Piranha or find any bugs, please [open a GitHub issue](https://github.com/uber/piranha/issues).
|
116
|
+
|
117
|
+
## License
|
118
|
+
Piranha is licensed under the Apache 2.0 license. See the LICENSE file for more information.
|
119
|
+
|
120
|
+
## Note
|
121
|
+
|
122
|
+
This is not an official Uber product, and provided as is.
|
123
|
+
|
124
|
+
|
125
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
LICENSE,sha256=7qqytxojDvLpt8CphcCVvEQilegiJ0x_oDkwHJU-1z4,11359
|
2
|
+
NOTICE,sha256=9bEJKCdL0MABjEknpMHXbYBZSkGVGRXYcSxSXS293X0,147
|
3
|
+
polyglot_piranha/__init__.pyi,sha256=Aef4ZhgaF9WkC5tmBDCLSPmO2_qZUa_hIFf42fpDrD8,10802
|
4
|
+
polyglot_piranha/__init__.py,sha256=pghVgChf0-NgAG_zd7CzKtvFuBDxg5Wh-GcHx2PoTzg,147
|
5
|
+
polyglot_piranha/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
+
polyglot_piranha/polyglot_piranha.cpython-312-darwin.so,sha256=morvMrWKynsY8ci5VbCY41_olbRkB62DLXpsDPL1K_0,49097536
|
7
|
+
polyglot_piranha-0.3.34.dist-info/RECORD,,
|
8
|
+
polyglot_piranha-0.3.34.dist-info/WHEEL,sha256=SVZTcGEpC5NtWyiHAiux7ywMVSDCSEYx48vIlz1gG60,138
|
9
|
+
polyglot_piranha-0.3.34.dist-info/METADATA,sha256=e4asUYM4U_pE7b5V-DIx0W9QwTGab9MPW4dQ16S8EHE,4918
|
10
|
+
polyglot_piranha-0.3.34.dist-info/licenses/LICENSE,sha256=7qqytxojDvLpt8CphcCVvEQilegiJ0x_oDkwHJU-1z4,11359
|
11
|
+
polyglot_piranha-0.3.34.dist-info/licenses/NOTICE,sha256=9bEJKCdL0MABjEknpMHXbYBZSkGVGRXYcSxSXS293X0,147
|
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner].
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|