line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
15
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
86
|
|
2
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
86
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Smartcat::App::DocumentExportApi; |
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
11
|
use File::Spec::Functions qw(catfile); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
109
|
|
7
|
2
|
|
|
2
|
|
13
|
use IO::Uncompress::Unzip qw($UnzipError); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
285
|
|
8
|
2
|
|
|
2
|
|
36
|
use Log::Any qw($log); |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
15
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
511
|
use Smartcat::Client::DocumentExportApi; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
74
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
|
|
124
|
use Smartcat::App::Constants qw( |
13
|
|
|
|
|
|
|
EXPORT_ZIP_FILES_COUNT |
14
|
|
|
|
|
|
|
MAX_ITERATION_WAIT_TIMEOUT |
15
|
|
|
|
|
|
|
ITERATION_WAIT_TIMEOUT |
16
|
2
|
|
|
2
|
|
12
|
); |
|
2
|
|
|
|
|
3
|
|
17
|
2
|
|
|
2
|
|
14
|
use Smartcat::App::Utils; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
175
|
|
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
|
13
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
182
|
|
20
|
|
|
|
|
|
|
$Carp::Internal{ ('Smartcat::Client::DocumentExportApi') }++; |
21
|
|
|
|
|
|
|
$Carp::Internal{ (__PACKAGE__) }++; |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
2
|
|
14
|
use Log::Any qw($log); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
8
|
|
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
2
|
|
300
|
use Data::Dumper; |
|
2
|
|
|
|
|
14
|
|
|
2
|
|
|
|
|
1973
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub new { |
28
|
1
|
|
|
1
|
0
|
45
|
my ( $class, $api, $rundata ) = @_; |
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
|
|
15
|
my $self = bless( |
31
|
|
|
|
|
|
|
{ |
32
|
|
|
|
|
|
|
api => Smartcat::Client::DocumentExportApi->new($api), |
33
|
|
|
|
|
|
|
rundata => $rundata |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
$class |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
316
|
return $self; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub export_files { |
42
|
0
|
|
|
0
|
0
|
|
my ( $self, $documents ) = @_; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $api = $self->{api}; |
45
|
0
|
|
|
|
|
|
my $rundata = $self->{rundata}; |
46
|
0
|
|
|
|
|
|
my %docs = map { $_->id => $_ } @$documents; |
|
0
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my @doc_ids = keys %docs; |
48
|
0
|
|
|
|
|
|
my @tasks; |
49
|
0
|
|
|
|
|
|
while ( $#doc_ids >= 0 ) { |
50
|
0
|
|
|
|
|
|
my @task_doc_ids = splice( @doc_ids, 0, EXPORT_ZIP_FILES_COUNT ); |
51
|
0
|
|
|
|
|
|
my $task = eval { |
52
|
|
|
|
|
|
|
$api->document_export_request_export( |
53
|
|
|
|
|
|
|
document_ids => \@task_doc_ids, |
54
|
0
|
|
|
|
|
|
mode => $rundata->{mode} ); |
55
|
|
|
|
|
|
|
}; |
56
|
0
|
0
|
|
|
|
|
die $log->error( |
57
|
|
|
|
|
|
|
sprintf( |
58
|
|
|
|
|
|
|
"Failed to register a task for exporting documents: '%s'.\nError:\n%s", |
59
|
|
|
|
|
|
|
join( ', ', @task_doc_ids ), |
60
|
|
|
|
|
|
|
format_error_message($@) |
61
|
|
|
|
|
|
|
) |
62
|
|
|
|
|
|
|
) unless ($task); |
63
|
0
|
|
|
|
|
|
push @tasks, $task; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
foreach my $task (@tasks) { |
67
|
0
|
|
|
|
|
|
my $single_file_export = $#{ $task->document_ids } == 0; |
|
0
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my $response; |
69
|
0
|
|
|
|
|
|
my $counter = 0; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
#die $task->id; |
72
|
0
|
|
|
|
|
|
$log->info( sprintf( "Processing task '%s'...", $task->id ) ); |
73
|
0
|
|
|
|
|
|
while ( 1 ) { |
74
|
0
|
|
|
|
|
|
$log->info("Downloading exported files..."); |
75
|
0
|
|
|
|
|
|
$response = eval { |
76
|
0
|
|
|
|
|
|
$api->document_export_download_export_result( |
77
|
|
|
|
|
|
|
task_id => $task->id ); |
78
|
|
|
|
|
|
|
}; |
79
|
0
|
0
|
|
|
|
|
unless ($response) { |
80
|
0
|
|
|
|
|
|
die $log->error( |
81
|
|
|
|
|
|
|
sprintf( |
82
|
|
|
|
|
|
|
"Failed to download exported files for task '%s'.\nError:\n%s", |
83
|
|
|
|
|
|
|
$task->id, format_error_message($@) |
84
|
|
|
|
|
|
|
) |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
last if $response->code != 204; |
89
|
0
|
|
|
|
|
|
$log->info("Export is not ready..."); |
90
|
0
|
|
|
|
|
|
$counter++; |
91
|
0
|
|
|
|
|
|
my $timeout = ITERATION_WAIT_TIMEOUT * $counter; |
92
|
0
|
0
|
|
|
|
|
sleep($timeout < MAX_ITERATION_WAIT_TIMEOUT ? $timeout : MAX_ITERATION_WAIT_TIMEOUT); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
die $log->error( |
95
|
|
|
|
|
|
|
sprintf( "Cannot download exported files: %s. Export task is failed.", |
96
|
0
|
0
|
|
|
|
|
join( ', ', @{ $task->document_ids } ) ) |
|
0
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
) if $response->code == 422; |
98
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
|
if ($single_file_export) { |
100
|
0
|
|
|
|
|
|
my $doc = $docs{ @{ $task->document_ids }[0] }; |
|
0
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $name = $doc->full_path; |
102
|
0
|
|
|
|
|
|
$log->info("Processing document '$name'..."); |
103
|
|
|
|
|
|
|
my $filepath = get_file_path( $rundata->{project_workdir}, |
104
|
0
|
|
|
|
|
|
$doc->target_language, $name, $rundata->{filetype} ); |
105
|
0
|
|
|
|
|
|
save_file( $filepath, $response->content ); |
106
|
0
|
|
|
|
|
|
$log->info("Saved to '$filepath'."); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
0
|
|
|
|
|
|
$self->_save_exported_files_from_zip( $task, $response->content ); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub _save_exported_files_from_zip { |
115
|
0
|
|
|
0
|
|
|
my ( $self, $task, $content ) = @_; |
116
|
|
|
|
|
|
|
|
117
|
0
|
0
|
|
|
|
|
my $u = new IO::Uncompress::Unzip \$content |
118
|
|
|
|
|
|
|
or die $log->error( |
119
|
|
|
|
|
|
|
"Cannot open downloaded content of $task->id: $UnzipError"); |
120
|
0
|
|
|
|
|
|
my $rundata = $self->{rundata}; |
121
|
0
|
|
|
|
|
|
my $status = 0; |
122
|
0
|
|
|
|
|
|
$log->info("Processing zipped exported file..."); |
123
|
0
|
|
|
|
|
|
do { |
124
|
0
|
|
|
|
|
|
my $name = $u->getHeaderInfo()->{Name}; |
125
|
0
|
0
|
|
|
|
|
if ($name =~ m/$rundata->{filetype}$/) { |
126
|
0
|
0
|
|
|
|
|
die $log->error( |
127
|
|
|
|
|
|
|
"Cannot parse '$name' (filetype='$rundata->{filetype}') to get filename and target_language" |
128
|
|
|
|
|
|
|
) unless $name =~ m/(.*)\((.*)\)$rundata->{filetype}$/; |
129
|
0
|
|
|
|
|
|
$log->info("Processing member '$name'..."); |
130
|
0
|
|
|
|
|
|
my $target_language = $2; |
131
|
|
|
|
|
|
|
my $filepath = |
132
|
0
|
|
|
|
|
|
get_file_path($rundata->{project_workdir}, $target_language, $1, $rundata->{filetype}); |
133
|
|
|
|
|
|
|
#print Dumper $self; |
134
|
0
|
0
|
|
|
|
|
open( my $fh, '>', $filepath ) |
135
|
|
|
|
|
|
|
or die $log->error("Could not open file '$filepath' $!"); |
136
|
0
|
|
|
|
|
|
binmode($fh); |
137
|
0
|
|
|
|
|
|
print $fh $_ while <$u>; |
138
|
0
|
|
|
|
|
|
close $fh; |
139
|
0
|
|
|
|
|
|
$log->info("Saved to '$filepath'."); |
140
|
|
|
|
|
|
|
} else { |
141
|
0
|
|
|
|
|
|
$log->info("Skipping member '$name'..."); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} while ( ( $status = $u->nextStream() ) > 0 ); |
144
|
0
|
0
|
|
|
|
|
die $log->error("Error processing downloaded content of $task->id: $!") |
145
|
|
|
|
|
|
|
if $status < 0; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |