From e222ea802339eb03edca72c625d8e5818572e16d Mon Sep 17 00:00:00 2001 From: Tomasz Dwojak Date: Tue, 12 Dec 2017 15:55:00 +0100 Subject: [PATCH] Homework part 1 --- homeworks/main.py | 124 +++++++++++++++++++++++++++++++++++++++++++ homeworks/results.md | 24 +++++++++ 2 files changed, 148 insertions(+) create mode 100755 homeworks/main.py create mode 100644 homeworks/results.md diff --git a/homeworks/main.py b/homeworks/main.py new file mode 100755 index 0000000..ad39069 --- /dev/null +++ b/homeworks/main.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import argparse +import logging +import git +import os +import importlib +import collections +import csv + +logger = logging.getLogger('Homeworks') +logger.setLevel(logging.DEBUG) +fm = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +ch = logging.StreamHandler() +ch.setFormatter(fm) +logger.addHandler(ch) + + +def arg_parse(): + parser = argparse.ArgumentParser(description='Framework for checking homeworks.') + parser.add_argument('-i', '--ids', type=str, + help='path to a file with student ids.') + return parser.parse_args() + +def get_ids(path): + ids = [] + with open(path) as _file: + for line in _file: + ids.append(line.strip().split(' ')[::2]) + return ids + + +def clone_repo(username, repo_name): + repo = "https://git.wmi.amu.edu.pl/{}/{}.git".format(username, repo_name) + dest = "./Python2017-{}".format(username) + if os.path.exists(dest): + logger.debug("{}: repo exests.".format(username)) + else: + try: + git.Repo.clone_from(repo, dest) + except git.GitCommandError: + logger.info("Repository '{}' does not exist".format(repo)) + return False + return True + +def check_tasks(username, repo_name, tasks): + logger.debug("Marking user: {}".format(username)) + scores = collections.defaultdict(dict) + for lab in tasks: + path = os.path.realpath("./Python2017-{}/{}".format(username, lab)) + for task, fun in tasks[lab]: + task_file = path + "/{}". format(task) + logger.debug(task_file) + package = 'Python2017-{}.{}.{}'.format(username, lab, task) + lib = importlib.import_module(package) + tested_fun = getattr(lib, fun) + tests = getattr(lib, 'tests') + try: + test_result = tests(tested_fun) + except Exception: + test_result = "FAILED" + + if test_result == 'TESTS PASSED': + scores[lab][task] = 1 + else: + scores[lab][task] = 0 + return scores + +def get_fieldnames(tasks): + fieldnames = ['ID'] + for lab in tasks: + for task, _ in tasks[lab]: + fieldnames.append('{}:{}'.format(lab, task)) + return fieldnames + +def parse_scores(fieldnames, uname, scores): + out = {} + for name in fieldnames: + if name == 'ID': + out[name] = uname + continue + lab, task = name.split(':') + out[name] = scores[lab][task] + return out + + +def write_scores(scores, filename, tasks): + fieldnames = get_fieldnames(tasks) + + with open(filename, 'w') as results: + writer = csv.DictWriter(results, fieldnames, restval=0) + writer.writeheader() + for student in scores: + writer.writerow(parse_scores(fieldnames, student, scores[student])) + + +def main(): + """ main """ + options = arg_parse() + ids = get_ids(options.ids) + logger.info("Liczba studentów: {}".format(len(ids))) + scores = {} + + tasks = { + 'labs02' : [ + ('task07', 'char_sum'), + ('task08', 'sum_div35'), + ('task09', 'leet_speak'), + ('task10', 'pokemon_speak'), + ('task11', 'common_chars') + ] + } + + for repo_id in ids: + st_uname = repo_id[0] + repo_name = repo_id[-1] + if clone_repo(st_uname, repo_name): + scores[st_uname] = check_tasks(st_uname, repo_name, tasks) + write_scores(scores, 'results.csv', tasks) + + +if __name__ == "__main__": + main() diff --git a/homeworks/results.md b/homeworks/results.md new file mode 100644 index 0000000..5ba6b49 --- /dev/null +++ b/homeworks/results.md @@ -0,0 +1,24 @@ +ID | labs02:task07 | labs02:task08 | labs02:task09 | labs02:task10 | labs02:task11 +---------|-----------------|-----------------|-----------------|-----------------|--------------- +s45168 | 0 | 0 | 0 | 0 | 0 +s45162 | 1 | 1 | 1 | 1 | 1 +s45158 | 1 | 1 | 0 | 0 | 1 +szwedek | 1 | 1 | 1 | 1 | 1 +s45155 | 1 | 1 | 1 | 1 | 1 +s45152 | 1 | 1 | 1 | 0 | 1 +s45148 | 1 | 1 | 1 | 1 | 1 +s45166 | 1 | 1 | 1 | 1 | 1 +s45151 | 1 | 1 | 1 | 1 | 1 +s45146 | 1 | 1 | 1 | 1 | 1 +s45150 | 1 | 1 | 1 | 1 | 1 +s45452 | 1 | 1 | 1 | 1 | 1 +s45165 | 1 | 1 | 1 | 1 | 1 +s45160 | 1 | 1 | 1 | 1 | 1 +s45153 | 0 | 0 | 0 | 0 | 0 +s45156 | 1 | 1 | 1 | 1 | 1 +s45157 | 0 | 0 | 1 | 1 | 1 +s45167 | 1 | 1 | 1 | 1 | 1 +s45147 | 1 | 0 | 0 | 1 | 0 +s45159 | 0 | 0 | 0 | 0 | 0 +s45161 | 1 | 1 | 1 | 1 | 1 +s45164 | 1 | 1 | 0 | 1 | 0