From 37b4d04d11d82fc6d21ec3250b032ea6ac2a761d Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 8 Jun 2020 17:39:36 +0200 Subject: [PATCH] Globbing --- commands/base.py | 5 ++-- commands/comments.py | 12 ++++++---- commands/merge_requests.py | 47 ++++++++++++++++++++++++-------------- git.py | 3 ++- gitlab_provider.py | 10 +++----- kontr_emails.py | 14 ++++++++---- pushee.py | 14 +++++++----- submission.py | 7 ++---- utils.py | 4 ++-- 9 files changed, 67 insertions(+), 49 deletions(-) diff --git a/commands/base.py b/commands/base.py index d1e20d0..c86dd3a 100644 --- a/commands/base.py +++ b/commands/base.py @@ -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 diff --git a/commands/comments.py b/commands/comments.py index 816a3a2..67c1aa3 100644 --- a/commands/comments.py +++ b/commands/comments.py @@ -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 diff --git a/commands/merge_requests.py b/commands/merge_requests.py index a251e55..fd7b3be 100644 --- a/commands/merge_requests.py +++ b/commands/merge_requests.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 +import glob +import os import re @@ -12,14 +14,22 @@ 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: - exit(1) + for file in files: + if ( + run_cmd( + "rsync", + "-avzP", + f"aisa:{submission.path}/{self.hw['prefix']}/{file}", + "./", + )[0] + != 0 + ): + exit(1) @staticmethod def call_flake(submission: Submission) -> None: @@ -31,18 +41,21 @@ 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"
\nMail\n\n
{submission.get_mail()}
" - self.gitlab.post_mr( - source_branch=submission.branch, - target_branch="master", - title=f"[{submission.homework}{'-opravne' if submission.correction else ''}] {submission.login}", - description=mail, - labels=submission.homework, - remove_source_branch="true", - assignee_ids=["1772"], - ) + try: + self.gitlab.post_mr( + source_branch=submission.branch, + target_branch="master", + title=f"[{submission.homework}{'-opravne' if submission.correction else ''}] {submission.login}", + description=mail, + labels=submission.homework, + remove_source_branch="true", + assignee_ids=["1772"], + ) + except: + pass diff --git a/git.py b/git.py index ec0db9a..e3a87a0 100644 --- a/git.py +++ b/git.py @@ -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: diff --git a/gitlab_provider.py b/gitlab_provider.py index 786c9f3..76eee4d 100644 --- a/gitlab_provider.py +++ b/gitlab_provider.py @@ -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): diff --git a/kontr_emails.py b/kontr_emails.py index fa98b51..e01a8ed 100644 --- a/kontr_emails.py +++ b/kontr_emails.py @@ -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 - diff --git a/pushee.py b/pushee.py index 45689f2..7254ab0 100755 --- a/pushee.py +++ b/pushee.py @@ -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__": diff --git a/submission.py b/submission.py index e38599d..3b03fd2 100644 --- a/submission.py +++ b/submission.py @@ -7,7 +7,7 @@ from typing import Dict, List class Submission: - MAIL_CONTENT= re.compile(r"
((.*\s+)+)<\/pre>")
+    MAIL_CONTENT = re.compile(r"
((.*\s+)+)<\/pre>")
 
     def __init__(
         self,
@@ -48,10 +48,7 @@ class Submission:
     def get_mail(self) -> str:
         left = self.mail.find("
")
         right = self.mail.rfind("
") - 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 + return self.mail[left + 5 : right] def print_submissions(all_submissions: Dict[str, List[Submission]]) -> None: diff --git a/utils.py b/utils.py index 1f48bc7..c29004b 100644 --- a/utils.py +++ b/utils.py @@ -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)