Skip to main content

a fork of facebook/codemod using pcre regular expressions

Reason this release was yanked:

release was missing the script

Project description

codemod

PyPI downloads

Overview

codemod2 is a tool/library to assist you with large-scale codebase refactors that can be partially automated but still require human oversight and occasional intervention.

Example: Let's say you're deprecating your use of the <font> tag. From the command line, you might make progress by running:

codemod2 -m -d /home/mdrohmann/www --extensions php,html \
    '<font *color="?(.*?)"?>(.*?)</font>' \
    '<span style="color: \1;">\2</span>'

For each match of the regex, you'll be shown a colored diff, and asked if you want to accept the change (the replacement of the <font> tag with a <span> tag), reject it, or edit the line in question in your $EDITOR of choice.

Install

In a virtual environment or as admin user

pip install codemod2

or with pipx

pipx install codemod2

Usage

The last two arguments are a regular expression to match and a substitution string, respectively. Or you can omit the substitution string, and just be prompted on each match for whether you want to edit in your editor.

Options (all optional) include:

-m
  Have regex work over multiple lines (e.g. have dot match newlines).  By
  default, codemod2 applies the regex one line at a time.
-d
  The path whose ancestor files are to be explored.  Defaults to current dir.
-i
  Make your search case-insensitive
--start
  A path:line_number-formatted position somewhere in the hierarchy from which
  to being exploring, or a percentage (e.g. "--start 25%") of the way through
  to start.  Useful if you're divvying up the substitution task across
  multiple people.
--end
  A path:line_number-formatted position somewhere in the hierarchy just
  *before* which we should stop exploring, or a percentage of the way
  through, just before which to end.
--extensions
  A comma-delimited list of file extensions to process. Also supports Unix
  pattern matching.
--include-extensionless
  If set, this will check files without an extension, along with any
  matching file extensions passed in --extensions
--accept-all
  Automatically accept all changes (use with caution)
--default-no
  Set default behavior to reject the change.
--editor
  Specify an editor, e.g. "vim" or "emacs".  If omitted, defaults to $EDITOR
  environment variable.
--count
  Don't run normally.  Instead, just print out number of times places in the
  codebase where the 'query' matches.
--test
  Don't run normally.  Instead, just run the unit tests embedded in the
  codemod2 library.

You can also use codemod for transformations that are much more sophisticated than regular expression substitution. Rather than using the command line, you write Python code that looks like:

import codemod2
codemod2.Query(...).run_interactive()

Note: I didn't test yet, that the Query object still works.

See the documentation for the Query class for details if you want to try it.

Motivation for the fork

Most programming languages have some kind of balanced parantheses or brackets. PCRE2 regular expressions can help for such a use case. In my specific case, I wanted to wrap Python dictionaries in a specific type constructor in some contexts.

The following codemod2 regular expression accomplishes this:

codemod2 -m 'context=(?<expr>\{(?:[^}{]+|(?P>expr))*+\})' 'context=DictConstructor($1)'

Dependencies

  • python2
  • pcre2

Credits

Copyright (c) 2024 Martin Drohmann.

Copyright (c) 2007-2008 Facebook.

Created by Justin Rosenstein.

Licensed under the Apache License, Version 2.0.

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

codemod2-0.1.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

codemod2-0.1.0-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file codemod2-0.1.0.tar.gz.

File metadata

  • Download URL: codemod2-0.1.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Linux/6.5.0-14-generic

File hashes

Hashes for codemod2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 70f21eecd732fe59fb8538c415bdd38647ad7a158b9749a3e44abe12e099c4b9
MD5 8bb34b33d3d462a0483364dd5e189949
BLAKE2b-256 194213a03c6c511a317c5d4372782c870ee5fa4334da50c218f26a179abf4bbc

See more details on using hashes here.

File details

Details for the file codemod2-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: codemod2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.6 Linux/6.5.0-14-generic

File hashes

Hashes for codemod2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be82da7631b42a5da18007a6df4c2b684874045eb387030a8821934ab7f03178
MD5 b419a35ac18bb5e8111d77cbc82c107f
BLAKE2b-256 2d2c15287429f5a448dbafba485b501faae0007bcb6b0de8627a635021998d92

See more details on using hashes here.

Supported by

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