~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysql-test/lib/My/Find.pm

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- cperl -*-
 
2
# Copyright (C) 2004-2006 MySQL AB
 
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,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU 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  USA
 
16
 
 
17
 
 
18
package My::Find;
 
19
 
 
20
#
 
21
# Utility functions to find files in a MySQL source or bindist
 
22
#
 
23
 
 
24
use strict;
 
25
use Carp;
 
26
use My::Platform;
 
27
 
 
28
use base qw(Exporter);
 
29
our @EXPORT= qw(my_find_bin my_find_dir my_find_file NOT_REQUIRED);
 
30
 
 
31
our $vs_config_dir;
 
32
 
 
33
my $bin_extension= ".exe" if IS_WINDOWS;
 
34
 
 
35
# Helper function to be used for fourth parameter to find functions
 
36
sub NOT_REQUIRED { return 0; }
 
37
 
 
38
#
 
39
# my_find_bin - find an executable with "name_1...name_n" in
 
40
# paths "path_1...path_n" and return the full path
 
41
#
 
42
# Example:
 
43
#    my $mysqld_exe= my_find_bin($basedir.
 
44
#                                ["sql", "bin"],
 
45
#                                ["mysqld", "mysqld-debug"]);
 
46
#    my $mysql_exe= my_find_bin($basedir,
 
47
#                               ["client", "bin"],
 
48
#                               "mysql");
 
49
#
 
50
#
 
51
#    To check if something exists, use the required parameter
 
52
#    set to 0, the function will return an empty string if the
 
53
#    binary is not found
 
54
#    my $mysql_exe= my_find_bin($basedir,
 
55
#                               ["client", "bin"],
 
56
#                               "mysql", NOT_REQUIRED);
 
57
#
 
58
# NOTE: The function honours MTR_VS_CONFIG environment variable
 
59
#
 
60
#
 
61
sub my_find_bin {
 
62
  my ($base, $paths, $names, $required)= @_;
 
63
  croak "usage: my_find_bin(<base>, <paths>, <names>, [<required>])"
 
64
    unless @_ == 4 or @_ == 3;
 
65
 
 
66
  # -------------------------------------------------------
 
67
  # Find and return the first executable
 
68
  # -------------------------------------------------------
 
69
  foreach my $path (my_find_paths($base, $paths, $names, $bin_extension)) {
 
70
    return $path if ( -x $path or (IS_WINDOWS and -f $path) );
 
71
  }
 
72
  if (defined $required and $required == NOT_REQUIRED){
 
73
    # Return empty string to indicate not found
 
74
    return "";
 
75
  }
 
76
  find_error($base, $paths, $names);
 
77
}
 
78
 
 
79
 
 
80
#
 
81
# my_find_file - find a file with "name_1...name_n" in
 
82
# paths "path_1...path_n" and return the full path
 
83
#
 
84
# Example:
 
85
#    my $mysqld_exe= my_find_file($basedir.
 
86
#                                ["sql", "bin"],
 
87
#                                "filename");
 
88
#
 
89
#
 
90
# Also supports NOT_REQUIRED flag
 
91
#
 
92
# NOTE: The function honours MTR_VS_CONFIG environment variable
 
93
#
 
94
#
 
95
sub my_find_file {
 
96
  my ($base, $paths, $names, $required)= @_;
 
97
  croak "usage: my_find_file(<base>, <paths>, <names>, [<required>])"
 
98
    unless @_ == 4 or @_ == 3;
 
99
 
 
100
  # -------------------------------------------------------
 
101
  # Find and return the first executable
 
102
  # -------------------------------------------------------
 
103
  foreach my $path (my_find_paths($base, $paths, $names, $bin_extension)) {
 
104
    return $path if ( -f $path );
 
105
  }
 
106
  if (defined $required and $required == NOT_REQUIRED){
 
107
    # Return empty string to indicate not found
 
108
    return "";
 
109
  }
 
110
  find_error($base, $paths, $names);
 
111
}
 
112
 
 
113
 
 
114
#
 
115
# my_find_dir - find the first existing directory in one of
 
116
# the given paths
 
117
#
 
118
# Example:
 
119
#    my $charset_set= my_find_dir($basedir,
 
120
#                                 ["mysql/share","sql/share", "share"],
 
121
#                                 ["charset"]);
 
122
# or
 
123
#    my $charset_set= my_find_dir($basedir,
 
124
#                                 ['client_release', 'client_debug',
 
125
#                                  'client', 'bin']);
 
126
#
 
127
# NOTE: The function honours MTR_VS_CONFIG environment variable
 
128
#
 
129
#
 
130
sub my_find_dir {
 
131
  my ($base, $paths, $dirs, $required)= @_;
 
132
  croak "usage: my_find_dir(<base>, <paths>[, <dirs>])"
 
133
    unless (@_ == 3 or @_ == 2);
 
134
 
 
135
  # -------------------------------------------------------
 
136
  # Find and return the first directory
 
137
  # -------------------------------------------------------
 
138
  foreach my $path (my_find_paths($base, $paths, $dirs)) {
 
139
    return $path if ( -d $path );
 
140
  }
 
141
  find_error($base, $paths, $dirs);
 
142
}
 
143
 
 
144
 
 
145
sub my_find_paths {
 
146
  my ($base, $paths, $names, $extension)= @_;
 
147
 
 
148
  # Convert the arguments into two normal arrays to ease
 
149
  # further mappings
 
150
  my (@names, @paths);
 
151
  push(@names, ref $names eq "ARRAY" ? @$names : $names);
 
152
  push(@paths, ref $paths eq "ARRAY" ? @$paths : $paths);
 
153
 
 
154
  #print "base: $base\n";
 
155
  #print "names: @names\n";
 
156
  #print "paths: @paths\n";
 
157
 
 
158
  # User can select to look in a special build dir
 
159
  # which is a subdirectory of any of the paths
 
160
  my @extra_dirs;
 
161
  my $build_dir= $vs_config_dir || $ENV{MTR_VS_CONFIG} || $ENV{MTR_BUILD_DIR};
 
162
  push(@extra_dirs, $build_dir) if defined $build_dir;
 
163
 
 
164
  if (defined $extension){
 
165
    # Append extension to names, if name does not already have extension
 
166
    map { $_.=$extension unless /\.(.*)+$/ } @names;
 
167
  }
 
168
 
 
169
  # -------------------------------------------------------
 
170
  # Windows specific
 
171
  # -------------------------------------------------------
 
172
  if (IS_WINDOWS) {
 
173
    # Add the default extra build dirs unless a specific one has
 
174
    # already been selected
 
175
    push(@extra_dirs,
 
176
         ("release",
 
177
          "relwithdebinfo",
 
178
          "debug")) if @extra_dirs == 0;
 
179
  }
 
180
 
 
181
  #print "extra_build_dir: @extra_dirs\n";
 
182
 
 
183
  # -------------------------------------------------------
 
184
  # Build cross product of "paths * extra_build_dirs"
 
185
  # -------------------------------------------------------
 
186
  push(@paths, map { my $path= $_;
 
187
                     map  { "$path/$_" } @extra_dirs
 
188
                   } @paths);
 
189
  #print "paths: @paths\n";
 
190
 
 
191
  # -------------------------------------------------------
 
192
  # Build cross product of "paths * names"
 
193
  # -------------------------------------------------------
 
194
  @paths= map { my $path= $_;
 
195
                map  { "$path/$_" } @names
 
196
              } @paths;
 
197
  #print "paths: @paths\n";
 
198
 
 
199
  # -------------------------------------------------------
 
200
  # Prepend base to all paths
 
201
  # -------------------------------------------------------
 
202
  @paths= map { "$base/$_" } @paths;
 
203
  #print "paths: @paths\n";
 
204
 
 
205
  # -------------------------------------------------------
 
206
  # Glob all paths to expand wildcards
 
207
  # -------------------------------------------------------
 
208
  @paths= map { glob("$_") } @paths;
 
209
  #print "paths: @paths\n";
 
210
 
 
211
  # -------------------------------------------------------
 
212
  # Return the list of paths
 
213
  # -------------------------------------------------------
 
214
  return @paths;
 
215
}
 
216
 
 
217
 
 
218
sub commify {
 
219
  return
 
220
    (@_ == 0) ? '' :
 
221
      (@_ == 1) ? $_[0] :
 
222
        (@_ == 2) ? join(" or ", @_) :
 
223
          join(", ", @_[0..($#_-1)], "or $_[-1]");
 
224
 
 
225
}
 
226
 
 
227
 
 
228
sub fnuttify {
 
229
  return map('\''.$_.'\'', @_);
 
230
}
 
231
 
 
232
 
 
233
sub find_error {
 
234
  my ($base, $paths, $names)= @_;
 
235
 
 
236
  my (@names, @paths);
 
237
  push(@names, ref $names eq "ARRAY" ? @$names : $names);
 
238
  push(@paths, ref $paths eq "ARRAY" ? @$paths : $paths);
 
239
 
 
240
  croak "** ERROR: Could not find ",
 
241
    commify(fnuttify(@names)), " in ",
 
242
      commify(fnuttify(my_find_paths($base, $paths, $names))), "\n";
 
243
}
 
244
 
 
245
1;