Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
224 hknight 1
#!/usr/bin/perl -w
2
 
3
########################################################################
4
# Copyright (C) 2008 ERG Limited, All rights reserved
5
#
6
# Module name   : commonExports.pm
7
# Module type   : Perl module file
8
# Compiler(s)   : n/a
9
# Environment(s): windows (dos), unix (solaris and linux)
10
#
11
# Description   : Contains functions and global variables that are
12
#                 commonly exported to be used by other scripts.
13
#                 In particular schemadump.pl and ddlfile.pl use
14
#                 this file.               
15
#
16
# History       : Created by Haydon Knight May 2008
17
#
18
# Usage         : n/a
19
#
20
########################################################################
21
 
22
#######################################################################
23
# Use stuff
24
#######################################################################
25
 
26
package commonExports;
27
require 5.6.1;
28
use strict;
29
use warnings;
30
use Exporter;
31
 
32
our @ISA = qw{ Exporter };
33
our($global) = "any global variable";
34
 
35
#######################################################################
36
# Function prototypes
37
#######################################################################
38
 
39
sub readArray($);
40
sub writeArray($@);
41
sub openLog();
42
sub runCommand($@);
43
sub drunCommand($@);
44
sub quoteCommand(@);
45
sub logprint($);
46
sub hchomp;
47
sub finish();
48
sub isOneOf($\@);
49
sub toUnixLineEnds($);
50
sub createDirectories($@);
51
 
52
sub getTopBotDirs($);
53
 
54
#######################################################################
55
# Constant global variables
56
#######################################################################
57
 
58
our $subSectionSplitter = "-- new object type path is: SCHEMA_EXPORT/";
59
our $UNIX = ($ENV{'OS'} && $ENV{'OS'} =~ m/win/i) ? 0 : 1;
60
 
61
#######################################################################
62
# Other global variables
63
#######################################################################
64
 
65
our $verbose = 0; # numeric argument - 1 for 'on'; '2' for high verbosity; '3' for max
66
our $logFile;
67
 
68
#######################################################################
69
# Hash tables - these are specific to the release manager database
70
#               and need to be adjusted if running this script on other databases
71
#######################################################################
72
 
73
# This hash is used to determine which types of SQL objects should be
74
# extracted to individual files, rather than just writing out one big
75
# file for all.  e.g. each synonym gets its own file, but all
76
# se_post_schema_procobjact things get lumped in allData.sql
77
our %wantToExtract;
78
 
79
$wantToExtract{'synonym/synonym'} = 1;
80
$wantToExtract{'type/type'} = 1;
81
$wantToExtract{'sequence/sequence'} = 1;
82
$wantToExtract{'table/table'} = 1;
83
$wantToExtract{'function/function'} = 1;
84
$wantToExtract{'procedure/procedure'} = 1;
85
$wantToExtract{'view/view'} = 1;
86
$wantToExtract{'package/package_body'} = 1;
87
$wantToExtract{'table/index'} = 1;
88
$wantToExtract{'package/package_spec'} = 1;
89
$wantToExtract{'table/se_tbl_fbm_index_index'} = 1;
90
 
91
#######################################################################
92
# Export everything
93
#######################################################################
94
 
95
# List of default exports
96
our @EXPORT = qw{ $subSectionSplitter $UNIX             $logFile      $verbose
97
                  %wantToExtract
98
                  readArray           writeArray        openLog       runCommand     drunCommand
99
                  quoteCommand        logprint          hchomp        finish         isOneOf
100
                  toUnixLineEnds      createDirectories getTopBotDirs };
101
 
102
# List of available non-default exports
103
our @EXPORT_OK = qw{ };
104
 
105
#######################################################################
106
# Generic function definitions 
107
#######################################################################
108
 
109
#-------------------------------------------------------------------------------
110
# Function  : writeArray
111
#
112
# Purpose   : Writes an array to disk
113
#
114
# Arguments : $filename (i) - file to write out to
115
#             @lines (i) - lines to be written out
116
#
117
# Returns   : nothing
118
#
119
# Notes     : Calls toUnixLineEnds()
120
#
121
sub writeArray($@)
122
{
123
    my ($filename, @lines) = @_;
124
 
125
    open( D, ">$filename") or die "Could not open '$filename' for writing\n";
126
    foreach my $line (@lines)
127
    {
128
        $line .= "\n" unless $line =~ m/\n$/;
129
        print D $line;
130
    }
131
    close( D );
132
 
133
    toUnixLineEnds( $filename );
134
}
135
 
136
 
137
 
138
#-------------------------------------------------------------------------------
139
# Function  : readArray
140
#
141
# Purpose   : Reads in an array from disk
142
#
143
# Arguments : $filename (i) - file to read from
144
#
145
# Returns   : @lines - lines returned
146
#
147
# Notes     : Lines will not have '\n' in them.
148
#
149
sub readArray($)
150
{
151
    my ($filename) = @_;
152
 
153
    my @lines;
154
 
155
    open( SRCFILE, $filename);    
156
    while( <SRCFILE> )
157
    {
158
        s~[\n\r]+$~~;
159
        push @lines, $_;
160
    }
161
    close( SRCFILE );
162
 
163
    return hchomp @lines;
164
}
165
 
166
 
167
#-------------------------------------------------------------------------------
168
# Function  : openLog
169
#
170
# Purpose   : Opens the log file if $logFile has been set
171
#
172
# Arguments : none
173
#
174
# Returns   : Nothing
175
#
176
# Notes     :
177
#
178
sub openLog()
179
{
180
    die "Could not open logfile '$logFile'\n" if( $logFile && !open(LOGFILE,">$logFile") );
181
}
182
 
183
 
184
#-------------------------------------------------------------------------------
185
# Function  : isOneOf
186
#
187
# Purpose   : Returns true is a string is a member of an array of strings
188
#
189
# Arguments : $cand (i) - Candidate string
190
#             $refElite(i) - reference to @elite - Array of strings (the elite list)
191
#
192
# Returns   : Either 1 or 0 depending on whether candidate is one of the elite list.
193
#
194
# Notes     :
195
#
196
sub isOneOf($\@)
197
{
198
    my ($cand, $refElite) = @_;
199
 
200
    foreach my $elitemember (@$refElite)
201
    {
202
        return 1 if $cand eq $elitemember;
203
    }
204
 
205
    return 0;
206
}
207
 
208
 
209
#-------------------------------------------------------------------------------
210
# Function  : logprint
211
#
212
# Purpose   : Prints comments
213
#
214
# Arguments : $s (i) - string to be printed
215
#
216
# Returns   : nothing
217
#
218
# Notes     : Prints to screen if verbose
219
#             Prints to log if logfile specified on command line
220
#
221
sub logprint($)
222
{
223
    my ($s) = @_;
224
    $s =~ s~[\n\r]$~~;
225
    print "Log: $s\n" if( $verbose );
226
    print LOGFILE "Log: $s\n" if( $logFile );
227
}
228
 
229
 
230
#-------------------------------------------------------------------------------
231
# Function  : finish
232
#
233
# Purpose   : closes log file handle and provides return value for script
234
#
235
# Arguments : none
236
#
237
# Returns   : none
238
#
239
# Notes     :
240
#
241
sub finish()
242
{
243
    logprint("Bye!");
244
    close LOGFILE if $logFile;
245
    exit( 0 );
246
}
247
 
248
 
249
#-------------------------------------------------------------------------------
250
# Function  : hchomp
251
#
252
# Purpose   : Remove newline characters from the end of a string
253
#
254
# Arguments : A single string, or an array of strings
255
#
256
# Returns   : Input without newline characters
257
#
258
# Notes     :
259
#
260
sub hchomp
261
{
262
    return @_ unless @_;
263
 
264
    my @stuff = @_;
265
 
266
    foreach my $a (@stuff)
267
    {
268
        $a =~ s~[\n\r]+$~~;
269
    }
270
 
271
    return ( (scalar(@stuff) == 1) ? $stuff[0] : @stuff );
272
}
273
 
274
 
275
#-------------------------------------------------------------------------------
276
# Function  : toUnixLineEnds
277
#
278
# Purpose   : Converts a file to have unix line endings
279
#
280
# Arguments : $filename (i) - file to convert
281
#
282
# Returns   : nothing
283
#
284
# Notes     : Reads in, gets rid of all '\r'; writes back out
285
#
286
sub toUnixLineEnds($)
287
{
288
    my ($filename) = @_;
289
 
290
    my $fileChars;
291
 
292
    open( FILEIN, $filename) or die "Could not open '$filename' for reading\n";
293
    $fileChars .= $_ while( <FILEIN> );
294
    close( FILEIN );
295
 
296
    $fileChars =~ s~\r~~g;
297
 
298
    open( FILEOUT, ">$filename") or die "Could not open '$filename' for writing\n";
299
    binmode FILEOUT;
300
    print FILEOUT $fileChars;
301
    close( FILEOUT );
302
}
303
 
304
 
305
#-------------------------------------------------------------------------------
306
# Function  : createDirectories
307
#
308
# Purpose   : mkdir's directories
309
#
310
# Arguments : $basedir (i) - prepend this path onto each of @subDirs before making them
311
#             @subDirs (i) - directories to be made
312
#
313
# Returns   : nothing
314
#
315
# Notes     : recursively works so can be passed in stuff like "a/b" where neither
316
#             directory a nor b currently exists.
317
#
318
sub createDirectories($@)
319
{
320
    my ($basedir, @subDirs) = @_;
321
 
322
    foreach my $subDir (@subDirs)
323
    {
324
        my @dirys = split(/\//,$subDir);
325
        my $diryString;
326
 
327
        foreach my $diry (@dirys)
328
        {
329
            $diryString .= "$diry/";
330
            mkdir "$basedir/$diryString" unless -e "$basedir/$diryString";
331
        }
332
    }
333
 
334
}
335
 
336
 
337
#-------------------------------------------------------------------------------
338
# Function  : runCommand
339
#
340
# Purpose   : Runs a command in the shell
341
#
342
# Arguments : $cmd (i) - command to run.  Basically this is all arguments that should
343
#                        not be quoted.
344
#             @args (i) - additional arguments.  These are all quoted, so be careful
345
#                         when passing in wildcard arguments.
346
#
347
# Returns   : The shell stdout output of the command
348
#
349
# Notes     :
350
#
351
sub runCommand($@)
352
{
353
    my ($cmd, @args) = @_;
354
 
355
    my $fullCmd = $cmd;
356
    $fullCmd .= " " . quoteCommand(@args) if @args;
357
 
358
    logprint "Running command '$fullCmd'";
359
    return hchomp `$fullCmd`;
360
}
361
 
362
 
363
#-------------------------------------------------------------------------------
364
# Function  : drunCommand
365
#
366
# Purpose   : Used for debugging - call when you don't want to run a command
367
#
368
# Arguments : See runCommand
369
#
370
# Returns   : An empty string.
371
#
372
# Notes     : drunCommand = don't run command.  Just like runCommand(), but doesn't run the
373
#             command.
374
#
375
sub drunCommand($@)
376
{
377
    my ($cmd, @args) = @_;
378
 
379
    my $fullCmd = $cmd;
380
    $fullCmd .= " " . quoteCommand(@args) if @args;
381
 
382
    logprint "Not running command '$fullCmd'";
383
    return "";
384
}
385
 
386
 
387
#-------------------------------------------------------------------------------
388
# Function  : quoteCommand
389
#
390
# Purpose   : Quotes its arguments
391
#
392
# Arguments : @words (i) - words to be surrounded by double quotes
393
#
394
# Returns   : $quotedCommand - words quoted and joined
395
#
396
# Notes     :
397
#
398
sub quoteCommand(@)
399
{
400
    my @words = @_;
401
    my $quotedCommand;
402
 
403
    foreach my $word (@words)
404
    {
405
        $quotedCommand .= qq("$word" );
406
    }
407
    return $quotedCommand;
408
}
409
 
410
#######################################################################
411
# Function definitions specific to scripts related to release manager database
412
#######################################################################
413
 
414
#-------------------------------------------------------------------------------
415
# Function  : getTopBotDirs
416
#
417
# Purpose   : Strip out the two directories from input string
418
#
419
# Arguments : $fullDiry (i) - a string e.g. of form 'TABLE/INDEX/STATISTICS/INDEX_STATISTICS'
420
#
421
# Returns   : $topDir - first directory
422
#             $botDir - directory within $topDir
423
#
424
# Notes     : Code is hardwired with special case handling for release manager database
425
#
426
sub getTopBotDirs($)
427
{
428
    my ($fullDiry) = @_;
429
 
430
    ###############################################################
431
    # From input string we extract the first word and the last word, where we split on a slash
432
    # i.e. for the string 'TABLE/INDEX/STATISTICS/INDEX_STATISTICS'
433
    # we get 'TABLE' and 'INDEX_STATISTICS'
434
    # The only objects not uniquely identified by their first and last argument
435
    # are TABLE/INDEX/INDEX and TABLE/INDEX/SE_TBL_FBM_INDEX_INDEX/INDEX
436
    # For those we do a special case
437
 
438
    my @dirys = split( /\//, $fullDiry);
439
 
440
    die "getTopBotDirs(): Could not parse any words from input string!"
441
        unless( @dirys );
442
 
443
    my $topDir = $dirys[0];
444
    my $botDir = $dirys[-1];
445
 
446
    $botDir = $dirys[-2] if( $dirys[0] =~ m/^TABLE$/i && $dirys[-1] =~ m/^INDEX$/i );
447
 
448
    return ($topDir, $botDir);
449
}
450
 
451
#######################################################################
452
# Final true value
453
#######################################################################
454
 
455
1;