Skip to content

Commit

Permalink
⚡ migrate to uv for packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgafni committed Aug 22, 2024
1 parent aff5ad4 commit 026877d
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[tool.uv]
dev-dependencies = [ "ruff==0.5.5",]
[tool.uv.workspace]
members = [ "python_modules/dagster-pipes",]


# ########################
# ##### PYRIGHT
# ########################
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions python_modules/dagster-pipes/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[project]
name = "dagster-pipes"
version = "1!0+dev"
description = "Toolkit for Dagster integrations with transform logic outside of Dagster"
readme = "README.md"
requires-python = ">=3.8,<3.13"
dependencies = []
classifiers = [ "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9",]
[[project.authors]]
name = "Dagster Labs"
email = "[email protected]"

[build-system]
requires = [ "hatchling",]
build-backend = "hatchling.build"

[project.license]
text = "Apache-2.0"

[project.urls]
Homepage = "https://github.com/dagster-io/dagster/tree/master/python_modules/dagster-pipes"
102 changes: 102 additions & 0 deletions scripts/migrate_to_uv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import os
import pathlib
import sys

import toml

ROOT_DIR = pathlib.Path(__file__).parent.parent
VIRTUAL_ENV = ROOT_DIR / ".venv"
ROOT_PYPROJECT = ROOT_DIR / "pyproject.toml"
PYTHON = sys.executable
MIGRATED_MARKER = ".uv_migrated"


def get_projects_to_migrate():
# find all directories with setup.py usin glob
projects_with_setup_py = [p.parent for path in pathlib.Path(".").rglob("setup.py")]

return projects


def cleanup_pyproject_after_pdm(file_path: str):
# remove tool.pdb and change build backend to setuptools.build_meta

with open(file_path, "r") as file:
data = toml.load(file)

if "tool" in data:
del data["tool"]
data["build-system"] = {
"requires": ["hatchling"],
"build-backend": "hatchling.build",
}

with open(file_path, "w") as file:
toml.dump(data, file)


def migrate_to_uv(project_dir: pathlib.Path, run_tests: bool = False):
project_dir = ROOT_DIR / project_dir

# change current directory to project_dir
os.chdir(str(project_dir))
# convert setup.py to pyproject.toml using pdm
os.system("pdm import -v setup.py")

# cleanup pyproject.toml
cleanup_pyproject_after_pdm("pyproject.toml")

# use uv src/ layout

with open("pyproject.toml", "r") as file:
package_name = toml.load(file)["project"]["name"].replace("-", "_")

os.system("mkdir -p src")
os.system(f"cp -r {package_name} src/")

# temporary remove the old package directory (first just move it to a temp location)
os.system(f"mv {package_name} {package_name}-old")

# try installing the project with uv to test if it works
os.system("uv pip install -e .")

if run_tests:
# run tests
try:
os.system("pytest .")
except:
# restore the old package directory
os.system(f"mv {package_name}-old {package_name}")
raise

with ROOT_PYPROJECT.open("r") as file:
root_pyproject = toml.load(file)

project_dir_relpath = project_dir.relative_to(ROOT_DIR)

if str(project_dir_relpath) not in root_pyproject["tool"]["uv"]["workspace"]["members"]:
root_pyproject["tool"]["uv"]["workspace"]["members"].append(str(project_dir_relpath))

with ROOT_PYPROJECT.open("w") as file:
toml.dump(root_pyproject, file)

# delete the old package directory
os.system(f"rm -rf {package_name}-old")

# mark project as migrated
(project_dir / MIGRATED_MARKER).touch()


if __name__ == "__main__":
# os.environ["PDM_IGNORE_ACTIVE_VENV"] = "false"
# os.environ["VIRTUAL_ENV"] = str(VIRTUAL_ENV)

projects = [pathlib.Path("python_modules/dagster-pipes")] # get_projects_to_migrate()

projects = [p for p in projects if not (p / MIGRATED_MARKER).exists()]

for project in projects:
try:
migrate_to_uv(project, run_tests=True)
except Exception as e:
print(f"Error migrating {project}: {e}")
37 changes: 37 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 026877d

Please sign in to comment.