This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pl
442 lines (351 loc) · 10.5 KB
/
main.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
use FileHandle;
use Text::ParseWords;
use File::Copy;
use File::Spec::Win32;
use English qw(-no_match_vars);
BEGIN {
use constant TRUE => 42;
use constant FALSE => 0;
our $VERSION = 1.08;
# No Buffering on perl I/O to STDOUT and STDERR:
FileHandle::autoflush STDERR 1;
FileHandle::autoflush STDOUT 1;
}
sub parseOptions {
our %Options;
Getopt::Long::GetOptions(
'h|help' => \$Options{'help'},
'm|man' => \$Options{'man'},
'f|file=s' => \$Options{'file'},
'PART=s' => \$Options{'part'},
'IDF=s' => \$Options{'idf'},
'IDT=s' => \$Options{'idt'},
'IDTU=s' => \$Options{'idtu'},
'FNAME=s' => \$Options{'fname'},
'FDATE=i' => \$Options{'fdate'},
'FDATE6:i' => \$Options{'fdate6'},
'FDATE36:s' => \$Options{'fdate36'},
'FTIME=i' => \$Options{'ftime'},
'FTIME6:i' => \$Options{'ftime6'},
'PARM=s' => \$Options{'parm'},
'PARM14:s' => \$Options{'parm14'},
'PARM16:s' => \$Options{'parm16'},
'PARM16F:s' => \$Options{'parm16f'},
'PARM17:s' => \$Options{'parm17'},
'PARM26:s' => \$Options{'parm26'},
'PARM32:s' => \$Options{'parm32'},
'PARM103:s' => \$Options{'parm103'},
'PARM124:s' => \$Options{'parm124'},
'SAPPL:s' => \$Options{'sappl'}
) or Pod::Usage::pod2usage( -exitval => 1, -verbose => 1 );
if ( defined $Options{'help'} ) {
Pod::Usage::pod2usage( -exitval => 0, -verbose => 1 );
}
if ( defined $Options{'man'} ) {
Pod::Usage::pod2usage( -exitval => 0, -verbose => 2 );
}
unless ( defined $Options{'part'}
&& defined $Options{'idf'}
&& defined $Options{'idt'}
&& defined $Options{'idtu'}
&& defined $Options{'fname'}
&& defined $Options{'fdate'}
&& defined $Options{'ftime'} )
{
print STDERR "[ERROR]["
. localtime()
. "] Some mandatory command line option(s) is/are missing (PART, IDF, IDT, IDTU, FNAME, FDATE, FTIME)\n";
Pod::Usage::pod2usage( -exitval => 1, -verbose => 1 );
}
unless ( defined $Options{'file'} && -f $Options{'file'} ) {
print STDERR "[ERROR]["
. localtime()
. "] Mandatory option -f / --file not provided or value provided is not a valid file.\n";
Pod::Usage::pod2usage( -exitval => 1, -verbose => 1 );
}
return (TRUE);
}
sub replaceVarsByOptionsValues {
my $string = shift;
our %Options;
print STDOUT "[DEBUG]["
. localtime()
. "] String replacement start on string : $string \n";
my @keys = keys(%Options);
foreach my $key (@keys) {
if ( exists $Options{$key} ) {
$string =~ s/&$key/$Options{$key}/gixms;
}
}
print STDOUT "[DEBUG]["
. localtime()
. "] String replacement done, final string is : $string \n";
return ($string);
}
sub readPostProcFile {
our %Options;
print STDOUT "[INFO]["
. localtime()
. "] Will attempt to open file : $Options{'file'} \n";
open( my $fh, '<:encoding(UTF-8)', $Options{'file'} )
or die "[ERROR]["
. localtime()
. "] Can't open file $Options{'file'} Reason : $OS_ERROR\n";
print STDOUT "[INFO][" . localtime() . "] File successfully opened.\n";
my $line_count = 0;
our %LinesToExecute;
while ( my $line = <$fh> ) {
chomp $line;
$line_count++;
my $firschar = substr( ($line), 0, 1 );
next if $firschar eq q{#};
my @CSVLine = Text::ParseWords::parse_line( q{;}, 1, $line );
if ( $CSVLine[0] && $CSVLine[0] =~ $Options{'idf'} ) {
$LinesToExecute{$line_count} = \@CSVLine;
print STDOUT "[INFO]["
. localtime()
. "] Line $line_count successfully parsed.\n";
}
}
print STDOUT "[INFO][" . localtime() . "] Close file $Options{'file'}\n";
close($fh) or die "[ERROR][" . localtime() . "] : $OS_ERROR";
print STDOUT "[INFO][" . localtime() . "] File successfully closed.\n";
return (TRUE);
}
sub execSystemCommand {
our @CurrentLine;
my $command = $CurrentLine[2];
$command = replaceVarsByOptionsValues($command);
print STDOUT "[INFO]["
. localtime()
. "] Will attempt to execute command : $command\n";
system($command);
if ( $CHILD_ERROR == -1 ) {
print STDERR "[ERROR]["
. localtime()
. "] Failed to execute: $OS_ERROR\n";
return (FALSE);
}
elsif ( $CHILD_ERROR & 127 ) {
printf STDERR "[ERROR]["
. localtime()
. "] Command died with signal %d, %s coredump\n",
( $CHILD_ERROR & 127 ), ( $CHILD_ERROR & 128 ) ? 'with' : 'without';
return (FALSE);
}
else {
printf STDOUT "[INFO]["
. localtime()
. "] Command exited with value %d\n", $CHILD_ERROR >> 8;
return (TRUE);
}
}
sub execPostProc {
our @CurrentLine;
unless ( $CurrentLine[4] && $CurrentLine[5] && $CurrentLine[6] ) {
print STDERR "[ERROR]["
. localtime()
. "] Insufficient DollarU arguments, you miss one of those : DollarU Session, Uproc or AG.\n";
return (FALSE);
}
print STDOUT "[INFO]["
. localtime()
. "] Will now attempt to execute uXorder to $CurrentLine[3], Session: $CurrentLine[4], Uproc: $CurrentLine[5], MU: $CurrentLine[6] \n";
my $tmp = $CurrentLine[2];
$CurrentLine[2] = "e:\\win32app\\dollarU\\BNPPAR\\exec\\uxordre.exe SOC=BNPPAR NODE=$CurrentLine[3] ESP=X SES=$CurrentLine[4] VSES=000 UPR=$CurrentLine[5] VUPR=000 MU=$CurrentLine[6] $CurrentLine[7])";
unless ( execSystemCommand() ) {
$CurrentLine[2] = $tmp;
print STDERR "[ERROR]["
. localtime()
. "] Exec for current line failed.\n";
return(FALSE);
}
$CurrentLine[2] = $tmp;
return (TRUE);
}
sub copyFile {
our @CurrentLine;
our %Options;
my $dest = $CurrentLine[1];
$dest = replaceVarsByOptionsValues($dest);
unless ( -f $Options{'fname'} ) {
print STDERR "[ERROR]["
. localtime()
. "] File $Options{'fname'} is not valid or does not exist.\n";
return (FALSE);
}
my ( $vol, $dir, $file ) = File::Spec::Win32->splitpath($dest);
my $directory = "$vol$dir";
unless ( -d $directory ) {
print STDERR "[ERROR]["
. localtime()
. "] Folder $directory is not valid or does not exist.\n";
return (FALSE);
}
print STDOUT "[INFO]["
. localtime()
. "] Will attempt to copy file $Options{'fname'} in $dest \n";
unless ( File::Copy::copy( $Options{'fname'}, $dest ) ) {
print STDERR "[ERROR]["
. localtime()
. "] Can't copy file : $OS_ERROR \n";
exit(1);
}
print STDOUT "[INFO][" . localtime() . "] File successfully copied.\n";
return (TRUE);
}
sub processLine {
our @CurrentLine;
if ( $CurrentLine[1] && $CurrentLine[1] =~ /NoCopy/gi ) {
print STDOUT "[INFO]["
. localtime()
. "] Found NoCopy for current line or line empty. Nothing to do.\n";
}
elsif ( $CurrentLine[1] ) {
print STDOUT "[INFO]["
. localtime()
. "] Will now try to copy file for current line.\n";
unless ( copyFile() ) {
print STDERR "[ERROR]["
. localtime()
. "] Copy for current line failed.\n";
return (FALSE);
}
print STDOUT "[INFO]["
. localtime()
. "] File copied successfully for current line.\n";
}
else {
print STDOUT "[WARN]["
. localtime()
. "] Line empty on copy for current line.\n";
}
if ( $CurrentLine[2] && $CurrentLine[2] =~ /NoSystem/gi ) {
print STDOUT "[INFO]["
. localtime()
. "] Found NoSystem for current line or line empty. Nothing to do.\n";
}
elsif ( $CurrentLine[2] ) {
print STDOUT "[INFO]["
. localtime()
. "] Will now try to exec system command for current line.\n";
unless ( execSystemCommand() ) {
print STDERR "[ERROR]["
. localtime()
. "] Exec for current line failed.\n";
}
print STDOUT "[INFO]["
. localtime()
. "] System command executed for current line.\n";
}
else {
print STDOUT "[WARN]["
. localtime()
. "] Line empty on system exec for current line.\n";
}
if ( $CurrentLine[3] && $CurrentLine[3] =~ /NoProc/gi ) {
print STDOUT "[INFO]["
. localtime()
. "] Found NoProc for current line or line empty. Nothing to do.\n";
}
elsif ( $CurrentLine[3] ) {
print STDOUT "[INFO]["
. localtime()
. "] Will now try to exec PostProc for current line.\n";
unless ( execPostProc() ) {
print STDERR "[ERROR]["
. localtime()
. "] PostProc for current line failed.\n";
}
print STDOUT "[INFO]["
. localtime()
. "] PostProc executed successfully for current line.\n";
}
else {
print STDOUT "[WARN]["
. localtime()
. "] Line empty on PostProc for current line.\n";
}
return (TRUE);
}
sub Main {
our %Options;
our %LinesToExecute;
parseOptions();
readPostProcFile();
my @keys = keys(%LinesToExecute);
my $size = @keys;
if ( $size > 0 ) {
print STDOUT "[INFO]["
. localtime()
. "] Found $size line(s) in file $Options{'file'} with IDF=$Options{'idf'} \n";
foreach my $key (@keys) {
our @CurrentLine = @{ $LinesToExecute{$key} };
print STDOUT "[DEBUG]["
. localtime()
. "] Ligne ."
. Data::Dumper::Dumper(@CurrentLine) . "\n";
processLine();
}
}
else {
print STDOUT "[WARN]["
. localtime()
. "] No line found in file $Options{'file'} with IDF=$Options{'idf'} \n";
}
exit(0);
}
Main();
__END__
=head1 NAME
PostExec - CFT post execution action as described in fichier_final.csv
=head1 VERSION
This documentation refers to yourprog version 1.08.
=head1 USAGE
PostExec -f fichier_final.csv --PART=BOUCLE --IDF=TESTGREG --IDT=B1619553 --IDTU=A00067JV --FNAME="H:\recept\rcv\test\TESTGREG\cra.txt --FDATE=20170216 --FTIME=19553100 --PARM="cra.txt"
=head1 REQUIRED ARGUMENTS
=head1 OPTIONS
=over 4
=item B<-help>
Print a brief help message and exits.
=item B<-man>
Prints the manual page and exits
=item B<-file>
Absolute path to PostExec file (fichier_final.csv). Mandatory.
=item B<-PART>
The id of the partner responsible for the transfer. Mandatory.
=item B<-IDF>
File identifier. Mandatory.
=item B<-IDT>
string8 - Transfer identifier. Identifies a transfer for a given partner and transfer direction. Mandatory.
=item B<-IDTU>
string8 - Catalog identifier. It is a unique, local reference to a transfer. Mandatory.
=item B<-FNAME>
string512 - Name of the physical receiver file, filename or complete path name, of the directory. Mandatory.
=item B<-FTIME>
int8 - File creation time (HHMMSSCC).
=item B<-FDATE>
File creation date.
=item B<-FPARM>
string512 - User message sent to the partner with the file transfer.
=item B<-SAPPL>
The identifier of the file sender application. Depending on the protocol profile, it is:
- string8 for PeSIT D CFT, PeSIT E, PeSIT SIT
- string48 for PeSIT E CFT/CFT
=back
=head1 DESCRIPTION
PostExec script will execute post file receive action as described in file "fichier_final.csv".
=head1 DIAGNOSTICS
=head1 EXIT STATUS
=head1 CONFIGURATION
=head1 DEPENDENCIES
=head1 INCOMPATIBILITIES
=head1 BUGS AND LIMITATIONS
=head1 LICENSE AND COPYRIGHT
=cut