| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$self->{"for_each"} and not $self->{"for_each"} =~ /\[PATH\]/
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\]/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | print "Run command: $for_each\n" if $self->{for_each}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
-f $_ and /\.po$/
MC/DC0%0/2
-f $_ and /\.po$/
-f $_
/\.po$/
Truth table0%0/3
A and B
A= -f $_B= /\.po$/
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/5
$n || print("Nothing to do.\n") && exit 1
print "Nothing to do.\n" and exit 1
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"
| |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 0 | my $n = 0; | |||||||||||||||||||||||||||||||
| 78 | 0 | foreach (@ts_files) { | |||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (!exists $scanner->{known_files}->{$_}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 0 | $n++; | |||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($self->{dry_run}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 0 | print "\tORPHANED: $_\n"; | |||||||||||||||||||||||||||||||
| 83 | } else { | ||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | unlink($_) or die "Failed to delete file $_: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 0 | print "\tDELETED: $_\n"; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($self->{for_each}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | rmdir($dir) or die "Failed to remove empty directory $dir: $!\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 0 | print "\tDELETED: $dir\n"; | |||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ($self->{for_each}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ($self->{dry_run}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | while (defined (my $entry = readdir($h))) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | return undef unless $entry eq '.' or $entry eq '..'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$entry eq "." or $entry eq ".."
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 ".."
| |||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 1 | 1; | |||||||||||||||||||||||||||||||