blib/lib/App/Open.pm

filter: Statementstmt 95.9% 71 / 74 Branchbran 83.3% 25 / 30 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 15 / 15 Podpod 100% 10 / 10 totaltotal 93.0% 134 / 144 CC 22 SCAR 16.6
CC
22
Cov
92%
lookup_program
10.0
new
6.0
execute_program
6.0
CRAP 5.3
↑ Summary blib/lib/App/Open/Backend/YAML.pm »
1 #===============================================================================
2 #
3 # FILE: Open.pm
4 #
5 # DESCRIPTION: App::Open, Command-Line interface library
6 #
7 # FILES: ---
8 # BUGS: ---
9 # NOTES: ---
10 # AUTHOR: Erik Hollensbe (), <erik@hollensbe.org>
11 # COMPANY:
12 # VERSION: 1.0
13 # CREATED: 06/02/2008 01:50:56 AM PDT
14 # REVISION: ---
15 #===============================================================================
16  
17 package App::Open;
18  
19 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 10 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 our $VERSION = version::qv("0.0.4");
24  
25 10 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =head1 NAME
29  
30 App::Open - Library to drive the 'openit' command line tool
31  
32 =head1 USING
33  
34 If you are just looking to use the `openit` command and learn how to configure
35 it, please see App::Open::Using, which addresses this issue.
36  
37 =head1 SYNOPSIS
38  
39 See the `openit` script.
40  
41 =head1 WARNING
42  
43 While this probably can be re-used, it has a specific function to support a
44 specific tool. Use this at your own risk and expect breakage on upgrades.
45 Expect side-effects, even if the author himself detests them.
46  
47 =head1 METHODS
48  
49 =over 4
50  
51 =item new( $config, $filename )
52  
53 `$config` is a App::Open::Config object. `$filename` is a filename or URL which
54 `openit` will attempt to locate a program to launch for it.
55  
56 =cut
57  
58 24 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
59 24 my ( $class, $config, $filename ) = @_;
60
61 24 my $self = bless { filename => $filename, config => $config }, $class;
62  
63 24 $self->parse_filename;
64  
65 24 ▶ die "MISSING_ARGUMENT" unless ( $config && $self->{filename} );
Branch: 1 branch
BranchTrueFalseTotal
unless $config and $self->{"filename"} 3 21 24
Condition100%3/3
$config and $self->{"filename"}
!ll&&!rl&&r
2121
MC/DC100%2/2
$config and $self->{"filename"}
$config $self->{"filename"}
Truth table100%3/3
A and B
A= $configB= $self->{"filename"}
ABresult
0X0
100
111
66 21 ▶ die "INVALID_ARGUMENT" unless ( $config->isa('App::Open::Config') );
Branch: 1 branch
BranchTrueFalseTotal
unless $config->isa("App::Open::Config") 1 20 21
67 20 ▶ die "FILE_NOT_FOUND" unless ( $self->is_url || -e $self->{filename} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_url or -e $self->{"filename"} 1 19 20
Condition100%3/3
$self->is_url or -e $self->{"filename"}
l!l&&r!l&&!r
3161
MC/DC100%2/2
$self->is_url or -e $self->{"filename"}
$self->is_url -e $self->{"filename"}
Truth table100%3/3
A or B
A= $self->is_urlB= -e $self->{"filename"}
ABresult
1X1
011
000
68  
69 19 $self->config->load_backends;
70  
71 19 return $self;
72 }
73  
74 =item filename
75  
76 Produces the stored filename.
77  
78 =cut
79  
80 56 ▶sub filename { $_[0]->{filename} }
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
81  
82 =item config
83  
84 Produces the App::Open::Config object
85  
86 =cut
87  
88 46 ▶sub config { $_[0]->{config} }
Subroutine config: called CC=1 SCAR=0.0
Pod documented
89  
90 =item is_url
91  
92 Predicate to indicate whether the `filename` is a URL or not. This is a bit
93 distracting as `file` URLs are not indicated by this method. I'll probably get
94 to fixing this shortly after I become a Nobel Laureate.
95  
96 =cut
97  
98 36 ▶sub is_url { $_[0]->{is_url} }
Subroutine is_url: called CC=1 SCAR=0.0
Pod documented
99  
100 =item scheme
101  
102 In the event the `filename` is a URL, return the URL scheme (http, ftp, etc)
103  
104 =cut
105  
106 4 ▶sub scheme { $_[0]->{scheme} }
Subroutine scheme: called CC=1 SCAR=0.0
Pod documented
107  
108 =item parse_filename
109  
110 Figure out if the file is a local file or not. `file` URLs are massaged into
111 filenames, see is_url().
112  
113 =cut
114  
115 24 ▶sub parse_filename {
Subroutine parse_filename: called CC=4 SCAR=14.0
Pod documented
116 24 my $self = shift;
117  
118 24 my $u = URI->new($self->filename);
119  
120 24 ▶ if (!$u->scheme || $u->scheme eq 'file') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $u->scheme or $u->scheme eq 'file') { } 21 3 24
Condition66%2/3
not $u->scheme or $u->scheme eq "file"
l!l&&r!l&&!r
2103
MC/DC50%1/2
not $u->scheme or $u->scheme eq "file"
not $u->scheme $u->scheme eq "file"
Truth table66%2/3
not(A) or B
A= not $u->schemeB= $u->scheme eq "file"
ABresult
1X1
011
000
121 21 ▶ $self->{filename} = $u->path if $u->scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $u->scheme 0 21 21
122 } else {
123 3 $self->{scheme} = $u->scheme;
124 3 $self->{is_url} = 1;
125 }
126  
127 24 return;
128 }
129  
130 =item extensions
131  
132 Build a list of extensions from the filename. Since it's possible that files
133 may have multiple extensions (e.g., .tar.gz), we break this down into
134 increasingly diminuitive portions. The idea is that we handle the "largest"
135 extension first, for example, using tar to unpack .tar.gz files, and falling
136 back to gunzip if we have to.
137  
138 =cut
139  
140 16 ▶sub extensions {
Subroutine extensions: called CC=2 SCAR=6.9
Pod documented
141 16 my $self = shift;
142  
143 16 my @extensions = split( /\./, basename($self->filename) );
144  
145 16 shift @extensions; # remove the filename
146  
147 #
148 # combine the extensions so that they are a list of full extensions,
149 # ranging from the largest combination to the smallest.
150 #
151 # e.g., foo.jpg.tar.gz would turn into this list:
152 #
153 # jpg.tar.gz, tar.gz, gz
154 #
155  
156 16 my @combined_extensions;
157  
158 16 ▶ while (@extensions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extensions) 44 16 60
159 44 push @combined_extensions, join( ".", @extensions );
160 44 shift @extensions;
161 }
162  
163 16 return @combined_extensions;
164 }
165  
166 =item backends
167  
168 Return the backend list. Please note that these are objects, not merely package
169 names.
170  
171 =cut
172  
173 16 ▶sub backends {
Subroutine backends: called CC=1 SCAR=0.0
Pod documented
174 16 my $self = shift;
175  
176 16 $self->config->backend_order;
177 }
178  
179 =item lookup_program
180  
181 Locate the program to execute the file, searching the provided backends. If we
182 have found a program and it has a template, it will replace '%s' with the
183 filename in all occurrences. Otherwise, it will append it to the end of the
184 command.
185  
186 This method returns a list suitable for sending to system(). It makes no
187 attempt to correct your potentially problematic shell quoting, but it does
188 ensure that the filename, whether templated or appended, is fully intact and
189 not split across list elements.
190  
191 =cut
192  
193 15 ▶sub lookup_program {
Subroutine lookup_program: called CC=10 SCAR=23.0
Pod documented
194 15 my $self = shift;
195  
196 15 my $program;
197 my @command;
198  
199 15 foreach my $backend ( @{ $self->backends } ) {
200  
201 15 ▶ if ($self->is_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_url) { } 3 12 15
202 3 $program = $backend->lookup_url($self->scheme);
203 } else {
204 12 foreach my $ext ( $self->extensions ) {
205 21 $program = $backend->lookup_file($ext);
206 21 ▶ last if $program;
Branch: 1 branch
BranchTrueFalseTotal
if $program 11 10 21
207 }
208 }
209  
210 15 ▶ last if $program;
Branch: 1 branch
BranchTrueFalseTotal
if $program 14 1 15
211 }
212  
213 15 ▶ if ($program) {
Branch: 1 branch
BranchTrueFalseTotal
if ($program) 14 1 15
214 14 @command = split(/\s+/, $program);
215 14 my $command_changed = 0;
216  
217 14 foreach (@command) {
218 15 ▶ if (/%s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/%s/) 1 14 15
219 1 s/%s/$self->filename/eg;
220 1 $command_changed = 1;
221 }
222 }
223  
224 # if the filename's already in the command, assume we don't need to append
225 # it.
226 14 ▶ push @command, $self->filename unless ($command_changed);
Branch: 1 branch
BranchTrueFalseTotal
unless $command_changed 13 1 14
227 }
228  
229 15 return @command;
230 }
231  
232 =item execute_program
233  
234 Execute the program against the filename supplied by the constructor.
235  
236 In most cases, this is the only method you need to call; it does all the work
237 for you.
238  
239 =cut
240  
241 10 ▶sub execute_program {
Subroutine execute_program: called CC=4 SCAR=17.9
Pod documented
242 10 my $self = shift;
243  
244 10 my @command = $self->lookup_program;
245  
246 10 ▶ die "NO_PROGRAM" unless @command;
Branch: 1 branch
BranchTrueFalseTotal
unless @command 0 10 10
247  
248 10 ▶ if ( $self->config->config->{"fork"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->config->{'fork'}) { } 0 10 10
249 0 ▶ if (fork) { exec( @command ); }
Branch: 1 branch
BranchTrueFalseTotal
if (fork) 0 0 0
250 0 return 0;
251 } else {
252 10 return system( @command );
253 }
254 }
255  
256 =back
257  
258 =head1 LICENSE
259  
260 This file and all portions of the original package are (C) 2008 Erik Hollensbe.
261 Please see the file COPYING in the package for more information.
262  
263 =head1 BUGS AND PATCHES
264  
265 Probably a lot of them. Report them to <erik@hollensbe.org> if you're feeling
266 kind. Report them to CPAN RT if you'd prefer they never get seen.
267  
268 =cut
269  
270 10 1;
↑ Summary blib/lib/App/Open/Backend/YAML.pm »