@@ -13,9 +13,6 @@ from typing import Any, Iterator
1313
1414PYTHON_VERSIONS = os .getenv ("PYTHON_VERSIONS" , "3.8 3.9 3.10 3.11 3.12 3.13" ).split ()
1515
16- exe = ""
17- prefix = ""
18-
1916
2017def shell (cmd : str , capture_output : bool = False , ** kwargs : Any ) -> str | None :
2118 """Run a shell command."""
@@ -37,18 +34,11 @@ def environ(**kwargs: str) -> Iterator[None]:
3734 os .environ .update (original )
3835
3936
40- def uv_install () -> None :
37+ def uv_install (venv : Path ) -> None :
4138 """Install dependencies using uv."""
42- uv_opts = ""
43- if "UV_RESOLUTION" in os .environ :
44- uv_opts = f"--resolution={ os .getenv ('UV_RESOLUTION' )} "
45- requirements = shell (f"uv pip compile { uv_opts } pyproject.toml devdeps.txt" , capture_output = True )
46- shell ("uv pip install -r -" , input = requirements , text = True )
47- if "CI" not in os .environ :
48- shell ("uv pip install --no-deps -e ." )
49- else :
50- shell ("uv pip install --no-deps ." )
51-
39+ with environ (UV_PROJECT_ENVIRONMENT = str (venv )):
40+ shell ("uv sync" )
41+
5242
5343def setup () -> None :
5444 """Setup the project."""
@@ -59,47 +49,27 @@ def setup() -> None:
5949 default_venv = Path (".venv" )
6050 if not default_venv .exists ():
6151 shell ("uv venv --python python" )
62- uv_install ()
52+ uv_install (default_venv )
6353
6454 if PYTHON_VERSIONS :
6555 for version in PYTHON_VERSIONS :
6656 print (f"\n Installing dependencies (python{ version } )" ) # noqa: T201
6757 venv_path = Path (f".venvs/{ version } " )
6858 if not venv_path .exists ():
6959 shell (f"uv venv --python { version } { venv_path } " )
70- with environ (VIRTUAL_ENV = str (venv_path .resolve ())):
71- uv_install ()
72-
73-
74- def activate (path : str ) -> None :
75- """Activate a virtual environment."""
76- global exe , prefix # noqa: PLW0603
77-
78- if (bin := Path (path , "bin" )).exists ():
79- activate_script = bin / "activate_this.py"
80- elif (scripts := Path (path , "Scripts" )).exists ():
81- activate_script = scripts / "activate_this.py"
82- exe = ".exe"
83- prefix = f"{ path } /Scripts/"
84- else :
85- raise ValueError (f"make: activate: Cannot find activation script in { path } " )
86-
87- if not activate_script .exists ():
88- raise ValueError (f"make: activate: Cannot find activation script in { path } " )
89-
90- exec (activate_script .read_text (), {"__file__" : str (activate_script )}) # noqa: S102
60+ with environ (UV_PROJECT_ENVIRONMENT = str (venv_path .resolve ())):
61+ uv_install (venv_path )
9162
9263
9364def run (version : str , cmd : str , * args : str , ** kwargs : Any ) -> None :
9465 """Run a command in a virtual environment."""
9566 kwargs = {"check" : True , ** kwargs }
9667 if version == "default" :
97- activate ( ".venv" )
98- subprocess .run ([f" { prefix } { cmd } { exe } " , * args ], ** kwargs ) # noqa: S603, PLW1510
68+ with environ ( UV_PROJECT_ENVIRONMENT = ".venv" ):
69+ subprocess .run (["uv" , "run" , cmd , * args ], ** kwargs ) # noqa: S603, PLW1510
9970 else :
100- activate (f".venvs/{ version } " )
101- os .environ ["MULTIRUN" ] = "1"
102- subprocess .run ([f"{ prefix } { cmd } { exe } " , * args ], ** kwargs ) # noqa: S603, PLW1510
71+ with environ (UV_PROJECT_ENVIRONMENT = f".venvs/{ version } " , MULTIRUN = "1" ):
72+ subprocess .run (["uv" , "run" , cmd , * args ], ** kwargs ) # noqa: S603, PLW1510
10373
10474
10575def multirun (cmd : str , * args : str , ** kwargs : Any ) -> None :
@@ -124,10 +94,10 @@ def clean() -> None:
12494 for path in paths_to_clean :
12595 shell (f"rm -rf { path } " )
12696
127- cache_dirs = [ ".cache" , ".pytest_cache" , ".mypy_cache" , ".ruff_cache" , "__pycache__" ]
128- for dirpath in Path ("." ).rglob ("*" ):
129- if any ( dirpath .match ( pattern ) for pattern in cache_dirs ) and not ( dirpath . match ( ".venv" ) or dirpath . match ( ".venvs" )) :
130- shutil .rmtree (path , ignore_errors = True )
97+ cache_dirs = { ".cache" , ".pytest_cache" , ".mypy_cache" , ".ruff_cache" , "__pycache__" }
98+ for dirpath in Path ("." ).rglob ("*/ " ):
99+ if dirpath .parts [ 0 ] not in ( ".venv" , ".venvs" ) and dirpath . name in cache_dirs :
100+ shutil .rmtree (dirpath , ignore_errors = True )
131101
132102
133103def vscode () -> None :
@@ -152,11 +122,7 @@ def main() -> int:
152122 print (" 3.x Run a command in the virtual environment for Python 3.x." ) # noqa: T201
153123 print (" clean Delete build artifacts and cache files." ) # noqa: T201
154124 print (" vscode Configure VSCode to work on this project." ) # noqa: T201
155- try :
156- run ("default" , "python" , "-V" , capture_output = True )
157- except (subprocess .CalledProcessError , ValueError ):
158- pass
159- else :
125+ if os .path .exists (".venv" ):
160126 print ("\n Available tasks" ) # noqa: T201
161127 run ("default" , "duty" , "--list" )
162128 return 0
0 commit comments