Skip to content

Commit

Permalink
chore: migrate to uv and hatch (#188)
Browse files Browse the repository at this point in the history
* chore: migrate to uv and hatch

* chore: revert to pypa upload action, uv publish is still experimental

* docs: document uv usage in DEVELOPMENT.md

* chore: review comments

* chore: move version comment to DEVELOPMENT.md instead
  • Loading branch information
Askir authored Nov 5, 2024
1 parent f4d8d73 commit 627cf33
Show file tree
Hide file tree
Showing 12 changed files with 2,057 additions and 119 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,21 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Set up Python
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: "0.4.29"
enable-cache: true
cache-dependency-glob: "./projects/pgai/uv.lock"

- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip" # caching pip dependencies
python-version-file: "./projects/pgai/.python-version"

- name: Install dev/test dependencies
- name: Install dependencies
working-directory: ./projects/pgai
run: pip install -r requirements-dev.txt
run: uv sync

- name: Lint
working-directory: ./projects/pgai
Expand Down
24 changes: 16 additions & 8 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,27 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
cache: "pip"

- name: Install pypa/build
run: python -m pip install build --user
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
version: "0.4.29"
enable-cache: true
cache-dependency-glob: "./projects/pgai/uv.lock"

- name: Build a binary wheel and a source tarball
run: python -m build --sdist --wheel --outdir dist/ projects/pgai
- name: "Set up Python"
uses: actions/setup-python@v5
with:
python-version-file: "./projects/pgai/.python-version"

- name: Build the project
working-directory: ./projects/pgai
run: uv build --no-sources

- name: Publish distribution to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: ./projects/pgai/dist

release-docker:
name: Publish to Docker Hub
Expand Down
28 changes: 22 additions & 6 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,32 @@ Prior to pgai v0.4.0, Python dependencies were installed system-wide. Until pgai
## Working on the pgai library
The experience of working on the pgai library is like developing most Python
libraries and applications. Use the [requirements-dev.txt](./projects/pgai/requirements-dev.txt)
file to create a virtual env for development.
libraries and applications. We use [uv](https://docs.astral.sh/uv/getting-started/installation/) to manage dependencies and python versions. Once you have uv installed it's easy to get started:
```bash
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements-dev.txt
uv python install 3.10
uv sync
```
Use the `help` target of the [Makefile](./projects/pgai/Makefile) to see what
Note: We try to somewhat follow the python release schedule for supported versions to allow more users to use our library.
Therefore we are about a year behind the latest python release.
Uv syncs the dependencies of all developers working on the project via the uv.lock file. If you want to add a new dependency make use of the uv add command:
```bash
uv add <package-name>
```
If it is a development dependency and not needed at runtime, you can add the --dev flag:
```bash
uv add --dev <package-name>
```
Uv installs all dependencies inside a virtual environment by default you can either activate this via the `uv shell` command or run commands directly via `uv run`.
For the most common commands use our [Makefile](./projects/pgai/Makefile).
Use the `help` target to see what
commands are available.
```bash
Expand Down
1 change: 1 addition & 0 deletions projects/pgai/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10
25 changes: 19 additions & 6 deletions projects/pgai/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
FROM python:3.12-slim AS build
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
UV_PROJECT_ENVIRONMENT=/usr/local/
WORKDIR /build
COPY --from=ghcr.io/astral-sh/uv:0.4.29 /uv /uvx /bin/

COPY pyproject.toml uv.lock ./

RUN uv sync --frozen --no-install-project --no-dev

FROM python:3.12-slim
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
PYTHONUNBUFFERED=1
WORKDIR /app

RUN useradd -ms /bin/bash pgaiuser

COPY ./requirements.txt /app/
RUN pip3 install --no-cache-dir --disable-pip-version-check --compile --root-user-action=ignore -r requirements.txt
# Copy installed dependencies from build stage
COPY --from=build /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages

USER pgaiuser
COPY pyproject.toml /app
COPY pyproject.toml /app/
COPY pgai /app/pgai

USER pgaiuser

ENTRYPOINT ["python", "-m", "pgai", "vectorizer", "worker"]
CMD ["-c", "4"]
CMD ["-c", "4"]
23 changes: 13 additions & 10 deletions projects/pgai/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

VERSION := $(shell awk "/^__version__ = .*/ {gsub(/__version__ = |\"/, \"\"); print}" ./pgai/__init__.py)

.PHONY: default
Expand Down Expand Up @@ -40,35 +39,39 @@ clean:
@rm -rf ./build
@rm -rf ./pgai.egg-info
@rm -rf ./dist
@rm -rf ./.ruff_cache
@rm -rf ./.pytest_cache
@rm -rf ./.mypy_cache
@find . -type d -name "__pycache__" -exec rm -rf {} +

.PHONY: build
build:
@python3 -m build --sdist --wheel
@twine check ./dist/*
@uv build
@uv run twine check ./dist/*

.PHONY: install
install:
@pip3 install -v --compile "./dist/pgai-$(VERSION)-py3-none-any.whl"
@uv sync

.PHONY: uninstall
uninstall:
@pip3 uninstall -v -y pgai
@uv pip uninstall -y pgai

.PHONY: test
test:
@pytest
@uv run pytest

.PHONY: lint
lint:
@ruff check ./
@uv run ruff check ./

.PHONY: type-check
type-check:
@pyright ./
@uv run pyright ./

.PHONY: format
format:
@ruff format --diff ./
@uv run ruff format --diff ./

.PHONY: docker-build
docker-build:
Expand All @@ -89,4 +92,4 @@ docker-rm:
.PHONY: install-commit-hook
install-commit-hook:
@cd ../.. && curl --fail -o .git/hooks/commit-msg https://raw.githubusercontent.com/hazcod/semantic-commit-hook/master/commit-msg \
&& chmod 500 .git/hooks/commit-msg
&& chmod 500 .git/hooks/commit-msg
113 changes: 62 additions & 51 deletions projects/pgai/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "pgai"
description = "AI workflows in your PostgreSQL database"
dynamic = ["version", "dependencies"]
requires-python = ">=3.10"
readme = "README.md"
keywords = ["ai", "postgres"]
dynamic = ["version"]
dependencies = [
"click>=8.0,<9.0",
"psycopg[binary]>=3.2,<4.0",
"langchain-openai>=0.1,<1.0",
"langchain-text-splitters>=0.2,<1.0",
"pydantic>=2.0,<3.0",
"openai>=1.44,<2.0",
"python-dotenv>=1.0,<2.0",
"structlog>=24.0,<25.0",
"pgvector>=0.3,<1.0",
"tiktoken>=0.7,<1.0",
"typing_extensions>=4.0,<5.0",
"datadog_lambda>=6.9,<7.0",
"pytimeparse>=1.1,<2.0",
]
classifiers = [
"License :: OSI Approved :: PostgreSQL License",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Database",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Typing :: Typed",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Operating System :: POSIX",
"Operating System :: MacOS",
"Operating System :: POSIX :: Linux",
"License :: OSI Approved :: PostgreSQL License",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Database",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Typing :: Typed",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Operating System :: POSIX",
"Operating System :: MacOS",
"Operating System :: POSIX :: Linux",
]

[project.urls]
Expand All @@ -32,14 +47,8 @@ Repository = "https://github.com/timescale/pgai"
"Bug Tracker" = "https://github.com/timescale/pgai/issues"
Documentation = "https://github.com/timescale/pgai/tree/main/docs"

[tool.setuptools.dynamic]
version = {attr = "pgai.__version__"}
dependencies = {file = "requirements.txt"}

[tool.setuptools.packages.find]
where = ["."] # list of folders that contain the packages (["."] by default)
include = ["pgai*"] # package names should match these glob patterns (["*"] by default)
namespaces = false # to disable scanning PEP 420 namespaces (true by default)
[tool.hatch.version]
path = "pgai/__init__.py"

[project.scripts]
pgai = "pgai.cli:cli"
Expand All @@ -48,51 +57,53 @@ pgai = "pgai.cli:cli"
addopts = [
"--import-mode=importlib",
]
python_files = ["test_*.py"]

[tool.pyright]
# this enables practically every flag given by pyright.
# there are a couple of flags that are still disabled by
# default in strict mode as they are experimental and niche.
typeCheckingMode = "strict"

exclude = [
".venv",
".venv",
]
reportImplicitOverride = true

[tool.ruff]
line-length = 88
indent-width = 4
output-format = "grouped"
target-version = "py310"

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.lint]

select = [
# pycodestyle
"E",
# Pyflakes
"F",
# pyupgrade
"UP",
# flake8-bugbear
"B",
# flake8-simplify
"SIM",
# isort
"I",
# unused arguments
"ARG",
# trailing whitespace
"W291",
# print statements
"PIE",
# flakes8-quote
"Q"
"E", # pycodestyle
"F", # Pyflakes
"UP", # pyupgrade
"B", # flake8-bugbear
"SIM", # flake8-simplify
"I", # isort
"ARG", # unused arguments
"W291", # trailing whitespace
"PIE", # print statements
"Q" # flakes8-quote
]

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[tool.hatch.metadata]
allow-direct-references = true

[tool.uv]
dev-dependencies = [
"ruff==0.6.9",
"pytest==8.3.2",
"python-dotenv==1.0.1",
"vcrpy==6.0.1",
"pyright==1.1.385",
"psycopg[binary]==3.2.1",
"testcontainers==4.8.1",
"build==1.2.2.post1",
"twine==5.1.1",
]
2 changes: 0 additions & 2 deletions projects/pgai/pytest.ini

This file was deleted.

6 changes: 0 additions & 6 deletions projects/pgai/requirements-dev.txt

This file was deleted.

12 changes: 0 additions & 12 deletions projects/pgai/requirements-test.txt

This file was deleted.

13 changes: 0 additions & 13 deletions projects/pgai/requirements.txt

This file was deleted.

Loading

0 comments on commit 627cf33

Please sign in to comment.