Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support alternate virtualenv directories #11

Merged
merged 1 commit into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Check alternative directory locations given by the `PYLINT_VENV_PATH`
environment variable. The variable consists of paths separated by a
colon `:`.

## [2.1.1] - 2020-08-17

### Fixed
Expand Down
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ The hook will then be used automatically if

- no env is activated but your CWD contains a virtualenv in ``.venv``

- it also reads the ``PYLINT_VENV_PATH`` environment variable for finding
alternative locations, e.g. for checking directories ``.venv`` and
``.virtualenv``:
.. code:: console

PYLINT_VENV_PATH=.venv:.virtualenv

and if pylint is not installed in that env, too.

You can also call the hook via a command line argument:
Expand Down
9 changes: 6 additions & 3 deletions pylint_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ def detect_venv():
if venv:
return venv

# Check if a local ".venv" folder exists
# Check if a virtualenv directory exists (.venv by default, or alternative
# paths given by environment variable)
venv_paths = os.getenv("PYLINT_VENV_PATH", ".venv").split(":")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This does not support any directories where the name itself contains a colon (:). However, I think it is fine because the same is true for Bash's $PATH variable and I hope no-one uses colons in the venv directory names (or any other directory names for that matter).

cwd = os.getcwd()
exec_path = "Scripts" if IS_WIN else "bin"
if os.path.isfile(os.path.join(cwd, ".venv", exec_path, "activate")):
return os.path.join(cwd, ".venv")
for venv_dir in venv_paths:
if os.path.isfile(os.path.join(cwd, venv_dir, exec_path, "activate")):
return os.path.join(cwd, venv_dir)

return None

Expand Down