Skip to content

Commit

Permalink
Add uv
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMili committed May 31, 2024
1 parent 9de6b96 commit d051a45
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**isVirtual** is a tool to detect if the current directory is linked to a virtual environment, get the config of this env and more. Work with venv, virtualenv, pipenv, poetry and soon hatch and pdm.
**isVirtual** is a tool to detect if the current directory is linked to a virtual environment, get the config of this env and more. Work with venv, virtualenv, pipenv, poetry, hatch, pdm and uv.

# Install

Expand Down
29 changes: 21 additions & 8 deletions isvirtual/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import json
import os
import re
import subprocess
import sys
from contextlib import suppress
from pathlib import Path
import subprocess

from platformdirs import user_cache_path


config = None


Expand Down Expand Up @@ -88,6 +89,18 @@ def is_virtualenv() -> bool:
else:
return False

def is_uv() -> bool:
if is_virtual_env() is False:
return False

prefix = _get_prefix()
_read_config(os.path.join(prefix, "pyvenv.cfg"))

if "uv" in config["root"]:
return True
else:
return False


def is_conda() -> bool:
is_conda = False
Expand Down Expand Up @@ -129,6 +142,7 @@ def _read_config(path: str) -> None:
conf.read_string("[root]\n" + stream.read())
config = conf


def _read_config_static(path: str) -> dict:
config = {}
conf = configparser.ConfigParser()
Expand All @@ -150,7 +164,7 @@ def check_dir(path: str | Path) -> dict:
# Check classic virtual env in a given directory
for file in path.iterdir():
if file.is_dir() is True:
# include/ dir is not checked because PDM is not creating it by default
# include/ dir is not checked because PDM, Hatch and uv are not creating it by default
if (
(file / "bin").exists() is True
and (file / "bin" / "activate").exists() is True
Expand All @@ -159,8 +173,10 @@ def check_dir(path: str | Path) -> dict:
):
config = _read_config_static(str(file / "pyvenv.cfg"))
config["path"] = str(file)
if "virtualenv" in config:
if "virtualenv" in config.keys():
config["source"] = "virtualenv"
elif "uv" in config.keys():
config["source"] = "uv"
else:
config["source"] = "venv"

Expand All @@ -171,10 +187,7 @@ def check_dir(path: str | Path) -> dict:

poetry_venvs = poetry_root / "virtualenvs"
poetry_envs = list(Path(poetry_venvs).glob(f"{poetry_hash}-*"))
if (
poetry_venvs.exists() is True
and len(poetry_envs) > 0
):
if poetry_venvs.exists() is True and len(poetry_envs) > 0:
config = _read_config_static(str(poetry_envs[0] / "pyvenv.cfg"))
config["path"] = str(poetry_envs[0])
config["source"] = "poetry"
Expand Down Expand Up @@ -202,7 +215,7 @@ def scan_dir(path: str) -> list[str]:
for line in envs.split("\n"):
if len(line) > 0:
cpath = Path(line)
# include/ dir is not checked because PDM is not creating it by default
# include/ dir is not checked because PDM, Hatch and uv are not creating it by default
if (
(cpath.parent.parent / "bin").exists() is True
and (cpath.parent.parent / "lib").exists() is True
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "isvirtual"
description = "Tool to detect if the current directory is linked to a virtual environment, get the config of this env and more. Work with venv, virtualenv, pipenv, poetry and soon hatch and pdm."
description = "Tool to detect if the current directory is linked to a virtual environment, get the config of this env and more. Work with venv, virtualenv, pipenv, poetry, hatch, pdm and uv."
dynamic = ["version"]
readme = "README.md"
authors = [{ name = "Alex Mili" }]
Expand All @@ -26,7 +26,7 @@ classifiers = [
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
]
keywords = ["virtual", "env", "venv", "virtualenv", "environment", "poetry", "pipenv", "pdm", "hatch"]
keywords = ["virtual", "env", "venv", "virtualenv", "environment", "poetry", "pipenv", "pdm", "hatch", "uv"]
dependencies = ["platformdirs", "typer", "sty"]

[project.urls]
Expand Down

0 comments on commit d051a45

Please sign in to comment.