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.1.tar.gz (107.2 kB view details)

Uploaded Source

Built Distributions

polyglot_piranha-0.4.1-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.1-cp312-cp312-manylinux_2_28_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.1-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.1-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.1-cp311-cp311-manylinux_2_28_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.1-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.1-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.1-cp310-cp310-manylinux_2_28_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.1-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.1-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.1-cp39-cp39-manylinux_2_28_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.28+ ARM64

polyglot_piranha-0.4.1-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.1.tar.gz.

File metadata

  • Download URL: polyglot_piranha-0.4.1.tar.gz
  • Upload date:
  • Size: 107.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for polyglot_piranha-0.4.1.tar.gz
Algorithm Hash digest
SHA256 b687a5cb1388f51dcb38fdcbfbfcf58bd1207b576094f6fdc5ecbaaed71d97cf
MD5 4e4a4351a820c725c9fe1dddce4f86ff
BLAKE2b-256 3b695601c397c6969a7fa94dc72a762f086857fb2cbb5aa7b546010621c83fcf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c0493f5df1f55ca37304b2fa93d6ce261f899ef0b574ad5cae2956687f9dd39a
MD5 f67e00557089b4e9884a3ab95e4048c4
BLAKE2b-256 3e5a49be772891ac4688afd29c711284c7d62d5c7b7f74c33e48fcc4d83b749d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fcbf12f3260b5eb822a1ea236611615f61d3836cdf4916cc2a2078f2a5c89350
MD5 35229e1591e3e831c4fa26ea92b50263
BLAKE2b-256 23e6f54c8c53873f91fe10ffb67d8a4c9a79ffe9892598d1d5c2bd0664a371bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 668ecf203399a5fe87f18088398592edb51b6c17be3dc6b84c1753efaa5386b9
MD5 d714223fe673d26d958c418eefba3930
BLAKE2b-256 8c27b2f9db69a149870d98834a159d38682125b1e2464173880a68eab30eb5b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5964b2855413c36db8ddaa264c42ccd302838570e768d5414faffbd813d6f163
MD5 2223860c6b3240b75615abc0a69de9fc
BLAKE2b-256 78270988e553040c00944a770e8c16c61b479859f472a620d76879c64bf5f31b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c7a9bbe946b5b1a3fba23efba7f9973abbd8f3405d3c090b319a54745a382069
MD5 f0e77bad500932206ef0708ef67cf684
BLAKE2b-256 e1d5a3be69839e9f33caa6fefa7d85373bbcd5c7320337cff4fa7a0c8fad2d83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp311-cp311-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 d14ac5cba9360fc250a31e06ac0abf126618c2a31b7ceade943bbb957a94b694
MD5 0a0065633bf29819d2016922185636ce
BLAKE2b-256 274d9f295da45a764b0d2c370443f6202ad06f7ebc0682bab1d211e7a9c072f1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 38e8821e4bb5cc66165ace55af5ace2e70f62aa5a2f768e69e50c405b966b9a5
MD5 8dd98e8442c9cf498bf950ace5afc0d2
BLAKE2b-256 873493f99c0a623b66d30c3d1d4efe3f841f3a5448b31909c033236fc93bfd46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7d35429f6232e47f5b23978cafd67269e85a167676be7363a137291813f873d3
MD5 eba59719c0ef544143ddf6b040ea93d6
BLAKE2b-256 4fde2cf16c0c81c1882527f18f5149fcccea3ebd384a0145c3adaa2096d14ef6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp310-cp310-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 0202be7f60dabcb5e1b75a71d97ea70078071fd95f70bd9ebd9584c4d2d999c8
MD5 6ef554412847bbaa2ecab5596b6f1fb5
BLAKE2b-256 5e68fff566541d8dd090029bb480edda9a9e5fe060ed14e58e1910be9a184c95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 789c99f55fe94dd34eaadaf47ecc45f4915bdbca46eeb4a8efb6118792604786
MD5 d2d748428ac312064b2a2594fa0de604
BLAKE2b-256 be49e751684ba5db1d547eccf78bf6489e423ac99cde96d0d2707c490ee6da2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4fb94237bc91c173cd5fe20b160e237e848ecf6740e8675fbbea95c74da9db17
MD5 24a5e3a2015f1c2458b73dafc9c5a5b1
BLAKE2b-256 93479501f2e7ae01ea17c8fed65b4caa0cfbcde31568137d7504e64a0f68fdf1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for polyglot_piranha-0.4.1-cp39-cp39-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 625e2d16fa3087b32cdf5885b82264d60498f53999ce5a3a0bf11162b4c07cf1
MD5 76a05e2d4dbbe58e94cd51033b51f7ba
BLAKE2b-256 8dff11abe8c2382cd8fb8a96f8e7d8dcfb3456f4a457c092032cd7107305e84d

See more details on using hashes here.

Supported by

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