-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtidy.pl
173 lines (163 loc) · 4.49 KB
/
tidy.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
use File::Find;
use File::Copy;
use Perl::Tidy;
use Term::ANSIColor qw(:constants);
use threads;
use threads::shared;
use Thread::Queue;
use Thread::Semaphore;
use Getopt::Long;
our $VERSION = '1.5';
our $mjobs = 1;
our $quiet = 0;
our $silent = 0;
s/^-j(\d+)$/-j=$1/ foreach @ARGV;
our ($stats,$copyr);
GetOptions(
'j|jobs:i' => \$mjobs,
'h|help' => \&help,
'q|quiet' => \$quiet,
's|silent' => \$silent,
'i|stats' => \$stats,
'c|copy' => \©r
) or &help;
if ($stats) {&stats;exit}
sub copyr {
print GREEN "
#########################################
# " . BOLD . "Copyright 2013 Jake Bott, Gosha Tugai" . RESET . GREEN . " #
#=>-----------------------------------<=#
# " . BOLD . " All Rights Reserved. " . RESET . GREEN . " #
#########################################
" . RESET unless $silent;
exit 255;
}
sub stats {
our $total = 0;
find({
wanted => sub {
if (/\.(?:(?:p[lm]x?)|t|xs)$/i) {
s/^\.\///;
return if $_ eq $0;
return if /\.(?:(?:BASE)|(?:BACKUP)|(?:LOCAL)|(?:REMOTE))\.\d+/ or /\.orig$/;
my $pth = $_;
return if $pth =~ m#blib/#;
$pth =~ s/^\.\///;
my $t = `wc -l $pth`;
$t =~ s/(\d+).*/$1/;
$t =~ s/\n//;
$total += int $t;
print GREEN "$pth: $t lines\n", CLEAR unless $quiet || $silent;
}
},
no_chdir => 1
},
'.');
print BRIGHT_GREEN "This project has $total lines of code\n", CLEAR unless $silent;
exit 0;
}
sub help {
print STDERR "tidy.pl v$VERSION\n
Usage
perl tidy.pl [OPTIONS]
Tidies all .pl, .pm and .t files in the current directory
tree using .perltidyrc for the specification.
Options
-j[obs] Specify the number of threads to use.
If a number is not given, use as many
threads as needed.
-h[elp] Shows this help then exit.
-q[uiet] Prints less output.
-s[ilent] No output at all.
-i --stats Print code statistics then exit.
-c[opy] Print copyright information then exit.
" unless $silent || $quiet;
exit 255;
}
our %err : shared;
our $running : shared = 0;
our $sem = new Thread::Semaphore($mjobs) if $mjobs > 0;
our $iq = new Thread::Queue();
$| = 1;
our $diff : shared = 0;
our $nerr : shared = 0;
our $nfiles : shared = 0;
our @kids;
find({
wanted => sub {
if (/\.(?:(?:p[lm]x?)|t)$/i) {
s/^\.\///;
return if $_ eq $0;
return if /\.(?:(?:BASE)|(?:BACKUP)|(?:LOCAL)|(?:REMOTE))\.\d+/ or /\.orig$/;
my $pth = $_;
return if $pth =~ m#blib/#;
$pth =~ s/^\.\///;
my $mn = $pth;
$mn =~ s/\//::/g if $mn =~ s/\.pm$//;
{
lock $running;
if (($mjobs == 0 and ($running + 1 >= scalar(@kids))) || $mjobs > scalar(@kids)) {
push @kids, threads->new(\&worker, $iq, $sem, $mjobs);
}
}
$iq->enqueue({
src => $pth,
tdy => $pth . '.tdy',
mn => $mn
});
}
},
no_chdir => 1
},
'.');
$iq->enqueue(undef) foreach @kids;
$_->join() foreach @kids;
sub worker {
while (defined(my $job = $_[0]->dequeue())) {
my $errstr;
$_[1]->down() if defined $_[1];
{ lock $running; $running++; }
printf CYAN. "\rTidying %-73s" . RESET, $job->{mn} unless $silent;
my $err = Perl::Tidy::perltidy(
stderr => \$errstr,
argv => '-pro=.../.perltidyrc ' . $job->{src},
postfilter => \&postf);
{ lock $running; $running--; }
$_[1]->up() if defined $_[1];
chomp($errstr);
if (!($errstr eq '')) {
$err = 1;
lock %err;
$err{ $job->{src} } = $errstr;
}
if ($err) {
lock $nerr;
$nerr++;
printf RED. "\r%-74s [FAIL]" . RESET . "\n", $job->{mn} unless $silent;
} else {
my $src = $job->{src};
if (`diff -q '$src' '$src.tdy'`) {
printf GREEN. "\r%-74s [ OK ]\n" . RESET, $job->{mn};
copy($src . ".tdy", $src);
lock $diff;
$diff++;
} else {
printf BLUE. "\r%-74s [SAME]%s" . RESET, $job->{mn}, ($quiet ? '' : "\n") unless $silent;
}
}
unlink($job->{tdy});
lock $nfiles;
$nfiles++;
}
}
if (scalar(keys %err) > 0) {
print BOLD RED "\r===== Error Report =====\n" . RESET unless $silent;
print join "\n", map { sprintf BOLD . RED . ' --- %s --- ' . RESET . "\n%s", $_, $err{$_} } keys %err unless $silent;
print BOLD RED "\n===== End Error Report =====\n" . RESET unless $silent;
}
printf(($nerr == 0 ? GREEN : RED) . "\r%-81s" . RESET . "\n", (sprintf "%i file%s, %i changed, %i error%s, used %s thread%s", $nfiles, ($nfiles > 1 ? 's' : ''), $diff, $nerr, ($nerr > 1 ? 's' : ''), scalar(@kids), (scalar(@kids) > 1 ? 's' : ''))) unless $silent;
exit($nerr) ? 1 : 0;
sub postf {
my ($content) = @_;
return $content;
}