Brackup/Config.pm

filter: Statementstmt 76.6% 82 / 107 Branchbran 53.5% 30 / 56 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.5% 12 / 17 Podpod 0.0% 0 / 11 totaltotal 63.7% 125 / 196 CC 34 SCAR 24.5
CC
34
Cov
68%
load
22.6
default_config_file_name
12.0
load_target
9.9
CRAP 11.6
« Brackup/Target/GoogleAppEngine.pm ↑ Summary Brackup/Dict/SQLite2.pm »
1 package Brackup::Config;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use Brackup::ConfigSection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Fcntl qw(O_WRONLY O_CREAT O_EXCL);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10 5 my ($class) = @_;
11 5 return bless {}, $class;
12 }
13  
14 9 ▶sub add_section {
Subroutine add_section: called CC=1 SCAR=0.0
Pod undocumented
15 9 my ($self, $sec) = @_;
16 9 $self->{$sec->name} = $sec;
17 }
18  
19 0 ▶sub get_section {
Subroutine get_section: not called CC=1 SCAR=6.9
Pod undocumented
20 0 my ($self, $name) = @_;
21 0 return $self->{$name};
22 }
23  
24 1 ▶sub load {
Subroutine load: called CC=18 SCAR=31.2
Pod undocumented
25 1 my ($class, $file) = @_;
26 1 ▶ $file ||= Brackup::Config->default_config_file_name;
Condition33%1/3
$file ||= "Brackup::Config"->default_config_file_name
l!l&&r!l&&!r
100
MC/DC0%0/2
$file ||= "Brackup::Config"->default_config_file_name
$file "Brackup::Config"->default_config_file_name
Truth table33%1/3
A ||= B
A= $fileB= "Brackup::Config"->default_config_file_name
ABresult
1X1
011
000
27  
28 1 my $self = bless {}, $class;
29  
30 1 ▶ open (my $fh, $file) or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or do { if (write_dummy_config($file)) { die "Your config file needs tweaking. I put a commented-out template at: $file\n"; } else { die "No config file at: $file\n"; } } 1 0 1
31 0 ▶ if (write_dummy_config($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_dummy_config($file)) { } 0 0 0
32 0 die "Your config file needs tweaking. I put a commented-out template at: $file\n";
33 } else {
34 0 die "No config file at: $file\n";
35 }
36 };
37 1 my $sec = undef;
38 1 my %inherit = ();
39 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 34 1 35
40 34 $line =~ s/^\#.*//; # kill comments starting at beginning of line
41 34 $line =~ s/\s\#.*//; # kill comments with whitespace before the # (motivation: let # be in regexps)
42 34 $line =~ s/^\s+//;
43 34 $line =~ s/\s$//;
44 34 ▶ next unless $line ne "";
Branch: 1 branch
BranchTrueFalseTotal
unless $line ne "" 8 26 34
45  
46 26 ▶ if ($line =~ /^\[(.+)\]$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\[(.+)\]$/) { } 7 19 26
elsif ($line =~ /^(\w+)\s*=\s*(.+)/) { } 19 0 19
47 7 my $name = $1;
48 7 $sec = Brackup::ConfigSection->new($name);
49 7 ▶ die "Duplicate config section '$name'" if $self->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$name} 0 7 7
50 7 $self->{$name} = $sec;
51 } elsif ($line =~ /^(\w+)\s*=\s*(.+)/) {
52 19 ▶ die "Declaration of '$1' outside of a section." unless $sec;
Branch: 1 branch
BranchTrueFalseTotal
unless $sec 0 19 19
53 19 ▶ if ($1 eq 'inherit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'inherit') { } 5 14 19
54 5 $inherit{$sec->name} = $2;
55 } else {
56 14 $sec->add($1, $2);
57 }
58 } else {
59 0 die "Bogus config line: $line";
60 }
61 }
62  
63 1 ▶ unless ($sec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sec) 0 1 1
64 0 die "Your config file needs tweaking. There's a starting template at: $file\n";
65 }
66  
67 # Config section inheritance
68 1 my $loop_count = 0;
69 1 ▶ while (keys %inherit) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %inherit) 1 1 2
70 1 for my $child_sec (keys %inherit) {
71 # If this parent_sec itself inherits from something else, defer this time around
72 5 ▶ next if exists $inherit{ $inherit{$child_sec} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $inherit{$inherit{$child_sec}} 0 5 5
73  
74 5 my $parent_sec = delete $inherit{$child_sec};
75 # If missing, derive prefix ([SOURCE|TARGET]:) from section name
76 5 ▶ $parent_sec = (split /:/, $child_sec, 2)[0] . ':' . $parent_sec
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_sec =~ /:/ 4 1 5
77 if $parent_sec !~ m/:/;
78 5 ▶ die "Cannot inherit from unknown section '$parent_sec'." unless $self->{$parent_sec};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$parent_sec} 0 5 5
79 5 $self->inherit_from($self->{$parent_sec}, $self->{$child_sec});
80 }
81 1 ▶ die "Inheritance chain too long - looping?" if ++$loop_count > 20;
Branch: 1 branch
BranchTrueFalseTotal
if ++$loop_count > 20 0 1 1
82 }
83  
84 1 return $self;
85 }
86  
87 0 ▶sub default_config_file_name {
Subroutine default_config_file_name: not called CC=3 SCAR=24.8
Pod undocumented
88 0 my ($class) = @_;
89  
90 0 ▶ if ($ENV{HOME}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'HOME'}) { } 0 0 0
elsif ($ENV{'APPDATA'}) { } 0 0 0
91 # Default for UNIX folk
92 0 return "$ENV{HOME}/.brackup.conf";
93 }
94 elsif ($ENV{APPDATA}) {
95 # For Windows users
96 0 return "$ENV{APPDATA}/brackup.conf";
97 }
98 else {
99 # Fall back on the current directory
100 0 return "brackup.conf";
101 }
102  
103 }
104  
105 0 ▶sub write_dummy_config {
Subroutine write_dummy_config: not called CC=2 SCAR=17.9
Pod undocumented
106 0 my $file = shift;
107 0 ▶ sysopen (my $fh, $file, O_WRONLY | O_CREAT | O_EXCL, 0600) or return;
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $file, 193, 384 or return 0 0 0
108 0 print $fh <<ENDCONF;
109 # This is an example config
110  
111 #[TARGET:raidbackups]
112 #type = Filesystem
113 #path = /raid/backup/brackup
114 #keep_backups = 10
115  
116 #[TARGET:amazon]
117 #type = Amazon
118 #aws_access_key_id = XXXXXXXXXX
119 #aws_secret_access_key = XXXXXXXXXXXX
120 #keep_backups = 10
121  
122 #[SOURCE:proj]
123 #path = /raid/bradfitz/proj/
124 #chunk_size = 5m
125 #gpg_recipient = 5E1B3EC5
126  
127 #[SOURCE:bradhome]
128 #path = /raid/bradfitz/
129 #noatime = 1
130 #chunk_size = 64MB
131 #ignore = ^\.thumbnails/
132 #ignore = ^\.kde/share/thumbnails/
133 #ignore = ^\.ee/minis/
134 #ignore = ^build/
135 #ignore = ^(gqview|nautilus)/thumbnails/
136  
137 ENDCONF
138 }
139  
140 5 ▶sub load_root {
Subroutine load_root: called CC=4 SCAR=14.1
Pod undocumented
141 5 my ($self, $name, $cache) = @_;
142 5 ▶ my $conf = $self->{"SOURCE:$name"} or
Branch: 1 branch
BranchTrueFalseTotal
my $conf = $$self{"SOURCE:$name"} or die "Unknown source '${name}'\n" 5 0 5
143 die "Unknown source '$name'\n";
144  
145 5 my $root = Brackup::Root->new($conf, $cache);
146  
147 # iterate over config's ignore, and add those
148 5 foreach my $pat ($conf->values("ignore")) {
149 1 $root->ignore($pat);
150 }
151  
152 # common things to ignore
153 5 $root->ignore(qr!~$!);
154 5 $root->ignore(qr!^\.thumbnails/!);
155 5 $root->ignore(qr!^\.kde/share/thumbnails/!);
156 5 $root->ignore(qr!^\.ee/minis/!);
157 5 $root->ignore(qr!^\.(gqview|nautilus)/thumbnails/!);
158  
159 # abort if the user had any configuration we didn't understand
160 5 ▶ if (my @keys = $conf->unused_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@keys) = $conf->unused_config) 0 5 5
161 0 die "Aborting, unknown configuration keys in SOURCE:$name: @keys\n";
162 }
163  
164 5 return $root;
165 }
166  
167 0 ▶sub list_sources {
Subroutine list_sources: not called CC=1 SCAR=6.9
Pod undocumented
168 0 my ($self) = @_;
169 0 return sort map { s/^SOURCE://; $_ } grep(/^SOURCE:/, keys %$self);
170 }
171  
172 0 ▶sub list_targets {
Subroutine list_targets: not called CC=1 SCAR=6.9
Pod undocumented
173 0 my ($self) = @_;
174 0 return sort map { s/^TARGET://; $_ } grep(/^TARGET:/, keys %$self);
175 }
176  
177 4 ▶sub load_target {
Subroutine load_target: called CC=8 SCAR=22.9
Pod undocumented
178 4 my ($self, $name, %opts) = @_;
179 4 my $testmode = delete $opts{testmode};
180 4 ▶ croak("Unknown options: " . join(', ', keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 4 4
181  
182 4 ▶ my $confsec = $self->{"TARGET:$name"} or
Branch: 1 branch
BranchTrueFalseTotal
my $confsec = $$self{"TARGET:$name"} or die "Unknown target '${name}'\n" 4 0 4
183 die "Unknown target '$name'\n";
184  
185 4 ▶ my $type = $confsec->value("type") or
Branch: 1 branch
BranchTrueFalseTotal
my $type = $confsec->value("type") or die "Target '${name}' has no 'type'" 4 0 4
186 die "Target '$name' has no 'type'";
187 4 ▶ die "Invalid characters in ${name}'s 'type'"
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^\w+$/ 0 4 4
188 unless $type =~ /^\w+$/;
189  
190 4 my $class = "Brackup::Target::$type";
191 4 ▶ eval "use $class; 1;" or die
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use $class; 1;" or die "Failed to load ${name}'s driver: $@\n" 4 0 4
192 "Failed to load ${name}'s driver: $@\n";
193 4 my $target = $class->new($confsec);
194  
195 4 ▶ if (my @unk_config = $confsec->unused_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unk_config) = $confsec->unused_config) 0 4 4
196 0 ▶ die "Unknown config params in TARGET:$name: @unk_config\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $testmode 0 0 0
197 unless $testmode;
198 }
199 4 return $target;
200 }
201  
202 # Copy all keys in $parent_sec that don't exist in $child_sec
203 5 ▶sub inherit_from {
Subroutine inherit_from: called CC=4 SCAR=13.9
Pod undocumented
204 5 my ($self, $parent_sec, $child_sec) = @_;
205  
206 5 for my $key ($parent_sec->keys) {
207 20 ▶ next if $child_sec->values($key);
Branch: 1 branch
BranchTrueFalseTotal
if $child_sec->values($key) 3 17 20
208 17 $child_sec->add($key, $_) foreach $parent_sec->values($key);
209 }
210 }
211  
212 14 1;
213  
214 __END__
« Brackup/Target/GoogleAppEngine.pm ↑ Summary Brackup/Dict/SQLite2.pm »