Example #1
0
def run_custom(cmds):
    for c in cmds:
        msg.info(f'Running {msg.underline(c)}')
        while sp.call(c, shell=True) != 0:
            print()
            msg.fail(f'Failed to run {msg.underline(c)}')
            if not msg.ask_retry():
                print()
                break
Example #2
0
def java(file, arg='', arg2=''):
    """ Run a compiled Java program. """
    # TODO: Refactor

    msg.info(f'Running {file}...')
    cmd = f'java -cp ".:*" {arg} {file.replace(".java", "")} {arg2}'
    if os.system(cmd) != 0:
        msg.fail(f'Failed to run by {msg.underline(" ".join(cmd.split()))}')
        if msg.ask_retry():
            java(file)
Example #3
0
def precheck():
    """ Pre-check the assignment structure before starting grading. """

    if not msg.ask_yn(f'Continue with {msg.underline(asmt_disp_name)}?'):
        print('Bye:)')
        exit(0)

    link = util.get_link(f'{asmt_name}{asmt_num}')
    if msg.ask_yn('Open link in browser?'):
        msg.info(f'Opening {msg.underline(link)}...')
        webbrowser.open_new_tab(link)

    if not os.path.exists('submission'):
        if len(glob.glob('CSC 116*.zip')) == 0:
            return 1
        msg.info('Extracting...')
        g = glob.glob('CSC 116*.zip')
        if len(g) == 1:
            unzip(g[0])

    return 0
Example #4
0
def javac(file, lib='.:../../../../../lib/*'):
    """ Compile a Java file to /bin. """

    msg.info(f'Compiling {msg.underline(file)}...', '')
    if not os.path.exists(file):
        print()
        msg.fail(f'{msg.underline(file)} does not exist', '')
        input()
        return -1

    # src
    cmd = f'javac {file}'
    # test
    if file.startswith('TS_') or file.endswith('Test.java'):
        if args.junit or args.tstest:
            cmd = f'javac -cp {lib} {file}'

    proc = sp.Popen(cmd, shell=True,
                    stdout=sp.PIPE, stderr=sp.PIPE)
    out, err = proc.communicate()
    rc = proc.wait()
    out = out.decode(encoding='utf-8')
    err = err.decode(encoding='utf-8')
    if not args.nostacktrace:
        if len(out) > 0:
            print()
            msg.info(f'Output:\n{out}')
        if len(err) > 0:
            print()
            msg.fail(f'Error:\n{err}')

    if rc != 0:
        print()
        msg.fail(f'Failed to compile by {msg.underline(cmd)}')
        sp.Popen([default_open, file])
        if msg.ask_retry():
            javac(file)
    else:
        print('done')
    return rc
Example #5
0
def rename():
    """ Rename all directories to [firstname lastname]. """

    msg.info('Renaming...')

    moodle_sub_specifier = '_assignsubmission_file_'
    g = glob.glob(f'submission/*{moodle_sub_specifier}')

    while len(g) == 0:
        if msg.ask_yn('Directory names do not match, remove /submission and retry?',
                      msgtype='warn'):
            shutil.rmtree('submission')
            msg.info('Extracting...')
            z = glob.glob('CSC 116*.zip')
            if len(z) == 1:
                unzip(z[0])
            g = glob.glob(f'submission/*{moodle_sub_specifier}')

    for entry in g:
        print(msg.align_left(msg.name(entry.split('/')[1]), 80))
        entry_new = entry.split('__')[0]
        shutil.move(entry, entry_new)
        print(f'\t-> {msg.name(entry_new.split("/")[1])}')

    msg.info(f'Renamed to [lastname firstname]')
    msg.press_continue()
Example #6
0
def hw():
    msg.info('Checking homework...')
    folders = glob.glob('* *')
    for f in folders:
        print(msg.name(f))
        os.chdir(f)

        files = glob.glob('*.*')
        num = len(files)
        if num == 0:
            msg.fail('No submission')
        elif num == 1:
            pdf = files[0]
            msg.info(f'Opening {msg.underline(pdf)}...')
            sp.Popen(f'{default_open} \"{pdf}\"', shell=True)
        else:
            msg.warn(f'Multiple files found (total {num}):')
            msg.index_list(files)
            print('Select a file to open: ', end='')
            i = msg.ask_index(0, num)
            if 0 < i < num:
                pdf = files[i]
                msg.info(f'Opening {msg.underline(pdf)}...')
                sp.Popen(f'{default_open} \"{pdf}\"', shell=True)

        input()
        os.chdir('..')
Example #7
0
def javac_all():
    """ Compile all Java files in /src and /test, then copy to /bin. """

    msg.info('Compiling...')

    for student in glob.glob('* *'):
        if not student.is_dir():
            continue
        os.chdir(student)

        print(msg.name(student.name))
        files = util.get_conf_asmt('files')
        if files:
            for file in files:
                javac(file)
        else:
            for file in glob.glob('*.java', recursive=True):
                javac(file)

        os.chdir('..')

    msg.press_continue()
Example #8
0
def init(force):
    import shutil

    msg.info('Initializing...')
    if force:
        option = msg.ask_yn(
            'Force initialize will possibly remove all files, '
            'continue with <force> flag?',
            msgtype='warn')
        if not option:
            force = False

    dirs = [
        'content', 'content/exercises', 'content/projects', 'content/homework'
    ]
    for d in dirs:
        if os.path.exists(d) and force:
            shutil.rmtree(d, ignore_errors=True)
        os.mkdir(d)
    # Day 1 to 27
    for i in range(1, 26):
        d = f'content/exercise/day{str(i).zfill(2)}'
        if os.path.exists(d) and force:
            shutil.rmtree(d, ignore_errors=True)
        os.mkdir(d)
    # Day 1 to 6
    for i in range(1, 7):
        d = f'content/project/p{str(i)}'
        if os.path.exists(d) and force:
            shutil.rmtree(d, ignore_errors=True)
        os.mkdir(d)
    # Day 1 to 4
    for i in range(1, 5):
        d = f'content/homework/hw{str(i)}'
        if os.path.exists(d) and force:
            shutil.rmtree(d, ignore_errors=True)
        os.mkdir(d)

    msg.info('Done')
Example #9
0
def ts_bbt():
    pdf = glob.glob('*.pdf')
    if len(pdf) != 1:
        msg.fail('BBTP pdf file not found')
        input()
        return

    msg.info(f'Opening {pdf[0]}...')
    sp.Popen([util.get_conf_glob('open'), pdf[0]])

    cmd = None
    while msg.ask_yn('Continue running?'):
        msg.info(f'Current command: {msg.underline(cmd)}')
        if cmd is None:
            msg.info('Enter a command to execute: ')
            cmd = input()
        else:
            if msg.ask_yn('Change the current command?'):
                cmd = input()
            else:
                msg.info('No change')
        msg.info(f'Running {cmd}...')
        os.system(cmd)
Example #10
0
def java_all():
    """ Run all Java classes. """

    msg.info('Running...')
    for student in sorted(os.scandir('.'), key=lambda s: s.name):
        if not student.is_dir():
            continue

        print(msg.name(student.name))
        os.chdir(student.name)

        cmds = util.get_conf_asmt('custom run')
        files = util.get_conf_asmt('files')

        if cmds:
            run_custom(cmds)
        elif files:
            for f in files:
                java(f)
        else:
            for f in glob.glob('*.java'):
                java(f)

        os.chdir('..')
Example #11
0
def ts_wce():
    msg.info('Compiling files...')
    msg.info(f'Current grading: {msg.underline("src")}')
    arg2 = args.argument if args.argument else ''
    for f in util.get_conf_asmt('src'):
        javac(f, '.:*')
        java(f, arg2=arg2)
    msg.press_continue()
    msg.info(f'Current grading: {msg.underline("test")}')
    for f in util.get_conf_asmt('test'):
        javac(f, '.:*')
        java(f, arg='org.junit.runner.JUnitCore')
    msg.press_continue()
Example #12
0
def ts_tswbt():
    msg.info('Compiling TS_WBT...')
    ts = glob.glob('TS_*_WB_Runner.java')
    javac(ts[0], '.:*')
    java(ts[0])
    msg.press_continue()
Example #13
0
def ts_tsbbt():
    msg.info('Compiling TS_BBT...')
    for f in sorted(glob.glob('TS_*_BB_Test.java')):
        javac(f, '.:*')
        java(f, arg='org.junit.runner.JUnitCore')
    msg.press_continue()
Example #14
0
def ts_test():
    """ Run all teaching staff tests. """

    msg.info('Running teaching staff tests...')
    with zipfile.ZipFile(args.tstest) as zf:
        msg.info(f'Extracting {args.tstest}...')
        zf.extractall('ts')

    ts_path = os.path.abspath('ts')
    os.chdir('submission')

    for student in sorted(glob.glob('* *')):
        os.chdir(student)
        print(msg.name(student))

        msg.info('Copying TS files...')
        distutils.dir_util.copy_tree(ts_path, '.')

        src = util.get_conf_asmt('src')
        for f in src:
            msg.info(f'Opening {msg.underline(f)}...')
            sp.Popen([default_open, f])

        test = util.get_conf_asmt('test')

        for item in util.get_conf_asmt('order'):
            # Custom run is a dict: {'custom run' : [..., ...]}
            if type(item) is dict:
                _item = next(iter(item))
                msg.info(f'Current grading: {msg.underline(_item)}')
                cmds = item.get(_item)
                run_custom(cmds)
                continue

            _item = item.lower()
            msg.info(f'Current grading: {msg.underline(item)}')
            if _item == 'wce':
                ts_wce()
            elif _item == 'tsbbt':
                ts_tsbbt()
            elif _item == 'tswbt':
                ts_tswbt()
            elif _item == 'bbt':
                ts_bbt()
            elif _item == 'wbt':
                ts_wbt(test)
            elif _item == 'style' or _item == 'checkstyle':
                cs = checkstyle(src, test)
                if cs == 0:
                    msg.info('No checkstyle error found')
                else:
                    msg.textbar('Total')
                    print(msg.align_right(cs, 3))

        msg.info('Testing done')
        msg.press_continue()
        os.chdir('..')
Example #15
0
                        help='do not compile',
                        action='store_true')
    parser.add_argument('-nr', '--norun',
                        help='do not run',
                        action='store_true')
    parser.add_argument('-ns', '--nostacktrace',
                        help='do not print stacktrace',
                        action='store_true')

    args = parser.parse_args()

    if args.version:
        print(open('./VERSION').read())
        exit(0)

    msg.info('Starting service...')
    if args.init:
        force = True if args.init == 'f' else False
        util.init(force)
        exit()

    asmt_name, asmt_cat, asmt_num = None, None, None
    if args.exercise:
        asmt_cat = 'exercise'
        asmt_name = 'day'
        asmt_num = args.exercise.zfill(2)
    elif args.project:
        asmt_cat = 'project'
        asmt_name = 'p'
        asmt_num = args.project
    elif args.homework: