script/nam

filter: Statementstmt 95.3% 61 / 64 Branchbran 65.2% 30 / 46 Conditioncond 64.2% 9 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 79.2% 111 / 140 CC 1 SCAR 1.2
CC
1
Cov
81%
help
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/File/Namaste.pm ↑ Summary
1 #!/usr/local/bin/perl
2  
3 # xxx list dir_types
4  
5 # xxx convert all module version numbers
6 # use hg keywords (?)
7 # equivalent
8 # decimal zero-padded dotted-decimal
9 # ------- ----------- --------------
10 # 1.2 1.200 v1.200.0
11 # 1.02 1.020 v1.20.0
12 # 1.002 1.002 v1.2.0
13 # 1.0023 1.002300 v1.2.300
14 # 1.00203 1.002030 v1.2.30
15 # 1.002003 1.002003 v1.2.3
16  
17 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 23 my $VERSION = $File::Namaste::VERSION;
22  
23 23 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 # this :config allows -h24w80 for '‐h 24 ‐w 80', -vax for --vax or --Vax
25 23 ▶use Getopt::Long qw(:config bundling_override);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 23 ▶use File::Namaste ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 23 ▶use File::OM;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 23 my %opt = (
31 portable => undef, # NOT 0 because Namaste.pm does
32 # if defined(...) to check if $portable_default applies
33 help => 0,
34 man => 0,
35 version => 0,
36 directory => 0,
37 format => 0,
38 verbose => 0,
39 );
40  
41 # main
42 {
43 23 ▶ GetOptions(\%opt,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opt, "portable", "help|?", "man", "version", "format|m=s", "directory|d=s", "verbose|v") or pod2usage(1) 23 0 23
44 'portable',
45 'help|?',
46 'man',
47 'version',
48 'format|m=s',
49 'directory|d=s',
50 'verbose|v',
51 ) or pod2usage(1);
52  
53 help(), exit(0)
54 23 ▶ if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 23 23
55 pod2usage(-exitstatus => 0, -verbose => 2)
56 23 ▶ if $opt{man};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"man"} 0 23 23
57 print "$VERSION\n" and exit(0)
58 23 ▶ if $opt{version};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"version"} 0 23 23
Condition25%1/4
$opt{'version'} && (print("$VERSION\n") && exit 0)
!ll
230
print "$VERSION\n" and exit 0
!ll
00
MC/DC0%0/2
$opt{'version'} && (print("$VERSION\n") && exit 0)
$opt{'version'} print "$VERSION\n"
Truth table33%1/3
A && B
A= $opt{'version'}B= print "$VERSION\n"
ABresult
0X0
100
111
59 23 ▶ help(), exit(1)
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV > 0 0 23 23
60 unless @ARGV > 0;
61  
62 $opt{listformats} and
63 23 ▶ print(join("\n", File::OM::listformats()), "\n"),
Branch: 1 branch
BranchTrueFalseTotal
$opt{"listformats"} and print(join("\n", File::OM::listformats()), "\n"), exit 0 0 23 23
64 exit 0;
65  
66 23 ▶ my $format = $opt{format} || 'Plain'; # given format name
Condition100%2/2
$opt{'format'} || 'Plain'
l!l
320
MC/DC100%1/1
$opt{'format'} || 'Plain'
$opt{'format'}
Truth table100%2/2
A
A= $opt{'format'}
Aresult
11
00
67 23 my %om_opt = (
68 outhandle => *STDOUT,
69 );
70 23 ▶ my $om = File::OM->new($format, \%om_opt) or
Branch: 1 branch
BranchTrueFalseTotal
my $om = "File::OM"->new($format, \%om_opt) or pod2usage("$0: unknown format: $format") 23 0 23
71 pod2usage("$0: unknown format: $format");
72  
73 23 ▶ my $dir = $opt{directory} || "";
Condition50%1/2
$opt{'directory'} || ''
l!l
230
MC/DC0%0/1
$opt{'directory'} || ''
$opt{'directory'}
Truth table50%1/2
A
A= $opt{'directory'}
Aresult
11
00
74  
75 23 my $cmd = lc shift @ARGV;
76 23 my ($num, $fname, $fvalue, $msg, @nnv);
77 23 my $delete = 0;
78  
79 23 ▶ if ($cmd eq "add") { # easiest, since don't need to check existence
Branch: 4 branches
BranchTrueFalseTotal
if ($cmd eq 'add') { } 2 21 23
elsif ($delete = $cmd eq 'rm' or $cmd eq 'set') { } 7 14 21
elsif ($delete = $cmd eq 'rmall' or $cmd eq 'get') { } 13 1 14
elsif ($cmd eq 'elide') { } 1 0 1
80 2 ▶ @ARGV > 1 or
Branch: 1 branch
BranchTrueFalseTotal
@ARGV > 1 or pod2usage("${cmd}: needs at least two arguments") 2 0 2
81 pod2usage("$cmd: needs at least two arguments");
82 2 ▶ ($msg = nam_add($dir, $opt{portable}, @ARGV)) and
Branch: 1 branch
BranchTrueFalseTotal
$msg = nam_add($dir, $opt{"portable"}, @ARGV) and die $msg 0 2 2
83 die $msg;
84 2 exit 0;
85 }
86 ▶ elsif (($delete = $cmd eq "rm") || $cmd eq "set") {
Condition66%2/3
$delete = $cmd eq "rm" or $cmd eq "set"
l!l&&r!l&&!r
0714
MC/DC50%1/2
$delete = $cmd eq "rm" or $cmd eq "set"
$delete = $cmd eq "rm" $cmd eq "set"
Truth table66%2/3
A or B
A= $delete = $cmd eq "rm"B= $cmd eq "set"
ABresult
1X1
011
000
87  
88 7 ▶ pod2usage("$cmd what? (more arguments needed)")
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV == 0 0 7 7
89 if (@ARGV == 0);
90  
91 # same args for command as for nam_get()
92 7 ▶ @nnv = ($delete
Branch: 1 branch
BranchTrueFalseTotal
$delete ? : 0 7 7
93 ? nam_get($dir, @ARGV) # delete all args or
94 : nam_get($dir, $ARGV[0])); # just 1st for "set"
95 # @nnv may contain multiple tags (even for "set") to delete
96  
97 7 ▶ while (defined($num = shift @nnv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($num = shift @nnv)) 4 7 11
98 4 $fname = shift @nnv;
99 4 $fvalue = shift @nnv;
100 4 ▶ unlink($fname) or
Branch: 1 branch
BranchTrueFalseTotal
unlink $fname or print STDERR "${fname}: $!" 4 0 4
101 print STDERR "$fname: $!";
102 }
103 7 ▶ $delete and exit 0; # since we're done
Branch: 1 branch
BranchTrueFalseTotal
$delete and exit 0 0 7 7
104 # "set" case
105 7 ▶ ($msg = nam_add($dir, $opt{portable}, @ARGV)) and
Branch: 1 branch
BranchTrueFalseTotal
$msg = nam_add($dir, $opt{"portable"}, @ARGV) and die $msg 0 7 7
106 die $msg;
107 7 exit 0;
108 }
109 ▶ elsif (($delete = $cmd eq "rmall") || $cmd eq "get") {
Condition100%3/3
$delete = $cmd eq "rmall" or $cmd eq "get"
l!l&&r!l&&!r
2111
MC/DC100%2/2
$delete = $cmd eq "rmall" or $cmd eq "get"
$delete = $cmd eq "rmall" $cmd eq "get"
Truth table100%3/3
A or B
A= $delete = $cmd eq "rmall"B= $cmd eq "get"
ABresult
1X1
011
000
110  
111 # same args for command as for nam_get()
112 13 @nnv = nam_get($dir, @ARGV);
113 13 ▶ while (defined($num = shift @nnv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($num = shift @nnv)) 16 13 29
114 16 $fname = shift @nnv;
115 16 $fvalue = shift @nnv;
116 16 ▶ if ($delete) { # we're doing a delete
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) 4 12 16
117 4 ▶ unlink($fname) or
Branch: 1 branch
BranchTrueFalseTotal
unlink $fname or print STDERR "${fname}: $!" 4 0 4
118 print STDERR "$fname: $!";
119 4 next;
120 }
121 12 $om->elem(File::Namaste::num2label($num), $fvalue);
122 $opt{verbose} and
123 12 ▶ $om->elem("file", $fname, '#');
Branch: 1 branch
BranchTrueFalseTotal
$opt{"verbose"} and $om->elem("file", $fname, "#") 6 6 12
124 }
125 13 exit 0;
126 }
127 elsif ($cmd eq "elide") {
128 1 $om->elem('elide', nam_elide(@ARGV));
129 1 exit 0;
130 }
131 0 pod2usage("$cmd: unrecognized command");
132 }
133  
134 # One of the main benefits of this help text is to make it easy to find
135 # out what the numeric assignments are. Absent a command for tracking
136 # the current assignments, it is a priority to track them here.
137 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
138 0 print << 'EOI';
139  
140 nam - manage Namaste (NAMe-AS-TExt) tag files for describing directories
141  
142 Usage:
143 nam [options] set|add N string [[maxlen] ellipsis]
144 nam [options] get|rm [N ...]
145 nam [options] rmall
146 nam elide string [[maxlen] ellipsis]
147  
148 A Namaste tag file holds a single metadata value and its filename is derived
149 from that value. N specifies Dublin Core Kernel metadata elements, roughly:
150  
151 0 dir_type directory type (e.g., bagit_0.97)
152 1 who creator (or contributor or publisher)
153 2 what title (human-oriented name or identifier)
154 3 when date of creation or collection of content
155 4 where machine-oriented identifier
156  
157 Commands (set, add, get, rm) act as you would expect, but "rmall" removes
158 all tags and "elide" just returns an elided string, operating on no files.
159 Use the "-d directory" option for tag files not in the current directory.
160 See "nam --man" for full documentation.
161  
162 EOI
163 0 return 1;
164 }
165  
166 __END__
« lib/File/Namaste.pm ↑ Summary