Skip to content

Commit

Permalink
Merge branch 'feature/cli' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
XYCode-Kerman committed Mar 31, 2024
2 parents 00fc3e9 + 0b3c3af commit ccae8a4
Show file tree
Hide file tree
Showing 10 changed files with 730 additions and 4 deletions.
5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import pathlib

from ccf_parser import CCF
from manager import start_server_background
from manager import cli_app, start_server_background

if '__main__' == __name__:
start_server_background()
cli_app()
# start_server_background()
1 change: 1 addition & 0 deletions manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .base import start_server_background
from .cli import cli_app
1 change: 1 addition & 0 deletions manager/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .base import app as cli_app
26 changes: 26 additions & 0 deletions manager/cli/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Optional

import requests
import typer
from rich import print
from rich.markdown import Markdown
from rich.panel import Panel
from rich.text import Text

from .judge import app as judge_typer

app = typer.Typer()
app.add_typer(judge_typer)


@app.command(name='intro', help='查看ItsWA介绍')
def intro():
md = Markdown(
"""
## 欢迎使用 ItsWA 评测系统
ItsWA是一个基于Python搭建,使用`Lrun`提供安全运行时的Linux下的竞赛代码评测系统。
查看`docs/guide.pdf`获取使用教程
"""
)

print(md)
26 changes: 26 additions & 0 deletions manager/cli/judge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json
from pathlib import Path
from typing import Annotated, Optional, Union

import typer

from ccf_parser import CCF
from judge import start_judging
from utils import manager_logger

app = typer.Typer(name='judge', help='评测相关')


@app.command('start', help='开始评测')
def start_judging_command(path: Annotated[Path, typer.Argument(help='比赛目录或CCF文件')] = Path('.')):
ccf: Optional[CCF] = None

if path.name == 'ccf.json':
ccf = CCF(**json.loads(path.read_text('utf-8')))
elif path.is_dir() and path.joinpath('ccf.json').exists():
ccf = CCF(**json.loads(path.joinpath('ccf.json').read_text('utf-8')))
else:
raise FileNotFoundError('评测目录不正确,可能是不存在CCF文件')

start_judging(ccf)
return 0
617 changes: 616 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pydantic = "^2.6.4"
typer = "^0.10.0"
fastapi = "^0.110.0"
uvicorn = "^0.29.0"
aiohttp = "^3.9.3"
requests = "^2.31.0"


[[tool.poetry.source]]
Expand Down
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import shutil
import tempfile
from pathlib import Path
from typing import Generator

import pytest


@pytest.fixture(scope='function')
def temp_contest():
tmpcon = tempfile.mkdtemp()
Path(tmpcon).mkdir(parents=True, exist_ok=True)

shutil.copytree(
Path('./tests/environment').absolute().__str__(),
tmpcon,
dirs_exist_ok=True
)

yield Path(tmpcon).absolute()

# shutil.rmtree(tmpcon)
32 changes: 32 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pathlib import Path

import pytest
from typer.testing import CliRunner

from manager import cli_app


@pytest.fixture
def cli_runner() -> CliRunner:
return CliRunner()


def test_intro(cli_runner: CliRunner):
result = cli_runner.invoke(cli_app, ["intro"])

assert result.exit_code == 0
assert "欢迎使用 ItsWA 评测系统" in result.stdout


def test_start_judging(cli_runner: CliRunner, temp_contest: Path):
result = cli_runner.invoke(
cli_app, ["judge", "start", temp_contest.absolute().__str__()])
assert result.exit_code == 0

result = cli_runner.invoke(
cli_app, ["judge", "start", temp_contest.joinpath('ccf.json').absolute().__str__()])
assert result.exit_code == 0

result = cli_runner.invoke(
cli_app, ["judge", "start", temp_contest.joinpath('114514').absolute().__str__()])
assert result.exit_code != 0
2 changes: 1 addition & 1 deletion utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

logging.basicConfig(
handlers=[RichHandler()],
level=logging.DEBUG,
level=logging.INFO,
)

manager_logger = logging.getLogger('ItsWA-Manager')
Expand Down

0 comments on commit ccae8a4

Please sign in to comment.