Beispiel #1
0
def do_incat(self, cmd):
    """
    usage: incat [--base64] <task> <filepath> [savepath]

    Tries to retrieve the specified ASCII file using .incbin directive.
    The data will be base64-encoded if --base64 is specified.
    """
    brutejudge.cheats.cheating(self)
    data = shlex.split(cmd)
    filter = ''
    if data and data[0] == '--base64':
        filter = base64_filter
        del data[0]
    if len(data) not in (2, 3):
        return self.do_help('incat')
    data.append(None)
    task, filepath, savepath = data[:3]
    task_list = tasks(self.url, self.cookie)
    for i in task_list:
        if i[1] == task:
            task_id = i[0]
            break
    else:
        raise BruteError("No such task.")
    if savepath == None:
        f = sys.stdout
    else:
        f = open(savepath, "w")
    incat(self, task, task_id, filepath, f, filter=filter)
    if savepath != None: f.close()
Beispiel #2
0
def do_info(self, cmd):
    """
    usage: info [-t] [task]

    Show testing information and problem statement for a task, or for the whole contest if task is not specified.
    If -t is specified, attempt to decode embedded TeX expressions into human-readable form.
    """
    cmd = cmd.strip()
    tex = False
    if cmd[:2] == '-t' and (len(cmd) == 2 or cmd[2:3].isspace()):
        tex = True
        cmd = cmd[2:].strip()
    if cmd == '--help':
        return self.do_help('info')
    if cmd:
        try: task_id = next(i.id for i in tasks(self.url, self.cookie) if i.short_name == cmd)
        except StopIteration:
            raise BruteError("No such task.")
        a, b = problem_info(self.url, self.cookie, task_id)
    else:
        b, a, j = contest_info(self.url, self.cookie)
        print(j)
    for k, v in a.items(): print(k+': '+v)
    print()
    print(untex(b) if tex else b)
Beispiel #3
0
def do_getfile(self, cmd):
    """
    usage: getfile <prob_id> <file> <shell command>

    Retrieve a file attached to a task.
    """
    id, file, cmd = (cmd.strip()+'  ').split(' ', 2)
    if not id or not file:
        return self.do_help('getfile')
    try: task_id = next(i.id for i in tasks(self.url, self.cookie) if i.short_name == id)
    except StopIteration:
        raise BruteError("No such task.")
    data = download_file(self.url, self.cookie, task_id, file)
    p = subprocess.Popen('cat '+cmd, stdin=subprocess.PIPE, shell=True)
    p.stdin.write(data)
    p.stdin.close()
    p.wait()
Beispiel #4
0
def do_scoreboard(self, cmd):
    """
    usage: scoreboard

    Show current standings.
    """
    cmd = cmd.strip()
    if cmd:
        return self.do_help('scoreboard')
    ts = [i.short_name for i in tasks(self.url, self.cookie)]
    scb = scoreboard(self.url, self.cookie)
    table = [[''] + ts]
    for u, i in scb:
        table.append([u.get('name', '')] +
                     ['' if j == None else format_single(j) for j in i])
    if table:
        clens = [max(len(j[i]) for j in table) for i in range(len(ts) + 1)]
        fmt_s = ' '.join('%%%ds' % i for i in clens)
        for i in table:
            print(fmt_s % tuple(i))
Beispiel #5
0
def do_clars(self, cmd):
    """
    usage: clars <command> [args...]

    `command` can be one of:
        list
        List clars.

        send <task> <subject>
        Send clar from stdin.

        read <clar_id>
        Read clar.
    """
    cmd = shlex.split(cmd)
    if not cmd: cmd = ['list']
    if cmd[0] not in ('list', 'send', 'read'):
        return self.do_help('clars')
    elif cmd[0] == 'list':
        if len(cmd) != 1: return self.do_help('clars')
        print('Clar ID\tSubject')
        for k, v in clar_list(self.url, self.cookie):
            print('%d\t%s' % (k, v))
    elif cmd[0] == 'read':
        if len(cmd) != 2 or not cmd[1].strip().isnumeric():
            return self.do_help('clars')
        print(read_clar(self.url, self.cookie, int(cmd[1])))
    elif cmd[0] == 'send':
        if len(cmd) != 3: return self.do_help('clars')
        try:
            task = next(i.id for i in tasks(self.url, self.cookie)
                        if i.short_name == cmd[1])
        except StopIteration:
            raise BruteError("No such task")
        data = ''
        while not data.endswith('\n\n'):
            try:
                data += input() + '\n'
            except EOFError:
                break
        submit_clar(self.url, self.cookie, task, cmd[2], data.strip())
Beispiel #6
0
def do_asubmit(self, cmd, *, afmt=False):
    """
    usage: asubmit [-w] [-x <extension>] <task> <lang_id> <file>

    Submit a new solution, using style-fixed version.
    Uses a specific style fixer if -x is specified.
    Waits until testing ends if -w is specified.
    """
    modname = ''
    wait = False
    sp = shlex.split(cmd)
    if len(sp) not in (range(3, 7) if not afmt else (1, 3)):
        return self.do_help('aformat' if afmt else 'asubmit')
    if not afmt and sp[0] == '-w':
        wait = True
        del sp[0]
    if len(sp) not in ((3, 5) if not afmt else (1, 3)):
        return self.do_help('aformat' if afmt else 'asubmit')
    if sp[0] == '-x':
        modname = sp[1]
        del sp[:2]
        if modname[:1] == '.':
            modname = 'brutejudge.commands.asubmit.format_' + modname[1:]
    if len(sp) != (1 if afmt else 3):
        return self.do_help('aformat' if afmt else 'asubmit')
    try:
        name = sp[-1]
        module = None
        ext = os.path.splitext(name)[1][1:]
        modname, *modargs = modname.split(',')
        modargs = set(modargs)
        if not modname: modname = 'brutejudge.commands.asubmit.format_' + ext
        try:
            module = __import__(modname, fromlist=True)
        except ImportError:
            pass
        if hasattr(module, 'cheats'):
            import brutejudge.cheats
            brutejudge.cheats.cheating(self)
        if not getattr(module, 'check_exists', check_exists)(name, modargs):
            raise BruteError("File not found.")
        if hasattr(module, 'read_file'):
            data = module.read_file(name, modargs)
        else:
            with open(name, 'rb') as file:
                data = file.read()
        if hasattr(module, 'format'):
            data = module.format(data, modargs)
    except UnicodeDecodeError:
        raise BruteError("File is binary.")
    if afmt:
        if isinstance(data, str): data = data.encode('utf-8')
        sys.stdout.buffer.write(data)
        sys.stdout.buffer.flush()
        return
    with may_cache(self.url, self.cookie):
        try:
            task_id = next(i.id for i in tasks(self.url, self.cookie)
                           if i.short_name == sp[0])
        except StopIteration:
            raise BruteError("No such task.")
        if not sp[1].isnumeric():
            sp[1] = get_lang_id(self, sp[1], task_id)
        before = submissions(self.url, self.cookie)
        submit_solution(self.url, self.cookie, task_id, int(sp[1]), data)
    after = submissions(self.url, self.cookie)
    if before == after:
        raise BruteError("Error while sending.")
    else:
        print('Submission ID is', after[0].id)
        if wait: self.do_astatus(str(after[0].id))