Skip to content

Commit

Permalink
write scripts as backoffice package
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Feb 22, 2024
1 parent 86d5752 commit d5f4691
Show file tree
Hide file tree
Showing 22 changed files with 141 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test stage call
name: test and build docs

on: push

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# collection

Maintains the resources displayed on bioimage.io (Successor to collection-bioimage-io)
3 changes: 3 additions & 0 deletions backoffice/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "0.1.0"
}
File renamed without changes.
55 changes: 55 additions & 0 deletions backoffice/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
from typing import Literal, Optional

import fire
from bioimageio.spec.model.v0_5 import WeightsFormat
from dotenv import load_dotenv

from .backup import backup
from .run_dynamic_tests import run_dynamic_tests
from .utils.remote_resource import PublishedVersion, RemoteResource, StagedVersion
from .utils.s3_client import Client
from .validate_format import validate_format

_ = load_dotenv()


class BackOffice:
def __init__(self) -> None:
super().__init__()
self.client = Client()

def stage(self, resource_id: str, package_url: str):
resource = RemoteResource(client=Client(), id=resource_id)
staged = resource.stage_new_version(package_url)
validate_format(staged)

def test(
self,
resource_id: str,
version: int,
weight_format: Optional[WeightsFormat] = None,
create_env_outcome: Literal["success", ""] = "success",
):
staged = StagedVersion(self.client, resource_id, version)
run_dynamic_tests(
staged=staged,
weight_format=weight_format,
create_env_outcome=create_env_outcome,
)

def await_review(self, resource_id: str, version: int):
staged = StagedVersion(self.client, resource_id, version)
staged.await_review()

def publish(self, resource_id: str, stage_nr: int):
staged = StagedVersion(client=self.client, id=resource_id, version=stage_nr)
published = staged.publish()
assert isinstance(published, PublishedVersion)

def backup(self):
_ = backup(self.client, os.environ["ZENODO_URL"])


if __name__ == "__main__":
fire.Fire(BackOffice)
14 changes: 4 additions & 10 deletions scripts/backup.py → backoffice/backup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
import os

import typer
from dotenv import load_dotenv
from loguru import logger
from utils.s3_client import Client

from backoffice.utils.s3_client import Client

_ = load_dotenv()


def backup():
def backup(client: Client, destination: str):
"""backup collection
Returns:
list of folders and file names backed up
"""
client = Client()
content_to_backup = list(client.ls(""))
destination = os.environ["ZENODO_URL"]
logger.error("Backup to '{}': {}", destination, content_to_backup)
logger.error("Not implemented: Backup to '{}': {}", destination, content_to_backup)
return content_to_backup


if __name__ == "__main__":
typer.run(backup)
19 changes: 5 additions & 14 deletions scripts/run_dynamic_tests.py → backoffice/run_dynamic_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import bioimageio.core
import bioimageio.spec
import typer
from bioimageio.spec.model.v0_5 import WeightsFormat
from bioimageio.spec.summary import (
ErrorEntry,
Expand All @@ -14,8 +13,8 @@
ValidationSummary,
)
from ruyaml import YAML
from utils.remote_resource import StagedVersion
from utils.s3_client import Client

from backoffice.utils.remote_resource import StagedVersion

try:
from tqdm import tqdm
Expand Down Expand Up @@ -44,14 +43,10 @@ def get_summary_detail_from_exception(name: str, exception: Exception):


def run_dynamic_tests(
resource_id: str,
version: int,
weight_format: Optional[WeightsFormat] = typer.Argument(
..., help="weight format to test model with."
),
create_env_outcome: str = "success",
staged: StagedVersion,
weight_format: Optional[WeightsFormat], # "weight format to test model with."
create_env_outcome: str,
):
staged = StagedVersion(client=Client(), id=resource_id, version=version)
staged.set_status(
"testing",
"Testing" + ("" if weight_format is None else f" {weight_format} weights"),
Expand Down Expand Up @@ -124,7 +119,3 @@ def run_dynamic_tests(
)

staged.add_log_entry("bioimageio.core", summary.model_dump(mode="json"))


if __name__ == "__main__":
typer.run(run_dynamic_tests)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from packaging.version import parse as parse_version
from ruyaml import YAML

# from utils.s3_client import create_client, version_from_resource_path_or_s3
# from backoffice.utils.s3_client import create_client, version_from_resource_path_or_s3

yaml = YAML(typ="safe")

Expand Down
Empty file added backoffice/utils/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ def _set_log(self, log: Log) -> None:
class StagedVersion(_RemoteResourceVersion):
version_prefix: ClassVar[str] = "staged/"

def await_review(self):
self.set_status(
"awaiting review",
description="Thank you for your contribution! Our bioimage.io maintainers will take a look soon.",
)

def publish(self) -> PublishedVersion:
# get next version and update versions.json
versions_path = f"{self.id}/versions.json"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from packaging.version import Version
from ruyaml import YAML

from .remote_resource import StagedVersion
from backoffice.utils.remote_resource import StagedVersion

yaml = YAML(typ="safe")

Expand Down
Empty file added py.typed
Empty file.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
target-version = ["py312"]

[tool.pyright]
include = ["scripts"]
include = ["backoffice", "tests"]
pythonPlatform = "All"
pythonVersion = "3.12"
reportIncompatibleMethodOverride = true
Expand All @@ -20,4 +20,4 @@ useLibraryCodeForTypes = true

[tool.pytest.ini_options]
addopts = "--capture=no --doctest-modules --failed-first"
testpaths = ["scripts", "tests"]
testpaths = ["backoffice", "tests"]
12 changes: 0 additions & 12 deletions requirements.txt

This file was deleted.

18 changes: 0 additions & 18 deletions scripts/conclude.py

This file was deleted.

13 changes: 0 additions & 13 deletions scripts/publish.py

This file was deleted.

14 changes: 0 additions & 14 deletions scripts/stage.py

This file was deleted.

50 changes: 50 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import json
from pathlib import Path

from setuptools import find_packages, setup

# Get the long description from the README file
ROOT_DIR = Path(__file__).parent.resolve()
long_description = (ROOT_DIR / "README.md").read_text(encoding="utf-8")
VERSION_FILE = ROOT_DIR / "backoffice" / "VERSION"
VERSION = json.loads(VERSION_FILE.read_text(encoding="utf-8"))["version"]


setup(
name="backoffice",
version=VERSION,
description="backoffice to control bioimage.io collection",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/bioimage-io/collection",
author="bioimage.io Team",
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3.12",
],
packages=find_packages(exclude=["tests"]),
install_requires=[
"bioimageio.core @ git+https://github.com/bioimage-io/core-bioimage-io-python@3a7875b5debc2d52b2fc87f6579afe217e1c7280", # TODO: change to released version
"bioimageio.spec @ git+https://github.com/bioimage-io/spec-bioimage-io@19105665ad779014e03c7b311c0b4003ab08f752", # TODO: change to released version
"fire",
"loguru",
"minio==7.2.3",
"ruyaml",
"tqdm",
],
extras_require={
"dev": [
"black",
"pdoc",
"pre-commit",
"pyright",
"pytest",
]
},
entry_points={"console_scripts": ["backoffice = backoffice.__main__"]},
project_urls={
"Bug Reports": "https://github.com/bioimage-io/collection/issues",
"Source": "https://github.com/bioimage-io/collection",
},
)
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from typing import TYPE_CHECKING
import os

if TYPE_CHECKING:
from scripts.utils.s3_client import Client
from backoffice.backup import backup
from backoffice.utils.remote_resource import (
PublishedVersion,
RemoteResource,
StagedVersion,
)
from backoffice.utils.s3_client import Client


def test_lifecycle(
client: "Client", package_url: str, package_id: str, s3_test_folder_url: str
client: Client, package_url: str, package_id: str, s3_test_folder_url: str
):
from scripts.backup import backup
from scripts.utils.remote_resource import (
PublishedVersion,
RemoteResource,
StagedVersion,
)

resource = RemoteResource(client=client, id=package_id)
staged = resource.stage_new_version(package_url)
assert isinstance(staged, StagedVersion)
Expand All @@ -29,5 +27,5 @@ def test_lifecycle(
published_rdf_url == f"{s3_test_folder_url}frank-water-buffalo/1/files/rdf.yaml"
)

backed_up = backup()
backed_up = backup(client, os.environ["ZENODO_TEST"])
assert backed_up == ["frank-water-buffalo"]
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from typing import TYPE_CHECKING
from backoffice.utils.s3_client import Client

if TYPE_CHECKING:
from scripts.utils.s3_client import Client


def test_client(client: "Client"):
def test_client(client: Client):
assert client.prefix.startswith("sandbox")
client.put_json("test/test1.json", "test")
client.put_json("test/dir/test2.json", "test")
Expand Down

0 comments on commit d5f4691

Please sign in to comment.