EnsEMBL/Analysis/Programs.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 16 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 131 CC 14 SCAR 30.5
CC
14
Cov
0%
import
42.0
_is_prog
30.0
_follow
12.0
CRAP 21.1
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 package Bio::EnsEMBL::Analysis::Programs;
32 $Bio::EnsEMBL::Analysis::Programs::VERSION = '114.0.0_50'; # TRIAL
33 $Bio::EnsEMBL::Analysis::Programs::VERSION = '114.0.050';
34 use strict;
35 use vars qw( %Program_Paths );
36 use Carp;
37 use Cwd;
38 use Sys::Hostname;
39 use Bio::EnsEMBL::Utils::Exception qw ( throw ) ;
40  
41 sub import {
42 my $pkg = shift;
43 foreach (@_) {
44 #print STDERR "importing: $_\n";
45 $Program_Paths{ $_ } = 0;
46 }
47 my( $home, @PATH, @missing );
48
49 $home = cwd() or die "Can't save cwd";
50 my $H = [ $home, 1 ];
51
52 @PATH = split /:/, $ENV{'PATH'};
53 foreach (@PATH) {
54 s|/?$|/|; # Append / to each path
55 }
56
57 # For each program, check there is an executable
58 foreach my $program (keys %Program_Paths) {
59
60 # Deal with paths
61 if ($program =~ m|/|) {
62 _go_home( $H );
63 my $path = $program;
64 # Deal with tildes
65 $path =~ s{^~([^/]*)}{ $1 ? (getpwnam($1))[7]
66 : (getpwuid($>))[7] }e;
67 if (my $real = _is_prog( $H, $path )) {
68 $Program_Paths{ $program } = $real;
69 }
70 }
71 # Or search through all paths
72 else {
73 foreach my $path (@PATH) {
74 _go_home( $H );
75 if (my $real = _is_prog( $H, $path, $program )) {
76 $Program_Paths{ $program } = $real;
77 last;
78 }
79 }
80 }
81 }
82 _go_home( $H ); # Return to home directory
83
84 # Make a list of all missing programs
85 foreach my $program (keys %Program_Paths) {
86 push( @missing, $program ) unless $Program_Paths{ $program };
87 }
88
89 # Give informative death message if programs weren't found
90 if (@missing) {
91 throw("Unable to locate the following programs as '". (getpwuid($<))[0]. "' on host '". hostname(). "' :\t".
92 join ( " --> " , @missing )) ;
93 }
94 }
95  
96 # Recursive function which follows links, or tests final destination
97 sub _is_prog {
98 my( $h, $path, $prog ) = @_;
99  
100 # Need to split path if $prog not provided
101 unless ($prog) {
102 ($path, $prog) = $path =~ m|(.*?)([^/]+)$|;
103 }
104
105 if (-l "$path$prog") {
106 # Follow link
107 _follow( $h, $path ) or return;
108 unless (-x readlink($prog)) {
109 confess "Can't read link '$path$prog' : $!";
110 }
111 my $link = $prog;
112 $path = cwd() or confess "Can't determine cwd";
113 return "$path/$prog";
114 } elsif (-f _ and -x _) {
115 # Return full path
116 _follow( $h, $path ) or return;
117 $path = cwd() or confess "Can't determine cwd";
118 return "$path/$prog";
119 } else {
120 # Not a link or an executable plain file
121 return;
122 }
123 }
124  
125 # To avoid unnecessary chdir'ing
126 sub _follow {
127 my( $H, $path ) = @_;
128
129 # Chdir without arguments goes to home dir.
130 # Can't use defined in test since $path may contain
131 # a real null string.
132 if ( ! $path and $path ne '0' ) {
133 return 1;
134 } elsif (chdir($path)) {
135 $H->[1] = 0;
136 return 1;
137 } else {
138 return;
139 }
140 }
141 sub _go_home {
142 my( $H ) = @_;
143
144 # Go home unless we're already there
145 if ($H->[1] == 0) {
146 if (chdir( $H->[0] )) {
147 $H->[1] = 1;
148 } else {
149 confess "Can't go home to [ ", $H->[0], ' ]';
150 }
151 }
152 }
153  
154 1;
155  
156 __END__