~ubuntu-branches/ubuntu/vivid/drizzle/vivid-proposed

« back to all changes in this revision

Viewing changes to tests/kewpie/randgen/lib/DBServer/DBServer.pm

  • Committer: Package Import Robot
  • Author(s): Tobias Frost
  • Date: 2013-08-22 20:18:31 UTC
  • mto: (20.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20130822201831-gn3ozsh7o7wmc5tk
Tags: upstream-7.2.3
ImportĀ upstreamĀ versionĀ 7.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights
2
 
# reserved.
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; version 2 of the License.
7
 
#
8
 
# This program is distributed in the hope that it will be useful, but
9
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
 
# General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
16
 
# USA
17
 
 
18
 
package DBServer::DBServer;
19
 
use base 'Exporter';
20
 
 
21
 
@EXPORT = ('say', 'sayFile', 'tmpdir', 'safe_exit', 
22
 
           'osWindows', 'osLinux', 'osSolaris', 'osMac',
23
 
           'isoTimestamp', 'isoUTCTimestamp',
24
 
           'DBSTATUS_OK','DBSTATUS_FAILURE');
25
 
 
26
 
use strict;
27
 
 
28
 
use Cwd;
29
 
use POSIX;
30
 
use Carp;
31
 
 
32
 
my $logger;
33
 
eval
34
 
{
35
 
    require Log::Log4perl;
36
 
    Log::Log4perl->import();
37
 
    $logger = Log::Log4perl->get_logger('randgen.dbserver');
38
 
};
39
 
 
40
 
use constant DBSTATUS_OK => 0;
41
 
use constant DBSTATUS_FAILURE => 1;
42
 
 
43
 
my $tmpdir;
44
 
 
45
 
1;
46
 
 
47
 
sub BEGIN {
48
 
        foreach my $tmp ($ENV{TMP}, $ENV{TEMP}, $ENV{TMPDIR}, '/tmp', '/var/tmp', cwd()."/tmp" ) {
49
 
                if (
50
 
                        (defined $tmp) &&
51
 
                        (-e $tmp)
52
 
                ) {
53
 
                        $tmpdir = $tmp;
54
 
                        last;
55
 
                }
56
 
        }
57
 
 
58
 
        if (defined $tmpdir) {
59
 
                if (
60
 
                        ($^O eq 'MSWin32') ||
61
 
                        ($^O eq 'MSWin64')
62
 
                ) {
63
 
                        $tmpdir = $tmpdir.'\\';
64
 
                } else {
65
 
                        $tmpdir = $tmpdir.'/';
66
 
                }
67
 
        }
68
 
 
69
 
        croak("Unable to locate suitable temporary directory.") if not defined $tmpdir;
70
 
        
71
 
        return 1;
72
 
}
73
 
 
74
 
sub new {
75
 
        my $class = shift;
76
 
        my $args = shift;
77
 
 
78
 
        my $obj = bless ([], $class);
79
 
 
80
 
        my $max_arg = (scalar(@_) / 2) - 1;
81
 
 
82
 
        foreach my $i (0..$max_arg) {
83
 
                if (exists $args->{$_[$i * 2]}) {
84
 
                        if (defined $obj->[$args->{$_[$i * 2]}]) {
85
 
                                carp("Argument '$_[$i * 2]' passed twice to ".$class.'->new()');
86
 
                        } else {
87
 
                                $obj->[$args->{$_[$i * 2]}] = $_[$i * 2 + 1];
88
 
                        }
89
 
                } else {
90
 
                        carp("Unkown argument '$_[$i * 2]' to ".$class.'->new()');
91
 
                }
92
 
        }
93
 
 
94
 
        return $obj;
95
 
}
96
 
 
97
 
sub say {
98
 
        my $text = shift;
99
 
    defaultLogging();
100
 
    if ($text =~ m{[\r\n]}sio) {
101
 
        foreach my $line (split (m{[\r\n]}, $text)) {
102
 
            if (defined $logger) {
103
 
                $logger->info($line);
104
 
            } else {
105
 
                print "# ".isoTimestamp()." $line\n";
106
 
            }
107
 
        }
108
 
    } else {
109
 
        if (defined $logger) {
110
 
            $logger->info($text);
111
 
        } else {
112
 
            print "# ".isoTimestamp()." $text\n";
113
 
        }
114
 
    }
115
 
}
116
 
 
117
 
sub sayFile {
118
 
    my ($file) = @_;
119
 
 
120
 
    say("--------- Contents of $file -------------");
121
 
    open FILE,$file;
122
 
    while (<FILE>) {
123
 
        say("| ".$_);
124
 
    }
125
 
    close FILE;
126
 
    say("----------------------------------");
127
 
}
128
 
 
129
 
sub tmpdir {
130
 
        return $tmpdir;
131
 
}
132
 
 
133
 
sub safe_exit {
134
 
        my $exit_status = shift;
135
 
        POSIX::_exit($exit_status);
136
 
}
137
 
 
138
 
sub osWindows {
139
 
        if (
140
 
                ($^O eq 'MSWin32') ||
141
 
                ($^O eq 'MSWin64')
142
 
        ) {
143
 
                return 1;
144
 
        } else {
145
 
                return 0;
146
 
        }       
147
 
}
148
 
 
149
 
sub osLinux {
150
 
        if ($^O eq 'linux') {
151
 
                return 1;
152
 
        } else {
153
 
                return 0;
154
 
        }
155
 
}
156
 
 
157
 
sub osSolaris {
158
 
        if ($^O eq 'solaris') {
159
 
                return 1;
160
 
        } else {
161
 
                return 0;
162
 
        }       
163
 
}
164
 
 
165
 
sub osMac {
166
 
    if ($^O eq 'darwin') {
167
 
        return 1;
168
 
    } else {
169
 
        return 0;
170
 
   }
171
 
}
172
 
 
173
 
sub isoTimestamp {
174
 
        my $datetime = shift;
175
 
 
176
 
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? localtime($datetime) : localtime();
177
 
        return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
178
 
 
179
 
}
180
 
 
181
 
sub isoUTCTimestamp {
182
 
        my $datetime = shift;
183
 
 
184
 
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
185
 
        return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
186
 
        
187
 
}
188
 
 
189
 
sub defaultLogging {
190
 
    if (defined $logger) {
191
 
        if (not Log::Log4perl::initialized()) {
192
 
            my $logconf = q(
193
 
log4perl.rootLogger = INFO, STDOUT
194
 
log4perl.appender.STDOUT=Log::Log4perl::Appender::Screen
195
 
log4perl.appender.STDOUT.layout=PatternLayout
196
 
log4perl.appender.STDOUT.layout.ConversionPattern=# %d{yyyy-MM-dd'T'HH:mm:ss} %m%n
197
 
);
198
 
            Log::Log4perl::init( \$logconf );
199
 
            say("Using Log::Log4perl");
200
 
        }
201
 
    }
202
 
}
203
 
 
204
 
1;