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

Add configuration for tox, poetry and mypy #3

Merged
merged 11 commits into from
Nov 6, 2023
34 changes: 34 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!-- TODO, write more about tox and poetry later -->

# What you need.

Install tox as mentioned [here](https://tox.wiki/en/latest/installation.html)

Then install poetry as detailed [here](https://python-poetry.org/docs/#installation)



## Adding a dependency
Warning, never use pip to add a dependency. Nasty things are gonna
happen if you ignore that advice.

```
$ poetry add yourdep yourdep2 ...
```

The command may take time due to dependency resolution sometimes, but
if it exceeds 5 minutes it is abnormal. Usually it should be done before you notice.

## Spawning a shell in the environment

```
$ poetry shell
```

You can learn more at https://python-poetry.org/docs/cli/

## Running tests

```
$ tox r
```
5 changes: 2 additions & 3 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ jobs:
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
- name: Install Tox
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
python -m pip install tox
- name: Run the tox environment
run: |
tox
2 changes: 1 addition & 1 deletion OVERVIEW.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Rough functionality:
- clear (NEW)
- Stops all running containers associated with the team
- CTF
- (id) -> Get CTF info
- (id) -> Get CTF info
- start
- Check if team has already completed it
- Only if no container is assigned to the team before
Expand Down
10 changes: 0 additions & 10 deletions app.py

This file was deleted.

1 change: 0 additions & 1 deletion config.py

This file was deleted.

1 change: 0 additions & 1 deletion db.py

This file was deleted.

8 changes: 0 additions & 8 deletions docs.py

This file was deleted.

649 changes: 649 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[virtualenvs]
in-project = true
59 changes: 59 additions & 0 deletions pyproject.toml
WizzyGeek marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[tool.poetry]
name = "pwncore"
version = "0.1.0"
description = "The backend for an advanced CTF platform"
authors = ["LUGVITC"]
license = "GNU GENERAL PUBLIC LICENSE"
readme = "README.md"

[[tool.poetry.packages]]
include = "pwncore"
from = "src"

# Format to add new packages to the wheel
# [[tool.poetry.packages]]
# include = "new_package_or_maybe_script?"
# from = "src"

[tool.poetry.dependencies]
python = "^3.11"
fastapi = "^0.104.1"
uvicorn = "^0.24.0"

[tool.poetry.group.dev.dependencies]
mypy = "^1.6.1"
httpx = "^0.25.1"
pytest = "^7.4.3"
flake8 = "^6.1.0"
black = "^23.10.1"
flake8-bugbear = "^23.9.16"

[tool.mypy]
python_version = 3.11
warn_return_any = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_unused_configs = true
check_untyped_defs = true
show_column_numbers = true
show_error_codes = true

enable_error_code = [
"ignore-without-code",
"truthy-bool",
"truthy-iterable",
"redundant-expr",
"no-any-unimported",
"redundant-self",
"type-arg"
]

pretty = true

# For per file / package config
# [[tool.mypy.overrides]]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
32 changes: 0 additions & 32 deletions requirements.txt

This file was deleted.

10 changes: 10 additions & 0 deletions src/pwncore/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from fastapi import FastAPI

import pwncore.docs as docs
import pwncore.routes as routes

app = FastAPI(
title="Pwncore", openapi_tags=docs.tags_metadata, description=docs.description
)

app.include_router(routes.router)
4 changes: 4 additions & 0 deletions src/pwncore/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pwncore import app # noqa

# I do not trust my self to call uvicorn from here for debugging and
# not use that in production
22 changes: 22 additions & 0 deletions src/pwncore/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

import typing as t

__all__ = (
"Config",
"BaseConfig",
)


class Config(t.Protocol):
development: bool


class BaseConfig(Config):
__slots__ = ("development",)

def __init__(self, development: bool) -> None:
self.development = development


DEV_CONFIG: t.Final[BaseConfig] = BaseConfig(True)
5 changes: 5 additions & 0 deletions src/pwncore/docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pwncore.routes import ctf, team

tags_metadata = [ctf.metadata, team.metadata]

description = "Example description to be edited from /docs.py"
File renamed without changes.
7 changes: 2 additions & 5 deletions routes/__init__.py → src/pwncore/routes/__init__.py
WizzyGeek marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
from . import ctf, team

# Main router (all routes go under /api)
router = APIRouter(
prefix="/api"
)
router = APIRouter(prefix="/api")

# Include all the subroutes
router.include_router(ctf.router)
router.include_router(team.router)

# Miscellaneous routes


# Miscellaneous routes
@router.get("/asd")
async def a():
return {"ASD": "asd"}
File renamed without changes.
19 changes: 9 additions & 10 deletions routes/ctf/__init__.py → src/pwncore/routes/ctf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
from __future__ import annotations

from fastapi import APIRouter

# Metadata at the top for instant accessibility
metadata = {
"name": "ctf",
"description": "Operations related to CTF, except create and delete (those are under admin)"
"description": "Operations related to CTF, except"
"create and delete (those are under admin)",
}

from fastapi import APIRouter

router = APIRouter(
prefix="/ctf",
tags=["ctf"]
)

from . import start
router = APIRouter(prefix="/ctf", tags=["ctf"])

# Routes that do not need a separate submodule for themselves


@router.get("/list")
async def ctf_list():
# Get list of ctfs
return [
{"name": "Password Juggling", "ctf_id": 2243},
{"name": "hexane", "ctf_id": 2242}
{"name": "hexane", "ctf_id": 2242},
]


Expand Down
14 changes: 8 additions & 6 deletions routes/ctf/start.py → src/pwncore/routes/ctf/start.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# Both work, but routes.ctf is more clear to indicate that the router is from ctf
# from . import router
from routes.ctf import router
from __future__ import annotations

from pwncore.routes.ctf import router


@router.get("/start/{ctf_id}")
async def start_the_docker_container(ctf_id: int): # The function name is inferred for the summary
async def start_the_docker_container(
ctf_id: int,
): # The function name is inferred for the summary
# This is a regular single-line comment.
# Will not be displayed in the documentation.
'''
"""
This is a multi-line comment, and will be displayed
in the documentation when the route is expanded.

The cool thing is that Markdown works here!
# See, Markdown works!
_Pretty_ **cool** right?
'''
"""
return {"status": "CTF started"}
Empty file.
14 changes: 5 additions & 9 deletions routes/team.py → src/pwncore/routes/team.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# Metadata at the top for instant accessibility
metadata = {
"name": "team",
"description": "Operations with teams"
}
from __future__ import annotations

from fastapi import APIRouter

router = APIRouter(
prefix="/team",
tags=["team"]
)
# Metadata at the top for instant accessibility
metadata = {"name": "team", "description": "Operations with teams"}

router = APIRouter(prefix="/team", tags=["team"])


@router.get("/list")
Expand Down
2 changes: 2 additions & 0 deletions src/pwncore/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""Only for type aliases and nothing else.
"""
3 changes: 2 additions & 1 deletion tests/test_login.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from fastapi.testclient import TestClient

from app import app
from pwncore import app

client = TestClient(app)


Expand Down
47 changes: 42 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
[flake8]
max-line-length = 80
select = C,E,F,W,B,B950
extend-ignore = E203, E501, E704

[tox]
# TODO: Lock down on one python version to test against
envlist = test_env
skipsdist = true
# DONE?
envlist =
py31{1,2}
type
lint


[testenv]
description = run api tests
deps =
fastapi
httpx
pytest
fastapi
httpx
pytest
commands = pytest

[testenv:type]
base_python = py311
description = run type checking
skip_install = true
deps =
mypy
commands = mypy tests/ src/ {posargs}

[testenv:black]
decription = Use black to format the code
skip_install = true
deps =
black
commands = black src/ tests/

[testenv:lint]
decription = Lint with flake8
skip_install = true
deps =
flake8
flake8-bugbear
commands = flake8 src/ tests/

[testenv:debug]
description = check if the package runs after any configuration changes, not for debugging dev changes
commands = uvicorn pwncore:app

# TODO: Add linting