diff --git a/commands/base.py b/commands/base.py index 2894972..6470e2c 100644 --- a/commands/base.py +++ b/commands/base.py @@ -2,37 +2,33 @@ import os +from typing import List -from constants import HOMEWORK from git import checkout_branch -from utils import make_pair, get_branch, mkcd +from utils import mkcd +from submission import Submission class BaseCommand: - def __init__(self, submissions): - self.submissions = map(make_pair, submissions) - self.branch = None + def __init__(self, submissions: List[Submission]) -> None: + self.submissions = submissions - def __call__(self): - for login, submission in self.submissions: - self.branch = get_branch(login) + def __call__(self) -> None: + for submission in self.submissions: + self.exec(submission) - self.exec(login, submission) - - def exec(self, login: str, submission: str): + def exec(self, submission: Submission) -> None: raise NotImplementedError() class BaseGitCommand(BaseCommand): - def __call__(self): - for login, submission in self.submissions: - self.branch = get_branch(login) + def __call__(self) -> None: + for submission in self.submissions: + checkout_branch(submission.branch) + mkcd(f"{submission.homework}/{submission.login}") - checkout_branch(self.branch) - mkcd(f"{HOMEWORK}/{login}") - - self.exec(login, submission) + self.exec(submission) os.chdir("../..") - checkout_branch("master") \ No newline at end of file + checkout_branch("master") diff --git a/commands/comments.py b/commands/comments.py index 2b994c0..0d1299a 100644 --- a/commands/comments.py +++ b/commands/comments.py @@ -6,6 +6,7 @@ import json from commands.base import BaseCommand from gitlab import get_mrs_for_branch, get_comments +from submission import Submission class Comments(BaseCommand): @@ -48,14 +49,14 @@ class Comments(BaseCommand): print() print() - def exec(self, login: str, submission: str): - mrs = get_mrs_for_branch(self.branch) + def exec(self, submission: Submission) -> None: + mrs = get_mrs_for_branch(submission.branch) if not mrs: return iid = mrs[0]["iid"] comments = get_comments(iid) - print(f"# {self.branch}") + print(f"### {submission.branch.center(20)} ###") self.print_comments(comments) - print(f"# {self.branch}") + print(f"### {submission.branch.center(20)} ###") diff --git a/commands/merge.py b/commands/merge.py index bedd294..d222574 100644 --- a/commands/merge.py +++ b/commands/merge.py @@ -3,10 +3,11 @@ from commands.base import BaseCommand from gitlab import get_mrs_for_branch, merge_mr +from submission import Submission class Merge(BaseCommand): - def exec(self, login: str, submission: str): - iid = get_mrs_for_branch(self.branch)[0]["iid"] + def exec(self, submission: Submission) -> None: + iid = get_mrs_for_branch(submission.branch)[0]["iid"] - merge_mr(iid) \ No newline at end of file + merge_mr(iid) diff --git a/commands/merge_requests.py b/commands/merge_requests.py index dd63747..699e855 100644 --- a/commands/merge_requests.py +++ b/commands/merge_requests.py @@ -4,25 +4,24 @@ import re -from constants import HOMEWORK, SUFFIX from commands.base import BaseGitCommand from git import add_files, commit, push from gitlab import post_mr +from submission import Submission from utils import run_cmd class MergeRequests(BaseGitCommand): @staticmethod - def get_files(submission: str) -> None: - relative_path = f"/home/kontr/kontr/_tmp_/ib111/{HOMEWORK}/{submission}" - files = f"{{master-naostro/LoadTest/{HOMEWORK}.py,teacher_email}}" + def get_files(submission: Submission) -> None: + files = f"{{master-naostro/LoadTest/{submission.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) @staticmethod - def call_flake() -> None: - process = run_cmd("flake8", "--exit-zero", f"{HOMEWORK}.py")[1] + def call_flake(submission: Submission) -> None: + process = run_cmd("flake8", "--exit-zero", f"{submission.homework}.py")[1] with open("flake.log", "w") as f: print(process.stdout, file=f) @@ -34,20 +33,20 @@ class MergeRequests(BaseGitCommand): 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.call_flake() + self.call_flake(submission) - add_files(f"{HOMEWORK}.py", "flake.log") - commit(f'"Add sources and flake log ({HOMEWORK}{SUFFIX} {login})"') - push("origin", self.branch) + add_files(f"{submission.homework}.py", "flake.log") + commit(f'"Add sources and flake log ({submission.branch} {submission.login})"') + push("origin", submission.branch) post_mr( - source_branch=self.branch, + source_branch=submission.branch, target_branch="master", - title=f"[{HOMEWORK}{SUFFIX}] {login}", + title=f"[{submission.branch}] {submission.login}", description=f"```\n{self.get_mail()}\n```", - labels=HOMEWORK, + labels=submission.homework, remove_source_branch="true", assignee_ids=["1772"], ) diff --git a/commands/test.py b/commands/test.py index d661291..f0f3b4f 100644 --- a/commands/test.py +++ b/commands/test.py @@ -2,8 +2,9 @@ from commands.base import BaseCommand +from submission import print_submissions, Submission class Test(BaseCommand): - def exec(self, login: str, submission: str): - print(f"{login} - {submission}") \ No newline at end of file + def exec(self, submission: Submission) -> None: + print(f"{submission.login}, {submission.submitted_at}, {submission.points}") diff --git a/commands/update_assignees.py b/commands/update_assignees.py index be51c61..05a320f 100644 --- a/commands/update_assignees.py +++ b/commands/update_assignees.py @@ -3,11 +3,12 @@ from commands.base import BaseCommand from gitlab import get_mrs_for_branch, set_assignees +from submission import Submission class UpdateAssignees(BaseCommand): - def exec(self, login: str, submission: str) -> None: - iid = get_mrs_for_branch(self.branch)[0]["iid"] + def exec(self, submission: Submission) -> None: + 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]) diff --git a/submission.py b/submission.py new file mode 100644 index 0000000..38eda85 --- /dev/null +++ b/submission.py @@ -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)