script/pt

filter: Statementstmt 88.5% 116 / 131 Branchbran 60.0% 24 / 40 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 76.1% 16 / 21 Podpod n/a totaltotal 77.1% 159 / 206 CC 22 SCAR 13.7
CC
22
Cov
79%
do_lstree
6.2
do_help
6.0
do_i2p
6.0
CRAP 3.9
« lib/File/Pairtree.pm ↑ Summary
1 #!/usr/local/bin/perl
2  
3 15 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # xxx support for shadow tree of deletions? pairtree_trash?
8  
9 15 my $VERSION = $File::Pairtree::VERSION;
10  
11 15 ▶use File::Pairtree ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use File::Value ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use File::Find; # for lstree, lsid
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use File::OM;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 15 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 # this :config allows -h24w80 for '‐h 24 ‐w 80', -vax for --vax or --Vax
19 15 ▶use Getopt::Long qw(:config bundling_override);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 15 my $dir; # global defining our directory, ends in "pairtree_root"
22  
23 15 my $R = $File::Pairtree::root;
24  
25 15 my $om; # output multiplexer routine shared among 'pt' routines
26 my %o; # global to communicate options to module
27  
28 15 my %opt = (
29 all => 0,
30 bud => 0,
31 directory => 0,
32 force => 0,
33 format => 0,
34 help => 0,
35 long => 0,
36 man => 0,
37 version => 0,
38 verbose => 0,
39 );
40  
41 # main
42 {
43 15 ▶ GetOptions(\%opt,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opt, "all|a", "bud|b=s", "directory|d=s", "force|f", "format|m=s", "help|h|?", "long|l", "man", "version", "verbose|v") or pod2usage("-exitstatus", 2, "-verbose", 1) 14 1 15
44 'all|a',
45 'bud|b=s', # xxx not implemented yet
46 'directory|d=s',
47 'force|f',
48 'format|m=s', # xxx not implemented yet
49 'help|h|?',
50 'long|l',
51 'man',
52 'version',
53 'verbose|v',
54 )
55 or pod2usage(-exitstatus => 2, -verbose => 1);
56  
57 14 ▶ $opt{help} and help(), exit(0);
Branch: 1 branch
BranchTrueFalseTotal
$opt{"help"} and help(), exit 0 0 14 14
58 14 ▶ $opt{man} and pod2usage(-exitstatus => 0, -verbose => 2);
Branch: 1 branch
BranchTrueFalseTotal
$opt{"man"} and pod2usage("-exitstatus", 0, "-verbose", 2) 0 14 14
59 14 ▶ $opt{version} and print("$VERSION\n"), exit(0);
Branch: 1 branch
BranchTrueFalseTotal
$opt{"version"} and print("$VERSION\n"), exit 0 0 14 14
60  
61 14 ▶ my $format = $opt{format} || 'Plain'; # given format name
Condition50%1/2
$opt{'format'} || 'Plain'
l!l
014
MC/DC0%0/1
$opt{'format'} || 'Plain'
$opt{'format'}
Truth table50%1/2
A
A= $opt{'format'}
Aresult
11
00
62 14 my %om_opt = (
63 outhandle => *STDOUT,
64 );
65 14 ▶ $om = File::OM->new($format, \%om_opt) or
Branch: 1 branch
BranchTrueFalseTotal
$om = "File::OM"->new($format, \%om_opt) or pod2usage("$0: unknown format: $format") 14 0 14
66 pod2usage("$0: unknown format: $format");
67  
68 14 $opt{bud} = 0; # xxx temporary kludge
69  
70 14 my $cmd = shift @ARGV;
71 14 ▶ defined($cmd) or help(), exit(1);
Branch: 1 branch
BranchTrueFalseTotal
defined $cmd or help(), exit 1 14 0 14
72 14 $cmd = "do_" . lc($cmd);
73 14 ▶ defined(&$cmd) or pod2usage("$0: unknown command: $cmd");
Branch: 1 branch
BranchTrueFalseTotal
defined &$cmd or pod2usage("$0: unknown command: $cmd") 14 0 14
74  
75 # Which pairtree?
76 #
77 14 ▶ $dir = $opt{directory} || ".";
Condition50%1/2
$opt{'directory'} || '.'
l!l
140
MC/DC0%0/1
$opt{'directory'} || '.'
$opt{'directory'}
Truth table50%1/2
A
A= $opt{'directory'}
Aresult
11
00
78 #$dir = fiso_dname($dir, $R);
79 #$dir = prep_file($dir, $R);
80  
81 15 ▶ no strict 'refs'; # permits the next call
Subroutine BEGIN: called CC=1 SCAR=0.0
82 #exit &$cmd(@ARGV);
83 14 my $ret = &$cmd(@ARGV);
84 #print "ret=$ret\n";
85 14 exit $ret;
86 }
87  
88 # Prepare base path with last component according to the table, normalizing
89 # multiple slashes between $base and $last. Useful to get tedious details
90 # right and when path may already have the last component on it (in which
91 # case we don't want it there twice). Removes final slashes from $last.
92 #
93 # $base $last Returns
94 # 1. / bar /bar
95 # 2. . bar bar
96 # 3. foo bar foo/bar
97 # 4. foo/ bar foo/bar
98 # 5. foo/bar bar foo/bar
99 # 6. bar bar bar
100 #
101 # Main use case, bar=pairtree_root
102 # xxx may not port to Windows due to use of explicit slashes (/)
103 # XXXXXX probably should use File::Spec
104 # xxx do some test cases for this
105 # xxx find a better name
106 #sub prep_file { my( $base, $last )=@_;
107 #
108 # $last =~ s{/*(.*)/*$}{$1}; # remove bounding slashes
109 # return "/$last" if $base =~ m{^/+$}; # case 1 eliminated
110 # $base =~ s{/+$}{}; # remove trailing slashes
111 # return "$last" if $base =~ m{^\./+$}; # case 2 eliminated
112 # return "$base/$last" if $base !~ m{$last$}; # cases 3-4 gone
113 # return "$last" if $base =~ m{^$last$}; # case 6 eliminated
114 # $base =~ s{/*$last$}{}; # remove $last and preceding slashes
115 # return "$base/$last"; # case 5 eliminated
116 #}
117  
118 #######################
119 #
120 # Command functions.
121 #
122  
123 0 ▶sub do_help { my( $topic )=@_;
Subroutine do_help: not called CC=2 SCAR=17.9
124  
125 0 ▶ $topic ||= "";
Condition0%0/2
$topic ||= ""
l!l
00
MC/DC0%0/1
$topic ||= ""
$topic
Truth table0%0/2
A
A= $topic
Aresult
11
00
126 0 pod2usage(-exitstatus => 0, -verbose => 2)
127 #return print "XXX place holder for help on $topic\n";
128 }
129  
130 4 ▶sub do_lsid { my( @nodes )=@_;
Subroutine do_lsid: called CC=4 SCAR=13.9
131  
132 # Don't allow empty node, especially for rmid, which could
133 # result in a disastrous rmtree against the entire pairtree.
134 #
135 4 ▶ scalar(@nodes) > 0 or pod2usage("$0: no id given");
Branch: 1 branch
BranchTrueFalseTotal
scalar @nodes > 0 or pod2usage("$0: no id given") 4 0 4
136  
137 4 my $exit_status = 0; # optimistic
138  
139 # set options so pt_lsid doesn't need to compute each time
140 4 $o{parent_dir} = fiso_uname(fiso_dname($dir, $R));
141 4 $o{prefix} = get_prefix( $o{parent_dir} );
142 4 $o{force} = $opt{force};
143  
144 4 my ($id, $ret);
145 4 for $id (@nodes) {
146 5 $ret = pt_lsid($dir, $id, \%o);
147 $ret or # if success
148 5 ▶ $om->elem($id, $o{bud}),
Branch: 1 branch
BranchTrueFalseTotal
$ret or $om->elem($id, $o{"bud"}), next 2 3 5
149 next;
150 2 $exit_status = $ret; # if we get here, we had trouble
151 2 $om->elem('error', "$id: $o{msg}");
152 }
153 4 return $exit_status;
154 }
155  
156 # XXX lsid should list (a) ppath, (b) bud, and (c) leaf
157 # id | pairpath_root/ab/c | abc | oxum ???
158 # pt_lsid to return ref to array representing a node, each of form
159 # (id | ppath | bud (enclosing dirname for leaf) | leaf oxum)
160 # or (id | "" | ... <no ppath>) # or (id | ppath | "" <no bud>>)
161  
162 4 ▶sub do_lstree { my( @trees )=@_;
Subroutine do_lstree: called CC=6 SCAR=18.2
163  
164 4 ▶ scalar(@trees) == 0 and
Branch: 1 branch
BranchTrueFalseTotal
scalar @trees == 0 and push @trees, $dir 4 0 4
165 push(@trees, $dir); # default if no args
166  
167 4 my $exit_status = 0; # optimistic
168  
169 4 my ($tree, $ret);
170 4 for $tree (@trees) {
171 4 ▶ if (! -e $tree) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $tree) 1 3 4
172 1 $exit_status = 1;
173 1 $om->elem('error', "$tree: no such file or directory");
174 1 next;
175 }
176 # set prefix based on $tree's parent (from fiso_uname)
177 3 $o{parent_dir} = fiso_uname(fiso_dname($tree, $R));
178 3 $o{prefix} = get_prefix( $o{parent_dir} );
179 3 $o{force} = $opt{force};
180 3 $o{long} = $opt{long};
181 3 $o{all} = $opt{all};
182 3 $o{om} = $om;
183  
184 $o{follow_fast} = 1 # xxx set with $opt?
185 3 ▶ unless defined $o{follow_fast};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o{"follow_fast"} 3 0 3
186 # Set follow_fast=1 to mean follow symlinks without rigorous
187 # checking (faster); it also means that (-X _) works from
188 # within &visit without doing an extra stat call, where -X
189 # is any file test operator and _ is the magic file handle.
190  
191 # optional 3rd arg is &visit function to use instead
192 3 $ret = pt_lstree($tree, \%o); # xxx set $o{summary} ?
193 3 ▶ $ret or # if success
Branch: 1 branch
BranchTrueFalseTotal
$ret or next 0 3 3
194 next;
195 0 $exit_status = $ret; # if we get here, we had trouble
196 0 $om->elem('error', "$tree: $o{msg}");
197 }
198 4 return $exit_status;
199 }
200  
201 3 ▶sub do_mkid { my( @nodes )=@_;
Subroutine do_mkid: called CC=4 SCAR=14.4
202  
203 # Don't allow empty node, especially for rmid, which could
204 # result in a disastrous rmtree against the entire pairtree.
205 #
206 3 ▶ scalar(@nodes) > 0 or pod2usage("$0: no id given");
Branch: 1 branch
BranchTrueFalseTotal
scalar @nodes > 0 or pod2usage("$0: no id given") 3 0 3
207  
208 3 my $exit_status = 0; # optimistic
209  
210 # set options so pt_mkid doesn't need to compute each time
211 3 $o{parent_dir} = fiso_uname(fiso_dname($dir, $R));
212 3 $o{prefix} = get_prefix( $o{parent_dir} );
213 3 $o{bud_style} = $opt{bud};
214 3 $o{force} = $opt{force};
215  
216 3 my ($id, $ret);
217 3 for $id (@nodes) {
218 ($ret = pt_mkid($dir, $id, \%o)) and
219 10 ▶ $om->elem($id, $o{bud}),
Branch: 1 branch
BranchTrueFalseTotal
$ret = pt_mkid($dir, $id, \%o) and $om->elem($id, $o{"bud"}), next 10 0 10
220 next;
221 0 $exit_status = 1; # if we get here, we had trouble
222 0 $om->elem('error', "$id: $o{msg}");
223 }
224 3 return $exit_status;
225 }
226  
227 1 ▶sub do_mktree { my( $tree, $prefix )=@_;
Subroutine do_mktree: called CC=5 SCAR=17.5
228  
229 1 ▶ $tree ||= $dir; # use default, but only if the
Condition33%1/3
$tree ||= $dir
l!l&&r!l&&!r
100
MC/DC0%0/2
$tree ||= $dir
$tree $dir
Truth table33%1/3
A ||= B
A= $treeB= $dir
ABresult
1X1
011
000
230 #$tree = prep_file($tree, $R) # tree wasn't given explicitly
231 1 ▶ $tree = fiso_dname($tree, $R) # tree wasn't given explicitly
Branch: 1 branch
BranchTrueFalseTotal
if $tree ne $dir 1 0 1
232 if ($tree ne $dir);
233
234 1 my $ret = pt_mktree($tree, $prefix, \%o);
235 1 ▶ $ret and # if call failed
Branch: 1 branch
BranchTrueFalseTotal
$ret and $om->elem("error", qq[${tree}: $o{"msg"}]), (return 1) 0 1 1
236 $om->elem("error", "$tree: $o{msg}"),
237 return 1;
238  
239 $opt{verbose} and
240 1 ▶ $om->elem("mktree", $tree);
Branch: 1 branch
BranchTrueFalseTotal
$opt{"verbose"} and $om->elem("mktree", $tree) 0 1 1
241 1 return 0; # success means exit status 0
242 }
243  
244 2 ▶sub do_rmid { my( @nodes )=@_;
Subroutine do_rmid: called CC=4 SCAR=13.9
245  
246 # Don't allow empty node, especially for rmid, which could
247 # result in a disastrous rmtree against the entire pairtree.
248 #
249 2 ▶ scalar(@nodes) > 0 or pod2usage("$0: no id given");
Branch: 1 branch
BranchTrueFalseTotal
scalar @nodes > 0 or pod2usage("$0: no id given") 2 0 2
250  
251 2 my $exit_status = 0; # optimistic
252  
253 # set options so pt_rmid doesn't need to compute each time
254 2 $o{parent_dir} = fiso_uname(fiso_dname($dir, $R));
255 2 $o{prefix} = get_prefix( $o{parent_dir} );
256 2 $o{force} = $opt{force};
257  
258 2 my ($id, $ret);
259 2 for $id (@nodes) {
260 2 $ret = pt_rmid($dir, $id, \%o);
261 $ret or # if success
262 2 ▶ $om->elem($id, $o{bud}),
Branch: 1 branch
BranchTrueFalseTotal
$ret or $om->elem($id, $o{"bud"}), next 1 1 2
263 next;
264 1 $exit_status = $ret; # if we get here, we had trouble
265 1 $om->elem('error', "$id: $o{msg}");
266 }
267 2 return $exit_status;
268 }
269  
270 0 ▶sub do_rmtree { my( $tree )=@_;
Subroutine do_rmtree: not called CC=1 SCAR=6.9
271  
272 0 print "XXX Not implemented yet; use 'rm -r'\n";
273 # XXX force use of tree arg
274 }
275  
276 0 ▶sub do_i2p { my( @ids )=@_;
Subroutine do_i2p: not called CC=2 SCAR=17.9
277  
278 0 print id2ppath($_), "\n" for (@ids);
279 }
280  
281 0 ▶sub do_p2i { my( @paths )=@_;
Subroutine do_p2i: not called CC=2 SCAR=17.9
282  
283 0 print ppath2id($_), "\n" for (@paths);
284 }
285  
286 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
287 0 print << 'EOI';
288  
289 pt - make, remove, and list pairtrees and pairtree buds
290  
291 pt [-d dir] mktree directory [prefix] # make a pairtree
292 pt [-l] [-d dir] [rmtree | lstree] [directory] ... # remove/list tree(s)
293 pt [-d dir] [mkid | rmid | lsid] id ... # manipulate bud(s)
294 pt [i2p | p2i] name ... # pure id/path mapping
295  
296 Pairtrees map identifiers to filesystem paths. Use sub-commands to create,
297 delete, modify, and report on them. Leaves are the user's concern, but with
298 "mkid" and an id, "pt" encloses every leaf in a bud directory using the full
299 id (with exceptions for short ids). See "pt --man" for more. Examples:
300  
301 $ pt mkid foo bar zafp # mkid with no tree first calls
302 ./pairtree_root/fo/o/foo # mktree if there's no pairtree, then
303 ./pairtree_root/ba/r/bar # creates each pairpath and bud (node)
304 ./pairtree_root/za/fp/zafp # directory at the pairpath's end
305 $ pt rmid bar # remove node identified by 'bar'
306 removed: bar | ./pairtree_root/ba/r/
307 $ touch ./pairtree_root/za/fp/zafp/{a,b,c} # create some leaf content
308 $ pt lstree -l # so we have something to report on
309 zafp 0.3 # after the bud name is the "oxum",
310 foo 0.0 # or num of octets and num of files
311 EOI
312 0 return 1;
313 }
314  
315 __END__
« lib/File/Pairtree.pm ↑ Summary