Skip to main content

Polyglot Piranha is a library for performing structural find and replace with deep cleanup.

Project description

PolyglotPiranha

PolyglotPiranha is a lightweight code transformation toolset for automating large scale changes. At Uber, it is mostly used to clean up stale feature flags.

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.

Installation

To install Polyglot Piranha, you can use it as a Python library or as a command-line tool.

Python API

To install the Python API, run the following command:

pip install polyglot-piranha

Command-line Interface

To install the command-line interface, follow these steps:

  1. Install Rust
  2. Clone the repository: git clone https://github.com/uber/piranha.git
  3. Navigate to the cloned directory: cd piranha
  4. Build the project: cargo build --release (or cargo build --release --no-default-features for macOS)
  5. The binary will be generated under target/release

Example Usage

from polyglot_piranha import execute_piranha, PiranhaArguments, Rule, RuleGraph, OutgoingEdges

# Original code snippet
code = """
if (obj.isLocEnabled() || x > 0) {
    // do something
} else {
    // do something else!
}
"""

# Define the rule to replace the method call
r1 = Rule(
    name="replace_method",
    query="cs :[x].isLocEnabled()", # cs indicates we are using concrete syntax
    replace_node="*",
    replace="true",
    is_seed_rule=True
)

# Define the edges for the rule graph. 
# In this case, boolean_literal_cleanup is already defined for java [see src/cleanup_rules]
edge = OutgoingEdges("replace_method", to=["boolean_literal_cleanup"], scope="parent")

# Create Piranha arguments
piranha_arguments = PiranhaArguments(
    code_snippet=code,
    language="java",
    rule_graph=RuleGraph(rules=[r1], edges=[edge])
)

# Execute Piranha and print the transformed code
piranha_summary = execute_piranha(piranha_arguments)
print(piranha_summary[0].content)

Documentation

For more examples and explanations of the toolset, please check our demos and extended POLYGLOT_README.md file.

Feature Flags

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:

  • Unnecessary code clutter increases the overall complexity w.r.t maintenance resulting in reduced developer productivity
  • The flags can interfere with other experimental flags (e.g., due to nesting under a flag that is always false)
  • Presence of unused code in the source as well as the binary
  • Stale flags can also cause bugs

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.

PolyglotPiranha (as of May 2022) is a common refactoring tool to support multiple languages and feature flag APIs. For legacy language-specific implementations please check following tag.

A few additional links on Piranha:

  • Research paper published at PLDI 2024 on PolyglotPiranha.
  • A technical report detailing our experiences with using Piranha at Uber.
  • A blogpost presenting more information on Piranha.
  • 6 minute video overview of Piranha.

Support

If you have any questions on how to use Piranha or find any bugs, please open a GitHub issue.

Piranha Development

Piranha uses several grammar repositories with custom patches to support the transformations. While these patches are being upstreamed, there may be discrepancies between the grammars in this repository and the upstream grammars. Therefore, we have built a custom tree-sitter playground that can be used to test the grammars and queries for easier development:

https://uber.github.io/piranha/tree-sitter-playground/

License

Piranha is licensed under the Apache 2.0 license. See the LICENSE file for more information.

Note

This is not an official Uber product, and provided as is.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polyglot_piranha-0.4.7.tar.gz (119.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polyglot_piranha-0.4.7-cp312-cp312-manylinux_2_28_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

polyglot_piranha-0.4.7-cp312-cp312-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.7-cp312-cp312-macosx_10_13_universal2.whl (7.7 MB view details)

Uploaded CPython 3.12macOS 10.13+ universal2 (ARM64, x86-64)

polyglot_piranha-0.4.7-cp311-cp311-manylinux_2_28_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

polyglot_piranha-0.4.7-cp311-cp311-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.7-cp311-cp311-macosx_10_13_universal2.whl (7.7 MB view details)

Uploaded CPython 3.11macOS 10.13+ universal2 (ARM64, x86-64)

polyglot_piranha-0.4.7-cp310-cp310-manylinux_2_28_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

polyglot_piranha-0.4.7-cp310-cp310-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.7-cp310-cp310-macosx_10_13_universal2.whl (7.7 MB view details)

Uploaded CPython 3.10macOS 10.13+ universal2 (ARM64, x86-64)

polyglot_piranha-0.4.7-cp39-cp39-manylinux_2_28_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ x86-64

polyglot_piranha-0.4.7-cp39-cp39-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.7-cp39-cp39-macosx_10_13_universal2.whl (7.7 MB view details)

Uploaded CPython 3.9macOS 10.13+ universal2 (ARM64, x86-64)

File details

Details for the file polyglot_piranha-0.4.7.tar.gz.

File metadata

  • Download URL: polyglot_piranha-0.4.7.tar.gz
  • Upload date:
  • Size: 119.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for polyglot_piranha-0.4.7.tar.gz
Algorithm Hash digest
SHA256 3cfc189cc0f3727947c1a9dc6989d7a17c85a50fecc99ab2cf01c0091917ed3e
MD5 8c6d8c7bfeee0e6fd618d583f0791592
BLAKE2b-256 93755c0df4d13f162aa5d518b88be8c1a4439cb9d58ff1e17c65e7c1fcf351f5

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c545402c93d400d5dfe2b266b0594b3ea2d1ef4ffae8985246d3a73abc665959
MD5 c5c650af39c429301f03096954a4879c
BLAKE2b-256 e143a49b1e4a3cb6114930aa2aaf757beab4669c38e0954e4ff2b14ce26cfd9d

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp312-cp312-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ea63171cc67e28b35003c941d340f8da77014d48d6da7089c051d6f76c684ecc
MD5 649dc7bd65265f4226ab110b985fb0fe
BLAKE2b-256 f5fc5bfe6c43322f143959c8694890c914ea741403956c65bee1de3708a6387e

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 5514b170c8a1ad008fa573b676883002fc5f957ea493319e35688dc6b8d9fdc9
MD5 76d5e576f8d857ad3fdea1c952343f95
BLAKE2b-256 1b38580951c8f7a714c20f878d0b1902fa8f8bb394633a5d5d8c79f4a00a8156

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6df6ba4bf66433979930e337e96c18a04fabef275000667fe92620cad9c81594
MD5 536baacbfc78a9325cbd66f5202f1eb0
BLAKE2b-256 371ffa1a46a7af1d79d18b8705017dfb80e17c7c7734427b905d00d126e03b74

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp311-cp311-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 d2bd62bb47cecb3f478d4e7124781e21c8e78ad1c8e86ff31771912cc46d1a9c
MD5 8c83245672e2547af42ce2c333bdcdd5
BLAKE2b-256 fb3fefb035d50baff10fba332c6cfb493754c25c93d03abd0bfc989037e6d1ea

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp311-cp311-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp311-cp311-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 2049dda3b70f2b6f4e0cd6a25266ef6da1bcabf7d418f6168bb485c70d2d678f
MD5 e9512852f2e5c0a46962b1f160bfbd66
BLAKE2b-256 55527a152aa3d6e6743856250ab96aa43a635df8be109fd707d1c6740adc009a

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1faf023d561c673bf19ecb7d4ba1cfc081fdaf3c4cb9422bd50d486b77fdb39e
MD5 f4533c1b5aab5b7434121de939cfa400
BLAKE2b-256 4c85a1db6c634506ffb5a943638363ce794346173905a5de0283152ab0265fd7

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp310-cp310-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4f369d6dfde4e92c4c39fb7188d1c1a1cc36f445de79387f89d0a5fa080fdbaa
MD5 75a6ea90170c49a38edff13999a5222e
BLAKE2b-256 b7736422c1f150a74949d986d245270a15a1f5a5d4af0bfacbfcf7a83a2897bb

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp310-cp310-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp310-cp310-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 0293a9d39d65144009410b6eb62a7f0dda7e4d975cfa451b6801ee294a640d64
MD5 f613fcd3747c8d22f94c3120f722a033
BLAKE2b-256 b83fe79a0a9470d54df2a6b26ab377c0aca8ec9952a9adf1968b82d3f511049a

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp39-cp39-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f049f682a547e7abedff18e0d2e94e64233c2a719221b7dcc1f70d717c22986c
MD5 ffb5e7136e8aea147f2f22c3f628b2bc
BLAKE2b-256 5bc2a1857edcdf4c61b09410272f8742e7689ff3e58c45304f6d1e28a6492893

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp39-cp39-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2016f962fbfbde6f323ff5e5bb9e508007c0f7f4810c973c9b255e744fe6cb1c
MD5 a512d1aba8fcbe2a29adbb1987c55ae9
BLAKE2b-256 0a8153f6d2c14e384ab77c175c5425564ae832a81f16f7fad92418cb5f6ac766

See more details on using hashes here.

File details

Details for the file polyglot_piranha-0.4.7-cp39-cp39-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for polyglot_piranha-0.4.7-cp39-cp39-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 ce12e9047662abc1d578ab70e465782484986a244f2f6fb3d01ff159c04d8a16
MD5 63a9d650db6eb943dedb497148d73828
BLAKE2b-256 8590de3cf4647be9936027a85e6cebfe1ee6ee50ceb3e9de885f095e0c472d4e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page