Skip to content

Commit 2977ffd

Browse files
authored
Convert to Python project, replace pipenv with uv (#205)
* Refactor bot.py to have a `main()` function * Create pip-installable setuptools package * Switch from PEP 735 Dependency Groups to PEP 508 Extra Dependencies Pip does not yet support this (PR merged, not released, general availability might take years) * Move from explicit package declaration to src-layout * Revert "Switch from PEP 735 Dependency Groups to PEP 508 Extra Dependencies" This reverts commit 979ac19. * Add uv files * Replace pipenv with uv * Raise Python version from 3.11.4 to 3.11.12 * Remove outdated configuration * Delete pipenv files * Fix isort configuration Ensure 'src/*' is classified as 'first-party' code * Update README * Add license information to pyproject.toml * Add trailing newline * Add 'uv lock --upgrade' to README * Adapt dependabot config to use uv
1 parent 0166ac2 commit 2977ffd

40 files changed

+1312
-2041
lines changed

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
.idea
44
.vscode
55
Dockerfile
6-
pyproject.toml

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: 2
22
updates:
3-
- package-ecosystem: "pip"
3+
- package-ecosystem: "uv"
44
directory: "/"
55
schedule:
66
interval: "weekly"

.github/workflows/ci.yml

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@ jobs:
1616
- uses: actions/checkout@v4
1717
with:
1818
fetch-depth: 0
19-
- uses: actions/setup-python@v5
19+
20+
- name: Install uv
21+
uses: astral-sh/setup-uv@v5
2022
with:
21-
python-version: '3.11.4'
23+
version: "0.6.14"
24+
25+
- name: Set up Python
26+
run: uv python install
2227

23-
- name: Install pipenv
24-
run: curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python
25-
- run: pipenv install --dev
28+
- name: Install dev dependencies
29+
run: uv sync --dev
2630

2731
- name: Run Black
28-
run: pipenv run black --check .
32+
run: uv run --dev black --check .
2933

3034
- name: Run Flake8
31-
run: pipenv run flake8 .
35+
run: uv run --dev flake8 .
3236

3337
- name: Run isort
34-
run: pipenv run isort --check .
38+
run: uv run --dev isort --check .
3539

3640
test:
3741
name: 🛠️ Test Python code
@@ -42,14 +46,16 @@ jobs:
4246
with:
4347
fetch-depth: 0
4448

45-
- uses: actions/setup-python@v5
49+
- name: Install uv
50+
uses: astral-sh/setup-uv@v5
4651
with:
47-
python-version: '3.11.4'
52+
version: "0.6.14"
4853

49-
- name: Install pipenv
50-
run: curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python
51-
- run: pipenv install --dev
54+
- name: Set up Python
55+
run: uv python install
5256

53-
- name: Run pytest
54-
run: pipenv run pytest .
57+
- name: Install dev dependencies
58+
run: uv sync --dev
5559

60+
- name: Run pytest
61+
run: uv run --dev pytest .

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414

1515
- uses: actions/setup-python@v5
1616
with:
17-
python-version: '3.11.4'
17+
python-version: '3.11.12'
1818

1919
- name: Install Ansible
2020
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ __pycache__
99
registered_log.txt
1010
schedule.json
1111
pretix_cache.json
12+
*.egg-info/

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11.12

Dockerfile

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
FROM python:3.11.4-slim
1+
FROM python:3.11.12-slim
2+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/
23

34
RUN groupadd --gid 1000 bot && \
45
useradd --uid 1000 --gid bot bot --create-home && \
@@ -7,16 +8,9 @@ RUN groupadd --gid 1000 bot && \
78
USER bot
89
WORKDIR /home/bot
910

10-
RUN pip install --upgrade --user pip && rm -rf /home/bot/.cache
11-
RUN pip install --user pipenv && rm -rf /home/bot/.cache
12-
RUN rm -rf /home/bot/.cache
13-
1411
ENV PATH="/home/bot/.local/bin:$PATH"
1512

16-
COPY --chown=bot:bot Pipfile Pipfile.lock ./
17-
COPY --chown=bot:bot EuroPythonBot ./EuroPythonBot
18-
19-
RUN pipenv sync && \
20-
rm -rf /home/bot/.cache
13+
COPY --chown=bot:bot pyproject.toml uv.lock ./
14+
COPY --chown=bot:bot src ./src
2115

22-
ENTRYPOINT ["pipenv", "run", "python", "EuroPythonBot/bot.py"]
16+
ENTRYPOINT ["uv", "run", "run-bot"]

Pipfile

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

Pipfile.lock

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

README.md

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,92 +26,93 @@ There are safeguard methods in place to prevent users from registering multiple
2626
Is a service to push the programme notification to Discord. Pretalx API is used to fetch the programme information, and `config.toml` holds information about livestream URLs.
2727

2828
### Organizers extension
29+
2930
A set of commands that are available only for organizers that are allowing to get statistics about the Discord server.
3031

3132
## Setup
32-
### Install Python, Pipenv, Pyenv (Ubuntu)
33-
```shell
34-
# dependencies of readline, sqlite3, ctypes
35-
sudo apt install libreadline-dev libsqlite3-dev lzma libbz2-dev liblzma-dev
33+
### Quickstart using `pip`
3634

37-
# python, pip
38-
sudo apt install python3 python3-pip
35+
This project uses [uv](https://github.com/astral-sh/uv) for managing dependencies.
36+
If you just want to try the bot and skip all the development setup,
37+
you can use `pip` instead of `uv` (requires Python >= 3.11):
3938

40-
# pyenv
41-
curl https://pyenv.run | bash
42-
pyenv install 3.11.4
39+
```shell
40+
# create and activate virtual environment (optional, but recommended)
41+
python -m venv .venv
42+
. .venv/bin/activate # Windows: '.venv/Scripts/activate'
4343

44-
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
45-
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
46-
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
47-
. ~/.bashrc
44+
# install this package
45+
pip install .
4846

49-
# pipenv
50-
python3 -m pip install pipenv
51-
echo 'alias pipenv="python3 -m pipenv"' >> ~/.bashrc
52-
. ~/.bashrc
47+
# run the bot
48+
run-bot
5349
```
5450

55-
### Using pipenv
56-
This is a summary of useful pipenv commands.
57-
Please refer to the [pipenv documentation](https://pipenv.pypa.io/en/latest/) for details.
51+
### Development setup using `uv`
5852

59-
```shell
60-
# generate venv from Pipfile.lock
61-
pipenv install
62-
pipenv install --dev # include dev dependencies
53+
Install `uv` as documented [here](https://docs.astral.sh/uv/getting-started/installation/), then
54+
create/update virtual environment with all dependencies according to [`uv.lock`](./uv.lock)
55+
with `uv sync --dev`.
6356

64-
# activate pipenv-generated venv
65-
pipenv shell
57+
If required, `uv` will download the required Python version, as specified in
58+
[`.python-version`](./.python-version).
6659

67-
# reset all packages to versions pinned in Pipfile.lock
68-
pipenv sync
69-
pipenv sync --dev # include dev dependencies
60+
### Using `uv`
7061

71-
# install package and update all other packages
72-
pipenv install package
73-
pipenv install --dev package # install as dev dependency
62+
This is a summary of useful `uv` commands.
63+
Please refer to the [uv documentation](https://docs.astral.sh/uv) or `uv help` for details.
7464

75-
# upgrade only specific package without updating all other packages
76-
pipenv upgrade package
77-
pipenv upgrade --dev package # upgrade as dev dependency
65+
```shell
66+
# generate .venv/ from uv.lock
67+
uv sync
68+
uv sync --dev # include dev dependencies
7869

79-
# remove package and update all other packages
80-
pipenv uninstall package
70+
# activate uv-generated venv
71+
. .venv/bin/activate # Windows: '.venv/Scripts/activate'
8172

82-
# remove only specific packages without updating all other package
83-
# [impossible]
84-
```
73+
# reset all packages to versions pinned in uv.lock
74+
uv sync
75+
uv sync --dev # include dev dependencies
8576

86-
### Clone repo, install dependencies, run tests
87-
```shell
88-
# clone repo, install dependencies
89-
git clone https://github.com/EuroPython/discord europython-discord/
90-
cd europython-discord
91-
92-
# install dependencies
93-
pipenv install --dev
94-
95-
# run linting and tests
96-
pipenv run black --check .
97-
pipenv run isort --check .
98-
pipenv run flake8 .
99-
pipenv run pytest .
77+
# add package
78+
uv add package
79+
uv add --dev package # install as dev dependency
80+
81+
# upgrade packages
82+
uv lock --upgrade
83+
84+
# remove package
85+
uv remove package
10086
```
10187

88+
### Development tools
89+
90+
* Format code: `uv run --dev black .`
91+
* Check code format: `uv run --dev black --check .`
92+
* Sort imports: `uv run --dev isort .`
93+
* Check import order: `uv run --dev isort --check .`
94+
* Check code style: `uv run --dev flake8 .`
95+
* Run tests: `uv run --dev pytest .`
96+
10297
### Configuration
98+
10399
Create `config.local.toml` file in EuroPythonBot directory, it would be used instead of `config.toml` if exists.
104100

105101
Add `.secrets` file to the root of the repository with the following content:
102+
106103
```shell
107104
DISCORD_BOT_TOKEN=<EuroPythonTestBotToken_from_1Password>
108105
PRETIX_TOKEN=<PretixStagingToken_from_1Password>
109106
````
107+
110108
After you have added the `.secrets` file, you can run the bot with the following command:
109+
111110
```shell
112-
pipenv run python EuroPythonBot/bot.py
111+
run-bot
113112
```
113+
114114
or with docker:
115+
115116
```shell
116117
docker build --tag discord_bot .
117118
docker run --interactive --tty --env DISCORD_BOT_TOKEN=$DISCORD_BOT_TOKEN --env PRETIX_TOKEN=$PRETIX_TOKEN discord_bot

0 commit comments

Comments
 (0)