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

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-10-29 15:43:40 UTC
  • mfrom: (1.2.12) (2.1.19 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029154340-2gp39el6cv8bwf2o
Tags: 1:7.2.3-2ubuntu1
* Merge from debian, remaining changes:
  - Link against boost_system because of boost_thread.
  - Add required libs to message/include.am
  - Add upstart job and adjust init script to be upstart compatible.
  - Disable -floop-parallelize-all due to gcc-4.8/4.9 compiler ICE
    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57732

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008-2010 Sun Microsystems, Inc. All rights reserved.
2
 
# Use is subject to license terms.
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', 'rqg_debug', 'unix2winPath');
24
 
 
25
 
use strict;
26
 
 
27
 
use Cwd;
28
 
use POSIX;
29
 
use Carp;
30
 
 
31
 
my $tmpdir;
32
 
 
33
 
1;
34
 
 
35
 
sub BEGIN {
36
 
        foreach my $tmp ($ENV{TMP}, $ENV{TEMP}, $ENV{TMPDIR}, '/tmp', '/var/tmp', cwd()."/tmp" ) {
37
 
                if (
38
 
                        (defined $tmp) &&
39
 
                        (-e $tmp)
40
 
                ) {
41
 
                        $tmpdir = $tmp;
42
 
                        last;
43
 
                }
44
 
        }
45
 
 
46
 
        if (defined $tmpdir) {
47
 
                if (
48
 
                        ($^O eq 'MSWin32') ||
49
 
                        ($^O eq 'MSWin64')
50
 
                ) {
51
 
                        $tmpdir = $tmpdir.'\\';
52
 
                } else {
53
 
                        $tmpdir = $tmpdir.'/';
54
 
                }
55
 
        }
56
 
 
57
 
        croak("Unable to locate suitable temporary directory.") if not defined $tmpdir;
58
 
        
59
 
        return 1;
60
 
}
61
 
 
62
 
sub new {
63
 
        my $class = shift;
64
 
        my $args = shift;
65
 
 
66
 
        my $obj = bless ([], $class);
67
 
 
68
 
        my $max_arg = (scalar(@_) / 2) - 1;
69
 
 
70
 
        foreach my $i (0..$max_arg) {
71
 
                if (exists $args->{$_[$i * 2]}) {
72
 
                        if (defined $obj->[$args->{$_[$i * 2]}]) {
73
 
                                carp("Argument '$_[$i * 2]' passed twice to ".$class.'->new()');
74
 
                        } else {
75
 
                                $obj->[$args->{$_[$i * 2]}] = $_[$i * 2 + 1];
76
 
                        }
77
 
                } else {
78
 
                        carp("Unkown argument '$_[$i * 2]' to ".$class.'->new()');
79
 
                }
80
 
        }
81
 
 
82
 
        return $obj;
83
 
}
84
 
 
85
 
sub say {
86
 
        my $text = shift;
87
 
 
88
 
        if ($text =~ m{[\r\n]}sio) {
89
 
                foreach my $line (split (m{[\r\n]}, $text)) {
90
 
                        print "# ".isoTimestamp()." $line\n";
91
 
                }
92
 
        } else {
93
 
                print "# ".isoTimestamp()." $text\n";
94
 
        }
95
 
}
96
 
 
97
 
sub sayFile {
98
 
    my ($file) = @_;
99
 
 
100
 
    say("--------- Contents of $file -------------");
101
 
    open FILE,$file;
102
 
    while (<FILE>) {
103
 
        say("| ".$_);
104
 
    }
105
 
    close FILE;
106
 
    say("----------------------------------");
107
 
}
108
 
 
109
 
sub tmpdir {
110
 
        return $tmpdir;
111
 
}
112
 
 
113
 
sub safe_exit {
114
 
        my $exit_status = shift;
115
 
        POSIX::_exit($exit_status);
116
 
}
117
 
 
118
 
sub osWindows {
119
 
        if (
120
 
                ($^O eq 'MSWin32') ||
121
 
                ($^O eq 'MSWin64')
122
 
        ) {
123
 
                return 1;
124
 
        } else {
125
 
                return 0;
126
 
        }       
127
 
}
128
 
 
129
 
sub osLinux {
130
 
        if ($^O eq 'linux') {
131
 
                return 1;
132
 
        } else {
133
 
                return 0;
134
 
        }
135
 
}
136
 
 
137
 
sub osSolaris {
138
 
        if ($^O eq 'solaris') {
139
 
                return 1;
140
 
        } else {
141
 
                return 0;
142
 
        }       
143
 
}
144
 
 
145
 
sub osMac {
146
 
    if ($^O eq 'darwin') {
147
 
        return 1;
148
 
    } else {
149
 
        return 0;
150
 
    }
151
 
}
152
 
 
153
 
sub isoTimestamp {
154
 
        my $datetime = shift;
155
 
 
156
 
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? localtime($datetime) : localtime();
157
 
        return sprintf("%04d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
158
 
 
159
 
}
160
 
 
161
 
sub isoUTCTimestamp {
162
 
        my $datetime = shift;
163
 
 
164
 
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = defined $datetime ? gmtime($datetime) : gmtime();
165
 
        return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $year+1900, $mon+1 ,$mday ,$hour, $min, $sec);
166
 
        
167
 
}
168
 
 
169
 
# unix2winPath:
170
 
#   Converts the given file path from unix style to windows native style
171
 
#   by replacing all forward slashes to backslashes.
172
 
sub unix2winPath {
173
 
    my $path = shift;
174
 
    $path =~ s/\//\\/g; # replace "/" with "\"
175
 
    return $path;
176
 
}
177
 
 
178
 
sub rqg_debug {
179
 
        if ($ENV{RQG_DEBUG}) {
180
 
                return 1;
181
 
        } else {
182
 
                return 0;
183
 
        }
184
 
}
185
 
 
186
 
1;