blib/lib/App/DownloadsDirUtils.pm

filter: Statementstmt 65.2% 60 / 92 Branchbran 18.7% 3 / 16 Conditioncond 15.0% 3 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 87.5% 14 / 16 Podpod 100% 1 / 1 totaltotal 51.5% 81 / 157 CC 16 SCAR 34.5
CC
16
Cov
52%
__ANON__
92.4
__ANON__
6.8
list_downloads_dirs
4.3
CRAP 31.5
↑ Summary
1 package App::DownloadsDirUtils;
2  
3 1 ▶use 5.010001;
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 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Util::Sort;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Perinci::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Perinci::Sub::Util qw(gen_modified_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14 1 our $DATE = '2026-09-25'; # DATE
15 1 our $DIST = 'App-DownloadsDirUtils'; # DIST
16 1 our $VERSION = '0.009'; # VERSION
17  
18 1 our %SPEC;
19  
20 $SPEC{list_downloads_dirs} = {
21 1 v => 1.1,
22 summary => 'List downloads directories',
23 result_naked => 1,
24 };
25 6 ▶sub list_downloads_dirs {
Subroutine list_downloads_dirs: called CC=4 SCAR=14.6
Pod documented
26 6 require File::HomeDir;
27  
28 6 my @res;
29 my $home;
30  
31 # ~/Downloads - firefox, ...
32 {
33 6 ▶ $home //= File::HomeDir->my_home;
Condition33%1/3
$home //= "File::HomeDir"->my_home
l!l&&r!l&&!r
060
MC/DC0%0/2
$home //= "File::HomeDir"->my_home
$home "File::HomeDir"->my_home
Truth table33%1/3
A //= B
A= $homeB= "File::HomeDir"->my_home
ABresult
1X1
011
000
34 6 push @res, "$home/Downloads";
35 }
36  
37 # mldonkey
38 {
39 6 ▶ $home //= File::HomeDir->my_home;
Condition33%1/3
$home //= "File::HomeDir"->my_home
l!l&&r!l&&!r
600
MC/DC0%0/2
$home //= "File::HomeDir"->my_home
$home "File::HomeDir"->my_home
Truth table33%1/3
A //= B
A= $homeB= "File::HomeDir"->my_home
ABresult
1X1
011
000
40 6 push @res, "$home/.mldonkey/incoming/files";
41 }
42  
43 6 @res = grep {-d} @res;
44  
45 6 ▶ wantarray ? @res : \@res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
46 }
47  
48 1 for my $which (qw/foremost hindmost largest smallest newest oldest/) {
49 6 my $res;
50  
51 $res = gen_modified_sub(
52 summary => "Return the $which file(s) in the downloads directories",
53 description => <<"MARKDOWN",
54  
55 This is a thin wrapper for the <prog:$which> utility; the wrapper sets the
56 default for the directories to the downloads directories, as well as by default
57 excluding partial downloads (`*.part` files).
58  
59 MARKDOWN
60 output_name => __PACKAGE__ . "::${which}_download",
61 base_name => "File::Util::Sort::$which",
62 modify_args => {
63 6 ▶ dirs => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 6 my $arg_spec = shift;
65 6 $arg_spec->{default} = scalar list_downloads_dirs();
66 },
67 6 ▶ exclude_filename_pattern => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 6 my $arg_spec = shift;
69 6 $arg_spec->{default} = '/\.part\z/';
70 },
71 },
72 0 ▶ output_code => sub {
Subroutine __ANON__: not called CC=3 SCAR=19.2
73 1 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
74 0 my %args = @_;
75 0 ▶ $args{dirs} //= scalar list_downloads_dirs();
Condition0%0/2
$args{"dirs"} //= scalar list_downloads_dirs()
l!l
00
MC/DC0%0/1
$args{"dirs"} //= scalar list_downloads_dirs()
$args{"dirs"}
Truth table0%0/2
A
A= $args{"dirs"}
Aresult
11
00
76 0 ▶ $args{exclude_filename_pattern} //= qr/\.part\z/;
Condition0%0/2
$args{"exclude_filename_pattern"} //= qr/\.part\z/
l!l
00
MC/DC0%0/1
$args{"exclude_filename_pattern"} //= qr/\.part\z/
$args{"exclude_filename_pattern"}
Truth table0%0/2
A
A= $args{"exclude_filename_pattern"}
Aresult
11
00
77 0 &{"File::Util::Sort::$which"}(%args);
78 },
79 6 );
80 6 ▶ die "Can't generate ${which}_download(): $res->[0] - $res->[1]"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 6 6
81 unless $res->[0] == 200;
82  
83 $res = gen_modified_sub(
84 summary => "Move the $which file(s) from the downloads directories to current directory",
85 description => <<"MARKDOWN",
86  
87 This is a thin wrapper for the <prog:${which}-download> utility; the wrapper
88 moves the files to current directory. It hopes to be a convenient helper to
89 organize your downloads.
90  
91 MARKDOWN
92 output_name => "mv_${which}_download_here",
93 base_name => "${which}_download",
94 add_args => {
95 to_dir => {
96 schema => 'dirname*',
97 default => '.',
98 },
99 overwrite => {
100 schema => 'true*',
101 cmdline_aliases => {O=>{}},
102 },
103 as => {
104 summary => 'Rename file',
105 schema => 'pathname::unix::basename*',
106 cmdline_aliases => {
107 rename=>{},
108 },
109 },
110 },
111 6 ▶ modify_meta => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
112 6 my $meta = shift;
113 6 ▶ $meta->{features} //= {};
Condition50%1/2
$meta->{"features"} //= {}
l!l
06
MC/DC0%0/1
$meta->{"features"} //= {}
$meta->{"features"}
Truth table50%1/2
A
A= $meta->{"features"}
Aresult
11
00
114 6 $meta->{features}{dry_run} = 1;
115 },
116 0 ▶ output_code => sub {
Subroutine __ANON__: not called CC=10 SCAR=45.3
117 1 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
118 0 require File::Basename;
119 0 require File::Copy::Recursive;
120  
121 0 my %args = @_;
122  
123 0 ▶ my $to_dir = delete($args{to_dir}) // '.';
Condition0%0/2
delete $args{"to_dir"} // "."
l!l
00
MC/DC0%0/1
delete $args{"to_dir"} // "."
delete $args{"to_dir"}
Truth table0%0/2
A
A= delete $args{"to_dir"}
Aresult
11
00
124  
125 0 my $res = &{"${which}_download"}(%args);
126 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
127 0 ▶ return [404, "No $which file(s) returned"] unless @{ $res->[2] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$res->[2];} 0 0 0
128  
129 0 my $envres = envresmulti();
130 0 my $i = 0;
131 0 for my $file (@{ $res->[2] }) {
132 0 $i++;
133 0 ▶ my $targetpath = $to_dir . '/' . ($args{as} // File::Basename::basename($file));
Condition0%0/3
$args{"as"} // File::Basename::basename($file)
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"as"} // File::Basename::basename($file)
$args{"as"} File::Basename::basename($file)
Truth table0%0/3
A // B
A= $args{"as"}B= File::Basename::basename($file)
ABresult
1X1
011
000
134 0 ▶ if (-e $targetpath && !$args{overwrite}) {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $targetpath and not $args{'overwrite'}) { } 0 0 0
elsif ($args{'-dry_run'}) { } 0 0 0
Condition0%0/3
-e $targetpath and not $args{"overwrite"}
!ll&&!rl&&r
000
MC/DC0%0/2
-e $targetpath and not $args{"overwrite"}
-e $targetpath not $args{"overwrite"}
Truth table0%0/3
A and not(B)
A= -e $targetpathB= not $args{"overwrite"}
ABresult
0X0
100
111
135 0 $envres->add_result(409, "File already exists '$targetpath', please specify -O to overwrite", {item_id=>$file});
136 } elsif ($args{-dry_run}) {
137 0 log_info "DRY-RUN: [%d/%d] Moving %s to %s ...", $i, scalar(@{ $res->[2] }), $file, $targetpath;
138 0 $envres->add_result(200, "OK (dry-run)", {item_id=>$file});
139 } else {
140 0 log_info "[%d/%d] Moving %s to %s ...", $i, scalar(@{ $res->[2] }), $file, $targetpath;
141 0 my $ok = File::Copy::Recursive::rmove($file, $targetpath);
142 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 0 0
143 0 $envres->add_result(200, "OK", {item_id=>$file});
144 } else {
145 0 $envres->add_result(500, "Error: $!", {item_id=>$file});
146 }
147 }
148 }
149 0 $envres->as_struct;
150 },
151 6 );
152 6 ▶ die "Can't generate mv_${which}_download_here(): $res->[0] - $res->[1]"
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 6 6
153 unless $res->[0] == 200;
154 } # $which
155  
156 1 1;
157 # ABSTRACT: Utilities related to downloads directories
158  
159 __END__
↑ Summary