Introduce Submission

This commit is contained in:
Matej Focko 2019-11-30 17:47:52 +01:00
parent 3bb2282dd0
commit d992e18bbd
7 changed files with 122 additions and 46 deletions

View file

@ -2,37 +2,33 @@
import os import os
from typing import List
from constants import HOMEWORK
from git import checkout_branch from git import checkout_branch
from utils import make_pair, get_branch, mkcd from utils import mkcd
from submission import Submission
class BaseCommand: class BaseCommand:
def __init__(self, submissions): def __init__(self, submissions: List[Submission]) -> None:
self.submissions = map(make_pair, submissions) self.submissions = submissions
self.branch = None
def __call__(self): def __call__(self) -> None:
for login, submission in self.submissions: for submission in self.submissions:
self.branch = get_branch(login) self.exec(submission)
self.exec(login, submission) def exec(self, submission: Submission) -> None:
def exec(self, login: str, submission: str):
raise NotImplementedError() raise NotImplementedError()
class BaseGitCommand(BaseCommand): class BaseGitCommand(BaseCommand):
def __call__(self): def __call__(self) -> None:
for login, submission in self.submissions: for submission in self.submissions:
self.branch = get_branch(login) checkout_branch(submission.branch)
mkcd(f"{submission.homework}/{submission.login}")
checkout_branch(self.branch) self.exec(submission)
mkcd(f"{HOMEWORK}/{login}")
self.exec(login, submission)
os.chdir("../..") os.chdir("../..")
checkout_branch("master") checkout_branch("master")

View file

@ -6,6 +6,7 @@ import json
from commands.base import BaseCommand from commands.base import BaseCommand
from gitlab import get_mrs_for_branch, get_comments from gitlab import get_mrs_for_branch, get_comments
from submission import Submission
class Comments(BaseCommand): class Comments(BaseCommand):
@ -48,14 +49,14 @@ class Comments(BaseCommand):
print() print()
print() print()
def exec(self, login: str, submission: str): def exec(self, submission: Submission) -> None:
mrs = get_mrs_for_branch(self.branch) mrs = get_mrs_for_branch(submission.branch)
if not mrs: if not mrs:
return return
iid = mrs[0]["iid"] iid = mrs[0]["iid"]
comments = get_comments(iid) comments = get_comments(iid)
print(f"# {self.branch}") print(f"### {submission.branch.center(20)} ###")
self.print_comments(comments) self.print_comments(comments)
print(f"# {self.branch}") print(f"### {submission.branch.center(20)} ###")

View file

@ -3,10 +3,11 @@
from commands.base import BaseCommand from commands.base import BaseCommand
from gitlab import get_mrs_for_branch, merge_mr from gitlab import get_mrs_for_branch, merge_mr
from submission import Submission
class Merge(BaseCommand): class Merge(BaseCommand):
def exec(self, login: str, submission: str): def exec(self, submission: Submission) -> None:
iid = get_mrs_for_branch(self.branch)[0]["iid"] iid = get_mrs_for_branch(submission.branch)[0]["iid"]
merge_mr(iid) merge_mr(iid)

View file

@ -4,25 +4,24 @@
import re import re
from constants import HOMEWORK, SUFFIX
from commands.base import BaseGitCommand from commands.base import BaseGitCommand
from git import add_files, commit, push from git import add_files, commit, push
from gitlab import post_mr from gitlab import post_mr
from submission import Submission
from utils import run_cmd from utils import run_cmd
class MergeRequests(BaseGitCommand): class MergeRequests(BaseGitCommand):
@staticmethod @staticmethod
def get_files(submission: str) -> None: def get_files(submission: Submission) -> None:
relative_path = f"/home/kontr/kontr/_tmp_/ib111/{HOMEWORK}/{submission}" files = f"{{master-naostro/LoadTest/{submission.homework}.py,teacher_email}}"
files = f"{{master-naostro/LoadTest/{HOMEWORK}.py,teacher_email}}"
if run_cmd("rsync", "-avzP", f"aisa:{relative_path}/{files}", "./")[0] != 0: if run_cmd("rsync", "-avzP", f"aisa:{submission.path}/{files}", "./")[0] != 0:
exit(1) exit(1)
@staticmethod @staticmethod
def call_flake() -> None: def call_flake(submission: Submission) -> None:
process = run_cmd("flake8", "--exit-zero", f"{HOMEWORK}.py")[1] process = run_cmd("flake8", "--exit-zero", f"{submission.homework}.py")[1]
with open("flake.log", "w") as f: with open("flake.log", "w") as f:
print(process.stdout, file=f) print(process.stdout, file=f)
@ -34,20 +33,20 @@ class MergeRequests(BaseGitCommand):
return match.group(1) if match else contents return match.group(1) if match else contents
def exec(self, login: str, submission: str) -> None: def exec(self, submission: Submission) -> None:
self.get_files(submission) self.get_files(submission)
self.call_flake() self.call_flake(submission)
add_files(f"{HOMEWORK}.py", "flake.log") add_files(f"{submission.homework}.py", "flake.log")
commit(f'"Add sources and flake log ({HOMEWORK}{SUFFIX} {login})"') commit(f'"Add sources and flake log ({submission.branch} {submission.login})"')
push("origin", self.branch) push("origin", submission.branch)
post_mr( post_mr(
source_branch=self.branch, source_branch=submission.branch,
target_branch="master", target_branch="master",
title=f"[{HOMEWORK}{SUFFIX}] {login}", title=f"[{submission.branch}] {submission.login}",
description=f"```\n{self.get_mail()}\n```", description=f"```\n{self.get_mail()}\n```",
labels=HOMEWORK, labels=submission.homework,
remove_source_branch="true", remove_source_branch="true",
assignee_ids=["1772"], assignee_ids=["1772"],
) )

View file

@ -2,8 +2,9 @@
from commands.base import BaseCommand from commands.base import BaseCommand
from submission import print_submissions, Submission
class Test(BaseCommand): class Test(BaseCommand):
def exec(self, login: str, submission: str): def exec(self, submission: Submission) -> None:
print(f"{login} - {submission}") print(f"{submission.login}, {submission.submitted_at}, {submission.points}")

View file

@ -3,11 +3,12 @@
from commands.base import BaseCommand from commands.base import BaseCommand
from gitlab import get_mrs_for_branch, set_assignees from gitlab import get_mrs_for_branch, set_assignees
from submission import Submission
class UpdateAssignees(BaseCommand): class UpdateAssignees(BaseCommand):
def exec(self, login: str, submission: str) -> None: def exec(self, submission: Submission) -> None:
iid = get_mrs_for_branch(self.branch)[0]["iid"] iid = get_mrs_for_branch(submission.branch)[0]["iid"]
print(f"{login} @ {self.branch} : {iid}") print(f"{submission.login} @ {submission.branch} : {iid}")
set_assignees(iid, [39, 1772]) set_assignees(iid, [39, 1772])

77
submission.py Normal file
View file

@ -0,0 +1,77 @@
#!/usr/bin/env python3
import datetime
from typing import Dict, List
class Submission:
def __init__(
self,
uco: str,
login: str,
path: str,
points: float,
homework: str,
correction: bool,
) -> None:
self.uco = uco
self.login = login
self.path = path
self.points = points
self.homework = homework
self.correction = correction
self.submitted_at = None
self.submitted_before_deadline = None
self.flag = None
self.branch = f"{homework}{'-opravne' if correction else ''}-{login}"
self.__set_submission_date()
def __set_submission_date(self) -> None:
self.submitted_at = datetime.datetime.strptime(
self.path.split("/")[-1][-16:], "%Y_%m%d_%H%M%S"
)
def set_late_tag(self, deadline: datetime.datetime) -> None:
self.submitted_before_deadline = self.submitted_at < deadline
if not self.submitted_before_deadline:
self.flag = "LATE"
def print_submissions(all_submissions: Dict[str, List[Submission]]) -> None:
header = (
"| "
+ " | ".join(
[
"###",
"UCO".center(6),
"LOGIN".center(8),
"SUBMITTED".center(19),
"POINTS",
"FLAG".center(6),
]
)
+ " |"
)
SEPARATOR = "-" * len(header)
print(SEPARATOR)
print(header)
print(SEPARATOR)
FORMAT = "| {:>3} | {:>6s} | {:>8s} | {} | {:>6.2f} | {:^6s} |"
for _, submissions in all_submissions.items():
for i, submission in enumerate(submissions):
date = submission.submitted_at.strftime("%Y-%m-%d %H:%M:%S")
print(
FORMAT.format(
i,
submission.uco if i == 0 else "",
submission.login if i == 0 else "",
date,
submission.points,
submission.flag if submission.flag else "",
)
)
print(SEPARATOR)