From d051a4583397aa5cb16fce77c71e0f623f3b5468 Mon Sep 17 00:00:00 2001 From: AlexMili Date: Fri, 31 May 2024 14:22:19 +0200 Subject: [PATCH] Add uv --- README.md | 2 +- isvirtual/main.py | 29 +++++++++++++++++++++-------- pyproject.toml | 4 ++-- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ef99489..3607359 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/isvirtual/main.py b/isvirtual/main.py index eb11697..2463a7b 100644 --- a/isvirtual/main.py +++ b/isvirtual/main.py @@ -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 @@ -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 @@ -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() @@ -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 @@ -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" @@ -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" @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 43108a4..174bb70 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }] @@ -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]