| 1 | package Lim::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use common::sense; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Log::Log4perl (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Fcntl qw(:seek); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use IO::File (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | eval 'use URI::Escape::XS qw(uri_unescape);'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 10 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 0 | eval 'use URI::Escape qw(uri_unescape);'; | |||||||||||||||||||||||||||
| 15 | 0 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use AnyEvent (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 10 | ▶ | use AnyEvent::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 10 | ▶ | use AnyEvent::Socket (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 10 | ▶ | use Lim (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =encoding utf8 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Lim::Util - Utilities for plugins | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 VERSION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | See L<Lim> for version. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 10 | our $VERSION = $Lim::VERSION; | |||||||||||||||||||||||||||
| 36 | 10 | our %CALL_METHOD = ( | |||||||||||||||||||||||||||
| 37 | Create => 'POST', | ||||||||||||||||||||||||||||
| 38 | Read => 'GET', | ||||||||||||||||||||||||||||
| 39 | Update => 'PUT', | ||||||||||||||||||||||||||||
| 40 | Delete => 'DELETE' | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =over 4 | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | use Lim::Util; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =back | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 METHODS | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =over 4 | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =item $full_path = Lim::Util::FileExists($file) | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Check if C<$file> exists by prefixing L<Lim::Config>->{prefix} and returns the | ||||||||||||||||||||||||||||
| 58 | full path to the file or undef if it does not exist. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub FileExists { | ||||||||||||||||||||||||||
|
Subroutine
FileExists: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (defined $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $file =~ s/^\///o; | |||||||||||||||||||||||||||
| 67 | 0 | foreach (@{Lim::Config->{prefix}}) { | |||||||||||||||||||||||||||
| 68 | 0 | my $real_file = $_.'/'.$file; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (-f $real_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | return $real_file; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item $full_path = Lim::Util::FileReadable($file) | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Check if C<$file> exists by prefixing L<Lim::Config>->{prefix} and if it is | ||||||||||||||||||||||||||||
| 81 | readable. Returns the full path to the file or undef if it does not exist. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub FileReadable { | ||||||||||||||||||||||||||
|
Subroutine
FileReadable: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (defined $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $file =~ s/^\///o; | |||||||||||||||||||||||||||
| 90 | 0 | foreach (@{Lim::Config->{prefix}}) { | |||||||||||||||||||||||||||
| 91 | 0 | my $real_file = $_.'/'.$file; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (-f $real_file and -r $real_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $real_file and -r $real_file
MC/DC0%0/2
-f $real_file and -r $real_file
-f $real_file
-r $real_file
Truth table0%0/3
A and B
A= -f $real_fileB= -r $real_file
| |||||||||||||||||||||||||||||
| 94 | 0 | return $real_file; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =item $full_path = Lim::Util::FileWritable($file) | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | Check if C<$file> exists by prefixing L<Lim::Config>->{prefix} and if it is | ||||||||||||||||||||||||||||
| 104 | writable. Returns the full path to the file or undef if it does not exist. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub FileWritable { | ||||||||||||||||||||||||||
|
Subroutine
FileWritable: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if (defined $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | $file =~ s/^\///o; | |||||||||||||||||||||||||||
| 113 | 0 | foreach (@{Lim::Config->{prefix}}) { | |||||||||||||||||||||||||||
| 114 | 0 | my $real_file = $_.'/'.$file; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if (-f $real_file and -w $real_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $real_file and -w $real_file
MC/DC0%0/2
-f $real_file and -w $real_file
-f $real_file
-w $real_file
Truth table0%0/3
A and B
A= -f $real_fileB= -w $real_file
| |||||||||||||||||||||||||||||
| 117 | 0 | return $real_file; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | return; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item $content = Lim::Util::FileReadContent($file) | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Read the file and return the content or undef if there was an error. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub FileReadContent { | ||||||||||||||||||||||||||
|
Subroutine
FileReadContent: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (-r $file and defined (my $fh = IO::File->new($file))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-r $file and defined(my $fh = "IO::File"->new($file))
MC/DC0%0/2
-r $file and defined(my $fh = "IO::File"->new($file))
-r $file
defined(my $fh = "IO::File"->new($file))
Truth table0%0/3
A and B
A= -r $fileB= defined(my $fh = "IO::File"->new($file))
| |||||||||||||||||||||||||||||
| 134 | 0 | my ($tell, $content); | |||||||||||||||||||||||||||
| 135 | 0 | $fh->seek(0, SEEK_END); | |||||||||||||||||||||||||||
| 136 | 0 | $tell = $fh->tell; | |||||||||||||||||||||||||||
| 137 | 0 | $fh->seek(0, SEEK_SET); | |||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($fh->read($content, $tell) == $tell) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | return $content; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 0 | return; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =item [$temp_file] = Lim::Util::FileWriteContent([$file | $object,] $content) | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Write the content to a file or a new temporary file, content in file will be | ||||||||||||||||||||||||||||
| 148 | reread and checked with a SHA checksum. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | If the C<$file> is specified, write the content to the filename and return 1 or | ||||||||||||||||||||||||||||
| 151 | undef on error. Will overwrite the file if it exists. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | If the C<$object> is a L<Temp::File> object, write the content to that file and | ||||||||||||||||||||||||||||
| 154 | return 1 or undef on error. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | If no C<$file> or C<$object> is specified, write the content to a new temporary | ||||||||||||||||||||||||||||
| 157 | file and return the L<File::Temp> object or undef on error. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub FileWriteContent { | ||||||||||||||||||||||||||
|
Subroutine
FileWriteContent: not called CC=16 SCAR=56.1
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 0 | my ($file, $content) = @_; | |||||||||||||||||||||||||||
| 163 | 0 | my $filename; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if (defined $file and !defined $content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $file and not defined $content
MC/DC0%0/2
defined $file and not defined $content
defined $file
not defined $content
Truth table0%0/3
A and not(B)
A= defined $fileB= not defined $content
| |||||||||||||||||||||||||||||
| 166 | 0 | $content = $file; | |||||||||||||||||||||||||||
| 167 | 0 | undef($file); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 0 | ▶ | if (blessed $file) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 170 | 0 | ▶ | unless ($file->isa('File::Temp')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | return; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | $filename = $file->filename; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | elsif (defined $file) { | ||||||||||||||||||||||||||||
| 176 | 0 | my $fh = IO::File->new; | |||||||||||||||||||||||||||
| 177 | 0 | ▶ | unless ($fh->open($file, '>')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | return; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 0 | $filename = $file; | |||||||||||||||||||||||||||
| 181 | 0 | $file = $fh; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | unless (defined $content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | return; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | unless (defined $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | eval { | |||||||||||||||||||||||||||
| 188 | 0 | $file = File::Temp->new; | |||||||||||||||||||||||||||
| 189 | }; | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | # TODO log error | ||||||||||||||||||||||||||||
| 192 | 0 | return; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 0 | $filename = $file->filename; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | print $file $content; | |||||||||||||||||||||||||||
| 198 | 0 | $file->flush; | |||||||||||||||||||||||||||
| 199 | 0 | $file->close; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | my $fh = IO::File->new; | |||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ($fh->open($filename)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | my ($tell, $read); | |||||||||||||||||||||||||||
| 204 | 0 | $fh->seek(0, SEEK_END); | |||||||||||||||||||||||||||
| 205 | 0 | $tell = $fh->tell; | |||||||||||||||||||||||||||
| 206 | 0 | $fh->seek(0, SEEK_SET); | |||||||||||||||||||||||||||
| 207 | 0 | ▶ | unless ($fh->read($read, $tell) == $tell) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 0 | $fh->close; | |||||||||||||||||||||||||||
| 209 | 0 | ▶ | unless ($file->isa('File::Temp')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | unlink($filename); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | 0 | return; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | 0 | ▶ | unless (Digest::SHA::sha1_base64($content) eq Digest::SHA::sha1_base64($read)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 0 | $fh->close; | |||||||||||||||||||||||||||
| 216 | 0 | ▶ | unless ($file->isa('File::Temp')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 0 | unlink($filename); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | 0 | return; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | return $file->isa('File::Temp') ? $file : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =item $temp_file = Lim::Util::TempFile | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Creates a temporary file. Returns a L<File::Temp> object or undef if there where | ||||||||||||||||||||||||||||
| 228 | problems creating the temporary file. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =cut | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | ▶ | sub TempFile { | ||||||||||||||||||||||||||
|
Subroutine
TempFile: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 0 | my $tmp; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | eval { | |||||||||||||||||||||||||||
| 236 | 0 | $tmp = File::Temp->new; | |||||||||||||||||||||||||||
| 237 | }; | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 0 | ▶ | unless ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | # TODO log error | ||||||||||||||||||||||||||||
| 241 | 0 | return $tmp; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | 0 | return; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =item $temp_file = Lim::Util::TempFileLikeThis($file) | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | Creates a temporary file that will have the same owner and mode as the specified | ||||||||||||||||||||||||||||
| 249 | C<$file>. Returns a L<File::Temp> object or undef if the specified file did not | ||||||||||||||||||||||||||||
| 250 | exist or if there where problems creating the temporary file. | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =cut | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | ▶ | sub TempFileLikeThis { | ||||||||||||||||||||||||||
|
Subroutine
TempFileLikeThis: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 255 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | if (defined $file and -f $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $file and -f $file
MC/DC0%0/2
defined $file and -f $file
defined $file
-f $file
Truth table0%0/3
A and B
A= defined $fileB= -f $file
| |||||||||||||||||||||||||||||
| 258 | 0 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, | |||||||||||||||||||||||||||
| 259 | $atime,$mtime,$ctime,$blksize,$blocks) | ||||||||||||||||||||||||||||
| 260 | = stat($file); | ||||||||||||||||||||||||||||
| 261 | 0 | my $tmp; | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 0 | eval { | |||||||||||||||||||||||||||
| 264 | 0 | $tmp = File::Temp->new; | |||||||||||||||||||||||||||
| 265 | }; | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # TODO log error | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | ▶ | unless ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if (chmod($mode, $tmp->filename) and chown($uid, $gid, $tmp->filename)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
chmod $mode, $tmp->filename and chown $uid, $gid, $tmp->filename
MC/DC0%0/2
chmod $mode, $tmp->filename and chown $uid, $gid, $tmp->filename
chmod $mode, $tmp->filename
chown $uid, $gid, $tmp->filename
Truth table0%0/3
A and B
A= chmod $mode, $tmp->filenameB= chown $uid, $gid, $tmp->filename
| |||||||||||||||||||||||||||||
| 271 | 0 | return $tmp; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | 0 | return; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =item ($method, $uri) = Lim::Util::URIize($call) | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Returns an URI based on the C<$call> given and the corresponding HTTP method to | ||||||||||||||||||||||||||||
| 281 | be used. | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Example: | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =over 4 | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | use Lim::Util; | ||||||||||||||||||||||||||||
| 288 | ($method, $uri) = Lim::Util::URIize('ReadVersion'); | ||||||||||||||||||||||||||||
| 289 | print "$method $ur\n"; | ||||||||||||||||||||||||||||
| 290 | ($method, $uri) = Lim::Util::URIize('CreateOtherCall'); | ||||||||||||||||||||||||||||
| 291 | print "$method $ur\n"; | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =back | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | Produces: | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =over 4 | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | GET /version | ||||||||||||||||||||||||||||
| 300 | PUT /other_call | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =back | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =cut | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub URIize { | ||||||||||||||||||||||||||
|
Subroutine
URIize: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 307 | 0 | my @parts = split(/([A-Z][^A-Z]*)/o, $_[0]); | |||||||||||||||||||||||||||
| 308 | 0 | my ($part, $method, $uri); | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 0 | ▶ | while (scalar @parts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 0 | $part = shift(@parts); | |||||||||||||||||||||||||||
| 312 | 0 | ▶ | if ($part ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 0 | last; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 0 | ▶ | unless (exists $CALL_METHOD{$part}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 0 | confess __PACKAGE__, ': No conversion found for ', $part, ' (', $_[0], ')'; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | $method = $CALL_METHOD{$part}; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 0 | @parts = grep !/^$/o, @parts; | |||||||||||||||||||||||||||
| 324 | 0 | ▶ | unless (scalar @parts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | confess __PACKAGE__, ': Could not build URI (', $_[0], ')'; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | 0 | $uri = lc(join('_', @parts)); | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 0 | return ($method, '/'.$uri); | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =item $hash_ref = Lim::Util::QueryDecode($query_string) | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | Returns an HASH reference of the decode query string. | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =cut | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 0 | ▶ | sub QueryDecode { | ||||||||||||||||||||||||||
|
Subroutine
QueryDecode: not called CC=22 SCAR=62.3
Pod
documented
| |||||||||||||||||||||||||||||
| 339 | 0 | my ($href, $href_final) = ({}, {}); | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | foreach my $part (split(/&/o, $_[0])) { | |||||||||||||||||||||||||||
| 342 | 0 | my ($key, $value) = split(/=/o, $part, 2); | |||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 0 | $key = uri_unescape($key); | |||||||||||||||||||||||||||
| 345 | 0 | $value = uri_unescape($value); | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 0 | ▶ | unless ($key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 0 | return; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # check if last element is array and remove it from $key | ||||||||||||||||||||||||||||
| 352 | 0 | ▶ | my $array = $key =~ s/\[\]$//o ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | # verify $key | ||||||||||||||||||||||||||||
| 354 | 0 | ▶ | unless ($key =~ /^[^\]]+(?:\[[^\]]+\])*$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 0 | return; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | # remove last ] so we don't split or get it in $k | ||||||||||||||||||||||||||||
| 358 | 0 | $key =~ s/\]$//o; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 0 | my @keys = split(/(?:\]\[|\[)/o, $key); | |||||||||||||||||||||||||||
| 361 | 0 | my $this = $href; | |||||||||||||||||||||||||||
| 362 | 0 | ▶ | while (defined (my $k = shift(@keys))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 0 | ▶ | unless (scalar @keys) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | 0 | ▶ | if ($array and exists $this->{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$array and exists $this->{$k}
MC/DC0%0/2
$array and exists $this->{$k}
$array
exists $this->{$k}
Truth table0%0/3
A and B
A= $arrayB= exists $this->{$k}
| |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | unless (ref($this->{$k}) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 0 | return; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | 0 | push(@{$this->{$k}}, $value); | |||||||||||||||||||||||||||
| 369 | 0 | last; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | 0 | ▶ | $this->{$k} = $array ? [ $value ] : $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 0 | last; | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | ▶ | if (exists $this->{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | 0 | $this = $this->{$k}; | |||||||||||||||||||||||||||
| 377 | 0 | next; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | $this = $this->{$k} = {}; | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | # restruct hashes with all numeric keys to arrays | ||||||||||||||||||||||||||||
| 385 | 0 | my @process = ([$href, $href_final, undef, undef]); | |||||||||||||||||||||||||||
| 386 | 0 | ▶ | while (defined (my $this = shift(@process))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 0 | my ($old, $new, $parent, $key) = @$this; | |||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 0 | my $numeric = 1; | |||||||||||||||||||||||||||
| 390 | 0 | foreach (keys %$old) { | |||||||||||||||||||||||||||
| 391 | 0 | ▶ | unless (/^\d+$/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 0 | $numeric = 0; | |||||||||||||||||||||||||||
| 393 | 0 | last; | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 0 | ▶ | if ($numeric) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 0 | my @array; | |||||||||||||||||||||||||||
| 399 | 0 | foreach (sort (keys %$old)) { | |||||||||||||||||||||||||||
| 400 | 0 | ▶ | if (ref($old->{$_}) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | 0 | my $entry = {}; | |||||||||||||||||||||||||||
| 402 | 0 | push(@array, $entry); | |||||||||||||||||||||||||||
| 403 | 0 | push(@process, [$old->{$_}, $entry, \@array, scalar @array - 1]); | |||||||||||||||||||||||||||
| 404 | 0 | next; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | 0 | push(@array, $old->{$_}); | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 0 | ▶ | if (ref($parent) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 410 | 0 | $parent->{$key} = \@array; | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | elsif (ref($parent) eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 413 | 0 | $parent->[$key] = \@array; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | else { | ||||||||||||||||||||||||||||
| 416 | 0 | return; | |||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | else { | ||||||||||||||||||||||||||||
| 420 | 0 | foreach (keys %$old) { | |||||||||||||||||||||||||||
| 421 | 0 | ▶ | if (ref($old->{$_}) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 422 | 0 | $new->{$_} = {}; | |||||||||||||||||||||||||||
| 423 | 0 | push(@process, [$old->{$_}, $new->{$_}, $new, $_]); | |||||||||||||||||||||||||||
| 424 | 0 | next; | |||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | 0 | $new->{$_} = $old->{$_}; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 0 | return $href_final; | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =item $camelized = Lim::Util::Camelize($underscore) | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | Convert underscored text to camelized, used for translating URI to calls. | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | Example: | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =over 4 | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | use Lim::Util; | ||||||||||||||||||||||||||||
| 443 | print Lim::Util::Camelize('long_u_r_i_call_name'), "\n"; | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =back | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | Produces: | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =over 4 | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | LongURICallName | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | =back | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =cut | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 0 | ▶ | sub Camelize { | ||||||||||||||||||||||||||
|
Subroutine
Camelize: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 458 | 0 | my ($underscore) = @_; | |||||||||||||||||||||||||||
| 459 | 0 | my $camelized; | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | 0 | foreach (split(/_/o, $underscore)) { | |||||||||||||||||||||||||||
| 462 | 0 | $camelized .= ucfirst($_); | |||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 0 | return $camelized; | |||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =item [$cv =] Lim::Util::run_cmd $cmd, key => value... | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | This function extends L<AnyEvent::Util::run_cmd> with a timeout and will also | ||||||||||||||||||||||||||||
| 471 | set C<close_all> option. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =over 4 | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =item timeout => $seconds | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | Creates a timeout for the running command and will try and kill it after the | ||||||||||||||||||||||||||||
| 478 | specified C<$seconds>, see below how you can change the kill functionallity. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | Using C<timeout> will set C<$$> option to L<AnyEvent::Util::run_cmd> so you | ||||||||||||||||||||||||||||
| 481 | won't be able to use that option. | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =item cb => $callback->($cv) | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | This is required if you'r using C<timeout>. | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | Call the given C<$callback> when the command finish or have timed out with the | ||||||||||||||||||||||||||||
| 488 | condition variable returned by L<AnyEvent::Util::run_cmd>. If the command timed | ||||||||||||||||||||||||||||
| 489 | out the condition variable will be set as if the command failed. | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | =item kill_sig => 15 | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | Signal to use when trying to kill the command. | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =item kill_try => 3 | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | Number of times to try and kill the command with C<kill_sig>. | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | =item interval => 1 | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | Number of seconds to wait between each attempt to kill the command. | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | =item kill_kill => 1 | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | If true (default) kill the command with signal KILL after trying to kill it with | ||||||||||||||||||||||||||||
| 506 | C<kill_sig> for the specified number of C<kill_try> attempts. | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | =back | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =cut | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 0 | ▶ | sub run_cmd { | ||||||||||||||||||||||||||
|
Subroutine
run_cmd: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 513 | 0 | my $cmd = shift; | |||||||||||||||||||||||||||
| 514 | 0 | my %args = ( | |||||||||||||||||||||||||||
| 515 | kill_try => 3, | ||||||||||||||||||||||||||||
| 516 | kill_kill => 1, | ||||||||||||||||||||||||||||
| 517 | kill_sig => 15, | ||||||||||||||||||||||||||||
| 518 | interval => 1, | ||||||||||||||||||||||||||||
| 519 | @_ | ||||||||||||||||||||||||||||
| 520 | ); | ||||||||||||||||||||||||||||
| 521 | 0 | my ($pid, $timeout) = (0, undef); | |||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | 0 | my %pass_args = %args; | |||||||||||||||||||||||||||
| 524 | 0 | foreach (qw(kill_try kill_kill timeout interval cb)) { | |||||||||||||||||||||||||||
| 525 | 0 | delete $pass_args{$_}; | |||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||
| 527 | 0 | $pass_args{close_all} = 1; | |||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | 0 | ▶ | if (exists $args{timeout}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 530 | 0 | $pass_args{'$$'} = \$pid; | |||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | 0 | ▶ | unless (exists $args{cb} and ref($args{cb}) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $args{"cb"} and ref $args{"cb"} eq "CODE"
MC/DC0%0/2
exists $args{"cb"} and ref $args{"cb"} eq "CODE"
exists $args{"cb"}
ref $args{"cb"} eq "CODE"
Truth table0%0/3
A and B
A= exists $args{"cb"}B= ref $args{"cb"} eq "CODE"
| |||||||||||||||||||||||||||||
| 533 | 0 | confess __PACKAGE__, ': must have cb with timeout or invalid'; | |||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | 0 | ▶ | unless ($args{timeout} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 537 | 0 | confess __PACKAGE__, ': timeout invalid'; | |||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | 0 | ▶ | unless ($args{interval} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 541 | 0 | confess __PACKAGE__, ': interval invalid'; | |||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | 0 | ▶ | unless ($args{kill_try} >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 545 | 0 | confess __PACKAGE__, ': kill_try invalid'; | |||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | $timeout = AnyEvent->timer( | ||||||||||||||||||||||||||||
| 549 | after => $args{timeout}, | ||||||||||||||||||||||||||||
| 550 | interval => $args{interval}, | ||||||||||||||||||||||||||||
| 551 | 0 | ▶ | cb => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 552 | 0 | ▶ | unless ($pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 553 | 0 | undef($timeout); | |||||||||||||||||||||||||||
| 554 | 0 | return; | |||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | 0 | ▶ | if ($args{kill_try}--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 558 | 0 | ▶ | Lim::DEBUG and Log::Log4perl->get_logger->debug('trying to kill cmd ', (ref($cmd) eq 'ARRAY' ? join(' ', @$cmd) : $cmd)); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 559 | 0 | kill($args{kill_sig}, $pid); | |||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||
| 561 | else { | ||||||||||||||||||||||||||||
| 562 | 0 | ▶ | if ($args{kill_kill}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | 0 | ▶ | Lim::DEBUG and Log::Log4perl->get_logger->debug('killing cmd ', (ref($cmd) eq 'ARRAY' ? join(' ', @$cmd) : $cmd)); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 564 | 0 | kill(9, $pid); | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | 0 | undef($timeout); | |||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||
| 568 | 0 | }); | |||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | 0 | ▶ | Lim::DEBUG and Log::Log4perl->get_logger->debug('run_cmd [timeout ', $args{timeout},'] ', (ref($cmd) eq 'ARRAY' ? join(' ', @$cmd) : $cmd)); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | 0 | my $cv = AnyEvent::Util::run_cmd | |||||||||||||||||||||||||||
| 573 | $cmd, | ||||||||||||||||||||||||||||
| 574 | %pass_args; | ||||||||||||||||||||||||||||
| 575 | 0 | ▶ | $cv->cb(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 576 | 0 | ▶ | Lim::DEBUG and Log::Log4perl->get_logger->debug('cmd end ', (ref($cmd) eq 'ARRAY' ? join(' ', @$cmd) : $cmd)); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 577 | 0 | undef($timeout); | |||||||||||||||||||||||||||
| 578 | 0 | $args{cb}->(@_); | |||||||||||||||||||||||||||
| 579 | 0 | }); | |||||||||||||||||||||||||||
| 580 | 0 | return; | |||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | 0 | ▶ | Lim::DEBUG and Log::Log4perl->get_logger->debug('run_cmd ', (ref($cmd) eq 'ARRAY' ? join(' ', @$cmd) : $cmd)); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | 0 | return AnyEvent::Util::run_cmd | |||||||||||||||||||||||||||
| 586 | $cmd, | ||||||||||||||||||||||||||||
| 587 | %pass_args; | ||||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | =item Lim::Util::resolve_host $host, $port, $cb->($ipAddress, $port); | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | =cut | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | 4 | ▶ | sub resolve_host { | ||||||||||||||||||||||||||
|
Subroutine
resolve_host: called CC=5 SCAR=27.4
Pod
documented
| |||||||||||||||||||||||||||||
| 595 | 4 | my ($host, $port, $cb) = @_; | |||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | 4 | ▶ | unless (defined $host) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 598 | 0 | confess __PACKAGE__, ': Missing host'; | |||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||
| 600 | 4 | ▶ | unless (ref($cb) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 601 | 0 | confess __PACKAGE__, ': Missing cb or is not CODE'; | |||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 4 | ▶ | if (AnyEvent::Socket::parse_address($host)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 605 | 4 | $cb->($host, $port); | |||||||||||||||||||||||||||
| 606 | 4 | return; | |||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | 0 | ▶ | if (Lim::Config->{rpc}->{skip_dns}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 610 | 0 | $cb->(Lim::Util::resolve_hosts($host), $port); | |||||||||||||||||||||||||||
| 611 | 0 | return; | |||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | 0 | ▶ | AnyEvent::Socket::resolve_sockaddr $host, $port, 0, undef, undef, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 615 | 0 | ▶ | unless (scalar @_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 616 | 0 | ▶ | if (AnyEvent::Socket->VERSION < 6.01) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 617 | 0 | $cb->(Lim::Util::resolve_hosts($host), $port); | |||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||
| 619 | else { | ||||||||||||||||||||||||||||
| 620 | 0 | $cb->(); | |||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||
| 622 | 0 | return; | |||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | 0 | ▶ | unless (ref($_[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 626 | # TODO: warn? | ||||||||||||||||||||||||||||
| 627 | 0 | $cb->(); | |||||||||||||||||||||||||||
| 628 | 0 | return; | |||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | 0 | my ($service, $ipn) = AnyEvent::Socket::unpack_sockaddr($_[0]->[3]); | |||||||||||||||||||||||||||
| 632 | 0 | $cb->(AnyEvent::Socket::format_address($ipn), $service); | |||||||||||||||||||||||||||
| 633 | 0 | }; | |||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | =item $ipAddress = Lim::Util::resolve_hosts $host | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | =cut | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | 0 | ▶ | sub resolve_hosts { | ||||||||||||||||||||||||||
|
Subroutine
resolve_hosts: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 641 | 0 | my ($host, $cb) = @_; | |||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | 0 | ▶ | unless (defined $host) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 644 | 0 | confess __PACKAGE__, ': Missing host'; | |||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | 0 | ▶ | if (open(HOSTS, $^O eq 'MSWin32' ? $ENV{SystemRoot}.'/system32/drivers/etc/hosts' : '/etc/hosts')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 648 | 0 | ▶ | while(<HOSTS>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 649 | 0 | s/[\r\n]+$//o; | |||||||||||||||||||||||||||
| 650 | 0 | s/#.*//o; | |||||||||||||||||||||||||||
| 651 | 0 | s/^\s+//o; | |||||||||||||||||||||||||||
| 652 | 0 | s/\s+$//o; | |||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | 0 | my ($addr, @aliases) = split(/\s+/o); | |||||||||||||||||||||||||||
| 655 | 0 | ▶ | if (grep(/^$host$/, @aliases)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 656 | 0 | close(HOSTS); | |||||||||||||||||||||||||||
| 657 | 0 | return $addr; | |||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||
| 659 | } | ||||||||||||||||||||||||||||
| 660 | 0 | close(HOSTS); | |||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | 0 | return; | |||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | =back | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >> | ||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | =head1 BUGS | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>. | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | perldoc Lim::Util | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | You can also look for information at: | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | =over 4 | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | =item * Lim issue tracker (report bugs here) | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | L<https://github.com/jelu/lim/issues> | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | =back | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | Copyright 2012-2013 Jerry Lundström. | ||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 699 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 700 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | =cut | ||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||
| 707 | 10 | 1; # End of Lim::Util | |||||||||||||||||||||||||||