X-Git-Url: https://defaria.com/gitweb/?a=blobdiff_plain;f=lib%2FUtils.pm;h=b9be1e45a510daaeb0955ddd7ff80a83102816ed;hb=f46862e41025e6d96509056229c28a90eb0ceb40;hp=9666d81d3e7ef3d1c3e28dd4a8d67e3bd1afd357;hpb=14a28ba376f8fd4fc69453813a46c9f48419a76f;p=clearscm.git diff --git a/lib/Utils.pm b/lib/Utils.pm index 9666d81..b9be1e4 100644 --- a/lib/Utils.pm +++ b/lib/Utils.pm @@ -79,6 +79,7 @@ our @EXPORT = qw ( PipeOutput PipeOutputArray ReadFile + RequiredFields RedirectOutput StartPipe Stats @@ -89,9 +90,9 @@ our @EXPORT = qw ( sub _restoreTerm () { # In case the user hits Ctrl-C print "\nControl-C\n"; - + ReadMode 'normal'; - + exit; } # _restoreTerm @@ -142,7 +143,7 @@ Returns: $errorlog ||= $NULL; my $file; - + # Redirect STDIN to $NULL open STDIN, '<', $NULL or error "Can't read $NULL ($!)", 1; @@ -156,7 +157,7 @@ Returns: or error "Can't write to $errorlog ($!)", 1; # Change the current directory to / - my $ROOT = $ARCH eq "windows" ? "C:\\" : "/"; + my $ROOT = $ARCHITECTURE eq "windows" ? "C:\\" : "/"; chdir $ROOT or error "Can't chdir to $ROOT ($!), 1"; @@ -169,7 +170,7 @@ Returns: # Now the parent exits exit if $pid; - + # Write pidfile if specified if ($pidfile) { $pidfile = File::Spec->rel2abs ($pidfile); @@ -178,14 +179,14 @@ Returns: or warning "Unable to open pidfile $pidfile for writing - $!"; print $file "$$\n"; - + close $file; } # if - + # Set process to be session leader setsid () or error "Can't start a new session ($!)", 1; - + return; } # EnterDaemonMode @@ -232,16 +233,10 @@ STDOUT then do so in the $command passed in. =cut - # Save $SIG{CHLD} so we can set it to 'DEFAULT' and then restore it later. - # Helps when you are doing process handling. - my $sigchld = $SIG{CHLD}; - local $SIG{CHLD} = 'DEFAULT'; my @output = `$cmd`; my $status = $?; - - local $SIG{CHLD} = $sigchld; chomp @output; @@ -347,22 +342,21 @@ Returns: =cut - $prompt ||= 'Password'; - - my $password; - + + my $password = ''; + local $| = 1; - + print "$prompt:"; - + $SIG{INT} = \&_restoreTerm; - + ReadMode 'cbreak'; while () { my $key; - + while (not defined ($key = ReadKey -1)) { } if ($key =~ /(\r|\n)/) { @@ -371,15 +365,24 @@ Returns: last; } # if - print '*'; - - $password .= $key; + # Handle backspaces + if ($key eq chr(127)) { + unless ($password eq '') { + chop $password; + + print "\b \b"; + } # unless + } else { + print '*'; + + $password .= $key; + } # if } # while - + ReadMode 'restore'; # Reset tty mode before exiting. $SIG{INT} = 'DEFAULT'; - + return $password; } # GetPassword @@ -468,16 +471,16 @@ In a scalar context just the 1 minute load average. # TODO: Make it work on Windows... return if $^O =~ /win/i; - - open my $loadAvg, '/proc/loadavg' + + open my $loadAvg, '<', '/proc/loadavg' or croak "Unable to open /proc/loadavg\n"; - + my $load = <$loadAvg>; - + close $loadAvg; - + my @loadAvgs = split /\s/, $load; - + if (wantarray) { return @loadAvgs; } else { @@ -530,10 +533,10 @@ Returns: if ($existingPipe) { close $existingPipe; - + open $existingPipe, '|-', $to or error "Unable to open pipe - $!", 1; - + return $existingPipe; } else { open $pipe, '|-', $to @@ -686,7 +689,7 @@ Returns: $pipeToStop ||= $pipe; close $pipeToStop if $pipeToStop; - + return; } # StopPipe @@ -739,7 +742,7 @@ Returns: sub RedirectOutput ($$@) { my ($to, $mode, @output) = @_; - + =pod =head2 RedirectOutput ($to, @ouput) @@ -788,7 +791,7 @@ Returns: chomp; print $out "$_\n"; } # foreach - + return; } # RedirectOutput @@ -835,27 +838,27 @@ Returns: open my $file, '<', $filename or error "Unable to open $filename ($!)", 1; - + if (wantarray) { local $/ = "\n"; my @lines = <$file>; - + close $file or error "Unable to close $filename ($!)", 1; - + my @cleansed_lines; - + foreach (@lines) { chomp; chop if /\r/; push @cleansed_lines, $_ if !/^#/; # Discard comment lines } # foreach - + return @cleansed_lines; } else { local $/ = undef; - + return <$file>; } # if } # ReadFile @@ -904,12 +907,12 @@ Returns: =cut my $msg = "$FindBin::Script Run Statistics:"; - + if ($log and ref $log eq 'Logger') { $total->{errors} = $log->{errors}; $total->{warnings} = $log->{warnings}; } # if - + if (keys %$total) { # Display statistics (if any) if ($log) { @@ -920,7 +923,7 @@ Returns: foreach (sort keys %$total) { $msg = $total->{$_} . "\t $_"; - + if ($log) { $log->msg ($total->{$_} . "\t $_"); } else { @@ -928,7 +931,7 @@ Returns: } # if } # foreach } # if - + return; } # Stats @@ -977,6 +980,67 @@ Returns: exit 1; } # Usage +sub RequiredFields($$) { + +=pod + +=head2 RequiredFields($total, $log) + +Check if a list of fields are contained in a hash + +Parameters: + +=for html
+ +=over + +=item $fields + +Array reference to a list of field names that are required + +=item $rec + +Hash reference whose key values we are checking + +=back + +=for html
+ +Returns: + +=for html
+ +=over + +=item Message + +Returns either an empty string or a string naming the first missing required +field + +=back + +=for html
+ +=cut + + my ($fields, $rec) = @_; + + for my $fieldname (@$fields) { + my $found = 0; + + for (keys %$rec) { + if ($fieldname eq $_) { + $found = 1; + last; + } # if + } # for + + return "$fieldname is required" unless $found; + } # for + + return; +} # RequiredFields + END { StopPipe; } # END