First commit

This commit is contained in:
tcsenpai 2025-01-01 01:04:11 +01:00
commit 49d672dd27
5 changed files with 211 additions and 0 deletions

0
.gitignore vendored Normal file
View File

7
LICENSE.md Normal file
View File

@ -0,0 +1,7 @@
Copyright 2025 tcsenpai
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

67
README.md Normal file
View File

@ -0,0 +1,67 @@
# uv_utils
uv_utils is a collection of utilities for the [UV project](https://github.com/astral-sh/uv).
## Disclaimer
This project is not affiliated with the UV project.
This project, while tested and used by the author, is not guaranteed to work for everyone. Please try to understand the code before using it.
## Prerequisites
- [uv](https://github.com/astral-sh/uv) and related dependencies
## Installation
```bash
git clone https://github.com/tcsenpai/uv_utils.git
cd uv_utils
chmod +x uv*
```
It is recommended to add the uv_utils directory to your PATH environment variable or to move the executable files to a directory in your PATH.
## Features and Usage
### uvc
uvc is a utility for converting a python's requirements.txt-like file to a fully fledged uv project.
```bash
uvc requirements.txt # replace requirements.txt with your requirements.txt-like file as needed
```
This will create a pyproject.toml file in the current directory, manage the empty hello.py file safely (backing up any existing hello.py file), and add the necessary dependencies to the pyproject.toml file; it will also automate the creation of a .venv directory and install the dependencies into it using uv.
You can now run the project using uv (included `uv run`, `uv sync`, `uv add`, etc.)
**Note:** You can use uvr (see below) to run the project without specifying the file name each time.
### uvr
uvr is a utility for running a uv project without specifying the file name each time.
#### First Run
```bash
uvr name_of_your_entry_file.py
```
This will create a uvr.toml file in the current directory, which will store the name of the entry file.
#### Subsequent Runs
```bash
uvr
```
This will run the entry file specified in the uvr.toml file.
## Under the Hood
All the files are written in plain bash, and are designed to be as simple as possible.
Also, the utilities are designed to be wrappers around `uv` commands, so that full compatibility is maintained.
## License
This project is licensed under the MIT License - see the LICENSE file for details.

90
uvc Executable file
View File

@ -0,0 +1,90 @@
#!/bin/bash
set -euo pipefail # Fail on errors, undefined vars, and pipeline failures
# Function to handle errors
error_exit() {
echo "[X] Error: $1" >&2
exit 1
}
# Check if uv is installed
command -v uv >/dev/null 2>&1 || error_exit "uv is not installed"
# Check write permissions in current directory
if [ ! -w "." ]; then
error_exit "No write permission in current directory"
fi
# SECTION 1: CHECKING THE USER INPUT AND SANITY CHECKS
echo "[*] Checking the user input and sanity checks"
# If .venv exists, ask the user if they want to remove it
if [ -d ".venv" ]; then
echo "[*] Directory .venv exists: do you want to remove it? (y/n)"
read -r answer
case "${answer,,}" in # Convert to lowercase
y|yes)
rm -rf .venv || error_exit "Failed to remove .venv directory"
echo "[OK] Directory .venv removed"
;;
*)
echo "[X] Directory .venv exists and was not removed: exiting"
exit 1
;;
esac
else
echo "[OK] Directory .venv does not exist: we can proceed"
fi
# Checking if the user specified a file to add to the project
# If no argument is passed, add requirements.txt as a default
if [ -z "$1" ]; then
echo "[*] No file specified: adding requirements.txt as a default"
FILE="requirements.txt"
# If an argument is passed, add it to the project instead of requirements.txt
else
echo "[*] File specified: adding $1"
FILE="$1"
fi
# Also, the specified file should exist
if [ ! -f "$FILE" ]; then
echo "[X] File $FILE does not exist: exiting"
exit 1
fi
echo "[OK] File $FILE exists"
echo "[OK] All sanity checks passed"
# SECTION 2: INITIALIZING THE UV PROJECT
echo "[*] Initializing the uv project"
# Create backup with timestamp to avoid conflicts
BACKUP_SUFFIX=".bak.$(date +%Y%m%d%H%M%S)"
if [ -f hello.py ]; then
echo "[*] File hello.py exists: backing up"
mv "hello.py" "hello.py${BACKUP_SUFFIX}" || error_exit "Failed to backup hello.py"
fi
# Initialize the uv project with error handling
if ! uv init; then
error_exit "Failed to initialize uv project"
fi
# Remove the hello.py file (if it exists)
echo "[*] Removing hello.py created by uv init"
rm -rf hello.py || true
# If a hello.py.bak file exists, restore it (use BACKUP_SUFFIX)
if [ -f "hello.py${BACKUP_SUFFIX}" ]; then
echo "[*] Restoring hello.py from backup"
mv "hello.py${BACKUP_SUFFIX}" hello.py
fi
echo "[OK] Initialized the uv project"
# SECTION 3: ADDING THE REQUIREMENTS.TXT OR SPECIFIED FILE TO THE PROJECT
echo "[*] Adding $FILE to the project"
uv add -r "$FILE"
echo "[OK] Added $FILE to the project"
echo "[OK] All uv commands passed! You can now run the project with uv run."

47
uvr Executable file
View File

@ -0,0 +1,47 @@
#!/bin/bash
set -euo pipefail # Fail on errors, undefined vars, and pipeline failures
# Check if a file named uvr.toml exists
if [ ! -f uvr.toml ]; then
echo "[X] File uvr.toml does not exist"
if [ $# -eq 0 ]; then
echo "[X] No script name passed: exiting"
exit 1
fi
# Validate input is a .py file
if [[ ! "$1" =~ \.py$ ]]; then
echo "[X] Script must be a Python file (.py extension)"
exit 1
fi
echo "script=$1" > uvr.toml
echo "[OK] Created uvr.toml with script=$1"
fi
# Read and validate the script from uvr.toml
SCRIPT=$(grep -oP 'script=\K[^ ]+' uvr.toml || echo "")
if [ -z "$SCRIPT" ]; then
echo "[X] Invalid or empty script in uvr.toml"
exit 1
fi
# Validate script exists and is a Python file
if [ ! -f "$SCRIPT" ]; then
echo "[X] File $SCRIPT does not exist: exiting"
exit 1
fi
if [[ ! "$SCRIPT" =~ \.py$ ]]; then
echo "[X] $SCRIPT is not a Python file"
exit 1
fi
# Run the script with error handling
echo "[*] Running the script with uv run"
if ! uv run "$SCRIPT"; then
echo "[X] Script execution failed"
exit 1
fi