Skip to content

Commit

Permalink
Use new upload endpoint (#7)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
simonbohnen authored Feb 27, 2023
1 parent c4fabec commit 310e0f6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/quetz_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/dev_config.toml
Original file line number Diff line number Diff line change
@@ -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"]
24 changes: 24 additions & 0 deletions tests/test-readme.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import re
import subprocess

import pytest

Expand All @@ -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))
Expand Down

0 comments on commit 310e0f6

Please sign in to comment.