~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2008, 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 GenTest;
 
19
use base 'Exporter';
 
20
 
 
21
@EXPORT = ('say', 'sayFile', 'tmpdir', 'safe_exit', 
 
22
           'osWindows', 'osLinux', 'osSolaris', 'osMac',
 
23
           'isoTimestamp', 'isoUTCTimestamp', 'isoUTCSimpleTimestamp', 
 
24
           'rqg_debug', 'unix2winPath',
 
25
           'setLoggingToFile','setLogConf');
 
26
 
 
27
use strict;
 
28
 
 
29
use Cwd;
 
30
use POSIX;
 
31
use Carp;
 
32
 
 
33
my $logger;
 
34
eval
 
35
{
 
36
    require Log::Log4perl;
 
37
    Log::Log4perl->import();
 
38
    $logger = Log::Log4perl->get_logger('randgen.gentest');
 
39
};
 
40
 
 
41
my $tmpdir;
 
42
 
 
43
1;
 
44
 
 
45
sub BEGIN {
 
46
        foreach my $tmp ($ENV{TMP}, $ENV{TEMP}, $ENV{TMPDIR}, '/tmp', '/var/tmp', cwd()."/tmp" ) {
 
47
                if (
 
48
                        (defined $tmp) &&
 
49
                        (-e $tmp)
 
50
                ) {
 
51
                        $tmpdir = $tmp;
 
52
                        last;
 
53
                }
 
54
        }
 
55
 
 
56
        if (defined $tmpdir) {
 
57
                if (
 
58
                        ($^O eq 'MSWin32') ||
 
59
                        ($^O eq 'MSWin64')
 
60
                ) {
 
61
                        $tmpdir = $tmpdir.'\\';
 
62
                } else {
 
63
                        $tmpdir = $tmpdir.'/';
 
64
                }
 
65
        }
 
66
 
 
67
        croak("Unable to locate suitable temporary directory.") if not defined $tmpdir;
 
68
        
 
69
        return 1;
 
70
}
 
71
 
 
72
sub new {
 
73
        my $class = shift;
 
74
        my $args = shift;
 
75
 
 
76
        my $obj = bless ([], $class);
 
77
 
 
78
        my $max_arg = (scalar(@_) / 2) - 1;
 
79
 
 
80
        foreach my $i (0..$max_arg) {
 
81
                if (exists $args->{$_[$i * 2]}) {
 
82
                        if (defined $obj->[$args->{$_[$i * 2]}]) {
 
83
                                carp("Argument '$_[$i * 2]' passed twice to ".$class.'->new()');
 
84
                        } else {
 
85
                                $obj->[$args->{$_[$i * 2]}] = $_[$i * 2 + 1];
 
86
                        }
 
87
                } else {
 
88
                        carp("Unkown argument '$_[$i * 2]' to ".$class.'->new()');
 
89
                }
 
90
        }
 
91
 
 
92
        return $obj;
 
93
}
 
94
 
 
95
sub say {
 
96
        my $text = shift;
 
97
    defaultLogging();
 
98
    if ($text =~ m{[\r\n]}sio) {
 
99
        foreach my $line (split (m{[\r\n]}, $text)) {
 
100
            if (defined $logger) {
 
101
                $logger->info($line);
 
102
            } else {
 
103
                print "# ".isoTimestamp()." $line\n";
 
104
            }
 
105
        }
 
106
    } else {
 
107
        if (defined $logger) {
 
108
            $logger->info($text);
 
109
        } else {
 
110
            print "# ".isoTimestamp()." $text\n";
 
111
        }
 
112
    }
 
113
}
 
114
 
 
115
sub sayFile {
 
116
    my ($file) = @_;
 
117
 
 
118
    say("--------- Contents of $file -------------");
 
119
    open FILE,$file;
 
120
    while (<FILE>) {
 
121
        say("| ".$_);
 
122
    }
 
123
    close FILE;
 
124
    say("----------------------------------");
 
125
}
 
126
 
 
127
sub tmpdir {
 
128
        return $tmpdir;
 
129
}
 
130
 
 
131
sub safe_exit {
 
132
        my $exit_status = shift;
 
133
        POSIX::_exit($exit_status);
 
134
}
 
135
 
 
136
sub osWindows {
 
137
        if (
 
138
                ($^O eq 'MSWin32') ||
 
139
                ($^O eq 'MSWin64')
 
140
        ) {
 
141
                return 1;
 
142
        } else {
 
143
                return 0;
 
144
        }       
 
145
}
 
146
 
 
147
sub osLinux {
 
148
        if ($^O eq 'linux') {
 
149
                return 1;
 
150
        } else {
 
151
                return 0;
 
152
        }
 
153
}
 
154
 
 
155
sub osSolaris {
 
156
        if ($^O eq 'solaris') {
 
157
                return 1;
 
158
        } else {
 
159
                return 0;
 
160
        }       
 
161
}
 
162
 
 
163
sub osMac {
 
164
    if ($^O eq 'darwin') {
 
165
        return 1;
 
166
    } else {
 
167
        return 0;
 
168
    }
 
169
}
 
170
 
 
171
sub isoTimestamp {
 
172
        my $datetime = shift;
 
173
 
 
174
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? localtime($datetime) : localtime();
 
175
        return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
 
176
}
 
177
 
 
178
sub isoUTCTimestamp {
 
179
        my $datetime = shift;
 
180
 
 
181
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
 
182
        return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
 
183
}
 
184
 
 
185
sub isoUTCSimpleTimestamp {
 
186
        my $datetime = shift;
 
187
 
 
188
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
 
189
        return sprintf("%04d%02d%02dT%02d%02d%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
 
190
}
 
191
        
 
192
 
 
193
# unix2winPath:
 
194
#   Converts the given file path from unix style to windows native style
 
195
#   by replacing all forward slashes to backslashes.
 
196
sub unix2winPath {
 
197
    my $path = shift;
 
198
    $path =~ s/\//\\/g; # replace "/" with "\"
 
199
    return $path;
 
200
}
 
201
 
 
202
sub rqg_debug {
 
203
        if ($ENV{RQG_DEBUG}) {
 
204
                return 1;
 
205
        } else {
 
206
                return 0;
 
207
        }
 
208
}
 
209
 
 
210
sub defaultLogging {
 
211
    if (defined $logger) {
 
212
        if (not Log::Log4perl::initialized()) {
 
213
            my $logconf = q(
 
214
log4perl.rootLogger = INFO, STDOUT
 
215
log4perl.appender.STDOUT=Log::Log4perl::Appender::Screen
 
216
log4perl.appender.STDOUT.layout=PatternLayout
 
217
log4perl.appender.STDOUT.layout.ConversionPattern=# %d{yyyy-MM-dd'T'HH:mm:ss} %m%n
 
218
);
 
219
            Log::Log4perl::init( \$logconf );
 
220
            say("Using Log::Log4perl");
 
221
        }
 
222
    }
 
223
}
 
224
 
 
225
 
 
226
sub setLoggingToFile {
 
227
    my $logfile = shift;
 
228
    my $logconf = {
 
229
        'log4perl.logger.randgen' => 'INFO, STDOUT, FILE',
 
230
        
 
231
        'log4perl.appender.STDOUT' => 'Log::Log4perl::Appender::Screen',
 
232
        'log4perl.appender.STDOUT.layout'=>'PatternLayout',
 
233
        'log4perl.appender.STDOUT.layout.ConversionPattern'=>"# %d{yyyy-MM-dd'T'HH:mm:ss} %m%n",
 
234
        
 
235
        'log4perl.appender.FILE'=>'Log::Log4perl::Appender::File',
 
236
        'log4perl.appender.FILE.filename'=>$logfile,
 
237
        'log4perl.appender.FILE.mode'=>'append',
 
238
        'log4perl.appender.FILE.layout'=>'PatternLayout',
 
239
        'log4perl.appender.FILE.layout.ConversionPattern'=>"# %d{yyyy-MM-dd'T'HH:mm:ss} %m%n"
 
240
    };
 
241
    Log::Log4perl::init($logconf);
 
242
    say("Logging to stdout and $logfile");
 
243
}
 
244
 
 
245
sub setLogConf {
 
246
    my $logfile = shift;
 
247
    Log::Log4perl::init($logfile);
 
248
    say("Logging defined by $logfile");
 
249
}
 
250
 
 
251
1;