Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

Magically refactor Python

Project description

Sourcery Logo

Sourcery Command Line Interface

Installation and usage

Installation

Sourcery command line interface can be installed by running:

pip install sourcery-cli

Login

Once installed you can interactively login with:

sourcery login

which will open up a browser tab and ask for confirmation. This is only needed once per computer.

Please be aware that the Sourcery CLI requires a Team subscription to use. You can upgrade here.

Usage

To display suggested refactorings as a diff:

sourcery refactor {file_or_directory}

And to apply those changes to the files:

sourcery refactor --in-place {file_or_directory}

Command line options

Sourcery provides a few options for running. You can list them by running sourcery refactor --help

sourcery refactor --help

Usage: sourcery refactor [OPTIONS] [SRC]...

  Refactor SRC files/directories. Reads from stdin when SRC is -

Options:
  -c, --code TEXT     Refactor the code passed in as a string.
  --check             Return exit code 1 if refactorings found.  [default:
                      False]

  --diff / --no-diff  Output a diff for each file on stdout.  [default: True]
  --in-place          Make changes to files in place  [default: False]
  -h, --help          Show this message and exit.

Configuration

Sourcery reads configuration settings from .sourcery.yaml in the project directory. Full details are described here.

Precommit Hook

Sourcery works great with pre-commit. Once you have it installed, add this to the .pre-commit-config.yaml in your repository:

repos:
  - repo: https://github.com/sourcery-ai/sourcery
    rev: v0.8.2
    hooks:
      - id: sourcery
      # - args: [--in-place]  Automatically apply the refactorings in the hook

Then run pre-commit install and you're ready to go.

Continuous Integration

Here's a script to run Sourcery in CI that will fail the job if any refactorings are found:

# Install sourcery
pip install sourcery-cli
# Login - store SOURCERY_TOKEN in a secret/masked environment variable
sourcery login --token $SOURCERY_TOKEN
# Find changed files and pass them to Sourcery
git diff master --name-only | xargs sourcery refactor --check

Project details


Download files

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

Files for sourcery-cli, version 0.8.2
Filename, size File type Python version Upload date Hashes
Filename, size sourcery_cli-0.8.2-py2.py3-none-macosx_10_14_x86_64.whl (63.5 MB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size sourcery_cli-0.8.2-py2.py3-none-manylinux1_x86_64.whl (72.5 MB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size sourcery_cli-0.8.2-py2.py3-none-win_amd64.whl (49.6 MB) File type Wheel Python version py2.py3 Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page