-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateDataFile.py
More file actions
132 lines (122 loc) · 4.03 KB
/
Copy pathGenerateDataFile.py
File metadata and controls
132 lines (122 loc) · 4.03 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import zipfile
import subprocess
import configparser
from tkinter import messagebox
cf = configparser.ConfigParser()
#改配置文件config.ini,不要在这里改
#生成数据的程序名
generatename = "create.exe"
#标程程序名
standardname = "standard.exe"
#生成的输入数据文件名
inputname = "sample"
#生成的输入数据文件后缀
inputsuffix = "in"
#生成的输出数据文件名
outputname = "sample"
#生成的输出数据文件后缀
outputsuffix = "out"
#生成的压缩包名
archivename = "data"
#数据文件行尾符号
endofline = "lf"
def findfile(filename):
list = os.listdir("./")
for i in range(0, len(list)):
path = os.path.join("./", list[i])
if os.path.isfile(path):
if (list[i] == filename):
return True
return False
def readconfig():
if(not findfile("config.ini")):
messagebox.showinfo("提示", "找不到配置文件config.ini,请新建配置文件config.ini,参考网址https://github.com/Wisdommmmmmmm/ACMGenerateDataHelper")
global cf
cf.read("./config.ini")
global generatename
generatename = cf.get("File", "generatename")
global standardname
standardname = cf.get("File", "standardname")
global inputname
inputname = cf.get("File", "inputname")
global inputsuffix
inputsuffix = cf.get("File", "inputsuffix")
global outputname
outputname = cf.get("File", "outputname")
global outputsuffix
outputsuffix = cf.get("File", "outputsuffix")
global archivename
archivename = cf.get("File", "archivename")
global endofline
endofline = cf.get("File", "endofline")
if(outputname+outputsuffix == inputname+inputsuffix):
messagebox.showerror("错误", "输入和输出文件名称加后缀不能一样")
os.exit(0)
def changeendofline(filename):
global endofline
bytes = b''
with open(filename, "rb") as f:
bytes = f.read()
origin = b''
if bytes.find(b'\x0D\x0A') != -1:
origin = b'\x0D\x0A'
elif bytes.find(b'\x0A') != -1:
origin = b'\x0A'
else:
origin = b'\x0D'
target = b''
if endofline == "crlf":
target = b'\x0D\x0A'
elif endofline == "lf":
target = b'\x0A'
else:
target = b'\x0D'
if origin != target:
bytes = bytes.replace(origin, target)
with open(filename, "wb") as f:
f.write(bytes)
def create(l, r):
global inputname
global inputsuffix
global outputname
global outputsuffix
global generatename
global standardname
for num in range(l, r):
inputfilename = inputname+str(num)+"."+inputsuffix
outputfilename = outputname+str(num)+"."+outputsuffix
subprocess.getoutput(generatename+" 1>"+inputfilename)
subprocess.getoutput(standardname+" 0<"+inputfilename+" 1>"+outputfilename)
changeendofline(inputfilename)
changeendofline(outputfilename)
def compress(l, r):
global archivename
zip_name = archivename+".zip"
zp = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
for num in range(l, r):
inputfilename = inputname + str(num) +"."+ inputsuffix
outputfilename = outputname + str(num) +"."+ outputsuffix
zp.write(os.path.join("./", inputfilename))
os.remove(os.path.join("./", inputfilename))
zp.write(os.path.join("./", outputfilename))
os.remove(os.path.join("./", outputfilename))
zp.close
def check():
global generatename
global standardname
if(not findfile(generatename)):
messagebox.showerror("错误", "找不到生成数据的程序"+generatename)
os._exit(0)
if (not findfile(standardname)):
messagebox.showerror("错误", "找不到标程"+standardname)
os._exit(0)
if __name__ == '__main__':
readconfig()
check()
l = int(input("请输入数据文件编号起点:"))
r = int(input("请输入数据文件编号终点:"))+1
iscompress = int(input("请输入是否压缩(否:0,是:1):"))
create(l, r)
if iscompress == 1:
compress(l, r)