| 1 | # ************************************************************************* | ||||||||||||||||||||||||||||
| 2 | # Copyright (c) 2014-2020, SUSE LLC | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # All rights reserved. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Redistribution and use in source and binary forms, with or without | ||||||||||||||||||||||||||||
| 7 | # modification, are permitted provided that the following conditions are met: | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||||||||||||||||||||||||||||
| 10 | # this list of conditions and the following disclaimer. | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||||||||||||||||||||||||||||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||||||||||||||||||||||||||||
| 14 | # documentation and/or other materials provided with the distribution. | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # 3. Neither the name of SUSE LLC nor the names of its contributors may be | ||||||||||||||||||||||||||||
| 17 | # used to endorse or promote products derived from this software without | ||||||||||||||||||||||||||||
| 18 | # specific prior written permission. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||||||||||||||||||||||||||||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||||||||||||||||||||||||||||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||||||||||||||||||||||||||||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||||||||||||||||||||||||||||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||||||||||||||||||||||||||||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||||||||||||||||||||||||||||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||||||||||||||||||||||||||||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||||||||||||||||||||||||||||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||||||||||||||||||||||||||||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||||||||||||||||||||||||||||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||||||||||
| 31 | # ************************************************************************* | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | package App::CELL::Util; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 18 | ▶ | use 5.012; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 18 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 18 | ▶ | use Date::Format; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 18 | ▶ | use Params::Validate qw( :all ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 NAME | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | App::CELL::Util - generalized, reuseable functions | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | use App::CELL::Util qw( utc_timestamp is_directory_viable ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # utc_timestamp | ||||||||||||||||||||||||||||
| 54 | print "UTC time is " . utc_timestamp() . "\n"; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # is_directory_viable | ||||||||||||||||||||||||||||
| 57 | my $status = is_directory_viable( $dir_foo ); | ||||||||||||||||||||||||||||
| 58 | print "$dir_foo is a viable directory" if $status->ok; | ||||||||||||||||||||||||||||
| 59 | if ( $status->not_ok ) { | ||||||||||||||||||||||||||||
| 60 | my $problem = $status->payload; | ||||||||||||||||||||||||||||
| 61 | print "$dir_foo is not viable because $problem\n"; | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | This module provides the following public functions: | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =over | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item C<is_directory_viable> | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =item C<stringify_args> | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =item C<utc_timestamp> | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =back | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 18 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 18 | our @EXPORT_OK = qw( is_directory_viable stringify_args utc_timestamp ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 PACKAGE VARIABLES | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 18 | our $not_viable_reason = ''; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 is_directory_viable | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Run viability checks on a directory. Takes: full path to directory. Returns | ||||||||||||||||||||||||||||
| 102 | true (directory viable) or false (directory not viable). If the directory | ||||||||||||||||||||||||||||
| 103 | is not viable, it sets the package variable | ||||||||||||||||||||||||||||
| 104 | C<< $App::CELL::Util::not_viable_reason >>. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 15 | ▶ | sub is_directory_viable { | ||||||||||||||||||||||||||
|
Subroutine
is_directory_viable: called CC=6 SCAR=19.4
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 15 | my $confdir = shift; | |||||||||||||||||||||||||||
| 111 | 15 | my $problem = ''; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | CRIT_CHECK: { | ||||||||||||||||||||||||||||
| 114 | 15 | ▶ | if ( not -e $confdir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 3 | $problem = "does not exist"; | |||||||||||||||||||||||||||
| 116 | 3 | last CRIT_CHECK; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 12 | ▶ | if ( not -d $confdir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | $problem = "exists but not a directory"; | |||||||||||||||||||||||||||
| 120 | 0 | last CRIT_CHECK; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 12 | ▶ | if ( not -r $confdir or not -x $confdir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-r $confdir and -x $confdir
MC/DC0%0/2
-r $confdir and -x $confdir
-r $confdir
-x $confdir
Truth table33%1/3
A and B
A= -r $confdirB= -x $confdir
| |||||||||||||||||||||||||||||
| 123 | 0 | $problem = "directory exists but insufficient permissions"; | |||||||||||||||||||||||||||
| 124 | 0 | last CRIT_CHECK; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } # CRIT_CHECK | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 15 | ▶ | if ( $problem ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 3 | $not_viable_reason = $problem; | |||||||||||||||||||||||||||
| 130 | 3 | return 0; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 12 | return 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 stringify_args | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Convert args (or any data structure) into a string -- useful for error | ||||||||||||||||||||||||||||
| 140 | reporting. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 179 | ▶ | sub stringify_args { | ||||||||||||||||||||||||||
|
Subroutine
stringify_args: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 179 | my $args = shift; | |||||||||||||||||||||||||||
| 146 | 179 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||
| 147 | 179 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 148 | 179 | my $args_as_string; | |||||||||||||||||||||||||||
| 149 | 179 | ▶ | if ( ref $args ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 176 | $args_as_string = Dumper( $args ); | |||||||||||||||||||||||||||
| 151 | } else { | ||||||||||||||||||||||||||||
| 152 | 3 | $args_as_string = $args; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 179 | return $args_as_string; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 utc_timestamp | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | ▶ | sub utc_timestamp { | ||||||||||||||||||||||||||
|
Subroutine
utc_timestamp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 3 | return uc time2str("%Y-%m-%d %H:%M %Z", time, 'GMT'); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # END OF App::CELL::Util.pm | ||||||||||||||||||||||||||||
| 168 | 18 | 1; | |||||||||||||||||||||||||||