App/lcpan/Cmd/copy_script.pm

filter: Statementstmt 38.7% 19 / 49 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 32.4% 24 / 74 CC 10 SCAR 43.7
CC
10
Cov
29%
handle_cmd
110.0
BEGIN
1.0
BEGIN
1.0
CRAP 78.9
« App/lcpan/Cmd/whatsnew.pm ↑ Summary App/lcpan/Cmd/rdeps_scripts.pm »
1 package App::lcpan::Cmd::copy_script;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require App::lcpan;
8  
9 1 ▶use Perinci::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 1 our $DATE = '2023-09-26'; # DATE
13 1 our $DIST = 'App-lcpan'; # DIST
14 1 our $VERSION = '1.074'; # VERSION
15  
16 1 our %SPEC;
17  
18 1 $SPEC{'handle_cmd'} = {
19 v => 1.1,
20 summary => "Copy a script's latest release file to current directory",
21 args => {
22 %App::lcpan::common_args,
23 %App::lcpan::script_args,
24 %App::lcpan::overwrite_args,
25 %App::lcpan::all_args,
26 },
27 tags => ['write-to-fs'],
28 };
29 0 ▶sub handle_cmd {
Subroutine handle_cmd: not called CC=10 SCAR=47.0
Pod documented
30 0 require File::Copy;
31  
32 0 my %args = @_;
33  
34 0 my $state = App::lcpan::_init(\%args, 'ro');
35 0 my $dbh = $state->{dbh};
36  
37 0 my $script = $args{script};
38  
39 0 my $sth = $dbh->prepare("SELECT
40 script.name script,
41 file.cpanid author,
42 file.name release
43 FROM script
44 LEFT JOIN file ON script.file_id=file.id
45 LEFT JOIN module ON file.id=module.file_id
46 WHERE script.name=?
47 GROUP BY file.id
48 ORDER BY module.version_numified DESC");
49  
50 0 $sth->execute($script);
51  
52 0 my @srcpaths;
53 my @targetpaths;
54 0 my %mem;
55 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
56 0 ▶ unless ($args{all}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"all"}) 0 0 0
57 0 ▶ next if $mem{$row->{script}}++;
Branch: 1 branch
BranchTrueFalseTotal
if $mem{$row->{"script"}}++ 0 0 0
58 }
59 push @srcpaths, App::lcpan::_fullpath(
60 0 $row->{release}, $state->{cpan}, $row->{author});
61 0 push @targetpaths, $row->{release};
62 }
63  
64 0 ▶ return [404, "No release for script '$script'"] unless @srcpaths;
Branch: 1 branch
BranchTrueFalseTotal
unless @srcpaths 0 0 0
65  
66 0 my $envres = envresmulti();
67 0 for my $i (0..$#srcpaths) {
68 0 my $srcpath = $srcpaths[$i];
69 0 my $targetpath = $targetpaths[$i];
70 0 ▶ (-f $srcpath) or do {
Branch: 1 branch
BranchTrueFalseTotal
-f $srcpath or do { $envres->add_result(404, "File not found: $srcpath", {"item_id", $srcpath}); next } 0 0 0
71 0 $envres->add_result(
72 404, "File not found: $srcpath",
73 {item_id => $srcpath},
74 );
75 0 next;
76 };
77 0 ▶ if ((-f $targetpath) && !$args{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $targetpath and not $args{"overwrite"}) 0 0 0
Condition0%0/3
-f $targetpath and not $args{"overwrite"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $targetpath and not $args{"overwrite"}
-f $targetpath not $args{"overwrite"}
Truth table0%0/3
A and not(B)
A= -f $targetpathB= not $args{"overwrite"}
ABresult
0X0
100
111
78 0 $envres->add_result(
79 412, "Refusing to overwrite existing file '$targetpath'",
80 {item_id => $srcpath},
81 );
82 0 next;
83 }
84 0 ▶ File::Copy::syscopy($srcpath, $targetpath) or do {
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::syscopy($srcpath, $targetpath) or do { $envres->add_result(500, "Can't copy '${srcpath}' to '${targetpath}': $!", {"item_id", $srcpath}); next } 0 0 0
85 0 $envres->add_result(
86 500, "Can't copy '$srcpath' to '$targetpath': $!",
87 {item_id => $srcpath},
88 );
89 0 next;
90 };
91 0 $envres->add_result(
92 200, "OK",
93 {item_id => $srcpath},
94 );
95 }
96 0 $envres->as_struct;
97 }
98  
99 1 1;
100 # ABSTRACT: Copy a script's latest release file to current directory
101  
102 __END__
« App/lcpan/Cmd/whatsnew.pm ↑ Summary App/lcpan/Cmd/rdeps_scripts.pm »