Skip to content

Commit 11d8b17

Browse files
authored
build: upgrade to poetry v2 (#248)
1 parent c3a6262 commit 11d8b17

11 files changed

Lines changed: 1401 additions & 771 deletions

File tree

.github/workflows/bk-storages.yml

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,16 @@ jobs:
1616
steps:
1717
- uses: actions/checkout@v2
1818
- name: Set up Python
19-
uses: actions/setup-python@v2
19+
uses: actions/setup-python@v5
2020
with:
2121
python-version: 3.11
22-
- name: Format with isort
23-
working-directory: sdks/bk-storages
24-
run: |
25-
pip install isort==5.12.0
26-
isort . --settings-path=pyproject.toml
27-
- name: Format with black
28-
working-directory: sdks/bk-storages
29-
run: |
30-
pip install black==23.7.0 click==8.1.6
31-
black . --config=pyproject.toml
32-
- name: Lint with flake8
33-
working-directory: sdks/bk-storages
34-
run: |
35-
pip install flake8==4.0.1 pyproject-flake8==0.0.1a5
36-
pflake8 . --config=pyproject.toml
22+
- name: Setup uv
23+
uses: astral-sh/setup-uv@v5
24+
- name: Install Poetry system-wide
25+
# Use uv install to install poetry directly instead github action for simplicity
26+
run: uv pip install --system poetry==2.1.3
27+
- name: Ruff check
28+
run: poetry install && poetry run ruff check ./sdks/bk-storages --config ./pyproject.toml
3729
- name: Lint with mypy
3830
working-directory: sdks/bk-storages
3931
run: |
@@ -43,21 +35,50 @@ jobs:
4335
strategy:
4436
fail-fast: false
4537
matrix:
46-
python-version: ["3.8", "3.9", "3.10", "3.11"]
38+
python-version: ["3.9", "3.10", "3.11"]
4739
os: [ubuntu-latest, macos-latest]
4840
runs-on: ${{ matrix.os }}
4941
steps:
5042
- uses: actions/checkout@v2
5143
- name: Set up Python
52-
uses: actions/setup-python@v2
44+
uses: actions/setup-python@v5
5345
with:
5446
python-version: ${{ matrix.python-version }}
47+
- name: Setup uv
48+
uses: astral-sh/setup-uv@v5
49+
- name: Install Nox
50+
run: uv pip install --system nox==2025.11.12
51+
- name: Show nox version
52+
working-directory: sdks/bk-storages
53+
run: nox --version
54+
- name: Run tests on ${{ matrix.os }}
55+
working-directory: sdks/bk-storages
56+
run: nox --non-interactive --error-on-missing-interpreters --session "tests(python='${{ matrix.python-version }}')" -- --full-trace
57+
58+
build:
59+
runs-on: macos-latest
60+
if: github.event.release && contains(github.event.release.tag_name, 'bk-storages')
61+
steps:
62+
- uses: actions/checkout@v2
63+
- name: Set up Python
64+
uses: actions/setup-python@v5
65+
with:
66+
python-version: 3.11
5567
- name: Set up Poetry
5668
uses: abatilo/actions-poetry@v2.3.0
5769
with:
58-
poetry-version: 1.5.1
59-
- name: Test bk-storages
70+
poetry-version: 2.1.1
71+
- name: Build bk-storages
6072
run: |
6173
cd sdks/bk-storages
6274
poetry install
63-
poetry run pytest
75+
poetry build
76+
echo "${{ github.event.release.tag_name }} ${{ github.sha }}" > Release.txt
77+
cat Release.txt
78+
- name: Release
79+
uses: softprops/action-gh-release@v1
80+
with:
81+
files: |
82+
Release.txt
83+
sdks/bk-storages/dist/*
84+

sdks/bk-storages/.pre-commit-config.yaml

Lines changed: 0 additions & 23 deletions
This file was deleted.

sdks/bk-storages/bkstorages/backends/bkrepo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def upload_fileobj(self, fh, key: str, allow_overwrite: bool = True, **kwargs):
106106
logger.exception("Request success, but the server rejects the upload request.")
107107
raise UploadFailedError(key=key, src=src) from e
108108
except Exception as e:
109-
logger.exception("An unexpected exception occurred, detail: %s", e)
109+
logger.exception("An unexpected exception occurred")
110110
raise UploadFailedError(key=key, src=src) from e
111111

112112
def download_file(self, key: str, filepath: PathLike, *args, **kwargs) -> PathLike:
@@ -146,7 +146,7 @@ def download_fileobj(self, key: str, fh, *args, **kwargs):
146146
if chunk:
147147
fh.write(chunk)
148148
except Exception as e:
149-
logger.exception("File save failed, detail %s", e)
149+
logger.exception("File save failed")
150150
raise DownloadFailedError(key=key, dest=dest) from e
151151

152152
def delete_file(self, key: str, *args, **kwargs):
@@ -282,7 +282,7 @@ def __init__(self, name, storage: 'BKRepoStorage'):
282282

283283
def _get_file(self):
284284
if self._file is None:
285-
self._file = SpooledTemporaryFile()
285+
self._file = SpooledTemporaryFile() # noqa: SIM115
286286
self._storage.client.download_fileobj(key=self.name, fh=self._file)
287287
self._file.seek(0)
288288
self._dirty = False

sdks/bk-storages/bkstorages/backends/rgw.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def size(self):
116116

117117
def _get_file(self):
118118
if self._file is None:
119-
self._file = SpooledTemporaryFile(
119+
self._file = SpooledTemporaryFile( # noqa: SIM115
120120
max_size=self._storage.max_memory_size,
121121
suffix=".RGWBoto3StorageFile",
122122
dir=setting("FILE_UPLOAD_TEMP_DIR", None),
@@ -182,8 +182,7 @@ def close(self):
182182
# s3boto's behavior.
183183
parts = [{'ETag': part.e_tag, 'PartNumber': part.part_number} for part in self._multipart.parts.all()]
184184
self._multipart.complete(MultipartUpload={'Parts': parts})
185-
else:
186-
if self._multipart is not None:
185+
elif self._multipart is not None:
187186
self._multipart.abort()
188187
if self._file is not None:
189188
self._file.close()
@@ -343,6 +342,7 @@ def lookup_env(names):
343342
value = os.environ.get(name)
344343
if value:
345344
return value
345+
return None
346346

347347
access_key = self.access_key or lookup_env(self.access_key_names)
348348
secret_key = self.secret_key or lookup_env(self.secret_key_names)
@@ -476,19 +476,21 @@ def delete(self, name):
476476
def exists(self, name):
477477
if not name:
478478
try:
479-
self.bucket
480-
return True
479+
_ = self.bucket
481480
except ImproperlyConfigured:
482481
return False
482+
else:
483+
return True
483484
name = self._normalize_name(clean_name(name))
484485
if self.entries:
485486
return name in self.entries
486487
obj = self.bucket.Object(self._encode_name(name))
487488
try:
488489
obj.load()
489-
return True
490490
except self.connection_response_error:
491491
return False
492+
else:
493+
return True
492494

493495
def listdir(self, name):
494496
name = self._normalize_name(clean_name(name))

sdks/bk-storages/noxfile.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
import tempfile
4+
5+
import nox
6+
7+
ALL_PYTHON = ["3.9", "3.10", "3.11"]
8+
9+
10+
# ref: https://stackoverflow.com/questions/59768651/how-to-use-nox-with-poetry
11+
def install_with_constraints(session, *args: str, **kwargs) -> None:
12+
"""
13+
Install packages constrained by Poetry's lock file.
14+
15+
This function is a wrapper for nox.sessions.Session.install. It
16+
invokes pip to install packages inside of the session's virtualenv.
17+
Additionally, pip is passed a constraints file generated from
18+
Poetry's lock file, to ensure that the packages are pinned to the
19+
versions specified in poetry.lock. This allows you to manage the
20+
packages as Poetry development dependencies.
21+
22+
Arguments:
23+
session: The Session object.
24+
args: Command-line arguments for pip.
25+
kwargs: Additional keyword arguments for Session.install.
26+
"""
27+
req_path = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
28+
session.run(
29+
"poetry",
30+
"export",
31+
"--with=dev",
32+
"--format=requirements.txt",
33+
f"--output={req_path}",
34+
external=True,
35+
)
36+
session.install(f"--constraint={req_path}", "--use-deprecated=legacy-resolver", *args, **kwargs)
37+
os.unlink(req_path)
38+
39+
40+
@nox.session(reuse_venv=True)
41+
@nox.parametrize("python", ALL_PYTHON)
42+
def tests(session):
43+
# Prepare pip and poetry
44+
session.run("python", "-m", "ensurepip", "--upgrade")
45+
session.install("poetry")
46+
session.run("poetry", "self", "add", "poetry-plugin-export@latest")
47+
48+
# Install dev/test dependencies
49+
session.install("-e", ".[all]")
50+
install_with_constraints(
51+
session,
52+
"pytest",
53+
"django",
54+
"pytest-django",
55+
"pytest-mock",
56+
"moto[s3]==3.1.3",
57+
"requests-mock",
58+
"boto3==1.21.32"
59+
)
60+
session.run("pytest", *session.posargs)

0 commit comments

Comments
 (0)