blib/lib/App/MTPUtils.pm

filter: Statementstmt 18.9% 22 / 116 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 40.0% 4 / 10 Podpod 100% 3 / 3 totaltotal 14.5% 29 / 199 CC 38 SCAR 51.9
CC
38
Cov
12%
list_files
380.0
_parse_mtp_files_output
90.0
get_files
42.0
CRAP 179.0
↑ Summary
1 package App::MTPUtils;
2  
3 1 our $DATE = '2017-07-10'; # DATE
4 1 our $VERSION = '0.04'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our %SPEC;
12  
13 1 my %arg_files = (
14 files => {
15 summary => 'Filenames/IDs/wildcards',
16 'summary.alt.plurality.singular' => 'Filename/ID/wildcard',
17 'x.name.is_plural' => 1,
18 schema => ['array*', of=>'str*', min_len=>1],
19 req => 1,
20 pos => 0,
21 greedy => 1,
22 element_completion => \&_complete_filename_or_id,
23 },
24 );
25  
26 1 $SPEC{':package'} = {
27 v => 1.1,
28 summary => 'CLI utilities related to MTP (Media Transfer Protocol)',
29 };
30  
31 1 my $out_name = "mtp-files.out";
32  
33 1 my $err_no_mtp_files_out = [412, "No $out_name present, please create it first using 'mtp-files > $out_name'"];
34  
35 0 ▶sub _parse_mtp_files_output {
Subroutine _parse_mtp_files_output: not called CC=9 SCAR=45.0
36 0 ▶ (-f $out_name) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
-f $out_name or return undef 0 0 0
37 0 ▶ open my($fh), "<", $out_name or die "Can't open '$out_name': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $out_name or die "Can't open '${out_name}': $!" 0 0 0
38 0 my %files_by_id; # key: id, value: {name=>..., size=>..., pid=>...}
39 my %files_by_name; # key: name, value: [id, ...]
40 0 my $cur_file;
41 0 ▶ my $code_add_file = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
42 0 ▶ my $file = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or return 0 0 0
43 0 $files_by_id{$file->{id}} = $file;
44 0 ▶ $files_by_name{$file->{name}} //= [];
Condition0%0/2
$files_by_name{$file->{"name"}} //= []
l!l
00
MC/DC0%0/1
$files_by_name{$file->{"name"}} //= []
$files_by_name{$file->{"name"}}
Truth table0%0/2
A
A= $files_by_name{$file->{"name"}}
Aresult
11
00
45 0 push @{ $files_by_name{$file->{name}} }, $file->{id};
46 0 };
47 0 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
48 0 ▶ if ($line =~ /^File ID: (\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^File ID: (\d+)/) 0 0 0
49 0 $code_add_file->($cur_file);
50 0 $cur_file = {id=>$1};
51 0 next;
52 }
53 0 ▶ if (defined $cur_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cur_file) 0 0 0
54 0 ▶ if ($line =~ /^\s+Filename: (.+)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^\s+Filename: (.+)/) { } 0 0 0
elsif ($line =~ /^\s+File size (\d+)/) { } 0 0 0
elsif ($line =~ /^\s+Parent ID: (\d+)/) { } 0 0 0
55 0 $cur_file->{name} = $1;
56 } elsif ($line =~ /^\s+File size (\d+)/) {
57 0 $cur_file->{size} = $1;
58 } elsif ($line =~ /^\s+Parent ID: (\d+)/) {
59 0 $cur_file->{pid} = $1;
60 }
61 }
62 }
63 0 $code_add_file->($cur_file);
64 #use DD; dd \%files_by_id; dd \%files_by_name;
65 0 return [\%files_by_id, \%files_by_name];
66 }
67  
68 0 ▶sub _complete_filename_or_id {
Subroutine _complete_filename_or_id: not called CC=2 SCAR=17.9
69 0 require Complete::Util;
70 0 my %args = @_;
71  
72 0 ▶ my $parse_res = _parse_mtp_files_output() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $parse_res = _parse_mtp_files_output() or return undef 0 0 0
73  
74 0 my ($files_by_id, $files_by_name) = @$parse_res;
75  
76 0 Complete::Util::complete_array_elem(
77 %args,
78 array => [keys(%$files_by_id), keys(%$files_by_name)],
79 );
80 }
81  
82 $SPEC{list_files} = {
83 1 v => 1.1,
84 summary => 'List files contained in mtp-files.out',
85 description => <<'_',
86  
87 This routine will present information in `mtp-files.out` in a more readable way,
88 like the Unix `ls` command.
89  
90 To use this routine, you must already run `mtp-files` and save its output in
91 `mtp-files.out` file, e.g.:
92  
93 % mtp-files > mtp-files.out
94  
95 _
96 args => {
97 queries => {
98 summary => 'Filenames/wildcards',
99 'summary.alt.plurality.singular' => 'Filename/wildcard',
100 'x.name.is_plural' => 1,
101 schema => ['array*', of=>'str*'],
102 pos => 0,
103 greedy => 1,
104 element_completion => \&_complete_filename_or_id,
105 },
106 detail => {
107 schema => 'bool',
108 cmdline_aliases => {l=>{}},
109 },
110 },
111 };
112 0 ▶sub list_files {
Subroutine list_files: not called CC=19 SCAR=59.4
Pod documented
113 0 require Regexp::Wildcards;
114 0 require String::Wildcard::Bash;
115  
116 0 my %args = @_;
117 0 ▶ my $qq = $args{queries} // [];
Condition0%0/2
$args{"queries"} // []
l!l
00
MC/DC0%0/1
$args{"queries"} // []
$args{"queries"}
Truth table0%0/2
A
A= $args{"queries"}
Aresult
11
00
118  
119 0 ▶ my $parse_res = _parse_mtp_files_output()
Branch: 1 branch
BranchTrueFalseTotal
my $parse_res = _parse_mtp_files_output() or return $err_no_mtp_files_out 0 0 0
120 or return $err_no_mtp_files_out;
121 0 my ($files_by_id, $files_by_name) = @$parse_res;
122  
123 # convert wildcards to regexes
124 0 $qq = [@$qq];
125 0 for (@$qq) {
126 0 ▶ next unless String::Wildcard::Bash::contains_wildcard($_);
Branch: 1 branch
BranchTrueFalseTotal
unless String::Wildcard::Bash::contains_wildcard($_) 0 0 0
127 0 my $re = Regexp::Wildcards->new(type=>'unix')->convert($_);
128 0 $re = qr/\A($re)\z/;
129 0 $_ = $re;
130 }
131  
132 0 my @res;
133 my %resmeta;
134  
135 0 ▶ if ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"detail"}) 0 0 0
136 0 $resmeta{'table.fields'} = [qw/name id pid size/];
137 }
138  
139 # XXX report error on non-matching query
140 0 my %seen_ids;
141 0 for my $name (sort keys %$files_by_name) {
142 0 my $ids;
143 0 ▶ if (@$qq) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$qq) { } 0 0 0
144 0 for my $q (@$qq) {
145 0 ▶ if (ref($q) eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $q eq 'Regexp') { } 0 0 0
elsif ($q =~ /\A\d+\z/) { } 0 0 0
146 0 ▶ if ($name =~ $q) { $ids = $files_by_name->{$name}; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /$q/) 0 0 0
147 } elsif ($q =~ /\A\d+\z/) {
148 0 ▶ if ($files_by_id->{$q} && !$seen_ids{$q}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files_by_id->{$q} and not $seen_ids{$q}) 0 0 0
Condition0%0/3
$files_by_id->{$q} and not $seen_ids{$q}
!ll&&!rl&&r
000
MC/DC0%0/2
$files_by_id->{$q} and not $seen_ids{$q}
$files_by_id->{$q} not $seen_ids{$q}
Truth table0%0/3
A and not(B)
A= $files_by_id->{$q}B= not $seen_ids{$q}
ABresult
0X0
100
111
149 0 $ids = [$q];
150 }
151 } else {
152 0 ▶ if ($name eq $q) { $ids = $files_by_name->{$name}; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $q) 0 0 0
153 }
154 }
155 } else {
156 0 $ids = $files_by_name->{$name};
157 }
158 0 ▶ next unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
159 0 for my $id (@$ids) {
160 0 ▶ next if $seen_ids{$id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_ids{$id}++ 0 0 0
161 0 my $rec = $files_by_id->{$id};
162 0 ▶ if ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'detail'}) { } 0 0 0
163 0 push @res, $rec;
164 } else {
165 0 push @res, $rec->{name};
166 }
167 }
168 }
169  
170 0 [200, "OK", \@res, \%resmeta];
171 }
172  
173 $SPEC{get_files} = {
174 1 v => 1.1,
175 summary => 'Get multiple files from MTP (wrapper for mtp-getfile)',
176 description => <<'_',
177  
178 This routine is a thin wrapper for `mtp-file` command from `mtp-tools`.
179  
180 To use this routine, you must already run `mtp-files` and save its output in
181 `mtp-files.out` file, e.g.:
182  
183 % mtp-files > mtp-files.out
184  
185 This file is used for tab completion as well as getting filename/ID when only
186 one is specified. This makes using `mtp-getfile` less painful.
187  
188 _
189 args => {
190 %arg_files,
191 overwrite => {
192 schema => 'bool',
193 cmdline_aliases => {O=>{}},
194 },
195 },
196 deps => {
197 prog => 'mtp-getfile',
198 },
199 };
200 0 ▶sub get_files {
Subroutine get_files: not called CC=6 SCAR=37.4
Pod documented
201 0 require IPC::System::Options;
202  
203 0 my %args = @_;
204  
205 0 my $res = list_files(queries=>$args{files}, detail=>1);
206 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
207  
208 0 ▶ return [412, "No matching files to get"] unless @{$res->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$res->[2];} 0 0 0
209  
210 0 my $num_files = @{ $res->[2] };
211 0 for my $i (1..$num_files) {
212 0 my $file = $res->[2][$i-1];
213 log_info("[%d/%d] Getting file '%s' ...",
214 0 $i, $num_files, $file->{name});
215 0 ▶ if ((-f $file->{name}) && !$args{overwrite}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file->{"name"} and not $args{"overwrite"}) 0 0 0
Condition0%0/3
-f $file->{"name"} and not $args{"overwrite"}
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file->{"name"} and not $args{"overwrite"}
-f $file->{"name"} not $args{"overwrite"}
Truth table0%0/3
A and not(B)
A= -f $file->{"name"}B= not $args{"overwrite"}
ABresult
0X0
100
111
216 log_warn("Skipped file '%s' (%d) (already exists)",
217 0 $file->{name}, $file->{id});
218 0 next;
219 }
220 IPC::System::Options::system(
221 {log=>1, shell=>0},
222 "mtp-getfile",
223 $file->{id},
224 $file->{name},
225 0 );
226 }
227  
228 0 [200, "OK"];
229 }
230  
231 $SPEC{delete_files} = {
232 1 v => 1.1,
233 summary => 'Delete multiple files from MTP (wrapper for mtp-delfile)',
234 description => <<'_',
235  
236 This routine is a thin wrapper for `mtp-delfile` command from `mtp-tools`.
237  
238 To use this routine, you must already run `mtp-files` and save its output in
239 `mtp-files.out` file, e.g.:
240  
241 % mtp-files > mtp-files.out
242  
243 This file is used for tab completion as well as getting filename/ID when only
244 one is specified. This makes using `mtp-delfile` less painful.
245  
246 _
247 args => {
248 %arg_files,
249 },
250 deps => {
251 prog => 'mtp-delfile',
252 },
253 };
254 0 ▶sub delete_files {
Subroutine delete_files: not called CC=4 SCAR=30.0
Pod documented
255 0 require IPC::System::Options;
256  
257 0 my %args = @_;
258  
259 0 my $res = list_files(queries=>$args{files}, detail=>1);
260 0 ▶ return $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
261  
262 0 ▶ return [412, "No matching files to delete"] unless @{$res->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$res->[2];} 0 0 0
263  
264 0 my $num_files = @{ $res->[2] };
265 0 for my $i (1..$num_files) {
266 0 my $file = $res->[2][$i-1];
267 log_info("[%d/%d] deleting file '%s' ...",
268 0 $i, $num_files, $file->{name});
269 IPC::System::Options::system(
270 {log=>1, shell=>0},
271 "mtp-delfile", "-n", $file->{id},
272 0 );
273 }
274  
275 0 [200, "OK"];
276 }
277  
278 1 1;
279 # ABSTRACT: CLI utilities related to MTP (Media Transfer Protocol)
280  
281 __END__
↑ Summary