Skip to content

Commit

Permalink
chore: 使用 uv 管理项目 (#232)
Browse files Browse the repository at this point in the history
* chore: 使用 uv 管理项目

* style: 使用 ruff 0.7.0 格式化代码

* ci: 使用 setup-uv

* build: 调整 docker 写法

* ci: 默认进行 Docker 镜像编译
  • Loading branch information
he0119 authored Oct 22, 2024
1 parent 33a52da commit c5513f1
Show file tree
Hide file tree
Showing 15 changed files with 1,418 additions and 1,884 deletions.
39 changes: 12 additions & 27 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
uses: he0119/setup-python@main
- name: Install the latest version of uv
uses: astral-sh/setup-uv@v3
with:
python-version: "3.12"

- name: Install prerequisites
run: poetry install
enable-cache: true

- name: Run tests
run: poetry run pytest --cov=src --cov-report xml
run: uv run poe test

- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
Expand All @@ -36,7 +39,6 @@ jobs:
name: Docker
runs-on: ubuntu-latest
needs: test
if: ${{ github.event_name != 'pull_request' }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -45,6 +47,7 @@ jobs:
uses: docker/setup-buildx-action@v3

- name: Login to Github Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
Expand All @@ -64,24 +67,6 @@ jobs:
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}

github-relase:
name: GitHub release
runs-on: ubuntu-latest
needs: test
if: ${{ github.event_name == 'release' && github.event.action == 'published' }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set Tag Name
run: echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV

- name: Upload Files
run: gh release upload --clobber ${{ env.TAG_NAME }} src/utils/plugin_test.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44 changes: 41 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down Expand Up @@ -43,6 +46,7 @@ htmlcov/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
Expand All @@ -56,6 +60,7 @@ cover/
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
Expand Down Expand Up @@ -93,7 +98,22 @@ ipython_config.py
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
Expand All @@ -104,6 +124,7 @@ celerybeat.pid
*.sage.py

# Environments
.env
.venv
env/
venv/
Expand Down Expand Up @@ -135,7 +156,24 @@ dmypy.json
# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml

# ruff
.ruff_cache/

# LSP config files
pyrightconfig.json

# End of https://www.toptal.com/developers/gitignore/api/python

plugin_test/
.vscode/

node_modules/
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ ci:
autoupdate_commit_msg: "chore: auto update by pre-commit hooks"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.2
rev: v0.7.0
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
stages: [commit]
stages: [pre-commit]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier
types_or: [javascript, jsx, ts, tsx, markdown, yaml, json]
stages: [commit]
stages: [pre-commit]
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.7
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/lang/zh-CN/

- 最初的版本

[unreleased]: https://github.com/nonebot/noneflow/compare/v3.3.3...HEAD
[Unreleased]: https://github.com/nonebot/noneflow/compare/v3.3.3...HEAD
[3.3.3]: https://github.com/nonebot/noneflow/compare/v3.3.2...v3.3.3
[3.3.2]: https://github.com/nonebot/noneflow/compare/v3.3.1...v3.3.2
[3.3.1]: https://github.com/nonebot/noneflow/compare/v3.3.0...v3.3.1
Expand Down
28 changes: 8 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
FROM python:3.12 as requirements-stage

WORKDIR /tmp

COPY ./pyproject.toml ./poetry.lock* /tmp/

RUN curl -sSL https://install.python-poetry.org -o install-poetry.py

RUN python install-poetry.py --yes

ENV PATH="${PATH}:/root/.local/bin"

RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

FROM python:3.12-slim-bullseye
FROM python:3.12.4-slim
COPY --from=ghcr.io/astral-sh/uv:0.4.25 /uv /bin/uv

# 设置时区
ENV TZ=Asia/Shanghai

# 安装依赖
COPY --from=requirements-stage /tmp/requirements.txt /app/requirements.txt
RUN apt-get update \
&& apt-get -y upgrade \
&& apt-get install -y --no-install-recommends git \
&& pip install --no-cache-dir --upgrade -r /app/requirements.txt \
&& apt-get purge -y --auto-remove \
&& rm -rf /var/lib/apt/lists/* \
&& rm /app/requirements.txt
&& rm -rf /var/lib/apt/lists/*

# Python 依赖
COPY pyproject.toml uv.lock ./
RUN uv sync --no-dev --frozen --compile-bytecode

COPY bot.py .env /app/
COPY src /app/src/

CMD ["python", "/app/bot.py"]
CMD ["uv", "run", "--no-dev", "/app/bot.py"]
6 changes: 3 additions & 3 deletions docker/plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
经测试可以直接在 Python 3.10+ 环境下运行,无需额外依赖。
"""
# ruff: noqa: T201, ASYNC101
# ruff: noqa: T201

import asyncio
import json
import re
import os
import re
from asyncio import create_subprocess_shell, subprocess
from pathlib import Path
from urllib.request import urlopen
Expand Down Expand Up @@ -276,7 +276,7 @@ async def run(self):

return self._run, self._lines_output

async def command(self, cmd: str, timeout: int = 300) -> tuple[bool, str, str]:
async def command(self, cmd: str, timeout: int = 300) -> tuple[bool, str, str]: # noqa: ASYNC109
"""执行命令
Args:
Expand Down
Loading

0 comments on commit c5513f1

Please sign in to comment.