Skip to content

Commit 9948d30

Browse files
Package init (#3)
* Package identification * Code * Add testing framework * Add workflows for tests and release * Reformat * Fix bugs, refactor * Refactor * Django integration (#4) * Add django integration * Remove debug * Add tests * Bugfixes, refactor * Fix tests * Formatter and test setup * Fix workflows --------- Co-authored-by: Tomasz Kwiatkowski <[email protected]>
1 parent e75df38 commit 9948d30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1598
-17
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: migrations
2+
run-name: Check if all migrations are created
3+
on:
4+
push:
5+
branches: 'main'
6+
pull_request:
7+
jobs:
8+
pytest:
9+
runs-on: ubuntu-latest
10+
name: migrations-ubuntu
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v3
14+
- name: Set up Python
15+
uses: actions/setup-python@v4
16+
with:
17+
python-version: 3.12
18+
- name: Install dependencies
19+
run: |
20+
pip3 install .[django]
21+
- name: Check migrations
22+
run: |
23+
python3 tests/test_django/manage.py makemigrations --dry-run --check

.github/workflows/formatter.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Python Formatter (isort & black)
22

33
on:
4+
push:
5+
branches: 'main'
46
pull_request:
5-
branches: [main]
6-
77
jobs:
88
formatting:
99
runs-on: ubuntu-latest
@@ -12,7 +12,7 @@ jobs:
1212
- name: Set up Python
1313
uses: actions/setup-python@v5
1414
with:
15-
python-version: '3.9'
15+
python-version: '3.10'
1616
- name: Install isort and black
1717
run: |
1818
python -m pip install --upgrade pip

.github/workflows/macOS.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: pytest-macos
2+
run-name: Run pytest on macOS
3+
on:
4+
push:
5+
branches: 'main'
6+
pull_request:
7+
8+
jobs:
9+
pytest:
10+
runs-on: macos-latest
11+
strategy:
12+
matrix:
13+
python-version: ["3.10", "3.13"]
14+
name: pytest-macos-python-${{ matrix.python-version }}
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v3
18+
- name: Install Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Set up Homebrew
23+
uses: Homebrew/actions/setup-homebrew@master
24+
- name: Install Homebrew dependencies
25+
run: |
26+
rm -f /usr/local/bin/2to3* /usr/local/bin/python3* /usr/local/bin/idle3* \
27+
/usr/local/bin/pydoc3* # Homebrew will fail if these exist
28+
brew install virtualenv
29+
- name: Install Python dependencies
30+
run: |
31+
python3 -m venv .venv
32+
source .venv/bin/activate
33+
pip install -e .[tests]
34+
- name: Run pytest without Django
35+
env:
36+
PYTEST_ADDOPTS: "--color=yes"
37+
run: |
38+
source .venv/bin/activate
39+
pytest -v -n auto
40+
- name: Run pytest with Django
41+
env:
42+
PYTEST_ADDOPTS: "--color=yes"
43+
run: |
44+
source .venv/bin/activate
45+
pip install -e .[tests,django_tests,django]
46+
./tests/test_django/manage.py migrate
47+
pytest -v -n auto

.github/workflows/release.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: publish
2+
run-name: Publish to PyPi
3+
on:
4+
release:
5+
types: [published]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
build-and-publish:
12+
name: Build and publish to PyPi
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v3
18+
- name: Set up Python
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: 3.11
22+
- name: Install build
23+
run: |
24+
python3 -m pip install build
25+
python3 -m pip install setuptools --upgrade
26+
- name: Build
27+
run: |
28+
python3 -m build --sdist --wheel --outdir dist/ .
29+
- name: Upload to release
30+
env:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
run: |
33+
gh release upload ${{ github.event.release.tag_name }} dist/*
34+
- name: Publish
35+
uses: pypa/gh-action-pypi-publish@release/v1
36+
with:
37+
password: ${{ secrets.TOKEN }}

.github/workflows/ubuntu.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: pytest-ubuntu
2+
run-name: Run pytest on Ubuntu
3+
on:
4+
push:
5+
branches: 'main'
6+
pull_request:
7+
jobs:
8+
pytest:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
python-version: ["3.10", "3.13"]
13+
name: pytest-ubuntu-python-${{ matrix.python-version }}
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v3
17+
- name: Set up Python ${{ matrix.python-version }}
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: ${{ matrix.python-version }}
21+
- name: Install dependencies
22+
run: |
23+
pip3 install -e .[tests]
24+
- name: Run pytest without Django
25+
env:
26+
PYTEST_ADDOPTS: "--color=yes"
27+
run: |
28+
python3 -m pytest -v -n auto
29+
- name: Run pytest with Django
30+
env:
31+
PYTEST_ADDOPTS: "--color=yes"
32+
run: |
33+
pip3 install -e .[tests,django_tests,django]
34+
./tests/test_django/manage.py migrate
35+
python3 -m pytest -v -n auto

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ build
55
.vscode
66
.idea
77
__pycache__
8+
tests/test_django/db.sqlite3
89

910
# pytest-cov
1011
.coverage*

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ include_trailing_comma = true
99

1010
[tool.black]
1111
line_length = 120
12+
exclude = "migrations/"

pytest.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
DJANGO_SETTINGS_MODULE = test_django.settings
3+
pythonpath = ./tests/test_django
4+
markers =
5+
no_django: marks tests that should be run without Django

setup.cfg

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ packages = find_namespace:
2121
packages_dir = src
2222
include_package_data = True
2323
python_requires = >=3.9
24-
install_requires =
24+
install_requires =
25+
PyYAML
2526

2627
[options.packages.find]
2728
where = src
@@ -31,6 +32,10 @@ tests =
3132
pytest
3233
pytest-cov
3334
pytest-xdist
35+
django_tests =
36+
pytest-django
37+
django =
38+
django
3439

3540
[tool:pytest]
3641
testpaths =

src/sio3pack/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
11
__version__ = "0.0.1"
2+
3+
from sio3pack.files import LocalFile
4+
from sio3pack.packages.exceptions import ImproperlyConfigured, PackageAlreadyExists
5+
from sio3pack.packages.package import Package
6+
7+
8+
def from_file(file: str | LocalFile, django_settings=None) -> Package:
9+
"""
10+
Initialize a package object from a file (archive or directory).
11+
:param file: The file path or File object.
12+
:param django_settings: Django settings object.
13+
:return: The package object.
14+
"""
15+
if isinstance(file, str):
16+
file = LocalFile(file)
17+
return Package.from_file(file, django_settings=django_settings)
18+
19+
20+
def from_db(problem_id: int) -> Package:
21+
"""
22+
Initialize a package object from the database.
23+
If sio3pack isn't installed with Django support, it should raise an ImproperlyConfigured exception.
24+
If there is no package with the given problem_id, it should raise an UnknownPackageType exception.
25+
:param problem_id: The problem id.
26+
:return: The package object.
27+
"""
28+
try:
29+
import django
30+
31+
return Package.from_db(problem_id)
32+
except ImportError:
33+
raise ImproperlyConfigured("sio3pack is not installed with Django support.")

0 commit comments

Comments
 (0)