Last active
September 27, 2024 16:53
-
-
Save wasdee/926cf96f80ab221bf9f5e5b28341e02d to your computer and use it in GitHub Desktop.
migrate from poetry to uv bash script
This file contains hidden or 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
#!/bin/bash | |
# Not Tested, Generated by ChatGPT o1 | |
# https://x.com/tiangolo/status/1839686032326836723 | |
# Check if uv is in PATH | |
if ! command -v uv &> /dev/null | |
then | |
echo "Error: uv is not installed or not in your PATH." | |
echo "Please install uv before running this script." | |
exit 1 | |
fi | |
echo "uv is installed. Proceeding with migration..." | |
cp pyproject.toml pyproject.toml.backup | |
echo "old pyproject.toml is backuped as pyproject.toml.backup" | |
# Ensure that Python 3.11 or newer is being used | |
PYTHON_VERSION=$(python3 -c 'import sys; print(sys.version_info >= (3, 11))') | |
if [ "$PYTHON_VERSION" != "True" ]; then | |
echo "Error: Python 3.11 or newer is required." | |
exit 1 | |
fi | |
# Step 1: Use uvx to import pyproject.toml | |
echo "Importing pyproject.toml using uvx..." | |
uvx pdm import pyproject.toml | |
# Step 2: Remove all [tool.poetry] sections from pyproject.toml | |
echo "Removing [tool.poetry] sections from pyproject.toml..." | |
python3 - << EOF | |
import tomllib | |
import tomli_w | |
with open('pyproject.toml', 'rb') as file: | |
data = tomllib.load(file) | |
# Remove the [tool.poetry] section | |
if 'tool' in data and 'poetry' in data['tool']: | |
del data['tool']['poetry'] | |
# If 'tool' is empty after deletion, remove it | |
if not data['tool']: | |
del data['tool'] | |
with open('pyproject.toml', 'wb') as file: | |
file.write(tomli_w.dumps(data).encode('utf-8')) | |
EOF | |
# Step 3: Modify dev dependencies section to [tool.uv] | |
echo "Modifying dev dependencies section to [tool.uv]..." | |
python3 - << EOF | |
import tomllib | |
import tomli_w | |
with open('pyproject.toml', 'rb') as file: | |
data = tomllib.load(file) | |
# Extract dev dependencies from [tool.pdm.dev-dependencies] | |
dev_deps = data.get('tool', {}).get('pdm', {}).get('dev-dependencies', {}) | |
dev = dev_deps.get('dev', []) | |
# Modify to [tool.uv] format | |
if dev: | |
data.setdefault('tool', {}).setdefault('uv', {})['dev-dependencies'] = dev | |
# Remove the original [tool.pdm.dev-dependencies] section | |
if 'pdm' in data['tool']: | |
del data['tool']['pdm']['dev-dependencies'] | |
# If 'pdm' is empty after deletion, remove it | |
if not data['tool']['pdm']: | |
del data['tool']['pdm'] | |
# If 'tool' is empty after deletion, remove it | |
if not data['tool']: | |
del data['tool'] | |
with open('pyproject.toml', 'wb') as file: | |
file.write(tomli_w.dumps(data).encode('utf-8')) | |
EOF | |
# Step 4: Run uv sync to set up the environment | |
echo "Running uv sync..." | |
uv sync | |
echo "Migration from Poetry to uv is complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment