#! /usr/bin/perl -w
@ISA
= (
'Exporter'
);
@EXPORT
=
qw()
;
our
$VERSION
= 0.08;
Hide Show 35 lines of Pod
my
$xcr_settings
=
{
'config_type'
, [
'scalar'
,0,
'word'
],
'restart_sleep'
, [
'scalar'
,0,
'integer'
,0],
'start_test_sleep'
, [
'scalar'
,0,
'integer'
,0],
'stop_test_sleep'
, [
'scalar'
,0,
'integer'
,0],
'unit_config_files'
, [
'list'
, 0,
'abs_file'
,
'r'
],
'xacd_path'
, [
'scalar'
,0,
'abs_file'
,
'x'
],
'xted_path'
, [
'scalar'
,0,
'abs_file'
,
'x'
]
};
Hide Show 8 lines of Pod
sub
new {
my
$class
=
shift
;
my
$path
=
shift
;
my
$verbose
= 0;
$verbose
=
shift
if
defined
$_
[0];
my
$self
= {};
Xcruciate::Utils::check_path(
'xcruciate config file'
,
$path
,
'r'
);
print
"Attempting to parse xcruciate config file... "
if
$verbose
;
my
$parser
= XML::LibXML->new();
my
$xcr_dom
=
$parser
->parse_file(
$path
);
my
@config
=
$xcr_dom
->findnodes(
"/config/scalar"
);
die
"Config file doesn't look anything like a config file - 'xcruciate file_help' for some clues"
unless
$config
[0];
my
@config_type
=
$xcr_dom
->findnodes(
"/config/scalar[\@name='config_type']/text()"
);
die
"config_type entry not found in unit config file"
unless
$config_type
[0];
my
$config_type
=
$config_type
[0]->toString;
die
"config_type in unit config file is '$config_type' (should be 'xcruciate') - are you confusing xcruciate and unit config files?"
unless
$config_type
eq
'xcruciate'
;
my
@errors
= ();
foreach
my
$entry
(
$xcr_dom
->findnodes(
"/config/*[(local-name() = 'scalar') or (local-name() = 'list')]"
)) {
push
@errors
,
sprintf
(
"No name attribute for element '%s'"
,
$entry
->nodeName)
unless
$entry
->hasAttribute(
'name'
);
my
$entry_record
=
$xcr_settings
->{
$entry
->getAttribute(
'name'
)};
if
(not
defined
$entry_record
) {
warn
"Unknown xcruciate config entry '"
. (
$entry
->getAttribute('name
')) ."'
";
}
elsif
(not(
$entry
->nodeName eq
$entry_record
->[0])){
push
@errors
,
sprintf
(
"Entry called %s should be a %s not a %s"
,
$entry
->getAttribute(
'name'
),
$entry_record
->[0],
$entry
->nodeName);
}
elsif
((not
$entry
->textContent) and ((not
$entry_record
->[1]) or
$entry
->textContent!~/^\s*$/s)) {
push
@errors
,
sprintf
(
"Entry called %s requires a value"
,
$entry
->getAttribute(
'name'
))
}
elsif
((
$entry
->nodeName eq
'scalar'
) and
$entry_record
->[2] and ((not
$entry_record
->[1]) or
$entry
->textContent!~/^\s*$/s or
$entry
->textContent)){
push
@errors
,Xcruciate::Utils::type_check(
$self
,
$entry
->getAttribute(
'name'
),
$entry
->textContent,
$entry_record
);
}
elsif
((
$entry
->nodeName eq
'list'
) and
$entry_record
){
my
@items
=
$entry
->findnodes(
'item/text()'
);
push
@errors
,
sprintf
(
"Entry called %s requires at least one item"
,
$entry
->getAttribute(
'name'
))
if
((not
$entry_record
->[2]) and (not
@items
));
my
$count
= 1;
foreach
my
$item
(
@items
) {
push
@errors
,Xcruciate::Utils::type_check(
$self
,
$entry
->getAttribute(
'name'
),
$item
->textContent,
$entry_record
,
$count
);
$count
++;
}
}
push
@errors
,
sprintf
(
"Duplicate entry called %s"
,
$entry
->getAttribute(
'name'
))
if
defined
$self
->{
$entry
->getAttribute(
'name'
)};
if
(
$entry
->nodeName eq
'scalar'
) {
$self
->{
$entry
->getAttribute(
'name'
)} =
$entry
->textContent;
}
else
{
$self
->{
$entry
->getAttribute(
'name'
)} = []
unless
defined
$self
->{
$entry
->getAttribute(
'name'
)};
foreach
my
$item
(
$entry
->findnodes(
'item/text()'
)) {
push
@{
$self
->{
$entry
->getAttribute(
'name'
)}},
$item
->textContent;
}
}
}
foreach
my
$entry
(
keys
%{
$xcr_settings
}) {
push
@errors
,
sprintf
(
"No xcruciate entry called %s"
,
$entry
)
unless
((
defined
$self
->{
$entry
}) or (
$xcr_settings
->{
$entry
}->[1]));
}
if
(
@errors
) {
print
join
"\n"
,
@errors
;
print
"\n"
;
die
"Errors in xcruciate config file - cannot continue"
;
}
else
{
bless
(
$self
,
$class
);
print
"done\n"
if
$verbose
;
return
$self
;
}
}
Hide Show 6 lines of Pod
sub
xcr_file_format_description {
my
$self
=
shift
;
my
$ret
=
''
;
foreach
my
$entry
(
sort
keys
%{
$xcr_settings
}) {
my
$record
=
$xcr_settings
->{
$entry
};
$ret
.=
"$entry ("
;
$ret
.=
"optional "
if
$record
->[1];
$ret
.=
"$record->[0])"
;
if
(not
$record
->[2]) {
}
elsif
((
$record
->[2] eq
'integer'
) or (
$record
->[2] eq
'float'
)) {
$ret
.=
" - $record->[2]"
;
$ret
.=
" >= $record->[3]"
if
defined
$record
->[3];
$ret
.=
" and <= $record->[4]"
if
defined
$record
->[4];
}
elsif
(
$record
->[2] eq
'ip'
) {
$ret
.=
" - ip address"
;
}
elsif
(
$record
->[2] eq
'word'
) {
$ret
.=
" - word (ie no whitespace)"
;
}
elsif
(
$record
->[2] eq
'path'
) {
$ret
.=
" - path (currently a word)"
;
}
elsif
(
$record
->[2] eq
'xml_leaf'
) {
$ret
.=
" - filename with an xml suffix"
;
}
elsif
(
$record
->[2] eq
'xsl_leaf'
) {
$ret
.=
" - filename with an xsl suffix"
;
}
elsif
(
$record
->[2] eq
'yes_no'
) {
$ret
.=
" - 'yes' or 'no'"
;
}
elsif
(
$record
->[2] eq
'email'
) {
$ret
.=
" - email address"
;
}
elsif
(
$record
->[2] eq
'abs_dir'
) {
$ret
.=
" - absolute directory path with $record->[3] permissions"
;
}
elsif
(
$record
->[2] eq
'abs_file'
) {
$ret
.=
" - absolute file path with $record->[3] permissions"
;
}
elsif
(
$record
->[2] eq
'abs_create'
) {
$ret
.=
" - absolute file path with $record->[3] permissions for directory"
;
}
$ret
.=
"\n"
;
}
return
$ret
;
}
Hide Show 6 lines of Pod
sub
restart_sleep {
my
$self
=
shift
;
return
$self
->{restart_sleep};
}
Hide Show 6 lines of Pod
sub
start_test_sleep {
my
$self
=
shift
;
return
$self
->{start_test_sleep};
}
Hide Show 6 lines of Pod
sub
stop_test_sleep {
my
$self
=
shift
;
return
$self
->{stop_test_sleep};
}
Hide Show 6 lines of Pod
sub
xacd_path {
my
$self
=
shift
;
return
$self
->{xacd_path};
}
Hide Show 6 lines of Pod
sub
xted_path {
my
$self
=
shift
;
return
$self
->{xted_path};
}
Hide Show 6 lines of Pod
sub
unit_config_files {
my
$self
=
shift
;
return
@{
$self
->{unit_config_files}};
}
Hide Show 30 lines of Pod
1;