|
| 1 | +#!/usr/bin/env python3 |
| 2 | +''' |
| 3 | +Deploys Python package onto [[https://pypi.org][PyPi]] or [[https://test.pypi.org][test PyPi]]. |
| 4 | +
|
| 5 | +- running manually |
| 6 | +
|
| 7 | + You'll need =UV_PUBLISH_TOKEN= env variable |
| 8 | +
|
| 9 | +- running on Github Actions |
| 10 | +
|
| 11 | + Instead of env variable, relies on configuring github as Trusted publisher (https://docs.pypi.org/trusted-publishers/) -- both for test and regular pypi |
| 12 | +
|
| 13 | + It's running as =pypi= job in [[file:.github/workflows/main.yml][Github Actions config]]. |
| 14 | + Packages are deployed on: |
| 15 | + - every master commit, onto test pypi |
| 16 | + - every new tag, onto production pypi |
| 17 | +''' |
| 18 | + |
| 19 | +UV_PUBLISH_TOKEN = 'UV_PUBLISH_TOKEN' |
| 20 | + |
| 21 | +import argparse |
| 22 | +import os |
| 23 | +import shutil |
| 24 | +from pathlib import Path |
| 25 | +from subprocess import check_call |
| 26 | + |
| 27 | +is_ci = os.environ.get('CI') is not None |
| 28 | + |
| 29 | +def main() -> None: |
| 30 | + p = argparse.ArgumentParser() |
| 31 | + p.add_argument('--use-test-pypi', action='store_true') |
| 32 | + args = p.parse_args() |
| 33 | + |
| 34 | + publish_url = ['--publish-url', 'https://test.pypi.org/legacy/'] if args.use_test_pypi else [] |
| 35 | + |
| 36 | + root = Path(__file__).absolute().parent.parent |
| 37 | + os.chdir(root) # just in case |
| 38 | + |
| 39 | + if is_ci: |
| 40 | + # see https://github.com/actions/checkout/issues/217 |
| 41 | + check_call('git fetch --prune --unshallow'.split()) |
| 42 | + |
| 43 | + # TODO ok, for now uv won't remove dist dir if it already exists |
| 44 | + # https://github.com/astral-sh/uv/issues/10293 |
| 45 | + dist = root / 'dist' |
| 46 | + if dist.exists(): |
| 47 | + shutil.rmtree(dist) |
| 48 | + |
| 49 | + # todo what is --force-pep517? |
| 50 | + check_call(['uv', 'build']) |
| 51 | + |
| 52 | + if not is_ci: |
| 53 | + # CI relies on trusted publishers so doesn't need env variable |
| 54 | + assert UV_PUBLISH_TOKEN in os.environ, f'no {UV_PUBLISH_TOKEN} passed' |
| 55 | + |
| 56 | + check_call(['uv', 'publish', *publish_url]) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + main() |
0 commit comments