Config.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 183 CC 15 SCAR 28.3
CC
15
Cov
0%
_proc_dir
42.0
new
30.0
add_site
12.0
CRAP 16.9
1 package HtDig::Config;
2  
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEFAULT_CONFIG_LOCATION $DEFAULT_LOG_LOCATION $DEFAULT_HTDIG_LOCATION $sites $htdig_base_path $configdig_log_path);
5  
6 use Data::Dumper;
7 use HtDig::Site;
8  
9 require Exporter;
10 require AutoLoader;
11 $DEFAULT_CONFIG_LOCATION = '/opt/www/conf/configdig_sites.pl';
12 $DEFAULT_LOG_LOCATION = '/opt/www/logs';
13 $DEFAULT_HTDIG_LOCATION = '/opt/www';
14 @ISA = qw(Exporter AutoLoader);
15 @EXPORT = qw();
16 $VERSION = '1.02';
17  
18 ##
19 #Constructor for the Config object
20 ##
21 sub new {
22 my ($class, %attribs) = @_;
23 my $self = {};
24 $self->{path} = $attribs{conf_path} ? $attribs{conf_path} : $DEFAULT_CONFIG_LOCATION;
25  
26 #Autocreate allows us to create a conf that doesn't
27 # already exist.
28 if ($attribs{auto_create}) {
29 unless (-f $self->{path}) {
30 #I avoid using "touch" on the file to ensure cross-platform
31 # compatibility, opting instead to just open it for writing.
32 open(TMP,">" . $self->{path}) or die "Couldn't auto_create configuration file at " . $self->{path} . ": $!";
33 print TMP "1;\n";
34 close(TMP);
35 }
36 }
37 else {
38 unless (-f $self->{path}) {
39 die "Configuation file " . $self->{path} . " missing and auto_create not set.";
40 }
41 }
42
43 require $self->{path};
44  
45 #These next few items are in the $self->{path} file "required" in the
46 # previous line
47 $self->{sites} = $sites;
48 $self->{configdig_log_path} = $configdig_log_path || $DEFAULT_LOG_LOCATION;
49 $self->{htdig_base_path} = $htdig_base_path || $DEFAULT_HTDIG_LOCATION;
50  
51 return bless $self, $class;
52 }
53  
54 ##
55 #Persist the site registry to the file
56 ##
57 sub save {
58 my ($self) = @_;
59 my $sites_file = Data::Dumper->Dump([$self->{sites}], ["sites"]);
60 open(CONF,">" . $self->{path}) or die "Couldn't open " . $self->{path} . " for writing.";
61 print CONF $sites_file;
62 print CONF qq|\n\$configdig_log_path = '| . $self->{configdig_log_path} . qq|';\n|;
63 print CONF q|$htdig_base_path = '| . $self->{htdig_base_path} . qq|';\n|;
64 close CONF;
65 }
66  
67 ##
68 #Get an array of site names
69 ##
70 sub sites {
71 my ($self) = @_;
72 return keys %{$self->{sites}};
73 }
74  
75 ##
76 #Get a Site object by name
77 ##
78 sub get_site {
79 my ($self, $site) = @_;
80 if ($site and defined($self->{sites}->{$site})) {
81 return HtDig::Site->new(conf_path=>$self->{sites}->{$site}, site_name=>$site, config=>$self);
82 }
83 else {
84 return undef;
85 }
86 }
87  
88 ##
89 #Add a registration for a particular site configuration
90 ##
91 sub add_site {
92 my ($self, %attribs) = @_;
93 my $site;
94 eval('$site = new HtDig::Site(%attribs)');
95 if ($@) {
96 $self->{errstr} = "Couldn't register site: $@";
97 return undef;
98 }
99 if ($site) {
100 $self->{sites}->{$attribs{site_name}} = $attribs{conf_path};
101 }
102 return $site;
103 }
104  
105 ##
106 #Remove registration for a particular site configuration
107 ##
108 sub delete_site {
109 my ($self, %attribs) = @_;
110 delete($self->{sites}->{$attribs{site_name}});
111 return 1;
112 }
113  
114 ##
115 #Attempt to find htdig configurations in suggested
116 # and default locations
117 ##
118 sub autodetect {
119 my ($self, %attribs) = @_;
120 my $configs_detected = 0;
121  
122 #Clear error string
123 $self->{errstr} = '';
124  
125 #A couple of educated guesses
126 # (using a hash automatically prevents duplicates)
127 my %paths = (
128 "/opt/www/conf"=> 1,
129 "/usr/local/htdig/conf"=> 1,
130 );
131  
132 #The user's desired search paths
133 for my $path (@{$attribs{paths}}) {
134 $paths{$path} = 1;
135 }
136  
137 #If they asked us to, use the search path to find htdig
138 #If found, then deduce the install directory, and add to
139 # list of searched paths
140 if ($attribs{use_env_path}) {
141 chomp(my $potential_path = `which htdig`);
142 if (-f $potential_path) {
143 my @path = split(/\//, $potential_path);
144 my $config_path = join("/", @path[0..($#path-2)]) . "/conf";
145 $paths{$config_path} = 1;
146 }
147 }
148  
149 #Next check each path
150 for my $path (keys %paths) {
151 $configs_detected += $self->_proc_dir($path);
152 }
153 return $configs_detected;
154 }
155  
156 ##
157 #Get error message using this method
158 ##
159 sub errstr {
160 my ($self) = @_;
161 return $self->{errstr};
162 }
163  
164 ##
165 #Some private methods
166 ##
167 sub _proc_dir {
168 my ($self, $config_path) = @_;
169 my $configs_detected = 0;
170 return 0 if !-d $config_path;
171  
172 opendir(CONFDIR, $config_path);
173 while (my $file = readdir(CONFDIR)) {
174 next if ($file eq '.' or $file eq '..');
175 if ($file =~ /\.conf$/) {
176 my $autoname = $file eq "htdig.conf" ? "default" : $file;
177 eval( qq|
178 \$self->add_site(site_name=>'$autoname', conf_path=>"$config_path/$file");
179 |);
180 if ($@) {
181 $self->{errstr} .= $@ . $! . " when adding $config_path/$file\n";
182 }
183 else {
184 ++$configs_detected;
185 }
186 }
187 }
188 return $configs_detected;
189 }
190  
191  
192 # Autoload methods go after =cut, and are processed by the autosplit program.
193  
194 1;
195 __END__