Example #1
0
    def do_trish(self, args):
        """Usage: trish [LOGIN LOGIN...]

Runs the Trish program and prints a ranking of the worst cheaters."""
        if len(args) != 0 and len(args) < 2:
            raise _BadUsageException()
        logins = set(args) if len(args) != 0 else None
        scores = []
        for i in range(len(self.__sessions.list())):
            sessionA = self.__sessions.list()[i]
            loginA = sessionA.submission().login()
            if logins is not None and loginA not in logins:
                continue
            for j in range(i + 1, len(self.__sessions.list())):
                sessionB = self.__sessions.list()[j]
                loginB = sessionB.submission().login()
                if logins is not None and loginB not in logins:
                    continue
                try:
                    score = CorrectingSession.run_trish(sessionA, sessionB)
                    scores.append((score, sessionA, sessionB))
                except Exception as e:
                    loginA = sessionA.submission().login()
                    loginB = sessionB.submission().login()
                    print_error(
                        f'Trish failed to compare {loginA} and {loginB}: {e}')
        scores.sort(key=lambda x: x[0])
        for score, sessionA, sessionB in scores:
            loginA = sessionA.submission().login()
            loginB = sessionB.submission().login()
            print_info(f'{score}: {loginA} // {loginB}')
        return False
Example #2
0
 def decorated_f(self, args):
     try:
         return f(self, shlex.split(args))
     except _BadUsageException:
         print_error('Bad usage of command.')
         print(decorated_f.__doc__)
     except Exception:
         print_current_exception()
         return False
Example #3
0
 def run(self, login, login_path, project, project_path):
     if exec_in_folder(
             login_path, run_shell_command,
             "find . -type f -iname '*README*' | egrep '.*'") is not 0:
         print_error('README not found')
     else:
         exec_in_folder(
             login_path, run_shell_command,
             "find . -type f -iname '*README*' -exec less {} \\;")
Example #4
0
    def run(self, login, login_path, project, project_path):
        print_info("Building project " + project)
        res = exec_in_folder(
            project_path, run_command,
            "msbuild " + os.path.join(os.path.basename(project) + ".csproj"))

        if res.returncode is not 0:
            print_error("Build failed:\n" + res.stdout + res.stderr)
        else:
            print_success("Build successful")
Example #5
0
def cmd_edit(tp_slug, login):
    """
    Open a shell into the student repo
    :param tp_slug: TP slug
    :param login: Student login
    """
    repo = Submission(tp_slug, login)
    if repo.exists_locally():
        open_subshell(repo.local_dir())
        return EXIT_SUCCESS
    else:
        print_error('Repository not found locally.')
        return EXIT_FAILURE
Example #6
0
    def run(self, login, login_path, project, project_path):
        print_info("Copying files of " + login + " (" + project + ")")
        student_project_folder = os.path.join(login_path, project)
        files = folder_find(student_project_folder,
                            includes=[".*\\.cs", ".*\\.csproj"],
                            excludes=["AssemblyInfo.cs"])

        if len(files) == 0:
            print_error("No files found")
            return

        for file in files:
            src = file
            dest = os.path.join(project_path,
                                file[len(student_project_folder) + 1:])
            folder_create(os.path.dirname(dest))
            file_copy(src, dest)
            if file.endswith(".cs"):
                file_insert_text_every_n_lines(dest, "// " + login, 20)
Example #7
0
def cmd_archive(tp_slug, logins, output_file, verbose):
    """
    Create an archive with all the students files (without trash files)
    :param tp_slug: TP slug
    :param logins: Students login
    :param output_file: Output file path
    :param verbose: Display more info
    """
    tp = Tp(tp_slug)

    if output_file:
        output_file = os.path.expanduser(output_file)
    else:
        today = datetime.datetime.today()
        archives_folder = os.path.join(ACDC_LOCAL_FOLDER, "archives")
        folder_create_if_not_exists(archives_folder)
        output_file = os.path.join(archives_folder, f"{tp_slug}_{today:%d-%m-%Y_%Hh%M}.zip")

    zip_file = zipfile.ZipFile(output_file, "w")

    if not tp.has_local_submissions():
        return EXIT_FAILURE

    if len(logins) == 0:
        submissions = tp.get_local_submissions()
    else:
        submissions = []
        for login in logins:
                submission = Submission(tp, login)
                if submission.exists_locally():
                        submissions.append(submission)
                else:
                        print_error("Missing submission for {}.".format(login))

    archive_all(submissions, zip_file, verbose)

    print_success("Archive successfully created (" + output_file + ")")
    return EXIT_SUCCESS
Example #8
0
    def fn(self, arg):
        try:
            opt = docopt(fn.__doc__, shlex.split(arg))

        except DocoptExit as usage:
            print(fn.__doc__)
            self._last_exit_status = EXIT_UNEXPECTED
            return False

        except SystemExit as e:
            self._last_exit_status = e.code
            return True

        try:
            return func(self, opt)

        except Exception as e:
            if __debug__:
                print_error('An exception occured:')
                traceback.print_exc()
            else:
                print_error(e)
            self._last_exit_status = EXIT_UNEXPECTED
            return False
Example #9
0
def cmd_tag(tp_slug, tag_name, date, logins):
    """
    Push a tag to the last commit of the students before 23h42 at the given date
    :param tp_slug: Slug of the TP
    :param tag_name: Tag name
    :param date: Date in yyyy-mm-dd format
    :param logins: List of student logins
    """
    if tag_name is None:
        tag_name = SUBMISSION_TAG

    cmd_get(tp_slug, logins, False)

    for i, login in enumerate(logins):
        print_info(login + ":", percent_pos=i, percent_max=len(logins))
        folder = Submission(tp_slug, login).local_dir()
        success = True

        try:
            exec_in_folder(folder, git_checkout_date, date, "23:42")
            print_success("Checkout last commit before " + date + " 23:42", 1)
        except GitException as e:
            print_error("Checkout: " + str(e), 1)
            success = False
            continue

        try:
            exec_in_folder(folder, git_tag, tag_name)
            print_success("Tagging commit", 1)
        except GitException as e:
            print_error("Tagging: " + str(e), 1)
            success = False
            continue

        try:
            exec_in_folder(folder, git_push_tags)
            print_success("Tagging commit", 1)
        except GitException as e:
            print_error("Tagging: " + str(e), 1)
            success = False
            continue

        return EXIT_SUCCESS if success else EXIT_FAILURE
Example #10
0
def cmd_remove(tp_slug, logins, remove_all, remove_moulinette):
    """
    Remove the students repo corresponding to the given TP slug
    :param tp_slug: Slug of the TP to remove
    :param logins: List of student logins
    :param remove_all: Should all the students files be removed
    :param remove_moulinette: Should the moulinette be removed
    """
    success = True
    tp = Tp(tp_slug)
    if not tp.has_local_submissions():
        print_error("TP " + tp_slug + " not found")
    else:
        if remove_all or len(logins) is 0:
            tp.remove_locally()
            print_success("Successfully removed " + tp_slug)
        else:
            for i, login in enumerate(logins):
                repo = Submission(tp, login)
                print_info("{tp_slug} ({login}) ".format(
                    tp_slug=repo.tp().slug(), login=repo.login()),
                           percent_pos=i,
                           percent_max=len(logins),
                           end='')
                if repo.exists_locally():
                    try:
                        repo.remove_locally()
                        print_success('')
                    except IOError:
                        print_error('')
                        success = False
                        continue
                else:
                    print_error('')

    if remove_moulinette:
        tp.remove_moulinette_locally()

    return EXIT_SUCCESS if success else EXIT_FAILURE
Example #11
0
def cmd_get(tp_slug, logins, overwrite_policy):
    """
    Download the students repo corresponding to the given TP slug
    :param tp_slug: Slug of the TP to download
    :param logins: List of student logins
    """
    tp = Tp(tp_slug)
    success = True

    # For each student
    for i, login in enumerate(logins):
        repo = Submission(tp_slug, login)
        dl_path = repo.local_dir()
        overwriting = False

        print_info(login + ":", percent_pos=i, percent_max=len(logins))

        # If folder exists, delete it
        if repo.exists_locally():
            overwriting = overwrite_policy
            if overwriting is None:
                print_error("Student project already downloaded", 1)
                ask = print_ask("Do you want to overwrite it?",
                                ['y', 'n', 'ya', 'na'], 1)
                overwriting = ask in ['y', 'ya']
                if ask == 'ya':
                    overwrite_policy = True
                elif ask == 'na':
                    overwrite_policy = False

            if not overwriting:
                print_info("Skipping student project", 1)
                continue

            print_info("Overwriting student project", 1)
            dl_path = to_tmp_path(repo.local_dir())

        try:
            git_clone(repo.url(), dl_path)
            print_success("Download repository", 1)
        except GitException as e:
            if os.path.isdir(dl_path):
                shutil.rmtree(dl_path)
            print_error("Download: Repository not found", 1)
            success = False
            continue

        if overwriting:
            shutil.rmtree(repo.local_dir())
            os.rename(dl_path, repo.local_dir())

        try:
            # Checkout tag submission
            exec_in_folder(repo.local_dir(), git_checkout_tag, SUBMISSION_TAG)
            print_success("Checkout tag " + SUBMISSION_TAG, 1)
        except GitException as e:
            print_error("Checkout: Tag " + SUBMISSION_TAG + " not found", 1)
            success = False

        if len(folder_ls(repo.local_dir(), excludes=["\..*"])) == 0:
            print_warning("The repository is empty", 1)

    return EXIT_SUCCESS if success else EXIT_FAILURE
Example #12
0
 def default(self, cmd):
     if cmd == 'EOF':
         print('exit')
         return True
     print_error(str(cmd).split(' ')[0] + ': no such command')
     return False