-
-
Save rgilton/8bbc1aa0810fed454bd47d37eb32051f to your computer and use it in GitHub Desktop.
uvs: A stand-in for the yet-to-exist `uv shell` command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# uvs: A stand-in for the yet-to-exist `uv shell` subcommand | |
# | |
# Copyright 2024 Rob Gilton | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions | |
# are met: | |
# | |
# 1. Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in | |
# the documentation and/or other materials provided with the | |
# distribution. | |
# 3. Neither the name of the copyright holder nor the names of its | |
# contributors may be used to endorse or promote products derived | |
# from this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
# “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
# POSSIBILITY OF SUCH DAMAGE. | |
from itertools import chain | |
from subprocess import run | |
from tempfile import NamedTemporaryFile | |
from pathlib import Path | |
import os | |
import sys | |
from typing import NoReturn | |
def require(condition: bool, error_msg: str) -> None: | |
"Require that condition is true, otherwise exit with the given message" | |
if not condition: | |
print("Error:", error_msg, file=sys.stderr) | |
exit(1) | |
def find_venv(root: Path) -> Path | None: | |
"Find the closest virtualenv" | |
root = root.absolute() | |
for parent in chain([root], root.parents): | |
venv = parent / ".venv" | |
if venv.exists(): | |
return venv | |
def pretty_path(path: Path) -> Path: | |
"Get a pretty, relative path to the given path from the working directory" | |
return path.relative_to(Path.cwd(), walk_up=True) | |
shell = Path(os.environ["SHELL"]) | |
require(shell.name == "bash", "This script only currently supports the bash shell") | |
require("VIRTUAL_ENV" not in os.environ, "Already within virtualenv") | |
venv = find_venv(Path.cwd()) | |
require(venv is not None, "No venv found") | |
activate = venv / "bin" / "activate" | |
require(activate.exists(), f"Virtualenv at {venv} missing activate script") | |
env_file = venv.parent / ".env" | |
rc_lines = ["source $HOME/.bashrc", f"source {activate}"] | |
if env_file.exists(): | |
print(f"Using environment from {pretty_path(env_file)}") | |
rc_lines += ["set -a", f"source {env_file}", "set +a"] | |
with NamedTemporaryFile("w", delete_on_close=False) as rc: | |
rc.write("\n".join(rc_lines)) | |
rc.close() | |
print(f"Entering virtualenv {pretty_path(venv)}") | |
run(["bash", "--rcfile", rc.name]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just updated to support
.env
files, and reduce the verbosity of the printed paths.