line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: push translation files to Smartcat |
2
|
2
|
|
|
2
|
|
1033
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
51
|
|
3
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
42
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
8
|
use utf8; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
12
|
|
6
|
2
|
|
|
2
|
|
32
|
no utf8; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
9
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
package Smartcat::App::Command::push; |
9
|
2
|
|
|
2
|
|
76
|
use Smartcat::App -command; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
10
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
464
|
use File::Basename; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
110
|
|
12
|
2
|
|
|
2
|
|
12
|
use File::Spec::Functions qw(catfile catdir); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
102
|
|
13
|
2
|
|
|
2
|
|
15
|
use File::Find qw(find); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
129
|
|
14
|
2
|
|
|
2
|
|
13
|
use List::Util qw(first); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
149
|
|
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
|
|
90
|
use Smartcat::App::Constants qw( |
17
|
|
|
|
|
|
|
MAX_ITERATION_WAIT_TIMEOUT |
18
|
|
|
|
|
|
|
ITERATION_WAIT_TIMEOUT |
19
|
|
|
|
|
|
|
DOCUMENT_DISASSEMBLING_SUCCESS_STATUS |
20
|
2
|
|
|
2
|
|
13
|
); |
|
2
|
|
|
|
|
4
|
|
21
|
2
|
|
|
2
|
|
10
|
use Smartcat::App::Utils; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
158
|
|
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
2
|
|
16
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
121
|
|
24
|
2
|
|
|
2
|
|
10
|
use Log::Any qw($log); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
10
|
|
25
|
2
|
|
|
2
|
|
352
|
use JSON qw/encode_json/; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
18
|
|
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
|
|
|
|
|
|
|
my $DELETE_BATCH_SIZE = 20; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub opt_spec { |
34
|
0
|
|
|
0
|
1
|
|
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
|
|
|
|
|
|
|
sub validate_args { |
55
|
0
|
|
|
0
|
1
|
|
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
|
0
|
|
|
|
|
if defined $opt->{disassemble_algorithm_name}; |
65
|
|
|
|
|
|
|
$self->app->{rundata}->{preset_disassemble_algorithm} = |
66
|
|
|
|
|
|
|
$opt->{preset_disassemble_algorithm} |
67
|
0
|
0
|
|
|
|
|
if defined $opt->{preset_disassemble_algorithm}; |
68
|
|
|
|
|
|
|
$self->app->{rundata}->{delete_not_existing} = |
69
|
0
|
0
|
|
|
|
|
defined $opt->{delete_not_existing} ? $opt->{delete_not_existing} : 0; |
70
|
|
|
|
|
|
|
$self->app->{rundata}->{extract_id_from_name} = |
71
|
0
|
0
|
|
|
|
|
defined $opt->{extract_id_from_name} ? $opt->{extract_id_from_name} : 0; |
72
|
|
|
|
|
|
|
$self->app->{rundata}->{external_tag} = |
73
|
0
|
0
|
|
|
|
|
defined $opt->{external_tag} ? $opt->{external_tag} : "source:Serge"; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub execute { |
77
|
0
|
|
|
0
|
1
|
|
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
|
0
|
|
|
|
|
$app->project_api->update_project_external_tag( $project, $rundata->{external_tag} ) if ($#{ $project->documents } >= 0); |
|
0
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my %documents; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
for ( @{ $project->documents } ) { |
|
0
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $key = &get_document_key( |
95
|
|
|
|
|
|
|
$_->full_path, |
96
|
|
|
|
|
|
|
$_->target_language, |
97
|
0
|
|
|
|
|
|
$rundata->{extract_id_from_name} ); |
98
|
0
|
0
|
|
|
|
|
$documents{$key} = [] unless defined $documents{$key}; |
99
|
0
|
|
|
|
|
|
push @{ $documents{$key} }, $_; |
|
0
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my %ts_files; |
103
|
|
|
|
|
|
|
find( |
104
|
|
|
|
|
|
|
sub { |
105
|
0
|
|
|
0
|
|
|
my $name = $File::Find::name; |
106
|
0
|
0
|
|
|
|
|
if ($^O !~ /MSWin32/) { # assume we are on Unix if not on Windows |
107
|
0
|
|
|
|
|
|
utf8::decode($name); # assume UTF8 filenames |
108
|
0
|
|
|
|
|
|
utf8::decode($_); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
0
|
0
|
0
|
|
|
|
if ( -f $name |
|
|
|
0
|
|
|
|
|
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
|
0
|
|
|
|
|
$ts_files{$key} = [] unless defined $ts_files{$key}; |
125
|
0
|
|
|
|
|
|
push @{ $ts_files{$key} }, $name; |
|
0
|
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
: defined $documents{$_} ? \@obsolete : \@skip |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
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 ) |
|
0
|
|
|
|
|
|
|
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
|
0
|
|
|
|
|
if ($rundata->{delete_not_existing}) { |
161
|
0
|
|
|
|
|
|
my @document_ids; |
162
|
0
|
|
|
|
|
|
push( @document_ids, map { $_->id } @{ $documents{$_} } ) for @obsolete; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# work in batches |
165
|
0
|
|
|
|
|
|
while (scalar(@document_ids) > 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
|
|
|
|
|
|
|
sub delete { |
181
|
0
|
|
|
0
|
0
|
|
my ( $self, $document_ids ) = @_; |
182
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
$self->app->document_api->delete_documents($document_ids); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub update { |
187
|
0
|
|
|
0
|
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; |
|
0
|
|
|
|
|
|
|
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
|
|
|
0
|
|
|
my $doc = first { $_->target_language eq $lang } @$documents; |
|
0
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# p $doc; |
205
|
0
|
0
|
|
|
|
|
if ( defined $doc ) { |
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; |
|
0
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
$log->warn( |
216
|
|
|
|
|
|
|
"No files for documents:" |
217
|
|
|
|
|
|
|
. join( ', ', |
218
|
0
|
0
|
|
|
|
|
map { $_->name . '(' . $_->target_language . ') [' . $_->id . ']' } |
|
0
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
@documents_without_files ) |
220
|
|
|
|
|
|
|
) if @documents_without_files; |
221
|
|
|
|
|
|
|
|
222
|
0
|
0
|
|
|
|
|
$log->warn( |
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
|
0
|
|
|
|
|
if ( $rundata->{extract_id_from_name} ) { |
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
|
0
|
|
|
|
|
if ($file_name ne $document_name) { |
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
|
|
|
|
|
|
|
sub _check_if_files_are_empty { |
254
|
0
|
|
|
0
|
|
|
my ($self, $filepaths) = @_; |
255
|
|
|
|
|
|
|
|
256
|
0
|
|
|
|
|
|
my $rundata = $self->app->{rundata}; |
257
|
|
|
|
|
|
|
|
258
|
0
|
0
|
|
|
|
|
if ($rundata->{filetype} eq ".po") { |
259
|
0
|
|
|
|
|
|
return are_po_files_empty($filepaths); |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
return 0; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub upload { |
266
|
0
|
|
|
0
|
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; |
|
0
|
|
|
|
|
|
|
271
|
0
|
|
|
|
|
|
my @project_target_languages = @{ $project->target_languages }; |
|
0
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
|
273
|
0
|
0
|
0
|
|
|
|
croak("Conflict: one target language to one file expected.") |
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
|
0
|
|
|
|
|
if ( $rundata->{extract_id_from_name}){ |
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 ) ); |
|
0
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
1; |