-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.pl
executable file
·369 lines (314 loc) · 9.68 KB
/
parser.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/perl
# Log Parser
# Created by Donald Jones
# 13th Oct 2006
use strict;
use warnings;
#use diagnostics;
use lib ('parsers/pkgs');
use Data::Dumper;
use File::Copy;
use File::Basename;
use Getopt::Std;
use parser;
use msc_parser;
use vars qw( $opt_c $opt_d $opt_g $opt_h $opt_l $opt_p $opt_x);
my $syntax = qq{
Usage: $0 -p <parser> [-l <logfile> | -x <xmlfile>] [-g logfile] [-d] [-h]
where:
\t-p <parser> parser to be use
\t-g <logfile> is the GPS logfile to parse
\t-l <logfile> is the logfile to parse
\t-x <xmlfile> is the xmlfile to display (the parser is not re-run)
\t-d Debug mode
\t-h This help screen
};
my $csvfile;
my $gpslogfile;
my $logfile;
my $xmlfile;
my $logfiledir;
my $debug = 0;
$PARSER="";
sub parse_params
{
# Are any command line options specified?
getopts('c:dhg:l:p:x:');
# debug mode
if( defined($opt_d) )
{
$debug = 1;
$parser::debug=$debug;
$opt_d = 1;
print "Debug mode\n" if $debug;
}
# Help
if( defined($opt_h) )
{
$opt_h = 1;
print $syntax;
my @parsers = sort(parser_get_parsers());
my $pars = join( "\n\t", @parsers );
print "The parsers available are:\n\n\t$pars\n";
die "\n";
}
# Parser
if( defined($opt_p) )
{
parser_set_parser($opt_p);
}
else
{
print $syntax;
die "Parser required\n";
}
# GPS Logfile
if( defined($opt_g) )
{
$gpslogfile = $opt_g;
print "Opening GPS Logfile: $gpslogfile\n" if $debug;
# check GPS logfile exists
die "Can't find $gpslogfile because $!" unless -f $gpslogfile;
}
# Combined CSV Logfile
if( defined($opt_c) )
{
$csvfile = $opt_c;
print "...Opening combined CSV file: $csvfile\n" if $debug;
die "Can't find combined CSV file $csvfile because $!" unless -f $csvfile;
}
# Need to ensure that either an XML or Log file is provided.
if( !defined $opt_l and !(defined $opt_x or defined $opt_c) )
{
print $syntax;
die "Please specify an input file\n";
}
# Check for mutual exclusive on logfile and xml file
if( defined $opt_l and defined $opt_x )
{
print $syntax;
die "Can't specify both a Log file AND an XML file together\n";
}
# Logfile
if( defined($opt_l) )
{
$logfile = $opt_l;
print "Opening Logfile: $logfile\n" if $debug;
# check logfile exists
die "Can't find $logfile because $!" unless -f $logfile;
}
# XMLfile
if( defined($opt_x) )
{
$xmlfile = $opt_x;
print "Opening XMLfile: $xmlfile\n" if $debug;
# check logfile exists
die "Can't find $xmlfile because $!" unless -f $xmlfile;
}
}
sub record_user_stats()
{
use LWP::Simple;
my $who = getlogin() || (getpwuid($<))[0] || "Kilroy";
print "User: $who $PARSER\n" if $debug;
my $output;
my $url = "http://10.128.36.3/cgi-bin/parser/upload.pl?user=$who&tool=parser&tool2=$PARSER";
print "URL: $url\n" if $debug;
unless( defined( $output = get($url) ) )
{
print "Unable to get $url\n";
}
print $output if $debug;
}
sub parser_create_output(@) {
my @outputs = @_;
my $filestub;
$filestub = parser_rm_file_ext($PARSER_CONFIG{'ext'},$logfile) if defined $logfile;
$filestub = parser_rm_file_ext($PARSER_CONFIG{'ext'},$xmlfile) if defined $xmlfile;
my $num_splits = parser_get_num_splits($xmlfile);
my %copied_files;
foreach my $output (@outputs)
{
my $ext = $output->{'ext'};
my $index = 0;
my $num_events;
# Handle Generation of output by exe file
if( exists $output->{'exe'} )
{
my $exe = $output->{'exe'};
print "\nExe: $exe\n";
print "XML: $xmlfile\n";
if( exists $output->{'split'} )
{
while( $index < $num_splits )
{
my $cmd = "\"$exe\" -s $index \"$xmlfile\"";
qx($cmd);
$index++;
}
}
else
{
my $cmd = "\"$exe\" \"$xmlfile\"";
print "CMD: $cmd\n";
qx($cmd);
}
}
# Handle Generation of output by XSL file
#print Dumper $output;
my $params = "";
if( exists $output->{'xsl'} )
{
my $xsl = $output->{'xsl'};
if( exists $output->{'split'} )
{
$index = 0;
while( $index < $num_splits )
{
$params = "index=$index";
parser_conv_xml_via_xsl($xmlfile, $xsl, $ext, $index++, $params);
}
}
else
{
parser_conv_xml_via_xsl($xmlfile, $xsl, $ext, undef, undef);
}
}
# Overcome issue with msxsl.exe which results in rogue xmlns attributes for nodes
if( exists $output->{'strip_xmlns'} )
{
my $tmpfile = $filestub.$ext;
if( exists $output->{'split'} )
{
$index = 0;
while( $index < $num_splits )
{
$tmpfile = $filestub.$index."_$ext";
strip_xmlns($tmpfile);
$index++;
}
}
else
{
strip_xmlns($tmpfile);
}
}
foreach my $file (keys %{$output->{'file'}})
{
# Only copy files the first time that they occur.
if( !exists($copied_files{$file}) )
{
print "...Copying $file to $logfiledir\n";
copy($file, $logfiledir) or print "Copy of $file failed: $!";
$copied_files{$file} = ""; # Record the fact that we have copied the file
#my $cmd = "CACLS \"$logfiledir/$file\" /E /P Everyone:F";
my $cmd = "ATTRIB -R \"$logfiledir\\$file\"";
#print "Cmd: $cmd\n";
print qx($cmd);
}
}
}
}
sub strip_xmlns($)
{
my $tmpfile = shift;
print "...Strip xmlns=\"\" from $tmpfile\n";
open INFILE,"$tmpfile" or die "Could not open $tmpfile for reading: @!";
my @lines = <INFILE>;
close( INFILE );
my @newsvg;
foreach my $line (@lines)
{
#print "Line: $line\n";
#chomp;
$line =~ s/xmlns=\"\"//;
#print "$newline\n";
push( @newsvg, $line );
};
open( TMPOUTFILE, ">$tmpfile" ) or die "Could not open $tmpfile for writing: @!";
print TMPOUTFILE @newsvg;
close( TMPOUTFILE );
}
# main program
parse_params();
my $dbfile = 'database.xml';
record_user_stats();
# Only call the parser if the user provided a logfile
if( !defined $xmlfile )
{
my $logdir = dirname($logfile);
my $logbase = basename($logfile);
my $logbasenoext = $logbase;
$logbasenoext = parser_rm_file_ext(undef,basename($logfile)) if $logbasenoext =~ /\./;
$logbasenoext =~ s/\.$//;
$logbasenoext =~ s/\.\\//;
my $newlogdir = "$logdir\\$logbasenoext"."_$PARSER";
# Create new directory
if( ! -d $newlogdir )
{
my $err = mkdir( $newlogdir) unless -f $newlogdir;
if( $err )
{
$err = $!;
if( ( $err )
&& !( ( $err =~ /File exists/i )
|| ( $err =~ /No such file or directory/i ) )
)
{
die "Unable to create directory \"$newlogdir\" : $err\n";
}
}
}
# Copy log file to new directory
copy($logfile, $newlogdir) or print "Copy of $logfile failed: $!";
$logfile = "$newlogdir\\$logbase";
if( defined($gpslogfile) )
{
# Copy gps log file to new directory
copy($gpslogfile, $newlogdir) or print "Copy of $gpslogfile failed: $!";
my $gpslogbase = basename($gpslogfile);
$gpslogfile = "$newlogdir\\$gpslogbase";
}
if( defined($csvfile) )
{
# Copy gps log file to new directory
copy($csvfile, $newlogdir) or print "Copy of $csvfile failed: $!";
my $csvbase = basename($csvfile);
$csvfile = "$newlogdir\\$csvbase";
}
my $parser_cmd = "parsers\\$PARSER";
print "$PARSER:\n $PARSER_CONFIG{'desc'}\n";
$parser_cmd .= "\\".$PARSER_CONFIG{'pl'};
$parser_cmd .= " -c \"$csvfile\"" if( defined($csvfile) );
$parser_cmd .= " -g \"$gpslogfile\"" if( defined($gpslogfile) );
$parser_cmd .= " -l \"$logfile\"" if( defined($logfile) );
$parser_cmd .= " -d " if $debug;
print "cmd: $parser_cmd";
system($parser_cmd);
}
print Dumper $PARSER_CONFIG{'output'} if $debug;
print "...Generating tagged version of log file\n";
parser_gen_tagged_log($gpslogfile) if( defined($gpslogfile) );
parser_gen_tagged_log($logfile) if( defined($logfile) ); # Do not create this if we were are processing XML files
$xmlfile = parser_rm_file_ext($PARSER_CONFIG{'ext'},$logfile)."xml" unless (defined($xmlfile) or defined($csvfile) );
# Populate the logfiledir;
$logfile =~ /([\S\s]+)\\/ if( defined($logfile) );
$xmlfile =~ /([\S\s]+)\\/ if( defined($xmlfile) );
$logfiledir = $1;
print "Logfile: $logfile\n" if $debug;
print "XMLfile: $xmlfile\n" if $debug;
# Process any output types
if (exists $PARSER_CONFIG{'output_type'})
{
my $output_type = $PARSER_CONFIG{'output_type'};
if( exists $PARSER_XML_CONFIG->{'output_type'}->{$output_type} )
{
parser_create_output(@{$PARSER_XML_CONFIG->{'output_type'}->{$output_type}->{'output'}});
}
else
{
print "Could not find output_type: $output_type config\n";
}
}
#process any specific output formats
parser_create_output(@{$PARSER_CONFIG{'output'}}) if exists $PARSER_CONFIG{'output'};