Skip to content

Commit

Permalink
Add feature to scan directory for virtual env
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMili committed May 30, 2024
1 parent 21906b4 commit 33211f4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,50 @@ if __name__ == "__main__":
print("404 Not Found")
```

You can recursively scan a directory to get all virtual environments in it:
```python
from isvirtual import scan_dir

if __name__ == "__main__":
scanned = scan_dir("/some/dir/path")
if len(scanned) > 0:
print(f"Found {len(scanned)} virtual env(s)")
else:
print("No virtual env found")
```

## CLI
```console
$ isvirtual
$ isvirtual --help
Usage: isvirtual [OPTIONS] COMMAND [ARGS]...

╭─ Options ──────────────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ─────────────────────────────────────────────────────────────────────────────╮
│ check Check if you are currently in a virtual env │
│ info If the given directory is linked to a virtual env, show its info │
│ scan Scan the given directory recursively to find all virtual environment in it │
╰────────────────────────────────────────────────────────────────────────────────────────╯
```

Check if a virtual env is currently activated (support conda):
```console
$ isvirtual check
Yes
```

Show info of current directory's virtual env info:
```console
$ isviritual info .
home=/usr/local/opt/python@3.10/bin
include-system-site-packages=false
version=3.10.14
path=/Users/MyUserName/MyDir/.venv
source=venv
```
Also display with the `source` key if the virtual env has been created through `venv`, `virtualenv`, `poetry` and `pipenv`.

# License

This project is licensed under the terms of the MIT license.
20 changes: 18 additions & 2 deletions isvirtual/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import typer
from sty import fg
from typing_extensions import Annotated

from sty import fg
from isvirtual import check_dir, is_virtual_env
from isvirtual import check_dir, is_virtual_env, scan_dir


app = typer.Typer(add_completion=False)
Expand All @@ -29,3 +29,19 @@ def info(
print(f"{fg.blue}{k}={fg.green}{v}")
else:
print(f"{fg.red}Virtual environment not found")

@app.command(
help="Scan the given directory recursively to find all virtual environment in it"
)
def scan(
path: Annotated[
str,
typer.Argument(),
] = ".",
) -> None:
envs = scan_dir(path)

for env in envs:
print(env)

print(f"\n{fg.blue}Found {len(envs)} virtual environment(s)")
20 changes: 20 additions & 0 deletions isvirtual/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
from contextlib import suppress
from pathlib import Path
import subprocess

from platformdirs import user_cache_path

Expand Down Expand Up @@ -192,6 +193,25 @@ def check_dir(path: str | Path) -> dict:
return config


def scan_dir(path: str) -> list[str]:
output = subprocess.check_output(
f"find {path} -name activate -type f 2>/dev/null", shell=True
)
envs = output.decode("utf-8")
validated_envs = []
for line in envs.split("\n"):
if len(line) > 0:
cpath = Path(line)
if (
(cpath.parent.parent / "bin").exists() is True
and (cpath.parent.parent / "include").exists() is True
and (cpath.parent.parent / "lib").exists() is True
and (cpath.parent.parent / "pyvenv.cfg").exists() is True
):
validated_envs.append(cpath.parent.parent)
return validated_envs


def _encode(string: str, encodings: list[str] | None = None) -> bytes:
if isinstance(string, bytes):
return string
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ classifiers = [
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
keywords = ["virtual", "env", "venv", "virtualenv", "environment", "poetry", "pipenv", "pdm"]
keywords = ["virtual", "env", "venv", "virtualenv", "environment", "poetry", "pipenv", "pdm", "hatch"]
dependencies = ["platformdirs", "typer", "sty"]

[project.urls]
Expand Down

0 comments on commit 33211f4

Please sign in to comment.