提交 c031678e 编辑于 作者: 刘 玉河's avatar 刘 玉河
浏览文件

Merge branch 'feat_submission_status' into 'main'

Feat submission status

See merge request !1
加载中
加载中
加载中
加载中
+15 −2
原始行号 差异行号 差异行
@@ -15,13 +15,26 @@
要从命令行使用该脚本,请切换到该脚本所在目录,用 Python 运行该脚本:

```bash
python submit.py [-h] [-s SERVER] [-c CONTEST] [-k TICKET] [result_path]
python submit.py [-h] [-s SERVER] [-c CONTEST] [-k TICKET] [-i SUBMISSION_ID] [result_path]
```

* `[result_path]`:提交的结果文件路径。如果未指定,默认使用当前目录下的 `result.jsonl`
* `-s, --server`:指定评测服务器的 URL。如果未提供,将使用脚本中定义的 `JUDGE_SERVER` 变量。
* `-c, --contest`:比赛标识。如果未提供,将使用脚本中定义的 `CONTEST` 变量。
* `-k, --ticket`:团队标识。如果未提供,将使用脚本中定义的 `TICKET` 变量。
* `-i, --submission_id` :提交ID,当指定这一参数时,该脚本将不会提交结果文件,而是会查询该提交ID的结果。

## 单次提交结果查询

未指定 `-i` 参数时,脚本将提交结果文件并返回提交 ID。

提交结果文件的一段时间后,您可以通过指定 `-i` 参数来查询单次提交的结果。

```bash
python submit.py -i <submission_id> -s <judge_server> -c <contest> -k <ticket>
```

由于评测服务器可能需要一段时间来处理提交,因此您可能需要等待一段时间才能获得结果。

## 编程方式提交

+63 −3
原始行号 差异行号 差异行
@@ -34,7 +34,46 @@ def submit(data, judge_server=None, contest=None, ticket=None):
    try:
        with request.urlopen(req) as response:
            response_body = response.read().decode('utf-8')
            return json.loads(response_body)['submission_id']
            submission_id = json.loads(response_body)['submission_id']
            remaining_attempts = json.loads(response_body).get('remaining_attempts', -1)
            return submission_id, remaining_attempts
    except error.HTTPError as e:
        msg = e.reason
        response_body = e.read().decode('utf-8')
        if response_body:
            try:
                msg = json.loads(response_body)['detail']
            except:
                pass
        print("[Error %s] %s" % (e.code, msg))

    except error.URLError as e:
        print(e.reason)
        return None
    
def check_status(submission_id, judge_server=None, contest=None, ticket=None):
    judge_server = judge_server or JUDGE_SERVER
    contest = contest or CONTEST
    ticket = ticket or TICKET
    
    if not judge_server or not contest or not ticket or not submission_id:
        missing = [
            "judge_server" if not judge_server else "",
            "contest" if not contest else "",
            "ticket" if not ticket else "",
            "submission_id" if not submission_id else "",
        ]
        missing = [m for m in missing if m]
        print("Required fields must be provided: %s" % ', '.join(missing))
        return None
    
    req = request.Request(judge_server + "/status/", headers={'ticket': ticket, 'contest': contest, 'submission_id': submission_id, 'Content-Type': 'application/json'})

    try:
        with request.urlopen(req) as response:
            response_body = response.read().decode('utf-8')
            status = json.loads(response_body)
            return status
    except error.HTTPError as e:
        msg = e.reason
        response_body = e.read().decode('utf-8')
@@ -55,9 +94,27 @@ if __name__ == "__main__":
    parser.add_argument('-s', '--server', help='Judge server URL, if not specified, the global JUDGE_SERVER variable will be used')
    parser.add_argument('-c', '--contest', help='Contest ID, if not specified, the global CONTEST variable will be used')
    parser.add_argument('-k', '--ticket', help='Submission ticket, if not specified, the global TICKET variable will be used')
    parser.add_argument('-i', '--submission_id', help='Submission ID, specified if you want to check the submission status', default=None)
    
    args = parser.parse_args()

    if args.submission_id:
        status = check_status(args.submission_id, judge_server=args.server, contest=args.contest, ticket=args.ticket)
        if status:
            submission_id = status.get('submission_id')
            score = status.get('score')
            create_time = status.get('create_time')
            judge_time = status.get('judge_time')

            if not judge_time: 
                print("Submission %s is still in queue." % submission_id)
            else:
                print("Submission %s score: %s" % (submission_id, score))
            exit(0)
        else:
            print("Failed to check submission status.")
            exit(1)

    try:
        with open(args.result_path, 'r') as file:
            data = [json.loads(line.strip()) for line in file if line.strip()]
@@ -65,9 +122,12 @@ if __name__ == "__main__":
        print(e)
        exit(1)

    submission_id = submit(data, judge_server=args.server, contest=args.contest, ticket=args.ticket)
    if submission_id:
    return_data = submit(data, judge_server=args.server, contest=args.contest, ticket=args.ticket)
    if return_data:
        submission_id, remaining_attempts = return_data
        print("Success! Your submission ID is %s." % submission_id)
        if remaining_attempts >= 0:
            print("You have %d remaining evaluation attempt(s)." % remaining_attempts)
        exit(0)
    else:
        exit(1)