Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle grouped test cases + auto-retry if out of tokens #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 43 additions & 36 deletions submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ def is_python2(files):
return False
if python2.search(line.split('#')[0]):
return True
except UnicodeDecodeError:
pass
except IOError:
return False
return False
Expand Down Expand Up @@ -198,6 +200,8 @@ def guess_mainfile(language, files):
return filename
if language == 'Pascal' and re.match(r'^\s*[Pp]rogram\b', conts):
return filename
except UnicodeDecodeError:
pass
except IOError:
pass
return files[0]
Expand Down Expand Up @@ -321,12 +325,12 @@ def get_submission_status(submission_url, cookies):

_RED_COLOR = 31
_GREEN_COLOR = 32
_YELLOW_COLOR = 33
def color(s, c):
return f'\x1b[{c}m{s}\x1b[0m'


def show_judgement(submission_url, cfg):
print()
login_reply = login_from_config(cfg)
while True:
status = get_submission_status(submission_url, login_reply.cookies)
Expand All @@ -336,7 +340,6 @@ def show_judgement(submission_url, cfg):

status_text = _STATUS_MAP.get(status_id, f'Unknown status {status_id}')


if status_id < _RUNNING_STATUS:
print(f'\r{status_text}...', end='')
else:
Expand All @@ -359,15 +362,14 @@ def show_judgement(submission_url, cfg):
if testcases_total == 0:
print('???', end='')
else:
s = '.' * (testcases_done - 1)
progress = ''
for i in re.findall(r'<i class="([\w\- ]*)" title', status['row_html']):
if 'is-empty' in i: break
if 'accepted' in i: progress += color('.', _GREEN_COLOR)
if 'rejected' in i: progress += color('x', _RED_COLOR)
if status_id == _RUNNING_STATUS:
s += '?'
elif status_id == _ACCEPTED_STATUS:
s += '.'
else:
s += 'x'

print(f'[{s: <{testcases_total}}] {testcases_done} / {testcases_total}', end='')
progress = progress[:10*(testcases_done - 1)] + color('?', _YELLOW_COLOR)
print(f'[{progress}{" " * (9*testcases_done + testcases_total - len(progress))}] {testcases_done} / {testcases_total}', end='')

sys.stdout.flush()

Expand All @@ -377,8 +379,12 @@ def show_judgement(submission_url, cfg):
success = status_id == _ACCEPTED_STATUS
try:
root = fragment_fromstring(status['row_html'], create_parent=True)
cpu_time = root.find('.//*[@data-type="cpu"]').text
status_text += " (" + cpu_time + ")"
cpu_time = root.xpath('.//*[@data-type="cpu"]')[0].text_content()
try:
score = re.findall('\(([\d\.]+)\)', root.xpath('.//*[@data-type="status"]')[0].text_content())[0]
except:
score = ''
status_text += " (" + cpu_time + ', ' + score + ")" if score else " (" + cpu_time + ")"
except:
pass
if status_id != _COMPILE_ERROR_STATUS:
Expand Down Expand Up @@ -470,31 +476,33 @@ def main():
if not args.force:
confirm_or_die(problem, language, files, mainclass, tag)

try:
result = submit(submit_url,
login_reply.cookies,
problem,
language,
files,
mainclass,
tag,
args.assignment,
args.contest)
except requests.exceptions.RequestException as err:
print('Submit connection failed:', err)
sys.exit(1)
while True:
try:
result = submit(submit_url,
login_reply.cookies,
problem,
language,
files,
mainclass,
tag)
except requests.exceptions.RequestException as err:
print('Submit connection failed:', err)
sys.exit(1)

if result.status_code != 200:
print('Submission failed.')
if result.status_code == 403:
print('Access denied (403)')
elif result.status_code == 404:
print('Incorrect submit URL (404)')
else:
print('Status code:', result.status_code)
sys.exit(1)
if result.status_code != 200:
print('Submission failed.')
if result.status_code == 403:
print('Access denied (403)')
elif result.status_code == 404:
print('Incorrect submit URL (404)')
else:
print('Status code:', result.status_code)
sys.exit(1)

plain_result = result.content.decode('utf-8').replace('<br />', '\n')
if plain_result.startswith('You are out of submission tokens.'): time.sleep(3); continue
break

plain_result = result.content.decode('utf-8').replace('<br />', '\n')
print(plain_result)

submission_url = None
Expand All @@ -504,7 +512,6 @@ def main():
pass

if submission_url:
print(submission_url)
if not show_judgement(submission_url, cfg):
sys.exit(1)

Expand Down