Skip to content

Commit

Permalink
Merge pull request #16 from torqs-project/cleanup-for-pip
Browse files Browse the repository at this point in the history
Cleanup for pip
  • Loading branch information
vstirbu authored Nov 6, 2024
2 parents 6e097be + 1b5d553 commit 84c7d7e
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 31 deletions.
119 changes: 119 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI

on: push

jobs:
build:
name: Build distribution 📦
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x"
- name: Install pypa/build
run: >-
python3 -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: python3 -m build
- name: Store the distribution packages
uses: actions/upload-artifact@v4
with:
name: python-package-distributions
path: dist/

publish-to-pypi:
name: >-
Publish Python 🐍 distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
needs:
- build
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/q8s # Replace <package-name> with your PyPI project name
permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing

steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to PyPI
uses: pypa/gh-action-pypi-publish@release/v1

github-release:
name: >-
Sign the Python 🐍 distribution 📦 with Sigstore
and upload them to GitHub Release
needs:
- publish-to-pypi
runs-on: ubuntu-latest

permissions:
contents: write # IMPORTANT: mandatory for making GitHub Releases
id-token: write # IMPORTANT: mandatory for sigstore

steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Sign the dists with Sigstore
uses: sigstore/[email protected]
with:
inputs: >-
./dist/*.tar.gz
./dist/*.whl
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ github.token }}
run: >-
gh release create
'${{ github.ref_name }}'
--repo '${{ github.repository }}'
--notes ""
- name: Upload artifact signatures to GitHub Release
env:
GITHUB_TOKEN: ${{ github.token }}
# Upload to GitHub Release using the `gh` CLI.
# `dist/` contains the built packages, and the
# sigstore-produced signatures and certificates.
run: >-
gh release upload
'${{ github.ref_name }}' dist/**
--repo '${{ github.repository }}'
publish-to-testpypi:
name: Publish Python 🐍 distribution 📦 to TestPyPI
needs:
- build
runs-on: ubuntu-latest

environment:
name: testpypi
url: https://test.pypi.org/p/q8s

permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing

steps:
- name: Download all the dists
uses: actions/download-artifact@v4
with:
name: python-package-distributions
path: dist/
- name: Publish distribution 📦 to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
skip-existing: true
verbose: true
55 changes: 37 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
# q8s-kernel
# q8s

Kernel extension for executing quantum programs in simulators on q8s clusters
Toolset for executing quantum jobs on [Qubernetes](https://www.qubernetes.dev).

## Installation

Install the for project folder:

```bash
pip install .
pip install q8s
```

## Development

### Prerequisites

The development environment requires the following tools to be installed:

- [Docker](https://www.docker.com/get-started)
- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
## Usage

### Setup
### CLI

Install dependencies:
Sumbit a job to the Qubernetes cluster:

```bash
pip install -e .
q8sctl execute app.py --kubeconfig /path/to/kubeconfig
```

The jupyter kernel needs to be installed locally for jupyter notebook to find it. To install the q8s-kernel when using a virtual environment, run the following command:
For more options, run:

```bash
jupyter kernelspec install . --name=q8s-kernel --sys-prefix
q8sctl execute --help
```

otherwise, run the following command:
### Jupyter Notebook

Install the `q8s-kernel`:

```bash
jupyter kernelspec install . --name=q8s-kernel --user
q8sctl jupyter --install
```

Start the jupyter notebook server:
Expand All @@ -50,3 +45,27 @@ or the jupyter lab server:
```bash
jupyter lab
```

Select the `Q8s kernel` when creating a new notebook.

## Development

### Prerequisites

The development environment requires the following tools to be installed:

- [Docker](https://www.docker.com/get-started)

### Setup

Install the project in editable mode:

```bash
pip install -e .
```

If the project is installed in a virtual environment, the `q8s-kernel` can be installed by running the following command:

```bash
q8sctl jupyter --install
```
22 changes: 15 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
[project]
name = "q8s_kernel"
name = "q8s"
version = "0.1.0"
description = "Kernel extension for executing quantum programs in simulators on q8s clusters"
authors = [{ name = "Vlad Stirbu", email = "[email protected]" }]
readme = "README.md"
license = { file = "LICENSE" }
keywords = ["jupyter", "quantum", "kernel"]
classifiers = [
"Development status :: 3 - Alpha",
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
"stdlib-list==0.10.0",
"ipython==8.17.2",
# "stdlib-list==0.10.0",
# "ipython==8.17.2",
"ipykernel==6.26.0",
"kubernetes==29.0.0",
"notebook==7.0.6",
"python_on_whales==0.70.0",
# "notebook==7.0.6",
# "python_on_whales==0.70.0",
"python-dotenv==1.0.1",
"typer==0.12.3",
]
requires-python = ">= 3.8"

[project.urls]
Homepage = "https://github.com/torqs-project/q8s-kernel"
Issues = "https://github.com/torqs-project/q8s-kernel/issues"

[build-system]
requires = ["setuptools >= 61.0"]
build-backend = "setuptools.build_meta"
Expand All @@ -34,4 +42,4 @@ benchmark = ["pandas", "testbook", "mlflow"]
development = ["pyre-check"]

[project.scripts]
q8sctl = "q8s_kernel.cli:app"
q8sctl = "q8s.cli:app"
File renamed without changes.
File renamed without changes.
20 changes: 18 additions & 2 deletions q8s_kernel/cli.py → q8s/cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from pathlib import Path
import typer
import sys
from typing_extensions import Annotated
from kubernetes import config
from q8s_kernel.execution import K8sContext
from q8s.execution import K8sContext
from q8s.install import install_my_kernel_spec

app = typer.Typer()

Expand Down Expand Up @@ -53,5 +54,20 @@ def execute(
print(f"output stream: {stream_name}")


@app.command()
def jupyter(
install: Annotated[
bool,
typer.Option(
help="Install kernel spec for Jupyter",
),
] = False,
):
print("install:", install)
if install:
install_my_kernel_spec(user=False, prefix=sys.prefix)
# install_my_kernel_spec(user=user, prefix=prefix)


# if __name__ == "__main__":
app()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
73 changes: 73 additions & 0 deletions q8s/install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import json
import os
import sys
import argparse
import pathlib
import shutil

from jupyter_client.kernelspec import KernelSpecManager
from IPython.utils.tempdir import TemporaryDirectory

# from .resources import _ICON_PATH

kernel_json = {
"argv": [sys.executable, "-m", "q8s", "-f", "{connection_file}"],
"display_name": "Q8s kernel",
"language": "python",
}


def install_my_kernel_spec(user=True, prefix=None):
with TemporaryDirectory() as td:
os.chmod(td, 0o755) # Starts off as 700, not user readable
with open(os.path.join(td, "kernel.json"), "w") as f:
json.dump(kernel_json, f, sort_keys=True)
# shutil.copyfile(_ICON_PATH, pathlib.Path(td) / _ICON_PATH.name)
print("Installing q8s kernel spec")
KernelSpecManager().install_kernel_spec(td, "q8s", user=user, prefix=prefix)


def _is_root():
try:
return os.geteuid() == 0
except AttributeError:
return False # assume not an admin on non-Unix platforms


def main(argv=None):
parser = argparse.ArgumentParser(
description="Install KernelSpec for Qubernetes Kernel"
)
prefix_locations = parser.add_mutually_exclusive_group()

prefix_locations.add_argument(
"--user",
help="Install KernelSpec in user's home directory",
action="store_true",
)
prefix_locations.add_argument(
"--sys-prefix",
help="Install KernelSpec in sys.prefix. Useful in conda / virtualenv",
action="store_true",
dest="sys_prefix",
)
prefix_locations.add_argument(
"--prefix", help="Install KernelSpec in this prefix", default=None
)

args = parser.parse_args(argv)

user = False
prefix = None
if args.sys_prefix:
prefix = sys.prefix
elif args.prefix:
prefix = args.prefix
elif args.user or not _is_root():
user = True

install_my_kernel_spec(user=user, prefix=prefix)


if __name__ == "__main__":
main()
File renamed without changes.
8 changes: 5 additions & 3 deletions q8s_kernel/kernel.py → q8s/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ipykernel.kernelbase import Kernel
import logging

from q8s_kernel.execution import K8sContext
from q8s.execution import K8sContext

USE_KUBERNETES = True
FORMAT = "[%(levelname)s %(asctime)-15s q8s_kernel] %(message)s"
Expand All @@ -18,8 +18,10 @@ class Q8sKernel(Kernel):
language_version = "0.1"
language_info = {
"name": "Any text",
"mimetype": "text/plain",
"file_extension": ".txt",
"mimetype": "text/x-python",
"pygments_lexer": "ipython%d" % 3,
"file_extension": ".py",
"nbconvert_exporter": "python",
}
banner = "q8s"

Expand Down
2 changes: 1 addition & 1 deletion q8s_kernel/testbook.py → q8s/testbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ipykernel.kernelbase import Kernel
import logging

from q8s_kernel.kernel import Q8sKernel
from q8s.kernel import Q8sKernel

from .k8s import execute

Expand Down

0 comments on commit 84c7d7e

Please sign in to comment.