App/Command/push.pm

filter: Statementstmt 27.8% 44 / 158 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 60.8% 14 / 23 Podpod 50.0% 3 / 6 totaltotal 24.2% 61 / 252 CC 36 SCAR 44.1
CC
36
Cov
20%
execute
240.0
update
72.0
validate_args
42.0
CRAP 82.6
↑ Summary App/Command/pull.pm »
1 # ABSTRACT: push translation files to Smartcat
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶no utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Smartcat::App::Command::push;
9 2 ▶use Smartcat::App -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Spec::Functions qw(catfile catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Smartcat::App::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 MAX_ITERATION_WAIT_TIMEOUT
18 ITERATION_WAIT_TIMEOUT
19 DOCUMENT_DISASSEMBLING_SUCCESS_STATUS
20 2 );
21 2 ▶use Smartcat::App::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use JSON qw/encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # How many documents to delete at a time
28 # (there's a limitation on the number of the document due to
29 # the fact that all document IDs are specified in a URL,
30 # and URLs itself have length limitations).
31 2 my $DELETE_BATCH_SIZE = 20;
32  
33 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
34 0 my ($self) = @_;
35  
36 0 my @opts = $self->SUPER::opt_spec();
37  
38 0 push @opts,
39 [ 'disassemble-algorithm-name:s' =>
40 'Optional disassemble file algorithm' ],
41 [ 'preset-disassemble-algorithm:s' =>
42 'Optional disassemble algorithm preset' ],
43 [ 'delete-not-existing' => 'Delete not existing documents' ],
44 $self->project_id_opt_spec,
45 $self->project_workdir_opt_spec,
46 $self->file_params_opt_spec,
47 $self->extract_id_from_name_opt_spec,
48 $self->external_tag_opt_spec,
49 ;
50  
51 0 return @opts;
52 }
53  
54 0 ▶sub validate_args {
Subroutine validate_args: not called CC=6 SCAR=37.4
Pod documented
55 0 my ( $self, $opt, $args ) = @_;
56  
57 0 $self->SUPER::validate_args( $opt, $args );
58 0 $self->validate_project_id( $opt, $args );
59 0 $self->validate_project_workdir( $opt, $args );
60 0 $self->validate_file_params( $opt, $args );
61  
62 $self->app->{rundata}->{disassemble_algorithm_name} =
63 $opt->{disassemble_algorithm_name}
64 0 ▶ if defined $opt->{disassemble_algorithm_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"disassemble_algorithm_name"} 0 0 0
65 $self->app->{rundata}->{preset_disassemble_algorithm} =
66 $opt->{preset_disassemble_algorithm}
67 0 ▶ if defined $opt->{preset_disassemble_algorithm};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"preset_disassemble_algorithm"} 0 0 0
68 $self->app->{rundata}->{delete_not_existing} =
69 0 ▶ defined $opt->{delete_not_existing} ? $opt->{delete_not_existing} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'delete_not_existing'} ? : 0 0 0
70 $self->app->{rundata}->{extract_id_from_name} =
71 0 ▶ defined $opt->{extract_id_from_name} ? $opt->{extract_id_from_name} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'extract_id_from_name'} ? : 0 0 0
72 $self->app->{rundata}->{external_tag} =
73 0 ▶ defined $opt->{external_tag} ? $opt->{external_tag} : "source:Serge";
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'external_tag'} ? : 0 0 0
74 }
75  
76 0 ▶sub execute {
Subroutine execute: not called CC=15 SCAR=54.8
Pod documented
77 0 my ( $self, $opt, $args ) = @_;
78  
79 0 my $app = $self->app;
80 0 my $rundata = $app->{rundata};
81 $log->info(
82 sprintf(
83 "Running 'push' command for project '%s' and translation files from '%s'...",
84 $rundata->{project_id},
85 $rundata->{project_workdir}
86 )
87 0 );
88  
89 0 my $project = $app->project_api->get_project;
90 0 ▶ $app->project_api->update_project_external_tag( $project, $rundata->{external_tag} ) if ($#{ $project->documents } >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $#{$project->documents;} >= 0 0 0 0
91 0 my %documents;
92  
93 0 for ( @{ $project->documents } ) {
94 my $key = &get_document_key(
95 $_->full_path,
96 $_->target_language,
97 0 $rundata->{extract_id_from_name} );
98 0 ▶ $documents{$key} = [] unless defined $documents{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $documents{$key} 0 0 0
99 0 push @{ $documents{$key} }, $_;
100 }
101  
102 0 my %ts_files;
103 find(
104 0 ▶ sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
105 0 my $name = $File::Find::name;
106 0 ▶ if ($^O !~ /MSWin32/) { # assume we are on Unix if not on Windows
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /MSWin32/) 0 0 0
107 0 utf8::decode($name); # assume UTF8 filenames
108 0 utf8::decode($_);
109 }
110  
111 0 ▶ if ( -f $name
Branch: 1 branch
BranchTrueFalseTotal
if (-f $name and not /^\.$/ and /$rundata->{"filetype"}$/) 0 0 0
Condition0%0/6
-f $name and not /^\.$/ and /$rundata->{"filetype"}$/
!ll&&!rl&&r
000
-f $name and not /^\.$/
!ll&&!rl&&r
000
MC/DC0%0/3
-f $name and not /^\.$/ and /$rundata->{"filetype"}$/
-f $name not /^\.$/ /$rundata->{"filetype"}$/
Truth table0%0/4
A and not(B) and C
A= -f $nameB= not /^\.$/C= /$rundata->{"filetype"}$/
ABCresult
0XX0
10X0
1100
1111
112 && !m/^\.$/
113 && m/$rundata->{filetype}$/ )
114 {
115 0 s/$rundata->{filetype}$//;
116 0 my $path = catfile( dirname($name), $_ );
117  
118 my $key = &get_ts_file_key(
119 $rundata->{project_workdir},
120 $path,
121 0 $rundata->{extract_id_from_name} );
122  
123 0 utf8::decode($key);
124 0 ▶ $ts_files{$key} = [] unless defined $ts_files{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ts_files{$key} 0 0 0
125 0 push @{ $ts_files{$key} }, $name;
126 }
127 },
128 $rundata->{project_workdir}
129 0 );
130  
131 0 my %stats;
132 0 $stats{$_}++ for ( keys %documents, keys %ts_files );
133  
134 0 my ( @upload, @obsolete, @update, @skip );
135 push @{
136 exists $ts_files{$_} && !$self->_check_if_files_are_empty( $ts_files{$_} )
137 ? defined $documents{$_} ? \@update : \@upload
138 0 ▶ : defined $documents{$_} ? \@obsolete : \@skip
Branch: 3 branches
BranchTrueFalseTotal
exists $ts_files{$_} && !$self->_check_if_files_are_empty($ts_files{$_}) ? : 0 0 0
defined $documents{$_} ? : 0 0 0
defined $documents{$_} ? : 0 0 0
Condition0%0/3
exists $ts_files{$_} and not $self->_check_if_files_are_empty($ts_files{$_})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ts_files{$_} and not $self->_check_if_files_are_empty($ts_files{$_})
exists $ts_files{$_} not $self->_check_if_files_are_empty($ts_files{$_})
Truth table0%0/3
A and not(B)
A= exists $ts_files{$_}B= not $self->_check_if_files_are_empty($ts_files{$_})
ABresult
0X0
100
111
139 },
140 $_
141 0 for ( keys %stats );
142  
143 $log->info(
144 sprintf(
145 "State:\n Upload [%d]\n %s\n Update [%d]\n %s\n Obsolete [%d]\n %s\n Skip [%d]\n %s\n",
146 scalar @upload,
147 0 join( ', ', map { "'$_'" } @upload ),
148 scalar @update,
149 0 join( ', ', map { "'$_'" } @update ),
150 scalar @obsolete,
151 0 join( ', ', map { "'$_'" } @obsolete ),
152 scalar @skip,
153 0 join( ', ', map { "'$_'" } @skip )
154 )
155 );
156  
157 0 $self->upload( $project, $ts_files{$_} ) for @upload;
158 0 $self->update( $project, $documents{$_}, $ts_files{$_} ) for @update;
159  
160 0 ▶ if ($rundata->{delete_not_existing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rundata->{"delete_not_existing"}) 0 0 0
161 0 my @document_ids;
162 0 push( @document_ids, map { $_->id } @{ $documents{$_} } ) for @obsolete;
163  
164 # work in batches
165 0 ▶ while (scalar(@document_ids) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @document_ids > 0) 0 0 0
166 0 my @batch = splice(@document_ids, 0, $DELETE_BATCH_SIZE);
167 0 $self->delete( \@batch );
168 }
169 }
170  
171 $log->info(
172 sprintf(
173 "Finished 'push' command for project '%s' and translation files from '%s'.",
174 $rundata->{project_id},
175 $rundata->{project_workdir}
176 )
177 0 );
178 }
179  
180 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my ( $self, $document_ids ) = @_;
182  
183 0 $self->app->document_api->delete_documents($document_ids);
184 }
185  
186 0 ▶sub update {
Subroutine update: not called CC=8 SCAR=42.8
Pod undocumented
187 0 my ( $self, $project, $documents, $ts_files ) = @_;
188  
189 0 my $app = $self->app;
190 0 my $api = $app->document_api;
191 0 my $rundata = $app->{rundata};
192  
193 my @target_languages =
194 0 map { &get_language_from_ts_filepath($rundata->{project_workdir}, $_) } @$ts_files;
195 0 my %doc_and_path_by_lang;
196 my @files_without_documents;
197  
198 #print Dumper $ts_files;
199 0 for (@$ts_files) {
200  
201 0 my $lang = get_language_from_ts_filepath($rundata->{project_workdir}, $_);
202 0 ▶ my $doc = first { $_->target_language eq $lang } @$documents;
Subroutine __ANON__: not called CC=1 SCAR=6.9
203  
204 # p $doc;
205 0 ▶ if ( defined $doc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $doc) { } 0 0 0
206 0 $doc_and_path_by_lang{$lang} = { path => $_, doc => $doc };
207 }
208 else {
209 0 push @files_without_documents, $_;
210 }
211 }
212 my @documents_without_files =
213 0 grep { !exists $doc_and_path_by_lang{ $_->target_language } } @$documents;
214  
215 $log->warn(
216 "No files for documents:"
217 . join( ', ',
218 0 ▶ map { $_->name . '(' . $_->target_language . ') [' . $_->id . ']' }
Branch: 1 branch
BranchTrueFalseTotal
if @documents_without_files 0 0 0
219 @documents_without_files )
220 ) if @documents_without_files;
221  
222 0 ▶ $log->warn(
Branch: 1 branch
BranchTrueFalseTotal
if @files_without_documents 0 0 0
223 "No documents for files:" . join( ', ', @files_without_documents ) )
224 if @files_without_documents;
225  
226 0 for ( keys %doc_and_path_by_lang ) {
227 0 my $doc_and_path = $doc_and_path_by_lang{$_};
228
229 0 $api->update_document( $doc_and_path->{path}, $doc_and_path->{doc}->id );
230
231 0 ▶ if ( $rundata->{extract_id_from_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rundata->{"extract_id_from_name"}) 0 0 0
232 my $file_name = get_file_name(
233 $doc_and_path->{path},
234 $rundata->{filetype},
235 0 $target_languages[0]);
236 0 my $document_name = $doc_and_path->{doc}->name;
237  
238 0 ▶ if ($file_name ne $document_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_name ne $document_name) 0 0 0
239 $log->info(
240 sprintf(
241 "Renaming document '%s' from '%s' to '%s'.",
242 $doc_and_path->{doc}->id,
243 0 $document_name,
244 $file_name
245 )
246 );
247 0 $api->rename_document( $doc_and_path->{doc}->id, $file_name );
248 }
249 }
250 }
251 }
252  
253 0 ▶sub _check_if_files_are_empty {
Subroutine _check_if_files_are_empty: not called CC=2 SCAR=17.9
254 0 my ($self, $filepaths) = @_;
255  
256 0 my $rundata = $self->app->{rundata};
257  
258 0 ▶ if ($rundata->{filetype} eq ".po") {
Branch: 1 branch
BranchTrueFalseTotal
if ($rundata->{"filetype"} eq ".po") 0 0 0
259 0 return are_po_files_empty($filepaths);
260 }
261  
262 0 return 0;
263 }
264  
265 0 ▶sub upload {
Subroutine upload: not called CC=4 SCAR=30.0
Pod undocumented
266 0 my ( $self, $project, $ts_files ) = @_;
267  
268 0 my $rundata = $self->app->{rundata};
269 my @target_languages =
270 0 map { &get_language_from_ts_filepath($rundata->{project_workdir}, $_) } @$ts_files;
271 0 my @project_target_languages = @{ $project->target_languages };
272  
273 0 ▶ croak("Conflict: one target language to one file expected.")
Branch: 1 branch
BranchTrueFalseTotal
unless @$ts_files == 1 and @target_languages == 1 0 0 0
Condition0%0/3
@$ts_files == 1 and @target_languages == 1
!ll&&!rl&&r
000
MC/DC0%0/2
@$ts_files == 1 and @target_languages == 1
@$ts_files == 1 @target_languages == 1
Truth table0%0/3
A and B
A= @$ts_files == 1B= @target_languages == 1
ABresult
0X0
100
111
274 unless @$ts_files == 1 && @target_languages == 1;
275 0 my $path = shift @$ts_files;
276  
277 my $filename = prepare_document_name( $rundata->{project_workdir}, $path, $rundata->{filetype},
278 0 $target_languages[0] );
279  
280 0 my $meta_info;
281 0 ▶ if ( $rundata->{extract_id_from_name}){
Branch: 1 branch
BranchTrueFalseTotal
if ($rundata->{"extract_id_from_name"}) 0 0 0
282 0 my $file_id = &get_file_id( $path );
283 0 $meta_info = encode_json( { file_id => $file_id } );
284 }
285  
286 0 my $documents = $self->app->project_api->upload_file( $path, $filename, undef, $meta_info,
287 \@target_languages );
288 $log->info( "Created documents ids:\n "
289 0 . join( ', ', map { $_->id } @$documents ) );
290 }
291  
292 2 1;
↑ Summary App/Command/pull.pm »