-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit.py
77 lines (60 loc) · 2.35 KB
/
submit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ts=4 sw=4 expandtab:
"""
完成作业以后,运行本文件,它会将你的代码、实验报告一起打包为 submit.zip。
"""
from __future__ import print_function # Requires Python 2.7+
import locale
import os
import re
import sys
import zipfile
# Python3's input == Python2's raw_input
try:
input_compat = raw_input
except NameError:
input_compat = input
def S(s):
# F*** systems that still refuse to use UTF-8 by default in the 21st century
if 'decode' in dir(s):
return s.decode('utf-8').encode(locale.getpreferredencoding()) # Python 2 is naughty
else:
return s # Python 3 is good
# FIXME: Can anybody please tell me whether there is a simpler way to make Chinese
# characters display correctly in both Python 2 and 3, and in all three major OSes
# (Win/Lin/Mac)?
def main():
# Preparations
locale.setlocale(locale.LC_ALL, '')
# Check whether decaf.jar exists
decaf_jar = os.path.join('result', 'decaf.jar')
if not os.path.exists(decaf_jar):
print(S('未找到 decaf.jar 文件。请重新编译。'), file=sys.stderr)
return 1
print(S('已找到 {}'.format(decaf_jar)))
# Check whether report exists
for report_file in ['report.txt', 'report.doc', 'report.docx', 'report.pdf', 'report.odf']:
if os.path.exists(report_file):
break
else:
print(S('未找到实验报告。请确认它的文件名正确 (report.txt/doc/docx/pdf/odf)。'), file=sys.stderr)
return 1
print(S('已找到实验报告 {}'.format(report_file)))
# Ask for E-mail
email = input_compat(S('请输入您的 Email: '))
while not re.match(r'[^@]+@\w+\.[\w.]+', email):
email = input_compat(S('Email 格式不正确,请重新输入: '))
# Creating submit.zip
with zipfile.ZipFile('submit.zip', 'w') as submit_zip:
submit_zip.write(decaf_jar, 'decaf.jar', zipfile.ZIP_STORED)
submit_zip.write(report_file, report_file, zipfile.ZIP_DEFLATED)
submit_zip.writestr('email.txt', email.encode('utf-8'), zipfile.ZIP_STORED)
# Finished
print(S('完成。请将 submit.zip 文件上传到网络学堂。'))
return 0
if __name__ == '__main__':
retcode = main()
if os.name == 'nt':
input_compat('Press Enter to continue...')
sys.exit(retcode)