lib/Bio/ToolBox/db_helper/config.pm

filter: Statementstmt 45.6% 37 / 81 Branchbran 7.6% 2 / 26 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 66.6% 8 / 12 Podpod 100% 2 / 2 totaltotal 36.6% 51 / 139 CC 18 SCAR 28.2
CC
18
Cov
35%
add_database
42.0
add_program
20.0
_rewrite_config
20.0
CRAP 16.8
« script/split_data_file.pl ↑ Summary script/db_types.pl »
1 package Bio::ToolBox::db_helper::config;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Config::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 require Exporter;
11  
12 1 our $VERSION = '2.00';
13  
14 # variables
15 1 my $default;
16 my $default_path;
17 1 my $config_path;
18 1 our $BTB_CONFIG;
19  
20 # Load the config file before exporting it
21 1 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=22.3
22  
23 # the default configuration
24 1 $default = <<DEFAULT;
25 #### BioToolBox Default Config file ####
26  
27 [default_db]
28 user = nobody
29 pass = hello
30 adaptor = DBI::mysql
31 dsn_prefix = dbi:mysql:
32  
33 [example_remote]
34 user = me
35 pass = secret
36 dsn = dbi:mysql:host=127.0.0.1;port=3306;database=example
37  
38 [example_sqlite]
39 adaptor = DBI::SQLite
40 dsn = /path/to/example.sqlite
41  
42 [applications]
43  
44 DEFAULT
45  
46 # possible paths
47 1 $default_path = File::Spec->catdir( $ENV{HOME}, '.biotoolbox.cfg' );
48 1 ▶ my $var = $ENV{'BIOTOOLBOX'} || undef;
Condition50%1/2
$ENV{'BIOTOOLBOX'} || undef
l!l
10
MC/DC0%0/1
$ENV{'BIOTOOLBOX'} || undef
$ENV{'BIOTOOLBOX'}
Truth table50%1/2
A
A= $ENV{'BIOTOOLBOX'}
Aresult
11
00
49  
50 # check for file possible paths
51 1 ▶ if ( defined $var and -s $var ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $var and -s $var) { } 1 0 1
elsif (-s $default_path) { } 0 0 0
Condition33%1/3
defined $var and -s $var
!ll&&!rl&&r
001
MC/DC0%0/2
defined $var and -s $var
defined $var -s $var
Truth table33%1/3
A and B
A= defined $varB= -s $var
ABresult
0X0
100
111
52 1 $config_path = $var;
53 }
54 elsif ( -s $default_path ) {
55 0 $config_path = $default_path;
56 }
57  
58 # Open the configuration file
59 1 ▶ if ($config_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_path) { } 1 0 1
60 1 $BTB_CONFIG = Config::Simple->new($config_path);
61 }
62 else {
63 # no path, open empty object
64 # this should still work, it just won't return anything useful
65 0 $BTB_CONFIG = Config::Simple->new( syntax => 'ini' );
66 }
67 }
68  
69 ## no critic
70 ## this is never intended to be used directly by end users
71 ## and exporting config mut always be done
72 1 our @ISA = qw(Exporter);
73 1 our @EXPORT = qw($BTB_CONFIG);
74 1 our @EXPORT_OK = qw(add_database add_program);
75 ## use critic
76  
77 0 ▶sub add_database {
Subroutine add_database: not called CC=6 SCAR=37.4
Pod documented
78 0 my %args = @_;
79  
80 # check
81 0 ▶ unless ( $args{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"name"}) 0 0 0
82 0 carp 'ERROR: no name provided for new database configuration';
83 0 return;
84 }
85 0 ▶ unless ( $args{dsn} or $args{dsn_prefix} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"dsn"} or $args{"dsn_prefix"}) 0 0 0
Condition0%0/3
$args{"dsn"} or $args{"dsn_prefix"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"dsn"} or $args{"dsn_prefix"}
$args{"dsn"} $args{"dsn_prefix"}
Truth table0%0/3
A or B
A= $args{"dsn"}B= $args{"dsn_prefix"}
ABresult
1X1
011
000
86 0 carp 'ERROR: no dsn provided for new database configuration';
87 0 return;
88 }
89  
90 # check that we have config file to update
91 0 ▶ unless ($config_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config_path) 0 0 0
92 0 ▶ _write_new_config() or return;
Branch: 1 branch
BranchTrueFalseTotal
_write_new_config() or return 0 0 0
93 }
94  
95 # set name
96 0 my $name = $args{name};
97 0 delete $args{name};
98  
99 # add configuration
100 0 $BTB_CONFIG->set_block( $name, \%args );
101 0 return _rewrite_config();
102 }
103  
104 0 ▶sub add_program {
Subroutine add_program: not called CC=4 SCAR=30.0
Pod documented
105 0 my $path = shift;
106 0 ▶ unless ( -e $path and -x _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $path and -x _) 0 0 0
Condition0%0/3
-e $path and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-e $path and -x _
-e $path -x _
Truth table0%0/3
A and B
A= -e $pathB= -x _
ABresult
0X0
100
111
107 0 carp "ERROR: $path either does not exist or is not executable";
108 0 return;
109 }
110  
111 # check that we have config file to update
112 0 ▶ unless ($config_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config_path) 0 0 0
113 0 _write_new_config();
114 }
115  
116 # add parameter
117 0 my ( $vol, $dir, $file ) = File::Spec->splitpath($path);
118 0 $BTB_CONFIG->param( 'applications.' . $file, $path );
119 0 return _rewrite_config();
120 }
121  
122 0 ▶sub _write_new_config {
Subroutine _write_new_config: not called CC=2 SCAR=17.9
123 0 my $fh = IO::File->new( '>', $default_path );
124 0 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
125 0 carp "ERROR: Cannot write biotoolbox configuration file $default_path! $OS_ERROR";
126 0 return;
127 }
128 0 $fh->print($default);
129 0 $fh->close;
130 0 $config_path = $default_path;
131 0 $BTB_CONFIG->read($default_path);
132 0 return 1;
133 }
134  
135 0 ▶sub _rewrite_config {
Subroutine _rewrite_config: not called CC=4 SCAR=30.0
136 0 my $updated;
137 0 ▶ if ( -w $config_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $config_path) 0 0 0
138 0 $updated = $BTB_CONFIG->write;
139 }
140 0 ▶ unless ($updated) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($updated) 0 0 0
141  
142 # attempt to write in users own directory, which takes precedence
143 0 my $file = File::Spec->catdir( $ENV{HOME}, '.biotoolbox.cfg' );
144 0 $updated = $BTB_CONFIG->write($file);
145 0 ▶ if ($updated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($updated) { } 0 0 0
146 0 $config_path = $file;
147 0 return 1;
148 }
149 else {
150 0 carp sprintf "ERROR: unable to write updated configuration to %s!\n%s",
151 $file, $BTB_CONFIG->error;
152 0 return;
153 }
154 }
155 }
156  
157 1 1;
158  
159 __END__
« script/split_data_file.pl ↑ Summary script/db_types.pl »