blib/lib/Ubic/Multiservice/Dir.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 131 SCAR 50.5
CC
12
Cov
0%
_name2file
20.0
simple_service
12.0
service_names
12.0
CRAP 156.0
1 package Ubic::Multiservice::Dir;
2 $Ubic::Multiservice::Dir::VERSION = '1.59';
3 use strict;
4 use warnings;
5  
6 # ABSTRACT: multiservice which uses directory with configs to instantiate services
7  
8 use parent qw(Ubic::Multiservice);
9 use Params::Validate qw(:all);
10 use Carp;
11 use File::Basename;
12 use Scalar::Util qw(blessed);
13 use Ubic::ServiceLoader;
14  
15 sub new {
16 my $class = shift;
17 my ($dir, @options) = validate_pos(@_, 1, 0);
18  
19 my $options = {};
20 if (@options) {
21 $options = validate(@options, {
22 protected => 0,
23 });
24 }
25 return bless { service_dir => $dir, %$options } => $class;
26 }
27  
28 sub has_simple_service {
29 my $self = shift;
30 my ($name) = validate_pos(@_, {type => SCALAR, regex => qr/^[\w.-]+$/});
31 if ($self->_name2file($name)) {
32 return 1;
33 }
34 else {
35 return;
36 }
37 }
38  
39 sub _filter_files {
40 my $self = shift;
41 my @files = @_;
42  
43 my @filtered;
44 for my $name (@files) {
45 # list of taboo extensions is stolen from logrotate(8)
46 if ($name =~ /(
47 \.rpmorig |
48 \.rpmsave |
49 ,v |
50 \.swp |
51 \.rpmnew |
52 ~ |
53 \.cfsaved |
54 \.rhn-cfg-tmp-.* |
55 \.dpkg-dist |
56 \.dpkg-old |
57 \.dpkg-new |
58 \.disabled
59 )$/x
60 ) {
61 next; # skip silently
62 }
63 push @filtered, $name;
64 }
65 return @filtered;
66 }
67  
68 sub _name2file {
69 my $self = shift;
70 my ($name) = @_;
71  
72 my $base = "$self->{service_dir}/$name";
73 my @files = glob "$base.*";
74 unshift @files, $base if -e $base;
75  
76 @files = $self->_filter_files(@files);
77  
78 unless (@files) {
79 return;
80 }
81  
82 if (@files > 1) {
83 for my $file (@files[1 .. $#files]) {
84 warn "Ignoring duplicate service config '$file', using '$files[0]' instead";
85 }
86 }
87 return shift @files;
88 }
89  
90 sub simple_service {
91 my $self = shift;
92 my ($name) = validate_pos(@_, {type => SCALAR, regex => qr/^[\w.-]+$/});
93  
94 my $file = $self->_name2file($name);
95 unless (defined $file) {
96 croak "Service '$name' not found";
97 }
98  
99 if (-d $file) {
100 # directory => multiservice
101 my $service = Ubic::Multiservice::Dir->new($file);
102 $service->name($name);
103 return $service;
104 }
105  
106 my $service = Ubic::ServiceLoader->load($file);
107 $service->name($name);
108 return $service;
109 }
110  
111 sub service_names {
112 my $self = shift;
113  
114 my %names;
115  
116 my @files = glob("$self->{service_dir}/*");
117 @files = $self->_filter_files(@files);
118 for my $file (@files) {
119 next unless -f $file or -d $file;
120 my $name = basename($file);
121  
122 my ($service_name, $ext) = Ubic::ServiceLoader->split_service_filename($name);
123 unless (defined $service_name) {
124 warn "Invalid file $file - only alphanumerics, underscores and hyphens are allowed\n";
125 next;
126 }
127  
128 $names{ $service_name }++;
129 }
130 return sort keys %names;
131 }
132  
133 sub multiop {
134 my $self = shift;
135 $self->{protected} ? 'protected' : 'allowed';
136 }
137  
138  
139 1;
140  
141 __END__