This commit is contained in:
Matej Focko 2020-06-08 17:39:36 +02:00
parent 1e74e27b6a
commit 37b4d04d11
9 changed files with 67 additions and 49 deletions

View file

@ -11,8 +11,9 @@ from submission import Submission
class BaseCommand:
def __init__(self, submissions: List[Submission], gitlab: Gitlab, details:
Dict) -> None:
def __init__(
self, submissions: List[Submission], gitlab: Gitlab, details: Dict
) -> None:
self.submissions = submissions
self.gitlab = gitlab
self.hw = details

View file

@ -21,7 +21,9 @@ class Comments(BaseCommand):
if author not in result:
result[author] = dict()
file_path = comment["position"]["new_path"] if "position" in comment else None
file_path = (
comment["position"]["new_path"] if "position" in comment else None
)
if file_path not in result[author]:
result[author][file_path] = list()
@ -30,9 +32,11 @@ class Comments(BaseCommand):
# sort by lines
for author in result:
for file_path in result[author]:
result[author][file_path].sort(key=lambda comment:
comment["position"]["new_line"] if "position" in comment
else math.inf)
result[author][file_path].sort(
key=lambda comment: comment["position"]["new_line"]
if "position" in comment
else math.inf
)
return result

View file

@ -1,6 +1,8 @@
#!/usr/bin/env python3
import glob
import os
import re
@ -12,13 +14,21 @@ from utils import run_cmd
class MergeRequests(BaseGitCommand):
def get_files(self, submission: Submission) -> None:
files = f"{self.hw['prefix']}/"
if len(self.hw['files']) > 1:
files += f"{{{','.join(self.hw['files'])}}}"
else:
files += f"{self.hw['files'][0]}"
files = []
for file in self.hw["files"]:
files.extend(glob.glob(f'{submission.path}/{self.hw["prefix"]}/{file}'))
files = map(lambda f: f.split("/")[-1], files)
if run_cmd("rsync", "-avzP", f"aisa:{submission.path}/{files}", "./")[0] != 0:
for file in files:
if (
run_cmd(
"rsync",
"-avzP",
f"aisa:{submission.path}/{self.hw['prefix']}/{file}",
"./",
)[0]
!= 0
):
exit(1)
@staticmethod
@ -31,12 +41,13 @@ class MergeRequests(BaseGitCommand):
self.get_files(submission)
# self.call_flake(submission)
add_files(*self.hw['files'])
add_files(*self.hw["files"])
commit(f'"Add sources ({submission.branch} {submission.login})"')
push("origin", submission.branch)
mail = f"<details>\n<summary>Mail</summary>\n\n<pre>{submission.get_mail()}</pre></details>"
try:
self.gitlab.post_mr(
source_branch=submission.branch,
target_branch="master",
@ -46,3 +57,5 @@ class MergeRequests(BaseGitCommand):
remove_source_branch="true",
assignee_ids=["1772"],
)
except:
pass

3
git.py
View file

@ -11,7 +11,8 @@ def checkout_branch(branch: str) -> None:
def add_files(*files: str) -> None:
run_cmd("git", "add", *files)
for f in files:
run_cmd("git", "add", f)
def commit(msg: str) -> None:

View file

@ -7,13 +7,9 @@ from ogr.services.gitlab import GitlabService
class Gitlab:
def __init__(self, namespace, repo, token):
self.ogr_service = GitlabService(
token=token,
instance_url="https://gitlab.fi.muni.cz"
)
self.ogr_project = self.ogr_service.get_project(
repo=repo,
namespace=namespace
token=token, instance_url="https://gitlab.fi.muni.cz"
)
self.ogr_project = self.ogr_service.get_project(repo=repo, namespace=namespace)
def post_mr(
self,
@ -29,7 +25,7 @@ class Gitlab:
title=title,
body=description,
target_branch=target_branch,
source_branch=source_branch
source_branch=source_branch,
)
def get_mrs_for_branch(self, branch):

View file

@ -25,7 +25,7 @@ class Parser:
def get_mail(mail: mboxMessage) -> str:
body = None
for payload in mail.get_payload():
if payload['Content-Type'].startswith('text/html'):
if payload["Content-Type"].startswith("text/html"):
body = payload.get_payload()
return body
@ -63,7 +63,9 @@ class Parser:
self.box = mbox(details["mbox_path"])
self.reviewer = reviewer
self.deadline = datetime.datetime.strptime(details["deadline"], Parser.DATE_FORMAT)
self.deadline = datetime.datetime.strptime(
details["deadline"], Parser.DATE_FORMAT
)
self.correction = details["correction"]
if self.correction:
# in case of correction pass the date of
@ -76,11 +78,14 @@ class Parser:
if self.from_ids:
print(self.submissions)
for submission_id in self.submissions:
with open(f'/home/kontr/kontr/_tmp_/pb071/{self.homework}/{submission_id}/teacher_email', 'r') as f:
with open(
f"/home/kontr/kontr/_tmp_/pb071/{self.homework}/{submission_id}/teacher_email",
"r",
) as f:
mails.append(f.read())
else:
for mail in self.box.values():
if self.reviewer not in mail['subject']:
if self.reviewer not in mail["subject"]:
continue
mails.append(Parser.get_mail(mail))
@ -167,4 +172,3 @@ class Parser:
for something in submissions.values():
functools.reduce(__reducer, something, result)
return result

View file

@ -54,13 +54,15 @@ def main():
details = config["homeworks"][config["homework"]]
submissions = Parser(
details, config["reviewer"], config["homework"]
).parse(config["homework"])
submissions = Parser(details, config["reviewer"], config["homework"]).parse(
config["homework"]
)
COMMANDS[sys.argv[1]](submissions, Gitlab(config["project_namespace"],
config["project_name"],
config["token"]), details)()
COMMANDS[sys.argv[1]](
submissions,
Gitlab(config["project_namespace"], config["project_name"], config["token"]),
details,
)()
if __name__ == "__main__":

View file

@ -49,9 +49,6 @@ class Submission:
left = self.mail.find("<pre>")
right = self.mail.rfind("</pre>")
return self.mail[left + 5 : right]
# print(self.mail)
# match = Submission.MAIL_CONTENT.search(self.mail)
# return match.group(1) if match else self.mail
def print_submissions(all_submissions: Dict[str, List[Submission]]) -> None:

View file

@ -17,11 +17,11 @@ def handle_error(process: CompletedProcess) -> int:
return process.returncode
def run_cmd(*args: str) -> Tuple[int, CompletedProcess]:
def run_cmd(*args: str, shell: bool = False) -> Tuple[int, CompletedProcess]:
if DRY_RUN:
print(" ".join(args))
return (0, None)
process = run(args, capture_output=True)
process = run(args, capture_output=True, shell=shell)
return (handle_error(process), process)