lib/Serge/Command/clean_ts.pm

filter: Statementstmt 27.7% 25 / 90 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 5 totaltotal 19.7% 33 / 167 SCAR 60.7
CC
27
Cov
18%
run
342.0
is_dir_empty
30.0
validate_data
12.0
CRAP 431.1
« lib/Serge/Sync.pm ↑ Summary lib/Serge/ScanTSFiles.pm »
1 package Serge::Command::clean_ts;
2 1 ▶use parent Serge::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Find qw(finddepth);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Serge::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Serge::Engine::Processor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Serge::ScanTSFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Serge::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub get_commands {
13 return {
14 0 ▶ 'clean-ts' => {need_config => 1, handler => \&run, info => 'Delete orphaned translation files'},
Subroutine get_commands: not called CC=1 SCAR=6.9
Pod undocumented
15 }
16 }
17  
18 sub init {
19 0 ▶ my ($self, $command) = @_;
Subroutine init: not called CC=2 SCAR=17.9
Pod undocumented
20  
21 0 $self->SUPER::init($command);
22  
23 GetOptions(
24 "dry-run" => \$self->{dry_run},
25 "for-each:s" => \$self->{for_each},
26 0 ▶ ) or die "Failed to parse some command-line parameters.";
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("dry-run", \$self->{"dry_run"}, "for-each:s", \$self->{"for_each"}) or die "Failed to parse some command-line parameters." 0 0 0
27 }
28  
29 sub validate_data {
30 0 ▶ my ($self, $command) = @_;
Subroutine validate_data: not called CC=3 SCAR=24.8
Pod undocumented
31  
32 0 $self->SUPER::validate_data($command);
33  
34 0 ▶ if ($self->{for_each} and $self->{for_each} !~ m/\[PATH\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"for_each"} and not $self->{"for_each"} =~ /\[PATH\]/) 0 0 0
Condition0%0/3
$self->{"for_each"} and not $self->{"for_each"} =~ /\[PATH\]/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"for_each"} and not $self->{"for_each"} =~ /\[PATH\]/
$self->{"for_each"} not $self->{"for_each"} =~ /\[PATH\]/
Truth table0%0/3
A and not(B)
A= $self->{"for_each"}B= not $self->{"for_each"} =~ /\[PATH\]/
ABresult
0X0
100
111
35 0 die "--for-each parameter must include the '[PATH]' placeholder";
36 }
37 }
38  
39 sub run {
40 0 ▶ my ($self, $command) = @_;
Subroutine run: not called CC=18 SCAR=58.3
Pod undocumented
41  
42 0 ▶ print "*** DRY RUN ***\n" if $self->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dry_run"} 0 0 0
43 0 ▶ print "Run command: $for_each\n" if $self->{for_each};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"for_each"} 0 0 0
44  
45 0 my @confs = $self->{parent}->get_config_files;
46  
47 0 my $scanner = Serge::ScanTSFiles->new();
48  
49 0 my %ts_directories;
50  
51 0 foreach (@confs) {
52 0 my $config = $self->{parent}->get_config_object($_);
53 0 $config->chdir;
54 0 my $processor = Serge::Engine::Processor->new($scanner, $config);
55 0 $processor->run();
56 }
57  
58 0 print "\nFound translation directories:\n";
59 0 foreach (sort keys %{$scanner->{ts_directories}}) {
60 0 print "\t$_\n";
61 }
62  
63 0 print "\nScanning translation files...";
64 0 my @ts_files;
65  
66 my $wanted = sub {
67 0 ▶ push @ts_files, $File::Find::name if (-f $_ && /\.po$/); # TODO: refactor; file extensions should not be hard-coded
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ and /\.po$/ 0 0 0
Condition0%0/3
-f $_ and /\.po$/
!ll&&!rl&&r
000
MC/DC0%0/2
-f $_ and /\.po$/
-f $_ /\.po$/
Truth table0%0/3
A and B
A= -f $_B= /\.po$/
ABresult
0X0
100
111
68 0 };
69  
70 0 foreach my $dir (sort keys %{$scanner->{ts_directories}}) {
71 0 finddepth({wanted => $wanted, follow => 1}, $dir);
72 }
73 0 $n = scalar(@ts_files);
74 0 print " $n files found\n\n";
75 0 ▶ print "Nothing to do.\n" and exit(1) unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
Condition0%0/5
$n || print("Nothing to do.\n") && exit 1
l!l&&r!l&&!r
000
print "Nothing to do.\n" and exit 1
!ll
00
MC/DC0%0/2
$n || print("Nothing to do.\n") && exit 1
$n print "Nothing to do.\n"
Truth table0%0/3
A || B
A= $nB= print "Nothing to do.\n"
ABresult
1X1
011
000
76  
77 0 my $n = 0;
78 0 foreach (@ts_files) {
79 0 ▶ if (!exists $scanner->{known_files}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $scanner->{"known_files"}{$_}) 0 0 0
80 0 $n++;
81 0 ▶ if ($self->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dry_run'}) { } 0 0 0
82 0 print "\tORPHANED: $_\n";
83 } else {
84 0 ▶ unlink($_) or die "Failed to delete file $_: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $_ or die "Failed to delete file ${_}: $!\n" 0 0 0
85 0 print "\tDELETED: $_\n";
86  
87 0 ▶ if ($self->{for_each}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"for_each"}) 0 0 0
88 0 my $cmd = $self->{for_each};
89 0 $cmd =~ s/\[PATH\]/$_/g;
90 0 system($cmd);
91 }
92  
93 0 my $dir = dirname($_);
94 0 ▶ while (is_dir_empty($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_dir_empty($dir)) 0 0 0
95 0 ▶ rmdir($dir) or die "Failed to remove empty directory $dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rmdir $dir or die "Failed to remove empty directory ${dir}: $!\n" 0 0 0
96 0 print "\tDELETED: $dir\n";
97  
98 0 ▶ if ($self->{for_each}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"for_each"}) 0 0 0
99 0 my $cmd = $self->{for_each};
100 0 $cmd =~ s/\[PATH\]/$dir/g;
101 0 system($cmd);
102 }
103  
104 0 $dir = dirname($dir); # get parent
105 }
106 }
107 }
108 }
109  
110 0 ▶ if ($n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) { } 0 0 0
111 0 ▶ if ($self->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'dry_run'}) { } 0 0 0
112 0 print "$n orphaned translation files found.\n";
113 } else {
114 0 print "$n orphaned translation files were deleted.\n";
115 }
116 } else {
117 0 print "No orphaned translation files were found.\n";
118 }
119  
120 0 return 0;
121 }
122  
123 sub is_dir_empty {
124 0 ▶ my $dir = shift;
Subroutine is_dir_empty: not called CC=5 SCAR=34.0
Pod undocumented
125  
126 0 ▶ opendir(my $h, $dir) or die "Failed to open directory $dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $h, $dir or die "Failed to open directory ${dir}: $!\n" 0 0 0
127  
128 0 ▶ while (defined (my $entry = readdir($h))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $entry = readdir $h)) 0 0 0
129 0 ▶ return undef unless $entry eq '.' or $entry eq '..';
Branch: 1 branch
BranchTrueFalseTotal
unless $entry eq "." or $entry eq ".." 0 0 0
Condition0%0/3
$entry eq "." or $entry eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$entry eq "." or $entry eq ".."
$entry eq "." $entry eq ".."
Truth table0%0/3
A or B
A= $entry eq "."B= $entry eq ".."
ABresult
1X1
011
000
130 }
131 0 return 1;
132 }
133  
134 1 1;
« lib/Serge/Sync.pm ↑ Summary lib/Serge/ScanTSFiles.pm »