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.8.tar.gz (135.1 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.8-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.8-cp312-cp312-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.8-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.8-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.8-cp311-cp311-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.8-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.8-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.8-cp310-cp310-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.8-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.8-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.8-cp39-cp39-manylinux_2_28_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.8-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.8.tar.gz.

File metadata

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

File hashes

Hashes for polyglot_piranha-0.4.8.tar.gz
Algorithm Hash digest
SHA256 e233441774651ba7ad601e37c961f2bdd9b1b4a078500e6a5765c966a46dbf68
MD5 143a6a2e0174f5934d808b4fb576ad3f
BLAKE2b-256 5c067d2e2ed46e9e110bad463cc6275f85e35774ebfa86d17b656fa8f724e5c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9208ebca840a515b045c82794aa089f721c8bcfa19270c1e64611afa998b4fd1
MD5 4feba30e396b075a2788e2de09c425b5
BLAKE2b-256 113772e9a77c3e8c738ec4dc4ffde221112e9bcd1c37f423ef8db7429cef22c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 304c0f39712d0f7af67a2a42c693364a0fdf4fda4bffe039183aba3291cd15a1
MD5 34c1dff44e8dc0d6acc4dcf3132da279
BLAKE2b-256 af500d4d761bfe4ae252cfbf0f75eb54103609fcbca9b0f61bcc43e6b030cd0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 8783540389c42935fec3660b95e461d88cb8be3c79f2ab5d0bb00ebc208ad334
MD5 4f51f9a7f0da68f0d3b4fbddebbb7d1d
BLAKE2b-256 657734990f53680a2df2e894eb9fadf909c678e9e0f7a7c9023c1b0ce6aa3db9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 73fc96ba4439db51aa5940f858448bb62900c04a9422be45bf8213eb66824522
MD5 6aed3be3a9ea37b13c8e15cfe8eda8f9
BLAKE2b-256 3d238f48a1e65610ee693e6c479f05d6597f1af180926b1b3a15d6a8cdbbd70f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4b73ad85e50ae7dd38507e5a9c7da41202db1ede1faaefd4a99356498f435837
MD5 28a020af23b1b3e5d98a475db8a0749f
BLAKE2b-256 8c4f3d21f4e0e770760a143c399054c669e41de361c44e6f791e3e64419c13f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp311-cp311-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 2f91cd5fedd422c8e07d49ed420cdd014464b8280c697a2cead3a0d75eddd537
MD5 8155c4cfa27a3624c6c265e08457dac4
BLAKE2b-256 029c7ade4c7bf2ad1e3ab81df4592377b8c1a6b93b0b8622a9e2d2c2fa3b808a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7e566c0ffdcccae68b7c76f94e0c72027a1c822babc350d1f38ba6b7690394e2
MD5 3ee2ce05943e40cadccceb047d84af7d
BLAKE2b-256 57bbdeff64a339d82d5fc3ed75da8bd9490fc091bb8d8b8c1963fceb844b6aab

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5b561d0717536f615a8713446c3d3a88b2496bf0e68f8493a7c9da92064c07b9
MD5 983bf84a42d24443f66e9e9694bad1ed
BLAKE2b-256 9c630bdac04373cf018b0d66acceb62b96ceebb7c4c5a2c92525fab374f72b73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp310-cp310-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 f4a0f637eb3772a49db097bf37c7bb49166016eb752f8998e51c411017b2bfb8
MD5 fc661606b146d81955769881e9563018
BLAKE2b-256 30de5ab34effd4e9ff0b01331e5185a0c2dca496b4186584ed559990477bea44

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f147141ecff8c5422496c28b27667e92ba7b5645dc4c14555ee10780ef8ef27e
MD5 c007553ecc5d6d681afe98a1d775966b
BLAKE2b-256 c62b108c8d7a62a253d76f406d7dda7184e875c00636c32bbab0710d1daa24ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 ed62e36a262afd17d8193b4b4d33f427f9af4565f1a4cc4a140ebf37db95abdc
MD5 20870ccc13f6d35f4c717f243c9b04f7
BLAKE2b-256 0459f529f713894460e20b4a60dc25063455afaf786afc2263f0512a395888b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.8-cp39-cp39-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 cd8ff6ed73da6524232f2153345f94be905d193cb36a59ffeaf6158743c1e5cb
MD5 c6ed2eb84c6f3218d97c98346e03cc26
BLAKE2b-256 48be268f27c095d948dea4fb806562f125a3dc8b5b9588b768fbf593795b0410

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