Skip to content

Commit 45d881a

Browse files
authored
feat: Migrate dependency management to Poetry (#247)
**Signed-off-by:** Areeb Ahmed [[email protected]](mailto:[email protected]) ## Summary of Changes **Fixes:** #154 - Replaced `requirements.txt` with `pyproject.toml` and `poetry.lock` - Added all dependencies to `pyproject.toml` using Poetry - Updated CI configuration to use Poetry for installing dependencies - Updated `README.md` and `INSTALL.md` to reflect the new dependency management process
1 parent e83fb5b commit 45d881a

File tree

7 files changed

+1638
-75
lines changed

7 files changed

+1638
-75
lines changed

.github/workflows/pull-request.yml

+40-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Pull Request checks
1+
name: Pull Request Checks
22

33
on:
44
pull_request:
@@ -9,46 +9,63 @@ concurrency:
99

1010
jobs:
1111
run-tests:
12-
name: Run pytest tests
12+
name: Run pytest Tests
1313
runs-on: ubuntu-latest
1414
env:
1515
POSTGRES_USER: folksonomy
1616
POSTGRES_PASSWORD: folksonomy
17-
# important: "localhost" does not work on github ci, only 127.0.0.1
17+
# Important: "localhost" does not work on GitHub CI, only 127.0.0.1
1818
POSTGRES_HOST: "127.0.0.1"
1919
POSTGRES_DATABASE: folksonomy
20+
2021
steps:
21-
- name: checkout project
22+
- name: Checkout Project
2223
uses: actions/checkout@v4
2324
with:
2425
fetch-depth: 1
25-
- name: create pgdata directory
26+
27+
- name: Create pgdata Directory
2628
run: mkdir -p /home/runner/work/folksonomy_api/folksonomy_api/data/pg/pgdata
27-
- name: change permissions for pgdata directory
29+
30+
- name: Change Permissions for pgdata Directory
2831
run: sudo chmod -R 777 /home/runner/work/folksonomy_api/folksonomy_api/data/pg/pgdata
29-
- name: install python
32+
33+
- name: Install Python
3034
uses: actions/[email protected]
3135
with:
32-
python-version: '3.8'
33-
cache: 'pip'
34-
- name: start a postgresql docker container
36+
python-version: '3.9'
37+
38+
- name: Install Poetry
39+
uses: snok/install-poetry@v1
40+
with:
41+
version: latest
42+
virtualenvs-create: true
43+
virtualenvs-in-project: false
44+
45+
- name: Start PostgreSQL Docker Container
3546
run: ./start_postgres.sh &
36-
- name: install packages in a virtual environment
37-
run: |
38-
python -m venv .venv && \
39-
.venv/bin/pip install -r requirements.txt
40-
- name: wait postgres to be ready
47+
48+
- name: Install Dependencies
49+
run: poetry install --no-interaction
50+
51+
- name: Wait for PostgreSQL to be Ready
4152
timeout-minutes: 10
4253
run: |
43-
while ! (nc -vz 127.0.0.1 5432);do echo "waiting postgres"; sleep 1;done
44-
- name: initialize database
45-
run: |
46-
.venv/bin/python db-migration.py
47-
- name: run tests
48-
# use PYTHONASYNCIODEBUG to ensure all asyncio tasks are closed
54+
while ! (nc -vz 127.0.0.1 5432); do
55+
echo "Waiting for PostgreSQL"
56+
sleep 1
57+
done
58+
59+
- name: Initialize Database
60+
run: poetry run python db-migration.py
61+
62+
- name: Run Tests
63+
# Use PYTHONASYNCIODEBUG to ensure all asyncio tasks are closed
4964
run: |
50-
PYTHONASYNCIODEBUG=1 .venv/bin/pytest -v --cov=folksonomy --cov-report xml tests/ folksonomy/
51-
- uses: codecov/[email protected]
65+
PYTHONASYNCIODEBUG=1 poetry run pytest -v --cov=folksonomy --cov-report xml tests/ folksonomy/
66+
67+
- name: Upload Coverage Reports
68+
uses: codecov/[email protected]
5269
with:
5370
token: ${{ secrets.CODECOV_TOKEN }}
5471
fail_ci_if_error: true

INSTALL.md

+23-29
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55
- PostgreSQL 13 or newer
66
- Python 3.9 or newer
7-
- Python modules in "requirements.txt"
7+
- Poetry (for dependency management)
88

99
## Setup on Debian 12 (Recommended Method)
1010

1111
```bash
1212
# Start with a fresh Debian 12 install, logged as root
13-
apt install git sudo postgresql python3-venv -y
13+
apt install git sudo postgresql python3-pip curl -y
14+
15+
# Install Poetry
16+
curl -sSL https://install.python-poetry.org | python3 -
1417

1518
# Create a user for the application (optional but recommended)
1619
adduser folksonomy
@@ -22,50 +25,41 @@ cd ~
2225
git clone https://github.com/openfoodfacts/folksonomy_api.git
2326
cd folksonomy_api
2427

25-
# Create and activate Python virtual environment
26-
python3 -m venv folksonomy
27-
. ./folksonomy/bin/activate
28-
29-
# Install required packages
30-
pip install -r requirements.txt
28+
# Install dependencies using Poetry
29+
poetry install
3130

3231
# Set up PostgreSQL database
3332
sudo -i -u postgres createuser $USER
3433
sudo -i -u postgres createdb folksonomy -O $USER
3534

36-
# Initialize the database using yoyo-migrations
37-
yoyo apply --database postgresql:///folksonomy
35+
# Initialize the database using yoyo-migrations (inside Poetry environment)
36+
poetry run yoyo apply --database postgresql:///folksonomy
3837

3938
# Create local settings file
4039
cp local_settings_example.py local_settings.py
4140
# Edit local_settings.py to fit your environment if needed
4241

4342
# Run the application
44-
uvicorn folksonomy.api:app --reload --host <your-ip-address>
43+
poetry run uvicorn folksonomy.api:app --reload --host <your-ip-address>
4544
```
4645

4746
## Alternative Setup Methods
4847

49-
### Using virtualenvwrapper
48+
### Using Poetry with an existing repository
5049

5150
```bash
5251
# Clone repo
5352
git clone https://github.com/openfoodfacts/folksonomy_api.git
5453
cd folksonomy_api
5554

56-
# Required packages to setup a virtualenv (optional, but recommended)
57-
apt install python3-virtualenv virtualenv virtualenvwrapper
58-
59-
# Create and switch to virtualenv
60-
# If mkvirtualenv command is not found, search for virtualenvwrapper.sh
61-
# (/usr/share/virtualenvwrapper/virtualenvwrapper.sh, or /usr/bin/virtualenvwrapper.sh, for example)
55+
# Install Poetry if not already installed
56+
curl -sSL https://install.python-poetry.org | python3 -
6257

63-
# Add the path in your bash profile
64-
mkvirtualenv folksonomy -p /usr/bin/python3
65-
workon folksonomy
58+
# Install dependencies
59+
poetry install
6660

67-
# Install
68-
pip install -r requirements.txt
61+
# Activate the virtual environment
62+
poetry shell
6963
```
7064

7165
### PostgreSQL Setup Options
@@ -108,30 +102,30 @@ docker rmi ef6f102be0da
108102

109103
### Database Migration
110104

111-
The recommended method now uses `yoyo-migrations`:
105+
Using Poetry with yoyo-migrations:
112106

113107
```bash
114-
yoyo apply --database postgresql:///folksonomy
108+
poetry run yoyo apply --database postgresql:///folksonomy
115109
```
116110

117111
Alternatively, you can use the original migration script:
118112

119113
```bash
120114
# At the end, launch database migration tool; it will initialize the db and/or update the database if there are migrations to apply
121115
# You can run it on a regular basis to apply new migrations
122-
python ./db-migration.py
116+
poetry run python ./db-migration.py
123117
```
124118

125119
## Run locally
126120

127121
```bash
128-
uvicorn folksonomy.api:app --reload
122+
poetry run uvicorn folksonomy.api:app --reload
129123
```
130124

131125
or use `--host` if you want to make it available on your local network:
132126

133127
```bash
134-
uvicorn folksonomy.api:app --reload --host <your-ip-address>
128+
poetry run uvicorn folksonomy.api:app --reload --host <your-ip-address>
135129
```
136130

137131
## Run with a local instance of Product Opener
@@ -148,7 +142,7 @@ To do so you can:
148142
This then should work:
149143

150144
```bash
151-
uvicorn folksonomy.api:app --host 127.0.0.1 --reload --port 8888
145+
poetry run uvicorn folksonomy.api:app --host 127.0.0.1 --reload --port 8888
152146
```
153147

154148
You can then access the API at http://api.folksonomy.openfoodfacts.localhost:8888/docs

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ PostgreSQL is used as the backend database.
2020

2121
# Dev
2222

23-
You should create unit tests for each new feature or API change (see [test_main.py](https://github.com/openfoodfacts/folksonomy_api/blob/main/tests/test_main.py)).
23+
You should create unit tests for each new feature or API change (see [test_main.py](https://github.com/openfoodfacts/folksonomy_api/blob/main/tests/test_main.py)).
2424
To run tests just launch:
2525
```bash
2626
PYTHONASYNCIODEBUG=1 pytest tests/ folksonomy/
@@ -41,8 +41,8 @@ FastAPI is based on [OpenAPI](https://github.com/OAI/OpenAPI-Specification) (pre
4141

4242
# How to install Folksonomy Engine on your local machine
4343

44-
1. Install Python 3.8+
45-
2. Install pip
44+
1. Install Python 3.9+
45+
2. Install Poetry
4646
3. Install PostgreSQL 13+
4747
4. Follow the steps in [INSTALL.md](https://github.com/openfoodfacts/folksonomy_api/blob/main/INSTALL.md) to install requirements and for creating a db user
4848
5. Make a copy of [local_settings_example.py](https://github.com/openfoodfacts/folksonomy_api/blob/main/local_settings_example.py) and rename it to *local_settings.py*

folksonomy/api.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ async def get_unique_values(response: Response,
601601
k: str,
602602
owner: str = '',
603603
q: str = '',
604-
limit: int = '',
604+
limit: int = 50,
605605
user: User = Depends(get_current_user)):
606606
"""
607607
Get the unique values of a given property and the corresponding number of products
@@ -613,8 +613,7 @@ async def get_unique_values(response: Response,
613613
"""
614614
check_owner_user(user, owner, allow_anonymous=True)
615615
k, _ = sanitize_data(k, None)
616-
if not limit:
617-
limit = 50
616+
618617
if limit > 1000:
619618
limit = 1000
620619

@@ -644,7 +643,7 @@ async def get_unique_values(response: Response,
644643

645644
cur, timing = await db.db_exec(sql, params)
646645
out = await cur.fetchone()
647-
data = out[0] if out and out[0] else []
646+
data = out[0] if out and out[0] is not None else []
648647
return JSONResponse(status_code=200, content=data, headers={"x-pg-timing": timing})
649648

650649

@@ -655,4 +654,4 @@ async def pong(response: Response):
655654
"""
656655
cur, timing = await db.db_exec("SELECT current_timestamp AT TIME ZONE 'GMT'",())
657656
pong = await cur.fetchone()
658-
return {"ping": "pong @ %s" % pong[0]}
657+
return {"ping": "pong @ %s" % pong[0]}

0 commit comments

Comments
 (0)