forked from cafe-grader-team/cafe-grader-judge-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_problem
executable file
·100 lines (80 loc) · 2.27 KB
/
rename_problem
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
#!/usr/bin/env ruby
ENVIRONMENT_DIRS = ['ev', 'ev-exam']
def config
Grader::Configuration.get_instance
end
def rename_problem(old_problem_name, new_problem_name)
if valid_problem_name(new_problem_name)
puts "Bad new problem name: #{new_problem_name}"
return
end
problem = Problem.find_by_name(old_problem_name)
if problem==nil
puts "Problem #{old_problem_name} does not exist."
return
end
puts "Problem: #{old_problem_name} -> #{new_problem_name}"
ENVIRONMENT_DIRS.each do |dir|
problem_dir = File.join(GRADER_ROOT,'..',dir,old_problem_name)
new_problem_dir = File.join(GRADER_ROOT,'..',dir,new_problem_name)
if FileTest.exists? problem_dir
puts "Moving #{problem_dir} to #{new_problem_dir}."
File.rename(problem_dir, new_problem_dir)
tr_problem_dir = File.join(GRADER_ROOT,'..',dir,
'test_request',old_problem_name)
new_tr_problem_dir = File.join(GRADER_ROOT,'..',dir,
'test_request',new_problem_name)
File.rename(tr_problem_dir, new_tr_problem_dir)
end
end
problem.name = new_problem_name
problem.save
end
def usage
puts <<USAGE
Usage:
rename_problem [old_name] [new_name]
or
rename_problem -f [filename]
When using with -f, that file should contain, for each line, the old
problem name and its new name.
This script should be called at the judge root dir where dirs 'ev' and
'ev-exam' are.
USAGE
end
def valid_problem_name(name)
if name.length==0:
return false
else
return !(/^[a-zA-Z0-9_\-]+$/ === name)
end
end
if (ARGV.length!=2)
usage
exit(0)
end
if ARGV[0]=='-f' and !FileTest.exists?(ARGV[1])
puts "File #{ARGV[1]} does not exist."
usage
exit(0)
end
# load grader environment
GRADER_ENV = 'grading'
require File.join(File.dirname(__FILE__),'config/environment')
# boot rails, to be able to rename the problem
RAILS_ENV = config.rails_env
require RAILS_ROOT + '/config/environment'
if ARGV[0]!='-f'
old_problem_name = ARGV[0]
new_problem_name = ARGV[1]
rename_problem(old_problem_name, new_problem_name)
else
lines = IO.readlines(ARGV[1])
lines.each do |line|
items = line.split
if items.length==2
old_name, new_name = items
rename_problem(old_name, new_name)
end
end
end