-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.pl
254 lines (192 loc) · 6.6 KB
/
test.pl
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/perl
use strict;
use warnings;
use configuration;
use messages;
use db_interface;
use db_structure;
configuration::option_add("duplicate", "no");
configuration::option_add("skip-checks", "no");
configuration::key_add("--duplicate", "duplicate");
configuration::key_add("-d", "duplicate");
configuration::key_add("--skip-checks", "skip-checks");
configuration::key_add("-s", "skip-checks");
@ARGV = configuration::keys_to_options(@ARGV);
configuration::config_read();
db_interface::db_connect();
my $cmd = shift @ARGV;
unless ( defined $cmd) {
die "Command expected. Try '$0 help' for more info.";
}
if ($cmd eq "add") {
while (my $path = shift @ARGV) {
test_add($path);
}
} elsif ($cmd eq "classify") {
while (my $path = shift @ARGV) {
test_classify($path);
}
} elsif ($cmd eq "remove") {
while (my $path = shift @ARGV) {
test_remove($path);
}
} elsif ($cmd eq "remove_id") {
while (my $test_id = shift @ARGV) {
test_remove_id($test_id);
}
} elsif ($cmd eq "help") {
usage();
} else {
die "Unknown command: '$cmd'. Try '$0 help' for more info";
}
db_interface::db_disconnect();
sub test_add {
my ($path) = @_;
my $msg_prefix = "'$path':";
unless ($configuration::options{"skip-checks"} eq "yes") {
check_file($path);
}
# checking if there is already such test
my $sql_query;
unless ($configuration::options{"duplicate"} eq "yes") {
messages::debug("$msg_prefix Checking if the test is already in DB");
my $test_id = db_structure::test_select_id_by_path($path);
if (defined $test_id) {
die "$msg_prefix Test is already in DB, test_id: $test_id";
}
}
# adding test to the Tests table
messages::debug("$msg_prefix Adding test to the DB");
my $test_id = db_structure::test_insert($path);
messages::debug("$msg_prefix test_id = $test_id");
my $classification = get_classification_line($path);
test_classify_id($test_id, $classification);
messages::verbose("A #$test_id\t$path\t$classification");
}
sub test_classify {
my ($path) = @_;
my $msg_prefix = "'$path':";
messages::debug("$msg_prefix Getting the test_id");
my $test_id = db_structure::test_select_id_by_path($path);
if ( ! defined $test_id ) {
die "$msg_prefix There is no such test."
}
messages::debug("$msg_prefix test_id = $test_id");
my $classification = get_classification_line($path);
test_classify_id($test_id, $classification);
}
sub test_classify_id {
my ($test_id, $classification) = @_;
my $msg_prefix = "#$test_id:";
messages::debug("$msg_prefix Classifying test. Checking if the test_id is not corect");
my $test = db_structure::test_select_by_id($test_id);
if ( ! defined $test ) {
die "$msg_prefix There is no such test";
}
messages::debug("$msg_prefix Deleting old classification if any");
db_structure::classification_delete_by_test_id($test_id);
# parsing classification line according to the Levels table
messages::debug("$msg_prefix Parsing the classification line");
my %values;
my @values_list = split /\//, $classification;
my $table_levels = db_structure::levels_select();
while (my $level = $table_levels->fetchrow_hashref()) {
if ($#values_list < 0) {
die "$msg_prefix Incorrect classification: '$classification'";
}
$values{$level->{level_id}} = shift(@values_list);
}
# writing the classification
messages::debug("$msg_prefix Writing the classification");
$table_levels = db_structure::levels_select();
while (my $level = $table_levels->fetchrow_hashref()) {
my $level_id = $level->{level_id};
my $value_id = db_structure::value_insert_or_select_id($level_id,
$values{$level_id});
# adding line to the Classification table
db_structure::classification_insert($test_id,
$level_id, $value_id);
}
messages::debug("$msg_prefix Updating the 'Tests.positive' field");
db_structure::test_update_positive_by_id($test_id, $values{"0"});
}
sub test_remove_id {
my ($test_id) = @_;
my $msg_prefix = "#$test_id:";
messages::debug("$msg_prefix Checking if there is no such test");
my $test = db_structure::test_select_by_id($test_id);
if ( ! defined $test ) {
die "$msg_prefix There is no such test: $test_id\n";
}
messages::debug("$msg_prefix Deleting the classification");
db_structure::classification_delete_by_test_id($test_id);
messages::debug("$msg_prefix Deleting the testing info");
db_structure::testing_delete_by_test_id($test_id);
messages::debug("$msg_prefix Deleting the basic test links");
db_structure::basictest_delete_by_test_id($test_id);
messages::debug("$msg_prefix Deleting the test");
db_structure::test_delete_by_id($test_id);
}
sub test_remove {
my ($path) = @_;
my $msg_prefix = "'$path':";
messages::debug("$msg_prefix Getting the test_id");
my $test_id = db_structure::test_select_id_by_path($path);
if ( ! defined $test_id ) {
die "$msg_prefix There is no such test: '$path'";
}
messages::debug("$msg_prefix test_id = $test_id");
test_remove_id($test_id);
}
sub get_classification_line {
my ($path) = @_;
my $msg_prefix = "'$path':";
messages::debug("$msg_prefix Getting the classification line");
my $fd;
open $fd, $path or die "$msg_prefix Can not open file '$path': $!";
my $first_line = <$fd>;
close $fd;
$first_line =~ s/[\r\n]//g;
$first_line =~ s/^\/\/\s*//;
$first_line =~ s/\s*$//;
my $classification = $first_line;
return $classification;
}
sub check_file {
my ($path) = @_;
my $msg_prefix = "'$path':";
# check if file exists, is readable and is a regular text file
messages::debug("$msg_prefix Checking file");
if ( ! -e $path ) {
die "$msg_prefix File does not exist";
}
if ( ! -r $path ) {
die "$msg_prefix File is not readable";
}
if ( ! -T $path ) {
die "$msg_prefix File is not a text file";
}
}
sub usage {
print "Usage:\n";
print " $0 [KEYS] add PATH1 [ PATH2 [ PATH3 ... ] ]\n";
print " $0 [KEYS] classify PATH1 [ PATH2 [ PATH3 ... ] ]\n";
print " $0 [KEYS] remove PATH1 [ PATH2 [ PATH3 ... ] ]\n";
print " $0 [KEYS] remove_id TEST_ID1 [ TEST_ID2 [ TEST_ID3 ... ] ]\n";
print " $0 help\n";
print "\n";
print "Commands:\n";
print " add add tests to the database\n";
print " classify (re-)classify the test\n";
print " remove remove tests from the database\n"
. " (doesn't actually remove the file)\n";
print " removeid remove tests from the database by their IDs\n";
print " help print this message and exit\n";
print "\n";
print "Keys:\n";
print " -v, --verbose verbose mode\n";
print " --debug additional debug information\n";
print " --debug-lib additional debug information from libraries\n";
print " -d, --duplicate allow to add one test multiple times\n";
print " -s, --skip-checks don't check if file is a readable text file\n";
}