forked from OTRS/module-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodePolicy.pl
executable file
·642 lines (523 loc) · 17.2 KB
/
CodePolicy.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#!/usr/bin/perl
# --
# CodePolicy.pl - a tool to remotely execute the OTRS code policy against local code
# Copyright (C) 2001-2012 OTRS AG, http://otrs.org/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# --
use strict;
use warnings;
our $VERSION = qw($Revision: 1.5 $) [1];
# disable output buffering
use IO::Handle;
STDOUT->autoflush(1);
STDERR->autoflush(1);
use sigtrap qw( die normal-signals error-signals );
use File::Basename;
use File::Find;
use MIME::Base64;
use SOAP::Lite;
my $ProtocolVersion = 1;
use Getopt::Long;
use Pod::Usage;
my ( $SOAP, $SessionID );
my $ExitCode = 0;
my %GeneralOption = (
Verbose => 0,
);
Getopt::Long::Configure(qw( default pass_through ));
GetOptions(
'dry-run' => \$GeneralOption{DryRun},
'help|?' => \$GeneralOption{Help},
'man' => \$GeneralOption{Man},
'verbose+' => \$GeneralOption{Verbose},
'version' => \$GeneralOption{Version},
) or pod2usage(2);
if ( $GeneralOption{Help} ) {
pod2usage(
-msg =>
'CodyPolicy.pl - a tool to remotely execute the OTRS code policy against local code',
-verbose => 0,
-exitval => 1
)
}
elsif ( $GeneralOption{Man} ) {
# avoid dubious problem with perldoc in combination with UTF-8 that
# leads to strange dashes and single-quotes being used
$ENV{LC_ALL} = 'POSIX';
pod2usage( -verbose => 2 );
}
elsif ( $GeneralOption{Version} ) {
print "$0: $VERSION (protocol version: $ProtocolVersion)\n";
exit 0;
}
my $Action = shift @ARGV || '';
if ( $Action =~ m[^check-d]i ) {
RunChecksRemotely(
'Dir',
"*** The action '$Action' requires a directory!\n*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^check-f]i ) {
RunChecksRemotely(
'File',
"*** The action '$Action' requires a file!\n*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^check-m]i ) {
RunChecksRemotely(
'Module',
"*** The action '$Action' requires a module path or *.sopm file!\n"
. "*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^fix-d]i ) {
RunFixesRemotely(
'Dir',
"*** The action '$Action' requires a directory!\n*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^fix-f]i ) {
RunFixesRemotely(
'File',
"*** The action '$Action' requires a file!\n*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^fix-m]i ) {
RunFixesRemotely(
'Module',
"*** The action '$Action' requires a module path or *.sopm file!\n"
. "*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^recode-d]i ) {
RunRecodingsRemotely(
'Dir',
"*** The action '$Action' requires a directory!\n*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^recode-f]i ) {
RunRecodingsRemotely(
'File',
"*** The action '$Action' requires a file!\n*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^recode-m]i ) {
RunRecodingsRemotely(
'Module',
"*** The action '$Action' requires a module path or *.sopm file!\n"
. "*** See $0 --help for more info.\n"
);
}
elsif ( $Action =~ m[^list-c]i ) {
ListChecks();
}
elsif ( $Action =~ m[^list-d]i ) {
ListDomains();
}
elsif ( $Action =~ m[^list-f]i ) {
ListFixes();
}
elsif ( $Action =~ m[^list-p]i ) {
ListProfiles();
}
elsif ( $Action =~ m[^list-r]i ) {
ListRecodings();
}
else {
print STDERR <<" End-of-Here";
You need to specify exactly one of these actions:
check-dir
check-file
check-module
fix-dir
fix-file
fix-module
list-checks
list-domains
list-fixes
list-profiles
list-recodings
recode-dir
recode-file
recode-module
Try '$0 --help' for more info.
End-of-Here
$ExitCode = 1;
}
exit $ExitCode;
sub RunChecksRemotely {
my ( $Mode, $ErrorMessage ) = @_;
# disable pass_through and evaluate remaining options
my %ActionOption = (
Actions => '',
);
GetOptions(
'actions=s' => \$ActionOption{Actions},
'profile=s' => \$ActionOption{Profile},
) or pod2usage(2);
my $Arg = shift @ARGV or die($ErrorMessage);
Connect();
my $ArgAsRelativePath = UploadFiles( $Mode, $Arg );
print STDERR "running checks...";
my $Result = eval {
$SOAP->RunChecks(
$SessionID,
{
GeneralOption => \%GeneralOption,
ActionOption => \%ActionOption,
Mode => $Mode,
Arg => $ArgAsRelativePath,
}
)->result;
};
if ( !$Result ) {
$ExitCode = 5;
print STDERR "an unexpected problem occurred!\n";
}
else {
my $Log = $Result->{Log};
if ($Log) {
print STDERR "\n", @{$Log};
}
if ( !$Result->{Status} ) {
$ExitCode = 10;
print STDERR "*** not ok - see the problems shown above!\n";
}
else {
print STDERR "ok - everything looks fine!\n";
}
}
Disconnect();
return;
}
sub RunFixesRemotely {
my ( $Mode, $ErrorMessage ) = @_;
# disable pass_through and evaluate remaining options
my %ActionOption = (
Actions => '',
);
GetOptions(
'actions=s' => \$ActionOption{Actions},
'profile=s' => \$ActionOption{Profile},
) or pod2usage(2);
my $Arg = shift @ARGV or die($ErrorMessage);
Connect();
my $ArgAsRelativePath = UploadFiles( $Mode, $Arg );
print STDERR "running fixes...";
my $Result = eval {
$SOAP->RunFixes(
$SessionID,
{
GeneralOption => \%GeneralOption,
ActionOption => \%ActionOption,
Mode => $Mode,
Arg => $ArgAsRelativePath,
}
)->result;
};
if ( !$Result ) {
$ExitCode = 5;
print STDERR "an unexpected problem occurred!\n";
}
else {
my $Log = $Result->{Log};
if ($Log) {
print STDERR "\n", @{$Log};
}
if ( !$Result->{Status} ) {
$ExitCode = 10;
print STDERR "*** something has gone wrong - see the problems shown above!\n";
}
else {
print STDERR "ok!\n";
my $ChangedFiles = $Result->{ChangedFiles};
if ( $ChangedFiles && @{$ChangedFiles} ) {
DownloadFiles($ChangedFiles);
}
}
}
Disconnect();
return;
}
sub RunRecodingsRemotely {
my ( $Mode, $ErrorMessage ) = @_;
# disable pass_through and evaluate remaining options
my %ActionOption = (
Actions => '',
);
GetOptions(
'actions=s' => \$ActionOption{Actions},
'profile=s' => \$ActionOption{Profile},
) or pod2usage(2);
my $Arg = shift @ARGV or die($ErrorMessage);
Connect();
my $ArgAsRelativePath = UploadFiles( $Mode, $Arg );
print STDERR "running recodings...";
my $Result = eval {
$SOAP->RunRecodings(
$SessionID,
{
GeneralOption => \%GeneralOption,
ActionOption => \%ActionOption,
Mode => $Mode,
Arg => $ArgAsRelativePath,
}
)->result;
};
if ( !$Result ) {
$ExitCode = 5;
print STDERR "an unexpected problem occurred!\n";
}
else {
my $Log = $Result->{Log};
if ($Log) {
print STDERR "\n", @{$Log};
}
if ( !$Result->{Status} ) {
$ExitCode = 10;
print STDERR "*** something has gone wrong - see the problems shown above!\n";
}
else {
print STDERR "ok!\n";
my $ChangedFiles = $Result->{ChangedFiles};
if ( $ChangedFiles && @{$ChangedFiles} ) {
DownloadFiles($ChangedFiles);
}
}
}
Disconnect();
return;
}
sub Connect {
print STDERR "connecting...";
$SOAP = SOAP::Lite
->uri('http://otrs.org/OTRS/CodePolicyAPI')
->proxy('http://172.17.17.1/soap/code-policy')
->on_fault(
sub {
my $SOAP = shift;
my $Result = shift;
my $errorMsg
= ref($Result)
? $Result->faultstring()
: $SOAP->transport->status();
die "*** SERVER-MSG: $errorMsg\n";
}
);
$SessionID = $SOAP->StartSession(
{
ARGV => \@ARGV,
ProtocolVersion => $ProtocolVersion,
}
)->result;
if ( !$SessionID ) {
die "*** Got no session - could not connect to CodePolicy server.\n";
}
print STDERR "ok - remote session ID is $SessionID\n";
return;
}
sub Disconnect {
my $ID = $SessionID;
$SessionID = undef;
$SOAP->EndSession($ID);
return;
}
sub UploadFiles {
my ( $Mode, $Arg ) = @_;
my $File = $Mode eq 'Dir' ? undef : basename($Arg);
my $Dir = $Mode eq 'Dir' ? $Arg : dirname($Arg);
my $ParentDir = dirname($Dir);
my $SubDir = basename($Dir);
chdir $ParentDir or die "*** unable to chdir into '$ParentDir'! ($!)\n";
print STDERR "uploading files...";
if ( $Mode eq 'File' ) {
UploadFile("$SubDir/$File");
# now check for CVS/Entries and .svn/entries as either of those might be used on server
# in order to automatically determine the name of the applicable profile
for my $EntriesFile (qw( CVS/Entries .svn/entries )) {
if ( -e "$SubDir/$EntriesFile" ) {
UploadFile("$SubDir/$EntriesFile");
}
}
}
elsif ( $Mode eq 'Dir' || $Mode eq 'Module' ) {
my $wanted = sub {
return if -d $File::Find::name;
UploadFile($File::Find::name);
};
find( { wanted => $wanted, no_chdir => 1 }, $SubDir );
}
else {
die "*** unknown mode '$Mode' given!";
}
print STDERR "done\n";
my $ArgAsRelativePath = $File ? "$SubDir/$File" : $SubDir;
return $ArgAsRelativePath;
}
sub UploadFile {
my ($File) = @_;
my $Contents = SlurpFile($File);
my $MD5Sum = MD5ForFile($File);
return $SOAP->UploadFile(
$SessionID,
{
Filename => $File,
Contents => encode_base64($Contents),
MD5Sum => $MD5Sum
}
)->result;
}
sub DownloadFiles {
my ($Files) = @_;
print STDERR "downloading changed files...";
for my $File ( @{$Files} ) {
DownloadFile($File);
}
print STDERR "done\n";
return 1;
}
sub DownloadFile {
my ($File) = @_;
my $FileInfo = $SOAP->DownloadFile( $SessionID, $File )->result;
die "*** unable to download file '$File'!\n" if !$FileInfo;
die "*** downloaded file '$File' does not exist on client!\n" if !-e $File;
my $Contents = decode_base64( $FileInfo->{Contents} );
OverwriteFile( $File, $Contents, $FileInfo->{MD5Sum} );
return 1;
}
sub SlurpFile {
my ($File) = @_;
my $FH;
open( $FH, '<', $File )
or die "*** could not open file '$File' for reading! ($!)\n";
if ( wantarray() ) {
my @Lines = <$FH>;
close($FH)
or die "*** could not close file '$File'! ($!)\n";
return @Lines;
}
local $/ = undef;
my $Text = <$FH>;
close($FH)
or die "*** could not close file '$File'! ($!)\n";
return $Text;
}
sub OverwriteFile {
my ( $File, $Content, $RequiredMD5Sum ) = @_;
my $FH;
my $TempFile = "${File}_tmp_$$";
open( $FH, '>', $TempFile )
or die "*** could not open file '$TempFile' for writing! ($!)\n";
print $FH $Content;
close($FH)
or die "*** could not close file '$TempFile'! ($!)\n";
if ( defined $RequiredMD5Sum ) {
my $MD5Sum = MD5ForFile($TempFile);
if ( $RequiredMD5Sum ne $MD5Sum ) {
die "*** wrong MD5-sum for '$File' (server: $RequiredMD5Sum <=> client: $MD5Sum)!\n";
}
}
rename $TempFile, $File
or die "*** could not rename file '$TempFile' to '$File'! ($!)\n";
return;
}
sub MD5ForFile {
my ($File) = @_;
my $Output = qx{md5sum $File};
return if !$Output;
return if $Output !~ m{^(\w+)};
return $1;
}
END {
# if we still have a session-ID, we need to disconnect, such that the server has a chance
# to cleanup the session
if ($SessionID) {
print STDERR "cleaning up...";
Disconnect();
print STDERR "done\n";
}
}
=head1 NAME
CodePolicy.pl - a tool to remotely execute the OTRS code policy against local code
=head1 SYNOPSIS
CodePolicy.pl [general options] <action> [action-options]
=head3 General Options
--dry-run only pretends to do any changes (for fixes and recodings)
--help brief help message
--man show full documentation
--verbose shows more info (can be given more than once)
--version show version
=head1 ACTIONS
=over
=item B<check-dir [--actions=action-name,...] [--profile=profile-name] Dir>
Checks the given directory recursively against compliance with the OTRS code policy.
During the traversal, dot-files and repository folders (CVS & .svn) will be skipped.
=item B<check-file [--actions=action-name,...] [--profile=profile-name] File>
Checks the given file against compliance with the OTRS code policy.
=item B<check-module [--actions=action-name,...] [--profile=profile-name] ( Module-Dir | Module.sopm )>
Checks the given module (the files in the given module dir) against compliance
with the OTRS code policy.
=item B<fix-dir [--actions=action-name,...] [--profile=profile-name] Dir>
Traverses the given directory recursively and applies all selected fixes to every file found
that does not already comply with the OTRS code policy.
During the traversal, dot-files and repository folders (CVS & .svn) will be skipped.
=item B<fix-file [--actions=action-name,...] [--profile=profile-name] File>
Applies all selected fixes against the given file.
=item B<fix-module [--actions=action-name,...] [--profile=profile-name] ( Module-Dir | Module.sopm )>
Loads and parses the *.sopm file of the given module and applies all selected fixes to the files
exported by the module.
=item B<list-checks [--domain=domain-name] [File]>
Lists all available checks. If a file is given, only the checks appropriate for that file are
listed.
=item B<list-domains>
Lists all available action domains.
=item B<list-fixes [--domain=domain-name] [File]>
Lists all available fixes. If a file is given, only the fixes appropriate for that file are
listed.
=item B<list-profiles>
Lists all available profiles (for checks, fixes and recodings). In verbose mode, the set of actions
defined by each profile is listed, too.
=item B<list-recodings [--domain=domain-name] [File]>
Lists all available recodings. If a file is given, only the recodings appropriate for that file are
listed.
=item B<recode-dir [--actions=action-name,...] [--profile=profile-name] Dir>
Traverses the given directory recursively and applies all selected recodings to every file found
that does not already comply with the OTRS code policy.
During the traversal, dot-files and repository folders (CVS & .svn) will be skipped.
=item B<recode-file [--actions=action-name,...] [--profile=profile-name] File>
Applies all selected recodings against the given file.
=item B<recode-module [--actions=action-name,...] [--profile=profile-name] ( Module-Dir | Module.sopm )>
Loads and parses the *.sopm file of the given module and applies all selected recodings to the files
exported by the module.
=back
=head3 Action Options
--actions=<string-list> specifies the list of actions that shall be run
--domain=<string> lists only actions applicable for the given domain
--profile=<string> sets the profile from which actions are picked
=head1 DESCRIPTION
B<CodePolicy.pl> is using a remote code policy service (via SOAP) to apply certain kinds
of actions against code, in order to implement the OTRS code policy.
It can be used to check and/or fix your code with respect to the standards that are set by
the OTRS code policy.
=head1 TERMS AND CONDITIONS
This Software is part of the OTRS project (http://otrs.org/).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.
=cut
=head1 VERSION
$Revision: 1.5 $ $Date: 2012-11-20 19:16:45 $
=cut