lib/Serge/ScanTSFiles.pm

filter: Statementstmt 25.2% 25 / 99 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 5 totaltotal 20.2% 33 / 163 SCAR 58.5
CC
24
Cov
18%
process_job
72.0
update_database_from_source_files
56.0
parse_source_file
30.0
CRAP 346.2
« lib/Serge/Command/clean_ts.pm ↑ Summary lib/Serge/Engine/Plugin/feature_branch.pm »
1 package Serge::ScanTSFiles;
2 1 ▶use parent Serge::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Serge::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub process_job {
10 0 ▶ my ($self, $job) = @_;
Subroutine process_job: not called CC=8 SCAR=42.8
Pod undocumented
11  
12 0 $self->init_job($job);
13 0 $self->adjust_job_defaults($job);
14 0 $self->adjust_destination_languages($job);
15  
16 #$self->{job}->{debug} = 1;
17  
18 # This hash will contain the list of known translation directories across all jobs
19 0 ▶ $self->{ts_directories} = {} unless exists $self->{ts_directories};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"ts_directories"} 0 0 0
20  
21 # This hash will contain the list of known translation files across all jobs
22 0 ▶ $self->{known_files} = {} unless exists $self->{known_files};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"known_files"} 0 0 0
23  
24 # for .po scanning purposes, skip jobs having output_only_mode as well
25  
26 0 ▶ if ($self->job_can_run($job) && !$job->{output_only_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->job_can_run($job) and not $job->{'output_only_mode'}) { } 0 0 0
Condition0%0/3
$self->job_can_run($job) and not $job->{"output_only_mode"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->job_can_run($job) and not $job->{"output_only_mode"}
$self->job_can_run($job) not $job->{"output_only_mode"}
Truth table0%0/3
A and not(B)
A= $self->job_can_run($job)B= not $job->{"output_only_mode"}
ABresult
0X0
100
111
27 0 print "\n\n*** [".$job->{id}."] ".$job->{name}." ***\n";
28 } else {
29 0 print "*** SKIPPING: [".$job->{id}."] ".$job->{name}." ***\n";
30 0 return;
31 }
32  
33 0 my $ts_file_path = $job->{ts_file_path};
34 0 ▶ die "ERROR: ts_file_path not defined" unless $ts_file_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $ts_file_path 0 0 0
35 0 my ($ts_base_dir) = split(/[\\\/]%LOCALE%[\\\/]/, $ts_file_path);
36 # die here, otherwise there's a chance some translation interchange files will be deleted by mistake
37 0 ▶ die "ERROR: ts_file_path has no %LOCALE% macro defined" unless $ts_base_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $ts_base_dir 0 0 0
38 0 $ts_base_dir =~ s/\\/\//sg; # always use forward slash for consistency
39  
40 0 ▶ die "source_dir [$job->{source_dir}] doesn't exist. Try doing an initial data checkout (`serge pull --initialize`), or reconfigure your job" unless -d $job->{source_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $job->{"source_dir"} 0 0 0
41  
42 0 $self->{ts_directories}->{$ts_base_dir} = 1;
43  
44 # always go through all destination languages with no optimizations
45 0 $self->{job}->{modified_languages} = $self->{job}->{destination_languages};
46  
47 0 print "TS base dir: [".$ts_base_dir."]\n";
48  
49 # this will just scan the files and prepare the list of relative file names
50 # in $self->{files} for update_database_from_ts_files() to work properly
51 0 $self->update_database_from_source_files;
52  
53 # this will generate the list of target translation files
54 # with 'can_process_ts_file' callback processing
55 0 $self->update_database_from_ts_files;
56 }
57  
58 sub update_database_from_source_files {
59 0 ▶ my ($self) = @_;
Subroutine update_database_from_source_files: not called CC=7 SCAR=40.3
Pod undocumented
60  
61 # Walk through the given directory and its subdirectories, recursively
62  
63 0 my $start = [gettimeofday];
64  
65 0 print "Scanning directory structure...\n";
66  
67 my $ff = Serge::FindFiles->new({
68 0 ▶ postcheck_callback => sub { return $self->update_database_from_source_files_postcheck_callback(@_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 });
70 0 ▶ $ff->{prefix} = $self->{job}->{source_path_prefix} if exists $self->{job}->{source_path_prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"job"}{"source_path_prefix"} 0 0 0
71 0 $ff->{process_subdirs} = $self->{job}->{source_process_subdirs};
72 0 ▶ $ff->{match} = $self->{job}->{source_match} if exists $self->{job}->{source_match};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"job"}{"source_match"} 0 0 0
73 0 ▶ $ff->{exclude} = $self->{job}->{source_exclude} if exists $self->{job}->{source_exclude};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"job"}{"source_exclude"} 0 0 0
74 0 ▶ $ff->{dir_exclude} = $self->{job}->{source_exclude_dirs} if exists $self->{job}->{source_exclude_dirs};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"job"}{"source_exclude_dirs"} 0 0 0
75  
76 0 $ff->find($self->{job}->{source_dir});
77  
78 0 @{$self->{found_files}} = sort keys %{$ff->{found_files}};
79  
80 0 my $end = [gettimeofday];
81 0 my $time = tv_interval($start, $end);
82 0 my $files_found = scalar(@{$self->{found_files}});
83 0 print("Scanned in $time sec, $files_found files match the criteria\n");
84  
85 0 ▶ die "No files match the search criteria. Please reconfigure your job" unless $files_found;
Branch: 1 branch
BranchTrueFalseTotal
unless $files_found 0 0 0
86  
87 # now parse files; during this process, plugins may report that some files
88 # should be considered orphaned
89  
90 0 foreach my $file_rel (@{$self->{found_files}}) {
91 0 $self->parse_source_file($file_rel);
92 }
93 }
94  
95 sub parse_source_file {
96 0 ▶ my ($self, $file_rel) = @_;
Subroutine parse_source_file: not called CC=5 SCAR=34.0
Pod undocumented
97  
98 # set global CURRENT_FILE_REL variable to current relative file path
99 # as it will later be used in different parts
100 0 $self->{current_file_rel} = $file_rel;
101  
102 # the file source is only needed when 'after_load_source_file_for_processing' or
103 # 'can_process_source_file' callback want to filter out files by their content
104  
105 # TODO: to optimize processing, the host could query the plugins if they need
106 # the file content, and load the file only when necessary
107  
108 0 my ($src) = $self->read_file($self->get_full_source_path($self->{current_file_rel}));
109  
110 0 $self->run_callbacks('after_load_source_file_for_processing', $self->{current_file_rel}, \$src);
111  
112 0 my $result = combine_and($self->run_callbacks('is_file_orphaned', $self->{current_file_rel}));
113 0 ▶ if ($result eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 1) 0 0 0
114 0 ▶ print "\tSkip $self->{current_file_rel} because callback said it is orphaned\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
115  
116 # remove the file from $self->{file_mappings} hash
117 0 delete $self->{file_mappings}->{$self->{current_file_rel}};
118  
119 0 return;
120 }
121  
122 0 $result = combine_and(1, $self->run_callbacks('can_process_source_file', $self->{current_file_rel}, undef, \$src));
123 0 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 0 0
124 0 ▶ print "\tSkip $self->{current_file_rel} because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
125 0 return;
126 }
127  
128 0 $self->{files}->{$self->{current_file_rel}} = [1]; # a dummy array of a positive size (originally this should include a list of item_id)
129 }
130  
131 sub update_database_from_ts_files_lang {
132 0 ▶ my ($self, $lang) = @_;
Subroutine update_database_from_ts_files_lang: not called CC=3 SCAR=24.8
Pod undocumented
133  
134 0 ▶ return if ($lang eq $self->{job}->{source_language});
Branch: 1 branch
BranchTrueFalseTotal
if $lang eq $self->{"job"}{"source_language"} 0 0 0
135  
136 0 foreach my $file (sort keys %{$self->{files}}) {
137 0 $self->update_database_from_ts_files_lang_file($lang, $file);
138 }
139 }
140  
141 sub update_database_from_ts_files_lang_file {
142 0 ▶ my ($self, $lang, $relfile) = @_;
Subroutine update_database_from_ts_files_lang_file: not called CC=5 SCAR=34.0
Pod undocumented
143  
144 0 my $fullpath = $self->{job}->get_full_ts_file_path($relfile, $lang);
145  
146 0 print "\t$fullpath\n";
147  
148 0 ▶ if (!-f $fullpath) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $fullpath) 0 0 0
149 0 ▶ print "\tFile does not exist: $fullpath\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
150 0 return;
151 }
152  
153 0 my $result = combine_and(1, $self->run_callbacks('can_process_ts_file', $relfile, $lang));
154 0 ▶ if ($result eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq 0) 0 0 0
155 0 ▶ print "\tSkip $fullpath because at least one callback returned 0\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
156 0 return;
157 }
158  
159 0 $self->{known_files}->{$fullpath} = 1;
160 }
161  
162 1 1;
« lib/Serge/Command/clean_ts.pm ↑ Summary lib/Serge/Engine/Plugin/feature_branch.pm »