Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package init #3

Merged
merged 9 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/check_migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: migrations
run-name: Check if all migrations are created
on:
push:
branches: 'main'
pull_request:
jobs:
pytest:
runs-on: ubuntu-latest
name: migrations-ubuntu
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.12
- name: Install dependencies
run: |
pip3 install .[django]
- name: Check migrations
run: |
python3 tests/test_django/manage.py makemigrations --dry-run --check
6 changes: 3 additions & 3 deletions .github/workflows/formatter.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Python Formatter (isort & black)

on:
push:
branches: 'main'
pull_request:
branches: [main]

jobs:
formatting:
runs-on: ubuntu-latest
Expand All @@ -12,7 +12,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.9'
python-version: '3.10'
- name: Install isort and black
run: |
python -m pip install --upgrade pip
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/macOS.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: pytest-macos
run-name: Run pytest on macOS
on:
push:
branches: 'main'
pull_request:

jobs:
pytest:
runs-on: macos-latest
strategy:
matrix:
python-version: ["3.10", "3.13"]
name: pytest-macos-python-${{ matrix.python-version }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Set up Homebrew
uses: Homebrew/actions/setup-homebrew@master
- name: Install Homebrew dependencies
run: |
rm -f /usr/local/bin/2to3* /usr/local/bin/python3* /usr/local/bin/idle3* \
/usr/local/bin/pydoc3* # Homebrew will fail if these exist
brew install virtualenv
- name: Install Python dependencies
run: |
python3 -m venv .venv
source .venv/bin/activate
pip install -e .[tests]
- name: Run pytest without Django
env:
PYTEST_ADDOPTS: "--color=yes"
run: |
source .venv/bin/activate
pytest -v -n auto
- name: Run pytest with Django
env:
PYTEST_ADDOPTS: "--color=yes"
run: |
source .venv/bin/activate
pip install -e .[tests,django_tests,django]
./tests/test_django/manage.py migrate
pytest -v -n auto
37 changes: 37 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: publish
run-name: Publish to PyPi
on:
release:
types: [published]

permissions:
contents: write

jobs:
build-and-publish:
name: Build and publish to PyPi
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11
- name: Install build
run: |
python3 -m pip install build
python3 -m pip install setuptools --upgrade
- name: Build
run: |
python3 -m build --sdist --wheel --outdir dist/ .
- name: Upload to release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release upload ${{ github.event.release.tag_name }} dist/*
- name: Publish
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TOKEN }}
35 changes: 35 additions & 0 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: pytest-ubuntu
run-name: Run pytest on Ubuntu
on:
push:
branches: 'main'
pull_request:
jobs:
pytest:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.13"]
name: pytest-ubuntu-python-${{ matrix.python-version }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip3 install -e .[tests]
- name: Run pytest without Django
env:
PYTEST_ADDOPTS: "--color=yes"
run: |
python3 -m pytest -v -n auto
- name: Run pytest with Django
env:
PYTEST_ADDOPTS: "--color=yes"
run: |
pip3 install -e .[tests,django_tests,django]
./tests/test_django/manage.py migrate
python3 -m pytest -v -n auto
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build
.vscode
.idea
__pycache__
tests/test_django/db.sqlite3

# pytest-cov
.coverage*
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ include_trailing_comma = true

[tool.black]
line_length = 120
exclude = "migrations/"
5 changes: 5 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
DJANGO_SETTINGS_MODULE = test_django.settings
pythonpath = ./tests/test_django
markers =
no_django: marks tests that should be run without Django
7 changes: 6 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ packages = find_namespace:
packages_dir = src
include_package_data = True
python_requires = >=3.9
install_requires =
install_requires =
PyYAML

[options.packages.find]
where = src
Expand All @@ -31,6 +32,10 @@ tests =
pytest
pytest-cov
pytest-xdist
django_tests =
pytest-django
django =
django

[tool:pytest]
testpaths =
Expand Down
32 changes: 32 additions & 0 deletions src/sio3pack/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
__version__ = "0.0.1"

from sio3pack.files import LocalFile
from sio3pack.packages.exceptions import ImproperlyConfigured, PackageAlreadyExists
from sio3pack.packages.package import Package


def from_file(file: str | LocalFile, django_settings=None) -> Package:
"""
Initialize a package object from a file (archive or directory).
:param file: The file path or File object.
:param django_settings: Django settings object.
:return: The package object.
"""
if isinstance(file, str):
file = LocalFile(file)
return Package.from_file(file, django_settings=django_settings)


def from_db(problem_id: int) -> Package:
"""
Initialize a package object from the database.
If sio3pack isn't installed with Django support, it should raise an ImproperlyConfigured exception.
If there is no package with the given problem_id, it should raise an UnknownPackageType exception.
:param problem_id: The problem id.
:return: The package object.
"""
try:
import django

return Package.from_db(problem_id)
except ImportError:
raise ImproperlyConfigured("sio3pack is not installed with Django support.")
File renamed without changes.
Empty file.
17 changes: 17 additions & 0 deletions src/sio3pack/django/sinolpack/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from sio3pack.django.sinolpack.models import SinolpackPackage
from sio3pack.packages.exceptions import PackageAlreadyExists
from sio3pack.packages.package.django.handler import DjangoHandler


class SinolpackDjangoHandler(DjangoHandler):
def save_to_db(self):
"""
Save the package to the database.
"""
if SinolpackPackage.objects.filter(problem_id=self.problem_id).exists():
raise PackageAlreadyExists(self.problem_id)

SinolpackPackage.objects.create(
problem_id=self.problem_id,
short_name=self.package.short_name,
)
21 changes: 21 additions & 0 deletions src/sio3pack/django/sinolpack/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.1.3 on 2024-12-01 17:22

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="SinolpackPackage",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("problem_id", models.IntegerField()),
("short_name", models.CharField(max_length=100)),
],
),
]
Empty file.
10 changes: 10 additions & 0 deletions src/sio3pack/django/sinolpack/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.db import models


class SinolpackPackage(models.Model):
"""
A package for the sinolpack package type.
"""

problem_id = models.IntegerField()
short_name = models.CharField(max_length=100)
1 change: 1 addition & 0 deletions src/sio3pack/files/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from sio3pack.files.file import File
from sio3pack.files.filetracker_file import FiletrackerFile
from sio3pack.files.local_file import LocalFile
9 changes: 9 additions & 0 deletions src/sio3pack/files/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ class File:

def __init__(self, path: str):
self.path = path

def __str__(self):
return f"<{self.__class__.__name__} {self.path}>"

def read(self) -> str:
raise NotImplementedError()

def write(self, text: str):
raise NotImplementedError()
2 changes: 2 additions & 0 deletions src/sio3pack/files/filetracker_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ class FiletrackerFile(File):

def __init__(self, path: str):
super().__init__(path)
# TODO: should raise FileNotFoundError if file is not tracked
raise NotImplementedError()
28 changes: 28 additions & 0 deletions src/sio3pack/files/local_file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from sio3pack.files.file import File


Expand All @@ -6,5 +8,31 @@ class LocalFile(File):
Base class for all files in a package that are stored locally.
"""

@classmethod
def get_file_matching_extension(cls, dir: str, filename: str, extensions: list[str]) -> "LocalFile":
"""
Get the file with the given filename and one of the given extensions.
:param dir: The directory to search in.
:param filename: The filename.
:param extensions: The extensions.
:return: The file object.
"""
for ext in extensions:
path = os.path.join(dir, filename + ext)
if os.path.exists(path):
return cls(path)
raise FileNotFoundError

def __init__(self, path: str):
if not os.path.exists(path):
raise FileNotFoundError
super().__init__(path)
self.filename = os.path.basename(path)

def read(self) -> str:
with open(self.path, "r") as f:
return f.read()

def write(self, text: str):
with open(self.path, "w") as f:
f.write(text)
4 changes: 3 additions & 1 deletion src/sio3pack/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from sio3pack.graph import Graph
from sio3pack.graph.graph import Graph
from sio3pack.graph.graph_manager import GraphManager
from sio3pack.graph.graph_op import GraphOperation
10 changes: 10 additions & 0 deletions src/sio3pack/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,15 @@ class Graph:
A class to represent a job graph.
"""

@classmethod
def from_dict(cls, data: dict):
raise NotImplementedError()

def __init__(self, name: str):
self.name = name

def get_prog_files(self) -> list[str]:
"""
Get all program files in the graph.
"""
raise NotImplementedError()
Loading
Loading