-
Notifications
You must be signed in to change notification settings - Fork 53
/
Sync.pm
167 lines (130 loc) · 5.29 KB
/
Sync.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package Serge::Sync;
use parent Serge::Interface::PluginHost;
use strict;
use utf8;
use File::Spec::Functions qw(rel2abs splitdir);
use Time::HiRes qw(gettimeofday tv_interval);
use Serge::Engine::Processor;
use Serge::Sync::Util;
use Serge::Util qw(subst_macros);
#
# Initialize object
#
sub new {
my ($class) = @_;
my $self = {
debug => undef, # enable debug output
debug_echo_commands => undef, # set to 1 to echo system commands about to be executed
debug_echo_output => undef, # set to 1 to echo commands' output to be parsed
no_pull => undef, # set to 1 to prevent checking out changes from VCS
no_localize => undef, # set to 1 to prevent running the actual l10n script and just do VCS checkout/commit
no_pull_ts => undef, # set to 1 to prevent executing pull_ts command (syncing changes from external translation service to local translation files)
no_push_ts => undef, # set to 1 to prevent updating push_ts command (pushing local translation files to external translation service)
no_push => undef, # set to 1 to prevent committing changes back to VCS
ts => undef, # holds TS (translation service) plugin
vcs => undef, # holds VCS (version control system) plugin
};
bless $self, $class;
return $self;
}
sub validate_config {
my ($self, $config) = @_;
die "\$config parameter not passed" unless defined $config;
die "bad config object" unless defined $config->{data};
if (!$self->{no_pull_ts} || !$self->{no_push_ts}) {
die "'sync' section missing" unless defined $config->{data}->{sync};
die "'sync'->'ts' section missing from " unless defined $config->{data}->{sync}->{ts};
}
if (!$self->{no_pull} || !$self->{no_push}) {
die "'sync' section missing" unless defined $config->{data}->{sync};
die "'sync'->'vcs' not defined" unless defined $config->{data}->{sync}->{vcs};
}
if (!$self->{no_localize}) {
die "'jobs' section missing" unless defined $config->{data}->{jobs};
}
}
sub reset_counters {
my ($self) = @_;
$self->{total_jobs} = 0;
$self->{skipped_jobs} = 0;
$self->{failed_jobs} = 0;
}
sub do_sync {
my ($self, $engine, $config) = @_;
$self->validate_config($config);
$self->{config} = $config;
# initialize translation service plugin (if necessary)
if (!$self->{no_pull_ts} || !$self->{no_push_ts}) {
$self->{ts} = $self->load_plugin_from_node(
'Serge::Sync::Plugin::TranslationService',
$config->{data}->{sync}->{ts}
);
$self->{ts}->{echo_commands} = 1 if $self->{echo_commands};
$self->{ts}->{echo_output} = 1 if $self->{echo_output};
}
# initialize VCS plugin (if necessary)
if (!$self->{no_pull} || !$self->{no_push }) {
$self->{vcs} = $self->load_plugin_from_node(
'Serge::Sync::Plugin::VCS',
$config->{data}->{sync}->{vcs}
);
$self->{vcs}->{initialize} = 1 if $self->{initialize};
$self->{vcs}->{echo_commands} = 1 if $self->{echo_commands};
$self->{vcs}->{echo_output} = 1 if $self->{echo_output};
}
# step 1
if ($self->{no_pull}) {
print "Skip 'pull' step\n" if $self->{debug};
} else {
print "\nUpdating project from VCS...\n\n";
my $start = [gettimeofday];
$self->{vcs}->checkout_all;
print "'pull' step took ", tv_interval($start), " seconds\n";
}
# step 2
if ($self->{no_pull_ts}) {
print "Skip 'pull_ts' step\n" if $self->{debug};
} else {
print "\nUpdating .po files from Pootle's internal database...\n\n";
my $start = [gettimeofday];
$self->{ts}->pull_ts; # update all languages
print "'pull-ts' step took ", tv_interval($start), " seconds\n";
}
# step 3
if ($self->{no_localize}) {
print "Skip 'localize' step\n" if $self->{debug};
} else {
print "\nRunning the translation framework...\n\n";
my $start = [gettimeofday];
my $processor = Serge::Engine::Processor->new($engine, $config);
$processor->run();
print "'localize' step took ", tv_interval($start), " seconds\n";
$self->{total_jobs} += $processor->{total_jobs};
$self->{skipped_jobs} += $processor->{skipped_jobs};
$self->{failed_jobs} += $processor->{failed_jobs};
}
# step 4
if ($self->{no_push_ts}) {
print "Skip 'push_ts' step\n" if $self->{debug};
} else {
print "\nUpdating Pootle's internal database from .po files...\n\n";
my $start = [gettimeofday];
$self->{ts}->push_ts; # update all languages
print "'push-ts' step took ", tv_interval($start), " seconds\n";
}
# step 5
if ($self->{no_push}) {
print "Skip 'push' step\n" if $self->{debug};
} else {
print "\nCommitting updated project files back to VCS\n\n";
my $start = [gettimeofday];
$self->{vcs}->commit_all($self->{commit_message});
print "'push' step took ", tv_interval($start), " seconds\n";
}
}
sub abspath {
my ($self, $path) = @_;
die "Config not initialized yet" unless defined $self->{config};
return $self->{config}->abspath($path);
}
1;