This commit is contained in:
2024-08-08 08:53:47 -04:00
parent 0fa3cb4ca4
commit 573dd92f19
4 changed files with 23 additions and 58 deletions
+3 -7
View File
@@ -2,8 +2,6 @@
# uv pip compile /Users/kennethreitz/repos/uvenv/requirements.in -o /Users/kennethreitz/repos/uvenv/requirements.txt
-e .
# via -r requirements.in
backports-tarfile==1.2.0
# via jaraco-context
certifi==2024.7.4
# via requests
charset-normalizer==3.3.2
@@ -15,9 +13,7 @@ docutils==0.21.2
idna==3.7
# via requests
importlib-metadata==8.2.0
# via
# keyring
# twine
# via twine
jaraco-classes==3.4.0
# via keyring
jaraco-context==5.3.0
@@ -30,7 +26,7 @@ markdown-it-py==3.0.0
# via rich
mdurl==0.1.2
# via markdown-it-py
more-itertools==10.3.0
more-itertools==10.4.0
# via
# jaraco-classes
# jaraco-functools
@@ -64,7 +60,7 @@ urllib3==2.2.2
# via
# requests
# twine
uv==0.2.33
uv==0.2.34
# via uvenv-cli
zipp==3.19.2
# via importlib-metadata
-1
View File
@@ -4,4 +4,3 @@ REQUIREMENTS_IN = os.environ.get("UVENV_REQUIREMENTS_IN", "requirements.in")
REQUIREMENTS_TXT = os.environ.get("UVENV_REQUIREMENTS_TXT", "requirements.txt")
VENV_DIR = os.environ.get("UVENV_VENV_DIR", ".venv")
UV_PATH = os.environ.get("UVENV_UV", "uv")
PYTHON = os.environ.get("UVENV_PYTHON", "python")
+1 -25
View File
@@ -37,17 +37,8 @@ def main():
try:
# Display information about the project.
if args["info"]:
print(f"uvenv {__version__}")
print(f"python {project.path_to_python}")
print(f"venv {project.path_to_venv}")
print(f"project {project.path}")
print(f"requirements {project.path_to_requirements_in}")
print(f"lockfile {project.path_to_requirements_txt}")
# Ensure the project has a virtual environment.
project.ensure_requirements_txt()
project.ensure_venv()
# Report the version of uvenv.
@@ -63,21 +54,6 @@ def main():
# Update the lockfile.
project.lock()
elif args["run"]:
# Get the command to run
command = shlex.join(*args["<command>"])
# Construct the path to the virtual environment's Python interpreter
venv_python = os.path.join(project.path_to_venv, "bin", "python")
# Construct the full command to run in the virtual environment
full_command = f"{venv_python} -c 'import os, sys; os.execvp(sys.argv[1], sys.argv[1:])' {command}"
# Run the command using os.system
exit_code = os.system(full_command)
# Exit with the same code as the command
sys.exit(exit_code >> 8)
except Exception as e:
logger.error(f"An error occurred: {str(e)}")
+19 -25
View File
@@ -3,12 +3,12 @@ import sys
from pathlib import Path
import shlex
from ._constants import REQUIREMENTS_IN, REQUIREMENTS_TXT, VENV_DIR, PYTHON
from ._constants import REQUIREMENTS_IN, REQUIREMENTS_TXT, VENV_DIR
def system_run(*command):
"""Run a system command and return the exit code."""
exit_code = os.system(shlex.join(*command))
exit_code = os.system(shlex.join(command))
return exit_code >> 8
class Project:
@@ -16,35 +16,29 @@ class Project:
self.path = Path(path).resolve()
@classmethod
def from_cwd(cls, *, search_depth=3, search_fname=REQUIREMENTS_TXT):
def from_cwd(cls, *, search_depth=3, search_fnames=[REQUIREMENTS_TXT, REQUIREMENTS_IN]):
"""Find the project root by searching up the directory tree for a file named `requirements.txt`."""
current_path = Path.cwd()
for _ in range(search_depth):
if (current_path / search_fname).exists():
return cls(current_path)
current_path = current_path.parent
raise Exception(f"No {search_fname} found")
for search_fname in search_fnames:
for _ in range(search_depth):
if (current_path / search_fname).exists():
return cls(current_path)
current_path = current_path.parent
def run(self, *command):
"""Run a command in the project."""
python = self.path_to_venv / "bin" / "python"
if not python.exists():
python = PYTHON
command = shlex.join(command)
full_command = f"{python} -c 'import os, sys; os.execvp(sys.argv[1], sys.argv[1:])' {command}"
exit_code = os.system(full_command)
return exit_code >> 8
raise Exception(f"No packaging files found!")
def ensure_venv(self, *args):
"""Ensure the project has a virtual environment."""
if not self.path_to_venv.exists():
self.run("uv", "venv", str(self.path_to_venv), "-p", PYTHON)
system_run("uv", "venv", str(self.path_to_venv))
def ensure_requirements_txt(self):
"""Ensure the project has a `requirements.txt` file."""
if not self.path_to_requirements_txt.exists():
self.lock()
def lock(self):
"""Lock the project's dependencies."""
@@ -53,7 +47,7 @@ class Project:
self.ensure_venv()
# Lock the dependencies.
self.run(
system_run(
"uv",
"pip",
"compile",
@@ -69,7 +63,7 @@ class Project:
self.ensure_venv()
# Install the packages from the lockfile.
self.run("uv", "pip", "install", "-r", str(self.path_to_requirements_txt))
system_run("uv", "pip", "install", "-r", str(self.path_to_requirements_txt))
def uninstall(self, uv, *packages):
"""Uninstall the project's dependencies."""
@@ -78,7 +72,7 @@ class Project:
self.ensure_venv()
# Uninstall the packages.
self.run("uv", "pip", "uninstall", *packages)
system_run("uv", "pip", "uninstall", *packages)
# Remove the packages from the requirements.in file.
with open(self.path_to_requirements_in, "r") as f: