Skip to content

Commit

Permalink
Merge pull request #2 from Canadian-Light-Source/testpypi
Browse files Browse the repository at this point in the history
TestPyPI publishing
  • Loading branch information
kivel authored Dec 19, 2024
2 parents 9e579ad + 09bb693 commit e6d83dc
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 42 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on:
workflow_call:
outputs:
branch-pr:
description: The PR number if the branch is in one
value: ${{ jobs.pr.outputs.branch-pr }}

jobs:
pr:
runs-on: "ubuntu-latest"
outputs:
branch-pr: ${{ steps.script.outputs.result }}
steps:
- uses: actions/github-script@v7
id: script
if: github.event_name == 'push'
with:
script: |
const prs = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: context.repo.owner + ':${{ github.ref_name }}'
})
if (prs.data.length) {
console.log(`::notice ::Skipping CI on branch push as it is already run in PR #${prs.data[0]["number"]}`)
return prs.data[0]["number"]
}
27 changes: 27 additions & 0 deletions .github/workflows/_dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on:
workflow_call:

jobs:
build:
runs-on: "ubuntu-latest"

steps:
- name: Checkout
uses: actions/checkout@v4
with:
# Need this to get version number from last tag
fetch-depth: 0

- name: Build sdist and wheel
run: >
export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) &&
pipx run build
- name: Upload sdist and wheel as artifacts
uses: actions/upload-artifact@v4
with:
name: dist
path: dist

- name: Check for packaging errors
run: pipx run twine check --strict dist/*
26 changes: 26 additions & 0 deletions .github/workflows/_pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
on:
workflow_call:

jobs:
upload:
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
steps:
- name: Download dist artifact
uses: actions/download-artifact@v4
with:
name: dist
path: dist

# - name: Publish to PyPI using trusted publishing
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# attestations: false

- name: Publish package distributions to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
attestations: false
27 changes: 27 additions & 0 deletions .github/workflows/_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Pytest
on:
workflow_call:
inputs:
python-version:
type: string
description: The version of python to install
required: true
runs-on:
type: string
description: The runner to run this job on
required: true

jobs:
pytest:
runs-on: ${{ inputs.runs-on }}
steps:
- uses: actions/checkout@v4
- name: Install the latest version of uv and set the python version
uses: astral-sh/setup-uv@v4
with:
python-version: ${{ inputs.python-version }}
- name: Test with python ${{ matrix.python-version }}
run: |
uv run --frozen pytest
uv run --frozen coverage run -m pytest -v
uv run --frozen coverage report
49 changes: 49 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

on:
push:
pull_request:

jobs:

pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: pre-commit/action@v3.0.1

check:
uses: ./.github/workflows/_check.yml

lint:
needs: check
if: needs.check.outputs.branch-pr == ''
uses: ./.github/workflows/ruff.yml

test:
needs: check
if: needs.check.outputs.branch-pr == ''
strategy:
matrix:
# runs-on: ["ubuntu-latest", "windows-latest", "macos-latest"]
runs-on: ["ubuntu-latest"] # Linux for now
# python-version: ["3.10","3.11","3.12"]
python-version: ["3.12"] # single Python version for now
fail-fast: false
uses: ./.github/workflows/_test.yml
with:
runs-on: ${{ matrix.runs-on }}
python-version: ${{ matrix.python-version }}

dist:
needs: check
if: needs.check.outputs.branch-pr == ''
uses: ./.github/workflows/_dist.yml

pypi:
if: github.ref_type == 'tag'
needs: dist
uses: ./.github/workflows/_pypi.yml
# permissions:
# id-token: write
8 changes: 6 additions & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
# python-version: ["3.10", "3.11", "3.12"]
python-version: ["3.12"] # single Python version for now
steps:
- uses: actions/checkout@v4
- name: Install the latest version of uv and set the python version
uses: astral-sh/setup-uv@v4
with:
python-version: ${{ matrix.python-version }}
- name: Test with python ${{ matrix.python-version }}
run: uv run --frozen pytest
run: |
uv run --frozen pytest
coverage run -m pytest -v
coverage report
9 changes: 9 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Ruff
on:
workflow_call:
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,4 @@ cython_debug/

# VSCode
.vscode
src/bluesky_nats/_version.py
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-ast
- id: check-case-conflict
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
- id: debug-statements
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# bluesky-nats

This module allows to:

* publish Bluesky documents to a NATS JetStream
* consume a NATS JetStream

## Installation

Install from pypi
```bash
uv add bluesky-nats
```

Install with dependecies to run the `examples`
```bash
uv add bluesky-nats --extra examples
```

## Prerequisites

The module requires a NATS JetStream-enabled server.
If not explicitly specified, the Publisher will automatically publish to the NATS JetStream "bluesky".
Make sure that the respective JetStream is available on the server and that your "subject" pattern matches the configuration.

A simple Docker setup for local development and testing is provided in the [docker](https://github.com/Canadian-Light-Source/bluesky-nats/tree/main/docker) directory, with a [Readme](https://github.com/Canadian-Light-Source/bluesky-nats/tree/main/docker/Readme.adoc) for guidance.

## Examples

This is the most basic example to create a NATS publisher subscribed to RE documents.
```python
from bluesky.run_engine import RunEngine

from bluesky_nats.nats_client import NATSClientConfig
from bluesky_nats.nats_publisher import CoroutineExecutor, NATSPublisher

if __name__ == "__main__":
RE = RunEngine({})
config = NATSClientConfig()

nats_publisher = NATSPublisher(
client_config=config,
executor=CoroutineExecutor(RE.loop),
subject_factory="events.nats-bluesky",
)

RE.subscribe(nats_publisher)
```

Follow the [instructions](https://github.com/Canadian-Light-Source/bluesky-nats/tree/main/examples) for more information about the examples.
6 changes: 3 additions & 3 deletions docker/Readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ docker compose up
[source,bash]
----
[+] Running 1/1
✔ Container nats Created 0.1s
✔ Container nats Created 0.1s
Attaching to nats
nats | [1] 2024/09/19 16:57:00.546060 [INF] Starting nats-server
nats | [1] 2024/09/19 16:57:00.546847 [INF] Version: 2.10.18
Expand All @@ -48,9 +48,9 @@ nats | [1] 2024/09/19 16:57:00.549017 [INF] _ ___ _____ ___ _____ ___ ___
nats | [1] 2024/09/19 16:57:00.549034 [INF] _ | | __|_ _/ __|_ _| _ \ __| /_\ | \/ |
nats | [1] 2024/09/19 16:57:00.549037 [INF] | || | _| | | \__ \ | | | / _| / _ \| |\/| |
nats | [1] 2024/09/19 16:57:00.549040 [INF] \__/|___| |_| |___/ |_| |_|_\___/_/ \_\_| |_|
nats | [1] 2024/09/19 16:57:00.549044 [INF]
nats | [1] 2024/09/19 16:57:00.549044 [INF]
nats | [1] 2024/09/19 16:57:00.549047 [INF] https://docs.nats.io/jetstream
nats | [1] 2024/09/19 16:57:00.549050 [INF]
nats | [1] 2024/09/19 16:57:00.549050 [INF]
nats | [1] 2024/09/19 16:57:00.549075 [INF] ---------------- JETSTREAM ----------------
nats | [1] 2024/09/19 16:57:00.549083 [INF] Max Memory: 5.82 GB
nats | [1] 2024/09/19 16:57:00.549087 [INF] Max Storage: 25.50 GB
Expand Down
1 change: 0 additions & 1 deletion examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
"""nats-bluesky examples."""

2 changes: 1 addition & 1 deletion examples/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"inbox_prefix": "_INBOX",
"pending_size": 2097152,
"flush_timeout": null
}
}
20 changes: 10 additions & 10 deletions examples/config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
servers:
- nats://localhost:4222
name:
name:
pedantic: false
verbose: false
allow_reconnect: true
Expand All @@ -13,16 +13,16 @@ max_outstanding_pings: 2
dont_randomize: false
flusher_queue_size: 1024
no_echo: false
tls:
tls_hostname:
tls:
tls_hostname:
tls_handshake_first: false
user:
password:
token:
user:
password:
token:
drain_timeout: 30
user_credentials:
nkeys_seed:
nkeys_seed_str:
user_credentials:
nkeys_seed:
nkeys_seed_str:
inbox_prefix: "_INBOX"
pending_size: 2097152
flush_timeout:
flush_timeout:
31 changes: 29 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
[project]
name = "bluesky-nats"
version = "0.1.2"
description = "NATS JetStream connectivity for Bluesky"
authors = [
{name = "Niko Kivel", email = "niko.kivel@lightsource.ca"},
]
maintainers = [
{ name = "Canadian Lightsource Inc.", email = "niko.kivel@lightsource.ca" },
]

readme = "README.md"
license.file = "LICENSE"
requires-python = ">=3.10"

classifiers = [
"Development Status :: 3 - Alpha",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering :: Physics",
]

dynamic = ["version"]

dependencies = [
"bluesky>=1.12.0",
"nats-py>=2.9.0",
"ormsgpack>=1.5.0",
]

[project.urls]
GitHub = "https://github.com/Canadian-Light-Source/bluesky-nats"

[project.optional-dependencies]
examples = [
"matplotlib>=3.9.2",
"ophyd>=1.9.0",
]

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

[tool.hatch]
version.source = "vcs"
version.fallback-version = "0.0.0"
build.hooks.vcs.version-file = "src/bluesky_nats/_version.py"

[tool.uv]
dev-dependencies = [
"pytest-mock>=3.14.0",
Expand All @@ -34,6 +60,7 @@ dev-dependencies = [
"ruff>=0.8.3",
"toml>=0.10.2",
"coverage>=7.6.1",
"pre-commit>=4.0.1",
]

[tool.ruff]
Expand Down
Loading

0 comments on commit e6d83dc

Please sign in to comment.