Subversion Repositories havirt

Rev

Rev 10 | Rev 15 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 rodolico 1
#!/usr/bin/env perl
2
 
3
# Common library for havirt. Basically, just a place to put things which may be used by any
4 rodolico 4
# part of havirt. More for organizations purposes.
3 rodolico 5
 
4 rodolico 6
# Copyright 2024 Daily Data, Inc.
7
# 
8
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 
9
# conditions are met:
10
#
11
#   Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
12
#   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
13
#   in the documentation and/or other materials provided with the distribution.
14
#   Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
15
#   from this software without specific prior written permission.
16
# 
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
18
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 
24
 
3 rodolico 25
# v0.0.1 20240602 RWR
26
# Initial setup
27
 
28
package havirt;
29
 
30
 
31
use warnings;
32
use strict;  
33
 
34
use Data::Dumper qw(Dumper); # Import the Dumper() subroutine
35
 
4 rodolico 36
# define the version number
37
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
38
use version;
39
our $VERSION = version->declare("0.0.1");
40
 
41
 
3 rodolico 42
use Exporter;
43
 
44
our @ISA = qw( Exporter );
45
our @EXPORT = qw( 
46
                  &readDB &writeDB
4 rodolico 47
                  &report
3 rodolico 48
                );
49
 
12 rodolico 50
# read a DB file (just a YAML)
51
# if $lock is set, will create a "lock" file so other processes will
52
# not try to write to it. Using custom code as flock is automagically
53
# release when the file is read
3 rodolico 54
 
55
sub readDB {
12 rodolico 56
   my $lock = shift;
57
   my $lockFileName = "$main::statusDBName.lock";
58
   my $lockTime = 5; # maximum time to wait for lock to clear
59
   # wait for lock to clear if it exists, if we are wanting a lock
60
   # and we have tried it for $locktime iterations
61
   while ( $lock && -f $lockFileName && $lockTime-- ) {
62
      sleep 1; # wait one second, then try again
63
   }
64
   if ( $lock ) {
65
      die "Something has $main::statusDBName locked, aborting\n" if -f $lockFileName;
66
      `touch $lockFileName`;
67
   }
3 rodolico 68
   my $yaml = YAML::Tiny->new( {} );
12 rodolico 69
   if ( -f $main::statusDBName ) {
70
      $yaml = YAML::Tiny->read( $main::statusDBName );
3 rodolico 71
   }
12 rodolico 72
   $main::statusDB = $yaml->[0];
3 rodolico 73
}
74
 
75
sub writeDB {
12 rodolico 76
   my $yaml = YAML::Tiny->new( $main::statusDB );
77
   $yaml->write( $main::statusDBName );
78
   unlink "$main::statusDBName.lock" if -f "$main::statusDBName.lock"; # release any lock we might have on it
3 rodolico 79
}
80
 
4 rodolico 81
sub report {
82
   if ( $main::reportFormat eq 'tsv' ) {
83
      return &report_tsv( @_ );
84
   } else {
85
      return &report_screen( @_ );
86
   }
87
}
88
 
3 rodolico 89
sub report_tsv {
90
   my ( $header, $data ) = @_;
91
   my @output;
92
   push @output, join( "\t", @$header );
93
   for( my $line = 0; $line < @$data; $line++ ) {
94
      push @output, join( "\t", @{$data->[$line]} );
95
   } # for
96
   return join( "\n", @output ) . "\n";
97
}
98
 
99
sub report_screen {
100
   my ( $header, $data ) = @_;
101
   my @output;
102
   my @widths;
103
   my $column;
104
   my $row;
105
   # First, initialize by using the length of the headers
106
   for ( $column = 0; $column < @$header; $column++ ) {
107
      @widths[$column] = length( $header->[$column] );
108
   }
109
   # now, go through all data in each row, for each column, and increment the width if it is larger
110
   for ( $row = 0; $row < @$data; $row++ ) {
111
      for ( $column = 0; $column < @$header; $column++ ) {
112
         $widths[$column] = length( $data->[$row][$column] ) 
113
            if length( $data->[$row][$column] ) > $widths[$column];
114
      } # for column
115
   } # for row
116
   # actually do the print now
117
   my @format;
118
   for ( $column = 0; $column < @widths; $column++ ) {
119
      push ( @format, '%' . $widths[$column] . 's' );
120
   }
121
   my $format = join( ' ', @format ) . "\n";
122
   my $output = sprintf( $format, @$header );
123
   for ( $row = 0; $row < @$data; $row++ ) {
124
      $output .= sprintf( $format, @{$data->[$row]} );
125
   } # for row
126
   return $output;
127
}
10 rodolico 128