RosettaCode.pm

filter: Statementstmt 42.4% 73 / 172 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 67.7% 21 / 31 Podpod 0.0% 0 / 10 totaltotal 37.3% 94 / 252 CC 25 SCAR 38.0
CC
25
Cov
35%
run
132.0
build_tasks
72.0
build_langs
56.0
CRAP 44.8
« RosettaCode/Task.pm ↑ Summary RosettaCode/Lang.pm »
1 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package RosettaCode;
3  
4 1 our $VERSION = '0.1.1';
5  
6 1 ▶use RosettaCode::Lang;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use RosettaCode::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MediaWiki::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 use Module::Pluggable
15 1 ▶ require => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 search_path => [ 'RosettaCode::Command' ];
17 1 RosettaCode->plugins;
18  
19 1 ▶use Mo qw'build builder default xxx';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 extends 'RosettaCode::Command';
21  
22 1 has text => '';
23 1 has log_io => '';
24  
25 1 ▶use File::Path 'rmtree';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use constant abstract => 'Sync local repository with remote RosettaCode wiki';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant usage_desc => 'rosettacode sync <target_directory> [<options>]';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant options => [qw( target )];
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use constant ROSETTACODE_API_URL => 'http://rosettacode.org/w/api.php';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant TASKS_FILE => 'Cache/tasks.txt';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant LANGS_FILE => 'Cache/langs.txt';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant TASKS_CATEGORY => 'Category:Programming_Tasks';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant LANGS_CATEGORY => 'Category:Programming_Languages';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use constant CACHE_TIME => 7 * 24 * 60 * 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 has bot => (builder => 'build_bot');
40 1 has tasks => (builder => 'build_tasks');
41 1 has langs => (builder => 'build_langs');
42 1 has log_io => io->file('rosettacode.log')->utf8;
43  
44 0 ▶sub run {
Subroutine run: not called CC=11 SCAR=48.8
Pod undocumented
45 0 my ($self) = @_;
46  
47 0 ▶ die "Current directory does not look like a RosettaCodeData directory"
Branch: 1 branch
BranchTrueFalseTotal
unless -f "Conf/task.yaml" 0 0 0
48 unless -f 'Conf/task.yaml';
49  
50 0 for my $dir (qw( Meta Lang Task )) {
51 0 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 0 0 0
52 0 print "Removing '$dir'\n";
53 0 ▶ rmtree $dir
Branch: 1 branch
BranchTrueFalseTotal
rmtree($dir) or die "Failed to rm -r Meta Lang Task" 0 0 0
54 or die "Failed to rm -r Meta Lang Task";
55 }
56 }
57  
58 0 $self->log("START RosettaCode Sync");
59 0 for my $lang (sort keys %{$self->langs}) {
60 0 my $langs = $self->langs;
61 0 ▶ my $info = $langs->{$lang} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $langs->{$lang} or next 0 0 0
62  
63 0 my $rcl = RosettaCode::Lang->new(
64 %$info,
65 langs => $self->langs,
66 tasks => $self->tasks,
67 bot => $self->bot,
68 log_io => $self->log_io,
69 );
70  
71 0 print "* LANG: $info->{name}\n";
72  
73 0 $rcl->fetch_lang;
74  
75 0 ▶ next if $ENV{RCD_FETCHONLY};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"RCD_FETCHONLY"} 0 0 0
76  
77 0 $rcl->build_lang;
78 }
79  
80 0 for my $task (sort keys %{$self->tasks}) {
81 # exit if $a++ > 100;
82 0 my $tasks = $self->tasks;
83 0 ▶ my $info = $tasks->{$task} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $tasks->{$task} or next 0 0 0
84  
85 0 my $rct = RosettaCode::Task->new(
86 %$info,
87 langs => $self->langs,
88 tasks => $self->tasks,
89 bot => $self->bot,
90 log_io => $self->log_io,
91 );
92  
93 0 print "* TASK: $rct->{name}\n";
94  
95 0 $rct->fetch_task;
96  
97 0 ▶ next if $ENV{RCD_FETCHONLY};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"RCD_FETCHONLY"} 0 0 0
98  
99 0 $rct->build_task;
100 }
101  
102 0 $self->log("COMPLETE RosettaCode Sync");
103 }
104  
105 0 ▶sub build_tasks {
Subroutine build_tasks: not called CC=8 SCAR=42.8
Pod undocumented
106 0 my ($self) = @_;
107 0 my $io = io->file(TASKS_FILE)->utf8;
108 0 my @task_list;
109 0 ▶ if ($io->exists and time - $io->mtime < CACHE_TIME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io->exists and time - $io->mtime < 604800) { } 0 0 0
Condition0%0/3
$io->exists and time - $io->mtime < 604800
!ll&&!rl&&r
000
MC/DC0%0/2
$io->exists and time - $io->mtime < 604800
$io->exists time - $io->mtime < 604800
Truth table0%0/3
A and B
A= $io->existsB= time - $io->mtime < 604800
ABresult
0X0
100
111
110 0 @task_list = $io->chomp->slurp;
111 }
112 else {
113 0 @task_list = $self->get_category(TASKS_CATEGORY);
114 0 $io->assert->println($_) for @task_list;
115 }
116 0 my $tasks = YAML::PP::LoadFile('Conf/task.yaml');
117 0 for my $name (keys %$tasks) {
118 0 ▶ my $info = $tasks->{$name} ||= {};
Condition0%0/2
$tasks->{$name} ||= {}
l!l
00
MC/DC0%0/1
$tasks->{$name} ||= {}
$tasks->{$name}
Truth table0%0/2
A
A= $tasks->{$name}
Aresult
11
00
119 0 $info->{name} = $name;
120 0 $info->{url} = $name;
121 0 $info->{url} =~ s/ /_/g;
122 0 $info->{path} = $name;
123 0 $info->{path} =~ s/[\'\"]//g;
124 0 $info->{path} =~ s/[\,\ \/\*\!\(\)\x{7f}-\x{ffff}]+/-/g;
125 0 ▶ die unless $info->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"path"} 0 0 0
126 0 ▶ die if $info->{path} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"path"} eq "-" 0 0 0
127 }
128 0 return $tasks;
129 }
130  
131 0 ▶sub build_langs {
Subroutine build_langs: not called CC=7 SCAR=40.3
Pod undocumented
132 0 my ($self) = @_;
133 0 my $io = io->file(LANGS_FILE)->utf8;
134 0 my @lang_list;
135 0 ▶ if ($io->exists and time - $io->mtime < CACHE_TIME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($io->exists and time - $io->mtime < 604800) { } 0 0 0
Condition0%0/3
$io->exists and time - $io->mtime < 604800
!ll&&!rl&&r
000
MC/DC0%0/2
$io->exists and time - $io->mtime < 604800
$io->exists time - $io->mtime < 604800
Truth table0%0/3
A and B
A= $io->existsB= time - $io->mtime < 604800
ABresult
0X0
100
111
136 0 @lang_list = $io->chomp->slurp;
137 }
138 else {
139 @lang_list = map {
140 0 s/^Category://;
141 0 $_;
142 } $self->get_category(LANGS_CATEGORY);
143 0 $io->assert->println($_) for @lang_list;
144 }
145 0 my $langs = YAML::PP::LoadFile('Conf/lang.yaml');
146 0 my $meta_langs = {};
147 0 for my $name (keys %$langs) {
148 0 my $text = $langs->{$name};
149 0 my $info = {};
150 0 $info->{name} = $name;
151 0 $info->{url} = $name;
152 0 $info->{url} =~ s/ /_/g;
153 0 $info->{path} = $name;
154 0 $info->{path} =~ s/[\ \/\*\!]/-/g;
155 0 ▶ $text =~ s/^\.(\S+)\ ?// or die "ERROR: '$name: $text'";
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/^\.(\S+)\ ?// or die "ERROR: '${name}: ${text}'" 0 0 0
156 0 $info->{ext} = $1;
157 0 $meta_langs->{lc $name} = $info;
158 }
159  
160 # Common mutations:
161 0 my $m = $meta_langs;
162 0 my $alias = YAML::PP::LoadFile('Conf/alias.yaml');
163  
164 0 for (sort keys %{$alias->{lang}}) {
165 0 $m->{$_} = $m->{$alias->{lang}{$_}};
166 }
167  
168 0 $self->log("Dump Meta/Lang.yaml");
169 0 my $yaml = YAML::PP::Dump($meta_langs);
170 0 $yaml =~ s/FALSE/'FALSE'/g; # Fix YAML for Ruby
171 0 io->file("Meta/Lang.yaml")->assert->print($yaml);
172 0 return $meta_langs;
173 }
174  
175 0 ▶sub build_bot {
Subroutine build_bot: not called CC=2 SCAR=17.9
Pod undocumented
176 0 my ($self) = @_;
177 0 ▶ ROSETTACODE_API_URL =~ m!^(https?)://([^/]+)/(.*)/api.php$! or die;
Branch: 1 branch
BranchTrueFalseTotal
"http://rosettacode.org/w/api.php" =~ m[^(https?)://([^/]+)/(.*)/api.php$] or die 0 0 0
178 0 my ($protocol, $host, $path) = ($1, $2, $3);
179 0 MediaWiki::Bot->new({
180 agent =>
181 'rosettacodedata/' . $VERSION
182 . ' (https://github.com/ingydotnet/rosettacode-pm) MediaWiki::Bot/'
183 . MediaWiki::Bot->VERSION,
184 assert => 'bot',
185 protocol => $protocol,
186 host => $host,
187 path => $path,
188 });
189 }
190  
191 0 ▶sub get_text {
Subroutine get_text: not called CC=1 SCAR=6.9
Pod undocumented
192 0 my ($self, $name) = @_;
193 0 $self->log("Fetch MediaWiki text for '$name'");
194 0 return $self->bot->get_text($name);
195 }
196  
197 0 ▶sub get_category {
Subroutine get_category: not called CC=1 SCAR=6.9
Pod undocumented
198 0 my ($self, $category) = @_;
199 0 $self->log("Fetch MediaWiki category '$category'");
200 0 $self->bot->get_pages_in_category($category, {max => 0});
201 }
202  
203 0 ▶sub write_file {
Subroutine write_file: not called CC=1 SCAR=6.9
Pod undocumented
204 0 my ($self, $file, $content, $indent) = @_;
205 0 $self->log(' ' x $indent . "Write $file");
206 0 io->file($file)->assert->utf8->print($content);
207 }
208  
209 0 ▶sub write_symlink {
Subroutine write_symlink: not called CC=1 SCAR=6.9
Pod undocumented
210 0 my ($self, $source, $target, $indent) = @_;
211 0 $self->log(' ' x $indent . "Symlink $source -> $target");
212 0 io->link($source)->assert->symlink($target);
213 }
214  
215 0 ▶sub dump_file {
Subroutine dump_file: not called CC=1 SCAR=6.9
Pod undocumented
216 0 my ($self, $file, $object, $indent) = @_;
217 0 $self->log(' ' x $indent . "Dump $file");
218 0 YAML::PP::DumpFile($file, $object);
219 }
220  
221 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod undocumented
222 0 my ($self, $string, @args) = @_;
223 0 my $time = gmtime();
224 0 $self->log_io->append(sprintf "<$time> $string\n", @args);
225 }
226  
227 1 1;
« RosettaCode/Task.pm ↑ Summary RosettaCode/Lang.pm »