Skip to content

Commit

Permalink
general: switch to pyproject and use uv for CI
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Feb 10, 2025
1 parent e1542a0 commit 365723a
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 153 deletions.
60 changes: 60 additions & 0 deletions .ci/release-uv
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3
'''
Deploys Python package onto [[https://pypi.org][PyPi]] or [[https://test.pypi.org][test PyPi]].
- running manually
You'll need =UV_PUBLISH_TOKEN= env variable
- running on Github Actions
Instead of env variable, relies on configuring github as Trusted publisher (https://docs.pypi.org/trusted-publishers/) -- both for test and regular pypi
It's running as =pypi= job in [[file:.github/workflows/main.yml][Github Actions config]].
Packages are deployed on:
- every master commit, onto test pypi
- every new tag, onto production pypi
'''

UV_PUBLISH_TOKEN = 'UV_PUBLISH_TOKEN'

import argparse
import os
import shutil
from pathlib import Path
from subprocess import check_call

is_ci = os.environ.get('CI') is not None

def main() -> None:
p = argparse.ArgumentParser()
p.add_argument('--use-test-pypi', action='store_true')
args = p.parse_args()

publish_url = ['--publish-url', 'https://test.pypi.org/legacy/'] if args.use_test_pypi else []

root = Path(__file__).absolute().parent.parent
os.chdir(root) # just in case

if is_ci:
# see https://github.com/actions/checkout/issues/217
check_call('git fetch --prune --unshallow'.split())

# TODO ok, for now uv won't remove dist dir if it already exists
# https://github.com/astral-sh/uv/issues/10293
dist = root / 'dist'
if dist.exists():
shutil.rmtree(dist)

# todo what is --force-pep517?
check_call(['uv', 'build'])

if not is_ci:
# CI relies on trusted publishers so doesn't need env variable
assert UV_PUBLISH_TOKEN in os.environ, f'no {UV_PUBLISH_TOKEN} passed'

check_call(['uv', 'publish', *publish_url])


if __name__ == '__main__':
main()
15 changes: 2 additions & 13 deletions .ci/run
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,5 @@ if [ -n "${CI-}" ]; then
esac
fi


PY_BIN="python3"
# some systems might have python pointing to python3
if ! command -v python3 &> /dev/null; then
PY_BIN="python"
fi


# TODO hmm for some reason installing uv with pip and then running
# "$PY_BIN" -m uv tool fails with missing setuptools error??
# just uvx directly works, but it's not present in PATH...
"$PY_BIN" -m pip install --user pipx
"$PY_BIN" -m pipx run uv tool run --with=tox-uv tox $tox_cmd "$@"
# NOTE: expects uv installed
uv tool run --with tox-uv tox $tox_cmd "$@"
21 changes: 14 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ jobs:
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- uses: astral-sh/setup-uv@v5
with:
enable-cache: false # we don't have lock files, so can't use them as cache key

- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -108,6 +112,7 @@ jobs:
- run: .ci/github-ci-compat

- run: |
# FIXME won't need it after pyproject migration??
# --use-pep517 is a work around for some breaking change in setuptools 66.0.0
# see https://github.com/pypa/setuptools/issues/3772#issuecomment-1384671296
python3 -m pip install --use-pep517 .
Expand All @@ -117,7 +122,9 @@ jobs:
pypi:
runs-on: ubuntu-latest
needs: [build, end2end_tests_chrome, end2end_tests_firefox, install_and_run_test]

permissions:
# necessary for Trusted Publishing
id-token: write
steps:
# ugh https://github.com/actions/toolkit/blob/main/docs/commands.md#path-manipulation
- run: echo "$HOME/.local/bin" >> $GITHUB_PATH
Expand All @@ -126,24 +133,24 @@ jobs:
with:
python-version: '3.10'

- uses: astral-sh/setup-uv@v5
with:
enable-cache: false # we don't have lock files, so can't use them as cache key

- uses: actions/checkout@v4
with:
submodules: recursive

- name: 'release to test pypi'
# always deploy merged master to test pypi
if: github.event_name != 'pull_request' && github.event.ref == 'refs/heads/master'
env:
TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD_TEST }}
run: pip3 install --user --upgrade build twine && .ci/release --test
run: .ci/release-uv --use-test-pypi

- name: 'release to pypi'
# always deploy tags to release pypi
# NOTE: release tags are guarded by on: push: tags on the top
if: github.event_name != 'pull_request' && startsWith(github.event.ref, 'refs/tags')
env:
TWINE_PASSWORD: ${{ secrets.TWINE_PASSWORD }}
run: pip3 install --user --upgrade build twine && .ci/release
run: .ci/release-uv

###
build_extension:
Expand Down
124 changes: 124 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# see https://github.com/karlicoss/pymplate for up-to-date reference
[project]
dynamic = ["version"] # version is managed by setuptools_scm
name = "promnesia"
dependencies = [
"appdirs", # for portable user directories detection
"tzlocal", # guessling local timezone
"more_itertools",
"typing-extensions",
"pytz",
"sqlalchemy>=2.0", # DB api

##
# NOTE: ideally we don't need to install them by default?
# i.e. server and indexer can run on different hosts/containers etc
# keeping here for backwards compatibility for now
"promnesia[indexer]",
"promnesia[server]",
##
]
requires-python = ">=3.9"

## these need to be set if you're planning to upload to pypi
description = "Enhancement of your browsing history"
license = {file = "LICENSE"}
authors = [
{name = "Dima Gerasimov (@karlicoss)", email = "[email protected]"},
]
maintainers = [
{name = "Dima Gerasimov (@karlicoss)", email = "[email protected]"},
]
[project.urls]
Homepage = "https://github.com/karlicoss/promnesia"
##


[project.optional-dependencies]
indexer = [
# indexer only dependencies
"urlextract",
]
server = [
# server only dependencies
"fastapi",
"uvicorn[standard]",
]
optional = [
# dependencies that bring some bells & whistles
"logzero" , # pretty colored logging
"python-magic", # better mimetype decetion
]
HPI = [
# dependencies for https://github.com/karlicoss/HPI
"HPI", # pypi version
# TODO add notes how to override with github version?
]
html = [
# dependencies for sources.html
"beautifulsoup4", # extracting links from the page
"lxml" , # bs4 backend
]
markdown = [
# dependencies for sources.html
"mistletoe",
]
org = [
# dependencies for sources.org
"orgparse>=0.3.0",
]
telegram = [
# used to depend on 'dataset', keeping for backwards compatibility
]
all = [
"promnesia[optional,HPI,html,markdown,org]",
]

[dependency-groups]
testing = [
"pytest",
"ruff",
"mypy",
"lxml", # for mypy coverage

"hypothesis",

"loguru", # used in addon_helper... not sure if should just use promnesia's logger?

"psutil", "types-psutil",
"requests", "types-requests",

## other mypy stubs
"types-pytz" ,
"types-requests" , # used in tests
##
]
testing-end2end = [
"selenium" , # browser automations
"click" , # confirmations for end2end test (might remove dependency)
"pytest-timeout", # for PYTEST_TIMEOUT env variable
"pytest-xdist" , # not used atm, but helpful to parallelise end2end tests
]
testing-gui = [
# pyautogui seems problematic, wheels often fail to build under windows
# we don't use it in CI, so keep in a separate extras section
"pyautogui", # for keyboard automation during end2end tests
]


[project.scripts]
promnesia = "promnesia.__main__:main"


[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]
version_scheme = "python-simplified-semver"
local_scheme = "dirty-tag"

# workaround for error during uv publishing
# see https://github.com/astral-sh/uv/issues/9513#issuecomment-2519527822
[tool.setuptools]
license-files = []
118 changes: 0 additions & 118 deletions setup.py

This file was deleted.

Empty file removed src/promnesia/sources/__init__.pyi
Empty file.
Loading

0 comments on commit 365723a

Please sign in to comment.