App/DocumentExportApi.pm

filter: Statementstmt 39.3% 39 / 99 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 2 totaltotal 35.6% 51 / 143 CC 16 SCAR 38.8
CC
16
Cov
31%
export_files
90.0
_save_exported_files_from_zip
72.0
BEGIN
1.0
CRAP 48.2
« App/Command/pull.pm ↑ Summary App/Command/config.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Smartcat::App::DocumentExportApi;
5  
6 2 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Uncompress::Unzip qw($UnzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Smartcat::Client::DocumentExportApi;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Smartcat::App::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 EXPORT_ZIP_FILES_COUNT
14 MAX_ITERATION_WAIT_TIMEOUT
15 ITERATION_WAIT_TIMEOUT
16 2 );
17 2 ▶use Smartcat::App::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 $Carp::Internal{ ('Smartcat::Client::DocumentExportApi') }++;
21 2 $Carp::Internal{ (__PACKAGE__) }++;
22  
23 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
28 1 my ( $class, $api, $rundata ) = @_;
29  
30 1 my $self = bless(
31 {
32 api => Smartcat::Client::DocumentExportApi->new($api),
33 rundata => $rundata
34 },
35 $class
36 );
37  
38 1 return $self;
39 }
40  
41 0 ▶sub export_files {
Subroutine export_files: not called CC=9 SCAR=45.0
Pod undocumented
42 0 my ( $self, $documents ) = @_;
43  
44 0 my $api = $self->{api};
45 0 my $rundata = $self->{rundata};
46 0 my %docs = map { $_->id => $_ } @$documents;
47 0 my @doc_ids = keys %docs;
48 0 my @tasks;
49 0 ▶ while ( $#doc_ids >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#doc_ids >= 0) 0 0 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 ▶ die $log->error(
Branch: 1 branch
BranchTrueFalseTotal
unless $task 0 0 0
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;
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 ▶ unless ($response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 0 0
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 ▶ last if $response->code != 204;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code != 204 0 0 0
89 0 $log->info("Export is not ready...");
90 0 $counter++;
91 0 my $timeout = ITERATION_WAIT_TIMEOUT * $counter;
92 0 ▶ sleep($timeout < MAX_ITERATION_WAIT_TIMEOUT ? $timeout : MAX_ITERATION_WAIT_TIMEOUT);
Branch: 1 branch
BranchTrueFalseTotal
$timeout < 300 ? : 0 0 0
93 }
94 die $log->error(
95 sprintf( "Cannot download exported files: %s. Export task is failed.",
96 0 ▶ join( ', ', @{ $task->document_ids } ) )
Branch: 1 branch
BranchTrueFalseTotal
if $response->code == 422 0 0 0
97 ) if $response->code == 422;
98  
99 0 ▶ if ($single_file_export) {
Branch: 1 branch
BranchTrueFalseTotal
if ($single_file_export) { } 0 0 0
100 0 my $doc = $docs{ @{ $task->document_ids }[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 0 ▶sub _save_exported_files_from_zip {
Subroutine _save_exported_files_from_zip: not called CC=8 SCAR=42.8
115 0 my ( $self, $task, $content ) = @_;
116  
117 0 ▶ my $u = new IO::Uncompress::Unzip \$content
Branch: 1 branch
BranchTrueFalseTotal
my $u = "IO::Uncompress::Unzip"->new(\$content) or die $Smartcat::App::DocumentExportApi::log->error("Cannot open downloaded content of $task->id: $Smartcat::App::DocumentExportApi::UnzipError") 0 0 0
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 {
Branch: 1 branch
BranchTrueFalseTotal
($status = $u->nextStream) > 0 and do { do { my $name = $u->getHeaderInfo->{"Name"}; if ($name =~ /$rundata->{"filetype"}$/) { die $Smartcat::App::DocumentExportApi::log->error(qq[Cannot parse '${name}' (filetype='$rundata->{"filetype"}') to get filename and target_language]) unless $name =~ /(.*)\((.*)\)$rundata->{"filetype"}$/; $Smartcat::App::DocumentExportApi::log->info("Processing member '${name}'..."); my $target_language = $2; my $filepath = get_file_path($rundata->{"project_workdir"}, $target_language, $1, $rundata->{"filetype"}); open my $fh, ">", $filepath or die $Smartcat::App::DocumentExportApi::log->error("Could not open file '${filepath}' $!"); binmode $fh; print $fh $_ while defined($_ = readline $u); close $fh; $Smartcat::App::DocumentExportApi::log->info("Saved to '${filepath}'."); } else { $Smartcat::App::DocumentExportApi::log->info("Skipping member '${name}'..."); } } } 0 0 0
124 0 my $name = $u->getHeaderInfo()->{Name};
125 0 ▶ if ($name =~ m/$rundata->{filetype}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$rundata->{'filetype'}$/) { } 0 0 0
126 0 ▶ die $log->error(
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /(.*)\((.*)\)$rundata->{"filetype"}$/ 0 0 0
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 ▶ open( my $fh, '>', $filepath )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filepath or die $Smartcat::App::DocumentExportApi::log->error("Could not open file '${filepath}' $!") 0 0 0
135 or die $log->error("Could not open file '$filepath' $!");
136 0 binmode($fh);
137 0 ▶ print $fh $_ while <$u>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $u) and do { print $fh $_ } 0 0 0
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 ▶ die $log->error("Error processing downloaded content of $task->id: $!")
Branch: 1 branch
BranchTrueFalseTotal
if $status < 0 0 0 0
145 if $status < 0;
146 }
147  
148 2 1;
« App/Command/pull.pm ↑ Summary App/Command/config.pm »