line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: pull translation files from Smartcat |
2
|
2
|
|
|
2
|
|
1312
|
use strict; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
65
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
88
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Smartcat::App::Command::pull; |
6
|
2
|
|
|
2
|
|
34
|
use Smartcat::App -command; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
35
|
|
7
|
2
|
|
|
2
|
|
608
|
use Smartcat::App::Constants qw(COMPLETE); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
141
|
|
8
|
2
|
|
|
2
|
|
17
|
use Smartcat::App::Utils; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
181
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
14
|
use Log::Any qw($log); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
26
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub opt_spec { |
13
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
|
|
|
my @opts = $self->SUPER::opt_spec(); |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
|
push @opts, |
18
|
|
|
|
|
|
|
[ 'complete-documents' => 'Pull "complete" documents only' ], |
19
|
|
|
|
|
|
|
[ 'complete-projects' => 'Pull "complete" projects only' ], |
20
|
|
|
|
|
|
|
[ 'skip-missing' => 'Do not create (skip) missing files' ], |
21
|
|
|
|
|
|
|
[ 'mode:s' => 'Unit export mode, available values: current, confirmed, complete; default value: current', |
22
|
|
|
|
|
|
|
{ default => "current" } |
23
|
|
|
|
|
|
|
], |
24
|
|
|
|
|
|
|
$self->project_id_opt_spec, |
25
|
|
|
|
|
|
|
$self->project_workdir_opt_spec, |
26
|
|
|
|
|
|
|
$self->file_params_opt_spec, |
27
|
|
|
|
|
|
|
; |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
return @opts; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub validate_args { |
33
|
0
|
|
|
0
|
1
|
|
my ( $self, $opt, $args ) = @_; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
$self->SUPER::validate_args( $opt, $args ); |
36
|
0
|
|
|
|
|
|
$self->validate_project_id( $opt, $args ); |
37
|
0
|
|
|
|
|
|
$self->validate_project_workdir( $opt, $args ); |
38
|
0
|
|
|
|
|
|
$self->validate_file_params( $opt, $args ); |
39
|
0
|
|
|
|
|
|
$self->validate_mode( $opt, $args ); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
$self->app->{rundata}->{complete_projects} = |
42
|
0
|
0
|
|
|
|
|
defined $opt->{complete_projects} ? $opt->{complete_projects} : 0; |
43
|
|
|
|
|
|
|
$self->app->{rundata}->{complete_documents} = |
44
|
0
|
0
|
|
|
|
|
defined $opt->{complete_documents} ? $opt->{complete_documents} : 0; |
45
|
|
|
|
|
|
|
$self->app->{rundata}->{skip_missing} = |
46
|
0
|
0
|
|
|
|
|
defined $opt->{skip_missing} ? $opt->{skip_missing} : 0; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub validate_mode { |
50
|
0
|
|
|
0
|
0
|
|
my ( $self, $opt, $args ) = @_; |
51
|
0
|
|
|
|
|
|
my $rundata = $self->app->{rundata}; |
52
|
|
|
|
|
|
|
$self->usage_error("Incorrect 'mode' value, 'current', 'confirmed', 'complete' are possible") |
53
|
0
|
0
|
0
|
|
|
|
if defined $opt->{mode} && $opt->{mode} !~ /current|confirmed|complete/; |
54
|
0
|
|
|
|
|
|
$rundata->{mode} = $opt->{mode}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub execute { |
58
|
0
|
|
|
0
|
1
|
|
my ( $self, $opt, $args ) = @_; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $rundata = $self->app->{rundata}; |
61
|
|
|
|
|
|
|
$log->info( |
62
|
|
|
|
|
|
|
sprintf( |
63
|
|
|
|
|
|
|
"Running 'pull' command for project '%s' and translation files from '%s'...", |
64
|
|
|
|
|
|
|
$rundata->{project_id}, |
65
|
|
|
|
|
|
|
$rundata->{project_workdir} |
66
|
|
|
|
|
|
|
) |
67
|
0
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
my $project = $self->app->project_api->get_project; |
70
|
0
|
0
|
|
|
|
|
exit 1 unless $project; |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
0
|
|
|
|
unless ( !$rundata->{complete_projects} || $project->status eq COMPLETE ) { |
73
|
0
|
|
|
|
|
|
$log->warn( |
74
|
|
|
|
|
|
|
sprintf( |
75
|
|
|
|
|
|
|
"Skip: project '%s' [%s] is not complete (status = %s)", |
76
|
|
|
|
|
|
|
$project->name, $project->id, $project->status |
77
|
|
|
|
|
|
|
) |
78
|
|
|
|
|
|
|
); |
79
|
0
|
|
|
|
|
|
exit 0; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
my $documents; |
83
|
0
|
0
|
|
|
|
|
if ( $rundata->{complete_documents} ) { |
84
|
0
|
|
|
|
|
|
$documents = []; |
85
|
0
|
|
|
|
|
|
for ( @{ $project->documents } ) { |
|
0
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
if ( $_->status eq COMPLETE ) { |
87
|
0
|
|
|
|
|
|
push @$documents, $_; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else { |
90
|
0
|
|
|
|
|
|
$log->info( |
91
|
|
|
|
|
|
|
sprintf( |
92
|
|
|
|
|
|
|
"Skip: document '%s(%s)' [%s] is not complete (status = %s)", |
93
|
|
|
|
|
|
|
$_->name, $_->target_language, $_->id, $_->status |
94
|
|
|
|
|
|
|
) |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
$log->info( |
99
|
0
|
|
|
|
|
|
sprintf( |
100
|
|
|
|
|
|
|
"Found %d complete documents on the server", |
101
|
|
|
|
|
|
|
scalar(@$documents) |
102
|
|
|
|
|
|
|
) |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
else { |
106
|
0
|
|
|
|
|
|
$documents = $project->documents; |
107
|
0
|
|
|
|
|
|
$log->info( |
108
|
|
|
|
|
|
|
sprintf( |
109
|
|
|
|
|
|
|
"Found %d documents on the server", |
110
|
|
|
|
|
|
|
scalar(@$documents) |
111
|
|
|
|
|
|
|
) |
112
|
|
|
|
|
|
|
); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
if ( $rundata->{skip_missing} ) { |
116
|
|
|
|
|
|
|
my @existing_documents = grep { |
117
|
0
|
|
|
|
|
|
my $filepath = get_file_path( |
118
|
|
|
|
|
|
|
$rundata->{project_workdir}, |
119
|
|
|
|
|
|
|
$_->target_language, |
120
|
0
|
|
|
|
|
|
$_->full_path, $rundata->{filetype}); |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
my $exists = -e $filepath; |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
|
|
|
|
if ($rundata->{debug}) { |
125
|
0
|
0
|
|
|
|
|
my $status = $exists ? 'exists' : 'does not exist'; |
126
|
0
|
|
|
|
|
|
$log->info( |
127
|
|
|
|
|
|
|
sprintf("%s (%s)", $filepath, $status) |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# return the status |
132
|
0
|
|
|
|
|
|
$exists; |
133
|
|
|
|
|
|
|
} @$documents; |
134
|
0
|
|
|
|
|
|
$documents = \@existing_documents; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
my $count = scalar(@$documents); |
138
|
0
|
0
|
|
|
|
|
if ($count == 0) { |
139
|
0
|
|
|
|
|
|
$log->warn('List of documents to download is empty; nothing to do.'); |
140
|
|
|
|
|
|
|
} else { |
141
|
0
|
|
|
|
|
|
$log->info( |
142
|
|
|
|
|
|
|
sprintf( |
143
|
|
|
|
|
|
|
"Will download %d documents", |
144
|
|
|
|
|
|
|
$count |
145
|
|
|
|
|
|
|
) |
146
|
|
|
|
|
|
|
); |
147
|
0
|
|
|
|
|
|
$self->app->document_export_api->export_files($documents); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
$log->info( |
151
|
|
|
|
|
|
|
sprintf( |
152
|
|
|
|
|
|
|
"Finished 'pull' command for project '%s' and translation files from '%s'.", |
153
|
|
|
|
|
|
|
$rundata->{project_id}, |
154
|
|
|
|
|
|
|
$rundata->{project_workdir} |
155
|
|
|
|
|
|
|
) |
156
|
0
|
|
|
|
|
|
); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
1; |