From 310e0f6d1df636e97ba43099f297f5a4102bfe86 Mon Sep 17 00:00:00 2001 From: Simon Bohnen Date: Mon, 27 Feb 2023 21:42:33 +0100 Subject: [PATCH] Use new upload endpoint (#7) * enable upload of multiple files * demo package upload * change trigger to on PR * fixes * fixes * fixes * fixes * fixes * fixes * fixes * fixes * fixes * fixes * fixes * fix checksum * fix * fix * fix * fix * fix * add newline * remove accidental release workflow * now it should work? * cmon * fix spelling * remove quetz as requirement * remove quetz as requirement * checkout correct branch * give it some time to upload * list all files * precommit * wut * wut * noww?? * remove debug prints * cleanup * trigger ci * switch trigger * revert to main branch * readme * use live quetz version * update test * trigger readme CI * add dev_config * add dev_config * pre-commit * pre-commit * feedback * move readme script to pytest * typo * add cleanup steps --- README.md | 10 +++++++++- src/quetz_client/client.py | 10 ++++++---- tests/conftest.py | 10 +++++----- tests/dev_config.toml | 14 ++++++++++++++ tests/test-readme.sh | 24 ++++++++++++++++++++++++ tests/test_client.py | 8 ++++++++ 6 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 tests/dev_config.toml create mode 100644 tests/test-readme.sh diff --git a/README.md b/README.md index 65dee4f..07c960c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,17 @@ # quetz-client -A Python client to interact with a Quetz server. +A Python client to interact with a Quetz server. Compatible with `quetz>=0.6.1`. ## Installation +### From conda-forge + +```bash +mamba install quetz-client +``` + +### From this repo + You can install the package in development mode using: ```bash diff --git a/src/quetz_client/client.py b/src/quetz_client/client.py index b0106fc..9263008 100644 --- a/src/quetz_client/client.py +++ b/src/quetz_client/client.py @@ -164,18 +164,20 @@ def yield_packages( yield Package(**user_json) def post_file_to_channel(self, channel: str, file: Path, force: bool = False): - url = f"{self.url}/api/channels/{channel}/upload/{file.name}" - body = open(file, "rb") + file_path = Path(file) + url = f"{self.url}/api/channels/{channel}/upload/{file_path.name}" + body_bytes = file_path.read_bytes() - upload_hash = hashlib.sha256(body.read()).hexdigest() + upload_hash = hashlib.sha256(body_bytes).hexdigest() params: Dict[str, Union[str, int]] = { "force": force, "sha256": upload_hash, } + response = self.session.post( url=url, - data=body, + data=body_bytes, params=params, ) response.raise_for_status() diff --git a/tests/conftest.py b/tests/conftest.py index dc3347d..6767684 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,11 +14,11 @@ @contextmanager def temporary_package_file() -> Iterator[Path]: url = "https://conda.anaconda.org/conda-forge/linux-64/xtensor-0.16.1-0.tar.bz2" - with requests.get(url, stream=True) as response: - with NamedTemporaryFile() as file: - with open(file.name, "wb") as fp: - shutil.copyfileobj(response.raw, fp) - yield Path(file.name) + xtensor_download = requests.get(url, stream=True) + with NamedTemporaryFile() as file: + with open(file.name, "wb") as fp: + shutil.copyfileobj(xtensor_download.raw, fp) + yield Path(file.name) @pytest.fixture diff --git a/tests/dev_config.toml b/tests/dev_config.toml new file mode 100644 index 0000000..b73c66a --- /dev/null +++ b/tests/dev_config.toml @@ -0,0 +1,14 @@ +[sqlalchemy] +database_url = "sqlite:///./quetz.sqlite" + +[session] +# openssl rand -hex 32 +secret = "b72376b88e6f249cb0921052ea8a092381ca17fd8bb0caf4d847e337b3d34cf8" +https_only = false + +[logging] +level = "DEBUG" +file = "quetz.log" + +[users] +admins = ["dummy:alice"] diff --git a/tests/test-readme.sh b/tests/test-readme.sh new file mode 100644 index 0000000..d8d9107 --- /dev/null +++ b/tests/test-readme.sh @@ -0,0 +1,24 @@ +set -e +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +# Run the steps described in the quetz README.md for uploading and installing a sample package + +quetz run test_quetz --copy-conf $SCRIPT_DIR/dev_config.toml --dev --reload --delete > quetz.log & +sleep 10 +export QUETZ_API_KEY=$(sed -n 's/.*key created for user.*: \(.*\)/\1/p' quetz.log) +export QUETZ_SERVER_URL=http://localhost:8000 + +mkdir -p xtensor/osx-64 +mkdir -p xtensor/linux-64 +wget https://conda.anaconda.org/conda-forge/osx-64/xtensor-0.16.1-0.tar.bz2 -P xtensor/osx-64/ +wget https://conda.anaconda.org/conda-forge/linux-64/xtensor-0.16.1-0.tar.bz2 -P xtensor/linux-64/ + +quetz-client post_file_to_channel channel0 xtensor/linux-64/xtensor-0.16.1-0.tar.bz2 +quetz-client post_file_to_channel channel0 xtensor/osx-64/xtensor-0.16.1-0.tar.bz2 + +sleep 2 + +micromamba install --override-channels --strict-channel-priority -c http://localhost:8000/get/channel0 -c conda-forge xtensor + +# Kill quetz +lsof -i:8000 | grep LISTEN | awk '{print $2}' | xargs kill -9 \ No newline at end of file diff --git a/tests/test_client.py b/tests/test_client.py index 32e2f3c..fa100a9 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,4 +1,6 @@ +import os import re +import subprocess import pytest @@ -7,6 +9,12 @@ from .conftest import temporary_package_file +def test_readme_script(): + dir_path = os.path.dirname(os.path.realpath(__file__)) + result = subprocess.call(["bash", f"{dir_path}/test-readme.sh"]) + assert result == 0 + + def test_yield_channels(quetz_client): expected_channel_names = ("a", "b", "c") channels = list(quetz_client.yield_channels(limit=2))