Skip to content

Commit d6bc113

Browse files
committed
上传题目的功能
1 parent 53a0454 commit d6bc113

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
TODO
2+
*.swp
3+
log
4+
header
5+
node_modules

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"commander": "^6.1.0",
4+
"js-yaml": "^3.14.0",
5+
"progress": "^2.0.3"
6+
}
7+
}

upload.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/env node
2+
3+
const root_name = "root"
4+
const root_password = "password"
5+
const roj_url = 'http://localhost:3000/'
6+
7+
const ProgressBar = require('progress');
8+
const { program } = require('commander');
9+
const fs = require("fs")
10+
const {execSync} = require("child_process")
11+
const yaml = require("js-yaml")
12+
13+
14+
if(!fs.existsSync('header')){
15+
console.log("先在网页上登录admin,然后获取 header存入header文件")
16+
process.exit(0)
17+
}
18+
19+
20+
21+
program.option("-f --force","强制上传")
22+
.arguments('<start> [end]')
23+
.action(function(start,end){
24+
Start = start
25+
End = end
26+
})
27+
program.parse(process.argv)
28+
29+
function upload({file,content,pid,title,time=1000,memory=128,stack=128,spj='default',level=1}){
30+
if( memory > 4096) memory = Math.ceil(memory/1024/1024)
31+
let args = [
32+
["file",`@${file}`],
33+
["content",`${content}`],
34+
["pid",pid],
35+
["title",title],
36+
["time",time],
37+
["memory",memory],
38+
["stack",stack],
39+
["spj",spj],
40+
["level",level]
41+
]
42+
let args_1 = (args.map( d => `-F "${d[0]}=${d[1]}"`)).join(" ")
43+
if(program.force)
44+
args_1 += " -F upload_force=1"
45+
return execSync(`curl -X POST ${args_1} -H @header ${roj_url}admin/problem/create`,{encoding:'utf8'})
46+
//console.log(`curl -X POST ${args_1} -H @header ${roj_url}admin/problem/create`)
47+
}
48+
49+
async function main(){
50+
if(!End ) End = Start
51+
var bar = new ProgressBar(':bar :current/:total\n :m1', { total: End-Start+1 });
52+
console.log(`你要上传的题目范围是:${Start} --> ${End}`)
53+
for( let i = Start ;i <= End ;i++){
54+
if( fs.existsSync(`./problems/${i}`) ){
55+
try {
56+
let config = yaml.safeLoad(fs.readFileSync(`./problems/${i}/reference.yml`, 'utf8'));
57+
//let content = fs.readFileSync(`./problems/${i}/content.md`, 'utf8')
58+
if(!fs.existsSync(`./problems/${i}/content.md`)){
59+
throw(`${i} content.md 不存在`)
60+
}
61+
execSync(`zip -j data.zip -r ./problems/${i}/data`)
62+
let ret = upload({file:'data.zip',content:`<problems/${i}/content.md`,pid:i,...config})
63+
if( JSON.parse(ret).status !== 0){
64+
throw(ret)
65+
}
66+
fs.unlinkSync(`data.zip`)
67+
bar.tick({
68+
"m1":`成功 ${i}`
69+
})
70+
}
71+
catch(e){
72+
bar.tick({
73+
"m1":`失败 ${i}`
74+
})
75+
fs.writeFileSync("log",`fail at ${i}\n${e}\n\n\n`,{flag:"w+"})
76+
}
77+
}
78+
else {
79+
80+
bar.tick({
81+
"m1":`失败 ${i} 不存在`
82+
})
83+
fs.writeFileSync("log",`fail at ${i} 不存在\n\n\n`,{flag:"w+"})
84+
}
85+
86+
if(fs.existsSync('data.zip')) fs.unlinkSync(`data.zip`)
87+
}
88+
}
89+
main()

yarn.lock

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
argparse@^1.0.7:
6+
version "1.0.10"
7+
resolved "https://registry.npm.taobao.org/argparse/download/argparse-1.0.10.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fargparse%2Fdownload%2Fargparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
8+
integrity sha1-vNZ5HqWuCXJeF+WtmIE0zUCz2RE=
9+
dependencies:
10+
sprintf-js "~1.0.2"
11+
12+
commander@^6.1.0:
13+
version "6.1.0"
14+
resolved "https://registry.npm.taobao.org/commander/download/commander-6.1.0.tgz?cache=0&sync_timestamp=1598576059374&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcommander%2Fdownload%2Fcommander-6.1.0.tgz#f8d722b78103141006b66f4c7ba1e97315ba75bc"
15+
integrity sha1-+Ncit4EDFBAGtm9Me6HpcxW6dbw=
16+
17+
esprima@^4.0.0:
18+
version "4.0.1"
19+
resolved "https://registry.npm.taobao.org/esprima/download/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
20+
integrity sha1-E7BM2z5sXRnfkatph6hpVhmwqnE=
21+
22+
js-yaml@^3.14.0:
23+
version "3.14.0"
24+
resolved "https://registry.npm.taobao.org/js-yaml/download/js-yaml-3.14.0.tgz#a7a34170f26a21bb162424d8adacb4113a69e482"
25+
integrity sha1-p6NBcPJqIbsWJCTYray0ETpp5II=
26+
dependencies:
27+
argparse "^1.0.7"
28+
esprima "^4.0.0"
29+
30+
progress@^2.0.3:
31+
version "2.0.3"
32+
resolved "https://registry.npm.taobao.org/progress/download/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
33+
integrity sha1-foz42PW48jnBvGi+tOt4Vn1XLvg=
34+
35+
sprintf-js@~1.0.2:
36+
version "1.0.3"
37+
resolved "https://registry.npm.taobao.org/sprintf-js/download/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
38+
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=

0 commit comments

Comments
 (0)