lib/CPAN/Mini/Inject/REST/Controller/API/Version1_0.pm

filter: Statementstmt 36.2% 54 / 149 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 52.9% 18 / 34 Podpod 45.4% 5 / 11 totaltotal 31.5% 77 / 244 CC 20 SCAR 25.9
CC
20
Cov
29%
repository_POST
56.0
mirror_GET
20.0
dist_GET
20.0
CRAP 13.3
↑ Summary script/cpan_mini_inject_rest_cgi.pl »
1 package CPAN::Mini::Inject::REST::Controller::API::Version1_0;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Archive::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec::Functions qw/catdir catfile splitpath/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Parse::CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶BEGIN {extends 'Catalyst::Controller::REST'; }
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 __PACKAGE__->config(
19 namespace => 'api/1.0',
20 default => 'application/json',
21 );
22  
23 =head1 NAME
24  
25 CPAN::Mini::Inject::REST::Controller::API::Version1_0
26  
27 =head1 DESCRIPTION
28  
29 Catalyst Controller.
30  
31 =head1 METHODS
32  
33 =head2 repository
34  
35 /api/1.0/repository/File-Name-1.00.tar.gz
36  
37 =cut
38  
39 1 ▶sub repository :Local :Args(1) :ActionClass('REST') {}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine repository: not called CC=1 SCAR=0.0
Pod documented
40  
41 =head3 GET
42  
43 Downloads a file from the repository. Returns status 404 if the file could
44 not be found.
45  
46 =cut
47  
48 0 ▶sub repository_GET {
Subroutine repository_GET: not called CC=2 SCAR=17.9
Pod undocumented
49 0 my ($self, $c, $file) = @_;
50
51 0 my $path = catfile($c->model('CMI')->config->get('repository'), 'authors/id/L/LO/LOCAL', $file);
52 0 ▶ if (-e $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) { } 0 0 0
53 0 $c->serve_static_file($path);
54 } else {
55 0 $self->status_not_found(
56 $c,
57 message => "Cannot find $file in repository"
58 );
59 }
60 }
61  
62 =head3 POST
63  
64 Adds a file to the repository and injects it into the mirror. If the file can be
65 added, status 201 (Created) is returned with details of which modules the file
66 provides.
67  
68 {
69 "provides": [
70 {
71 "version": "0.04",
72 "module": "hello"
73 }
74 ],
75 "file": "hello-0.04.tar.gz"
76 }
77  
78 If the file cannot be added to the repository, status 400 is returned with an
79 appropriate error message:
80  
81 {
82 "error": "File hello-0.03.tar.gz is already in the repository"
83 }
84  
85 Note that if a file already exists in the repository, it cannot be added again.
86  
87 =cut
88  
89 0 ▶sub repository_POST {
Subroutine repository_POST: not called CC=7 SCAR=40.3
Pod undocumented
90 0 my ($self, $c, $file) = @_;
91
92 # Check to see if the file already exists in the repository
93 0 ▶ unless (-e catfile($c->model('CMI')->config->get('repository'), 'authors/id/L/LO/LOCAL', $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e catfile($c->model('CMI')->config->get('repository'), 'authors/id/L/LO/LOCAL', $file)) { } 0 0 0
94 0 ▶ if ($c->req->upload('file')->filename =~ /([^\/]*)-([0-9._]+)\.tar\.gz$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->upload("file")->filename =~ m[([^/]*)-([0-9._]+)\.tar\.gz$]) 0 0 0
95 0 my $module = $1;
96 0 my $version = $2;
97 0 $module =~ s/-/::/g;
98
99 0 my $ftmp = File::Temp->new();
100 0 my $tmpdir = $ftmp->newdir();
101 0 my $newfile = $tmpdir. "/" . $c->req->upload('file')->filename;
102 0 copy($c->req->upload('file')->tempname, $newfile);
103  
104 0 $c->model('CMI')->readlist;
105 0 $c->model('CMI')->add(
106 module => $module,
107 version => $version,
108 authorid => 'LOCAL',
109 file => $newfile,
110 );
111
112 0 my @provides;
113 0 push @provides, {module => $module, version => $version};
114
115 # Add all modules listed in META.json / META.yml
116 0 ▶ if (my $meta = _load_meta($newfile)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta = _load_meta($newfile)) 0 0 0
117 0 ▶ while (my ($module, $details) = each %{$meta->{provides}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$meta{"provides"};}) 0 0 0
118 $c->model('CMI')->add(
119 module => $module,
120 0 ▶ version => $details->{version} // 'undef',
Condition0%0/2
$details->{"version"} // "undef"
l!l
00
MC/DC0%0/1
$details->{"version"} // "undef"
$details->{"version"}
Truth table0%0/2
A
A= $details->{"version"}
Aresult
11
00
121 authorid => 'LOCAL',
122 file => $newfile,
123 );
124 0 ▶ push @provides, {module => $module, version => $details->{version} // 'undef'};
Condition0%0/2
$details->{"version"} // "undef"
l!l
00
MC/DC0%0/1
$details->{"version"} // "undef"
$details->{"version"}
Truth table0%0/2
A
A= $details->{"version"}
Aresult
11
00
125 }
126 }
127
128 0 $c->model('CMI')->writelist;
129 0 $c->model('CMI')->inject;
130
131 0 $self->status_created(
132 $c,
133 location => $c->uri_for($self->action_for('mirror'), $file),
134 entity => {
135 file => $c->req->upload('file')->filename,
136 provides => \@provides,
137 }
138 );
139 }
140 } else {
141 0 $self->status_bad_request(
142 $c,
143 message => "File $file is already in the repository",
144 );
145 }
146 }
147  
148  
149 =head2 mirror
150  
151 /api/1.0/mirror/File-Name-1.0.tar.gz
152  
153 =cut
154  
155 1 ▶sub mirror :Local :Args(1) :ActionClass('REST') {}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine mirror: not called CC=1 SCAR=0.0
Pod documented
156  
157 =head3 GET
158  
159 Returns a list of modules provided by the file, and the CPAN-style path to
160 download the file from the mirror (e.g. F<L/LO/LOCAL/File-Name-1.0.tar.gz>).
161  
162 {
163 "provides": {
164 "CPAN::Mini::Inject::Config": {
165 "version":"0.28"
166 },
167 "CPAN::Mini::Inject": {
168 "version":"0.28"
169 }
170 },
171 "file": "CPAN-Mini-Inject-0.28.tar.gz",
172 "path": "L/LO/LOCAL/CPAN-Mini-Inject-0.28.tar.gz"
173 }
174  
175 If the file has not been added to the mirror, status 404 is returned with an
176 appropriate error message:
177  
178 {
179 "error": "File My-Module-1.04.tar.gz does not exist"
180 }
181  
182 =cut
183  
184 0 ▶sub mirror_GET {
Subroutine mirror_GET: not called CC=4 SCAR=30.0
Pod undocumented
185 0 my ($self, $c, $file) = @_;
186
187 0 $c->model('CMI')->readlist;
188 0 ▶ if ($c->model('CMI')->{modulelist} && (my @modules = grep {m!L/LO/LOCAL/$file!} @{$c->model('CMI')->{modulelist}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->model('CMI')->{'modulelist'} and my(@modules) = grep({m[L/LO/LOCAL/$file];} @{$c->model('CMI')->{'modulelist'};})) { } 0 0 0
Condition0%0/3
$c->model("CMI")->{"modulelist"} and my(@modules) = grep({m[L/LO/LOCAL/$file];} @{$c->model("CMI")->{"modulelist"};})
!ll&&!rl&&r
000
MC/DC0%0/2
$c->model("CMI")->{"modulelist"} and my(@modules) = grep({m[L/LO/LOCAL/$file];} @{$c->model("CMI")->{"modulelist"};})
$c->model("CMI")->{"modulelist"} my(@modules) = grep({m[L/LO/LOCAL/$file];} @{$c->model("CMI")->{"modulelist"};})
Truth table0%0/3
A and B
A= $c->model("CMI")->{"modulelist"}B= my(@modules) = grep({m[L/LO/LOCAL/$file];} @{$c->model("CMI")->{"modulelist"};})
ABresult
0X0
100
111
189 0 my %response = (
190 file => $file,
191 path => "L/LO/LOCAL/$file",
192 );
193
194 0 foreach (@modules) {
195 0 my ($name, $version, $file) = split;
196 0 $response{provides}{$name}{version} = $version;
197 }
198
199 $self->status_ok(
200 0 $c,
201 entity => \%response
202 );
203 } else {
204 0 $self->status_not_found(
205 $c,
206 message => "File $file does not exist",
207 );
208 }
209 }
210  
211  
212 =head2 all_files
213  
214 =cut
215  
216 1 ▶sub all_files :Local :Args(0) :ActionClass('REST') {}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine all_files: not called CC=1 SCAR=0.0
Pod documented
217  
218 =head3 GET
219  
220 Returns a list of all files that have been added to the mirror.
221  
222 {
223 "files": [
224 "CPAN-Mini-Inject-0.28.tar.gz",
225 "My-Private-Module-0.02.tar.gz"
226 ]
227 }
228  
229 If no files have been added, status 204 (No Content) is returned.
230  
231 =cut
232  
233 0 ▶sub all_files_GET {
Subroutine all_files_GET: not called CC=3 SCAR=24.8
Pod undocumented
234 0 my ($self, $c) = @_;
235
236 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 0 $c->model('CMI')->readlist;
238 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
239 0 $c->log->debug("Could not read the modulelist: $_");
240 0 $self->status_no_content;
241 0 $c->detach;
242 0 };
243
244 0 ▶ if ($c->model('CMI')->{modulelist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->model('CMI')->{'modulelist'}) { } 0 0 0
245 0 $c->log->debug('Got it');
246 0 ▶ my @files = map {m!L/LO/LOCAL/(.*)! && $1}
Condition0%0/3
m[L/LO/LOCAL/(.*)] && $1
!ll&&!rl&&r
000
MC/DC0%0/2
m[L/LO/LOCAL/(.*)] && $1
m[L/LO/LOCAL/(.*)] $1
Truth table0%0/3
A && B
A= m[L/LO/LOCAL/(.*)]B= $1
ABresult
0X0
100
111
247 0 grep {m!L/LO/LOCAL/!} @{$c->model('CMI')->{modulelist}};
248 0 @files = uniq @files;
249 0 @files = sort @files;
250
251 0 $self->status_ok(
252 $c,
253 entity => {
254 files => \@files
255 }
256 );
257 } else {
258 0 $c->log->debug('No modulelist there');
259 0 $self->status_no_content($c);
260 }
261 }
262  
263  
264 =head2 dist
265  
266 /api/1.0/dist/Distribution-Name
267  
268 =cut
269  
270 1 ▶sub dist :Local :Args(1) :ActionClass('REST') {}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine dist: not called CC=1 SCAR=0.0
Pod documented
271  
272 =head3 GET
273  
274 Returns a list of files, from both the repository and the mirror, which match
275 the specified distibution name.
276  
277 {
278 "repository": [
279 "hello-0.01.tar.gz",
280 "hello-0.02.tar.gz",
281 "hello-0.03.tar.gz"
282 ],
283 "mirror": [
284 "hello-0.02.tar.gz"
285 ]
286 }
287  
288 If the distribution cannot be found, status 404 is returned with an appropriate
289 error message:
290  
291 {
292 "error": "Cannot find dist My-Private-Module"
293 }
294  
295 =cut
296  
297 0 ▶sub dist_GET {
Subroutine dist_GET: not called CC=4 SCAR=30.0
Pod undocumented
298 0 my ($self, $c, $dist) = @_;
299  
300 0 $c->model('CMI')->readlist;
301 0 ▶ if ($c->model('CMI')->{modulelist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->model("CMI")->{"modulelist"}) 0 0 0
302 0 ▶ if (my @mirror_files = map {m!L/LO/LOCAL/(.*)! && $1}
Branch: 1 branch
BranchTrueFalseTotal
if (my(@mirror_files) = map({m[L/LO/LOCAL/(.*)] and $1;} grep({m[L/LO/LOCAL/$dist-[0-9._]+(\.tar|tar\.gz|tgz|zip)];} @{$c->model("CMI")->{"modulelist"};}))) 0 0 0
Condition0%0/3
m[L/LO/LOCAL/(.*)] && $1
!ll&&!rl&&r
000
MC/DC0%0/2
m[L/LO/LOCAL/(.*)] && $1
m[L/LO/LOCAL/(.*)] $1
Truth table0%0/3
A && B
A= m[L/LO/LOCAL/(.*)]B= $1
ABresult
0X0
100
111
303 0 grep {m!L/LO/LOCAL/$dist-[0-9._]+(\.tar|tar\.gz|tgz|zip)!} @{$c->model('CMI')->{modulelist}}) {
304 0 @mirror_files = uniq @mirror_files;
305 0 @mirror_files = sort @mirror_files;
306
307 0 my $repository_dir = catdir($c->model('CMI')->config->get('repository'), 'authors/id/L/LO/LOCAL');
308 0 my @repository_files = map {basename($_)} File::Find::Rule->file->name(qr/$dist-[0-9._]+(\.tar|tar\.gz|tgz|zip)/)->in($repository_dir);
309
310 0 $self->status_ok(
311 $c,
312 entity => {
313 repository => \@repository_files,
314 mirror => \@mirror_files
315 }
316 );
317
318 0 $c->detach;
319 }
320 }
321
322 $self->status_not_found(
323 0 $c,
324 message => "Cannot find dist $dist"
325 );
326 }
327  
328  
329 =head2 all_dists
330  
331 /api/1.0/all_dists
332  
333 =cut
334  
335 1 ▶sub all_dists :Local :Args(0) :ActionClass('REST') {}
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine all_dists: not called CC=1 SCAR=0.0
Pod documented
336  
337 =head3 GET
338  
339 Returns a list of all distributions that have been added to the mirror.
340  
341 {
342 "dists": [
343 "CPAN-Mini-Inject",
344 "My-Private-Module"
345 ]
346 }
347  
348 If no distributions have been added, status 204 (No Content) is returned.
349  
350 =cut
351  
352 0 ▶sub all_dists_GET {
Subroutine all_dists_GET: not called CC=3 SCAR=24.8
Pod undocumented
353 0 my ($self, $c) = @_;
354
355 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 0 $c->model('CMI')->readlist;
357 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
358 0 $c->log->debug("Could not read the modulelist: $_");
359 0 $self->status_no_content;
360 0 $c->detach;
361 0 };
362
363 0 ▶ if ($c->model('CMI')->{modulelist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->model('CMI')->{'modulelist'}) { } 0 0 0
364 0 ▶ my @dists = map {m!L/LO/LOCAL/(.*)-[0-9._]+\.tar|tar\.gz|tgz|zip! && $1}
Condition0%0/3
m[L/LO/LOCAL/(.*)-[0-9._]+\.tar|tar\.gz|tgz|zip] && $1
!ll&&!rl&&r
000
MC/DC0%0/2
m[L/LO/LOCAL/(.*)-[0-9._]+\.tar|tar\.gz|tgz|zip] && $1
m[L/LO/LOCAL/(.*)-[0-9._]+\.tar|tar\.gz|tgz|zip] $1
Truth table0%0/3
A && B
A= m[L/LO/LOCAL/(.*)-[0-9._]+\.tar|tar\.gz|tgz|zip]B= $1
ABresult
0X0
100
111
365 0 grep {m!L/LO/LOCAL/!} @{$c->model('CMI')->{modulelist}};
366 0 @dists = uniq @dists;
367 0 @dists = sort @dists;
368
369 0 $self->status_ok(
370 $c,
371 entity => {
372 dists => \@dists
373 }
374 );
375 } else {
376 0 $c->log->debug('Nothing in the modulelist');
377 0 $self->status_no_content($c);
378 }
379 }
380  
381  
382 #-------------------------------------------------------------------------------
383  
384 0 ▶sub _load_meta {
Subroutine _load_meta: not called CC=3 SCAR=24.8
385 0 my $filename = shift;
386 0 my ($vol, $dir, $file) = splitpath($filename);
387 0 my $archive = Archive::Extract->new(archive => $filename);
388 0 $archive->extract(to => "$vol/$dir");
389
390 0 ▶ if (my @meta = File::Find::Rule->file->name('META.json')->in("$vol/$dir")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@meta) = "File::Find::Rule"->file->name("META.json")->in("$vol/$dir")) 0 0 0
391 0 return Parse::CPAN::Meta->load_file(shift @meta);
392 }
393  
394 0 ▶ if (my @meta = File::Find::Rule->file->name('META.yml')->in("$vol/$dir")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@meta) = "File::Find::Rule"->file->name("META.yml")->in("$vol/$dir")) 0 0 0
395 0 return Parse::CPAN::Meta->load_file(shift @meta);
396 }
397 }
398  
399  
400 #-------------------------------------------------------------------------------
401  
402 =head1 AUTHOR
403  
404 Jon Allen (JJ) <jj@jonallen.info>
405  
406 =head1 LICENSE
407  
408 This library is free software. You can redistribute it and/or modify
409 it under the same terms as Perl itself.
410  
411 =cut
412  
413 1 __PACKAGE__->meta->make_immutable;
414  
415 1 1;
↑ Summary script/cpan_mini_inject_rest_cgi.pl »