1# *************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20# *************************************************************
21eval 'exec perl -wS $0 ${1+\"$@\"}'
22    if 0;
23
24# This program has to start for the new convwatch,
25# once on Windows environment and once on Linux environment
26# Solaris is handled by the linux also.
27#
28# This program polls the database (documentcompare) every 60s for new jobs
29# it runs over the given directory from documentpoolpath and pool, and create for every file
30# a new database entry in documents.
31#
32
33BEGIN
34{
35        #       Adding the path of this script file to the include path in the hope
36        #       that all used modules can be found in it.
37        $0 =~ /^(.*)[\/\\]/;
38        push @INC, $1;
39}
40
41use ConvwatchHelper;
42use CallExternals;
43use stringhelper;
44use filehelper;
45use oshelper;
46use timehelper;
47use cwstestresulthelper;
48
49use strict;
50use Cwd;
51use File::Basename;
52use English;                  # $OSNAME, ...
53use Getopt::Long;
54use File::Path;
55use Cwd 'chdir';
56
57my $cwd = getcwd();
58
59our $help;                    # Help option flag
60our $version;                 # Version option flag
61our $test;
62
63our $version_info = 'convwatch.pl $Revision: 1.24 $ ';
64
65our $SOLARENV;
66our $COMMON_ENV_TOOLS;
67
68
69our $documentpoolname;
70our $documentpoolpath;
71our $dbdistinct;
72our $sParentDistinct;
73our $sCurrentDocumentPool;
74
75our $fs;
76our @aEntries;
77
78# Prototypes
79# sub getJavaFileDirSeparator();
80sub readdirectory($$$);
81sub putDocumentInDB($$$);
82
83# flush STDOUT
84my $old_handle = select (STDOUT); # "select" STDOUT and save # previously selected handle
85$| = 1; # perform flush after each write to STDOUT
86select ($old_handle); # restore previously selected handle
87
88setPrefix("gfxcmp");
89
90if (!GetOptions(
91                "test"          => \$test,
92                "help"          => \$help,
93                "version"       => \$version
94                ))
95{
96    print_usage(*STDERR);
97    exit(1);
98}
99if ($help)
100{
101    print_usage(*STDOUT);
102    exit(0);
103}
104# Check for version option
105if ($version)
106{
107    print STDERR "$version_info\n";
108    exit(0);
109}
110
111# ------------------------------------------------------------------------------
112# within mysql it is better to use only '/'
113$fs = "/"; # getJavaFileDirSeparator();
114# ------------------------------------------------------------------------------
115sub readdirectory($$$)
116{
117    my $startdir = shift;
118    my $sValues  = shift;
119    my $hook     = shift;
120
121    my $myfile;
122
123    local *DIR;
124    chdir $startdir;
125    cwd();
126    if (! endswith($startdir, $fs))
127    {
128        $startdir .= $fs;
129    }
130
131    my $nCountFiles = 0;
132    if (opendir (DIR, $startdir))           # Directory oeffnen
133    {
134        while ($myfile = readdir(DIR))
135        {                                  # ein filename holen
136            #if (! -l $myfile)              # not a link
137            #{
138            if (-d $myfile ) # is a directory
139            {
140                if ( -l $myfile)
141                {
142                    next;
143                }
144                if ($myfile ne "." && $myfile ne "..")
145                {
146                    my $sNewStartDir = $startdir . $myfile ."/";    # neuen Directorystring erstellen
147                    if ($sNewStartDir =~ "^\/proc" ||
148                        $sNewStartDir =~ "^\/dev" ||
149                        $sNewStartDir =~ "^\/udev" ||
150                        $sNewStartDir =~ "lost+found" )
151                    {
152                        next;
153                    }
154                    # my $sNewDestDir  = $destdir . $myfile ."/";
155                    # do a recursive call
156                    # $nCountFiles++;
157                    my $nFileCount = readdirectory($sNewStartDir, $sValues, $hook);
158                    # workOnDir($sNewDir, $nFileCount);
159                    $nCountFiles += $nFileCount;
160
161                    chdir ($startdir);                      # zurueckwechseln.
162                    cwd();
163                }
164            }
165            else
166            {
167                # File must exist, be a regular file and must not be the $onlyOnFile
168                if (-f $myfile)
169                {
170                    # print " $startdir" . "$myfile\n";
171                    $nCountFiles++;
172                    # workOnFile($startdir, $myfile, $destdir);
173                    $hook->($startdir, $myfile, $sValues);
174                }
175            }
176            #}
177            #else
178            #{
179            #    print "linked file: $dir/$myfile\n";
180            #}
181        }
182        closedir(DIR);
183    }
184    else
185    {
186        print "could not open $startdir\n";
187    }
188    return $nCountFiles;
189}
190# ------------------------------------------------------------------------------
191sub putDocumentInDB($$$)
192{
193    my $currentDir = shift;
194    my $currentFile = shift;
195    my $sValues = shift;
196
197    my $sSourceFilename = $currentDir . $currentFile;
198    # we cut down all the previous names like documentpoolpath and the documentpoolname
199    $sSourceFilename = substr($sSourceFilename, length($sCurrentDocumentPool . $fs));
200
201    my $sSQL = "INSERT INTO documents (dbdistinct2, name, pagecount, priority, parentdistinct) VALUES";
202    $sSQL .= "('" . $dbdistinct . "', '" . $sSourceFilename . "', 0, 1, '". $sParentDistinct . "')";
203    # print $sSQL . "\n";
204
205    push(@aEntries, $sSQL);
206    # ExecSQL($sSQL);
207}
208
209# ------------------------------------------------------------------------------
210sub createDBEntriesForEveryDocument($)
211{
212    my $sStr = shift;
213    if ($sStr =~ /^MySQL-Error/ )
214    {
215        # we don't do anything if an error occured
216        return;
217    }
218
219    # interpret the follows string
220    # documentpoolpath='//so-gfxcmp-documents/doc-pool', documentpool='demo_lla', dbdistinct=62,
221
222    # my $sDocumentPoolDir;
223    if ( $sStr =~ /documentpoolpath='(.*?)',/ )
224    {
225        $documentpoolpath = $1;
226    }
227    if (! $documentpoolpath)
228    {
229        print "Error: no value for documentpoolpath found.\n";
230        return;
231    }
232
233    # my $sDocumentPool;
234    if ( $sStr =~ /documentpool='(.*?)',/ )
235    {
236        $documentpoolname = $1;
237    }
238    if (! $documentpoolname)
239    {
240        print "Error: no value for documentpool found.\n";
241        return;
242    }
243    # my $dbdistinct;
244    if ( $sStr =~ /dbdistinct2='(\S*?)',/ )
245    {
246        $dbdistinct = $1;
247    }
248    if (! $dbdistinct)
249    {
250        print "Error: no dbdistinct given.\n";
251        return;
252    }
253
254    if (! -d $documentpoolpath )
255    {
256        my $sEnv = getEnvironment();
257        if ( isUnixEnvironment() )
258        {
259            $documentpoolpath = "/net/so-gfxcmp-documents" . $documentpoolpath;
260        }
261        if ( -d $documentpoolpath )
262        {
263            print "Warning: given documentpoolpath seems to be local, fix to '$documentpoolpath'\n";
264            my $sSQL = "UPDATE documentcompare SET documentpoolpath='$documentpoolpath' WHERE dbdistinct2='$dbdistinct'";
265            print "$sSQL\n";
266            ExecSQL($sSQL);
267        }
268        else
269        {
270            print "Error: documentpoolpath '$documentpoolpath' not found. Don't insert anything.\n";
271            my $sSQL = "UPDATE documentcompare SET state='failed',info='documentpoolpath not found.' WHERE dbdistinct2='$dbdistinct'";
272            print "$sSQL\n";
273            ExecSQL($sSQL);
274            return;
275        }
276    }
277    # create the documentpool directory, to run through
278    $sCurrentDocumentPool = $documentpoolpath;
279    if (! endswith($sCurrentDocumentPool, $fs))
280    {
281        $sCurrentDocumentPool .= $fs;
282    }
283    $sCurrentDocumentPool .= $documentpoolname;
284
285    if ( -d $sCurrentDocumentPool )
286    {
287        if ( $sStr =~ /parentdistinct='(.*?)',/ )
288        {
289            $sParentDistinct = $1;
290        }
291        else
292        {
293            $sParentDistinct = "";
294        }
295
296        # remove any doubles, if any
297        my $sSQL = "DELETE FROM documents WHERE dbdistinct2='$dbdistinct'";
298        print "$sSQL\n";
299        ExecSQL($sSQL);
300
301        # run over the whole given document pool and store every found document name in the database
302        readdirectory($sCurrentDocumentPool, "", \&putDocumentInDB);
303
304        chdir $cwd;
305        cwd();
306
307        foreach $sSQL (@aEntries)
308        {
309            # print "# $sSQL\n";
310            print "$sSQL\n";
311            ExecSQL($sSQL);
312        }
313
314        my $sSQL = "UPDATE documentcompare SET state='inprogress' WHERE dbdistinct2='$dbdistinct'";
315        print "$sSQL\n";
316        ExecSQL($sSQL);
317        print "----------------------------------------------------------------------\n";
318        $sParentDistinct = "";
319        @aEntries = ();
320    }
321    else
322    {
323        print "Error: Given document pool '$sCurrentDocumentPool' doesn't exists.\n";
324        my $sSQL = "UPDATE documentcompare SET state='cancelled' WHERE dbdistinct2='$dbdistinct'";
325        ExecSQL($sSQL);
326        return;
327    }
328    # Send Mail, due to startconvwatch now
329    sendMail($sStr, $documentpoolname, $dbdistinct);
330}
331
332# ------------------------------------------------------------------------------
333sub sendMail($$$)
334{
335    my $sStr = shift;
336    my $documentpool = shift;
337    my $dbdistinct = shift;
338    my $sourceversion;
339    if ( $sStr =~ /sourceversion='(.*?)',/ )
340    {
341        $sourceversion = $1;
342    }
343    if (! $sourceversion)
344    {
345        print "Warning: no value for sourceversion found.\n";
346        return;
347    }
348    my $destinationversion;
349    if ( $sStr =~ /destinationversion='(.*?)',/ )
350    {
351        $destinationversion = $1;
352    }
353    if (! $destinationversion)
354    {
355        print "Warning: no value for destinationversion found.\n";
356        return;
357    }
358    my $mailaddress;
359    if ( $sStr =~ /mailfeedback='(.*?)',/ )
360    {
361        $mailaddress = $1;
362    }
363    if (! $mailaddress)
364    {
365        print "Warning: no value for mailfeedback found.\n";
366        return;
367    }
368
369    # state is 'inprogress', so send a mail
370    # my $sMailAddress = getMailAddress($sDoneStr);
371    my $sParams = "$sourceversion";
372    $sParams .= " $destinationversion";
373    $sParams .= " $documentpool";
374    $sParams .= " $dbdistinct";
375    $sParams .= " $mailaddress";
376    $sParams .= " starts";                    # run through state of convwatch
377
378    my $sMailProgram = appendPath(getQADEVToolsPath(), "mailsend.php");
379
380    my $err;
381    my @lines;
382    my $sLine;
383    ($err, @lines) = callphp(getPHPExecutable(), $sMailProgram, $sParams);
384    foreach $sLine (@lines)
385    {
386        log_print( "Mail: $sLine\n");
387    }
388
389    if ($documentpool eq "EIS-tests")
390    {
391        cwstestresult("running", $dbdistinct, $sourceversion, $destinationversion, $SOLARENV, $COMMON_ENV_TOOLS);
392    }
393}
394# ------------------------------------------------------------------------------
395# ------------------------------------------------------------------------------
396
397my $sEnvironmentCondition;
398if (isWindowsEnvironment())
399{
400    $sEnvironmentCondition = "environment='" . getEnvironment() . "'";
401}
402elsif (isUnixEnvironment())
403{
404    # $sEnvironmentCondition = " ( environment='unxlngi' OR environment='unxsoli' ) ";
405    $sEnvironmentCondition = " environment='" . getEnvironment() . "'";
406}
407else
408{
409    print "Error: wrong environment.\n";
410    exit(1);
411}
412my $sWhereClause = "WHERE ";
413if ($sEnvironmentCondition)
414{
415    $sWhereClause .= $sEnvironmentCondition . " AND ";
416}
417$sWhereClause .= " state='new'";
418
419setToolsPath(getQADEVToolsPath());
420
421# ---------------------------------- main loop ----------------------------------
422while (1)
423{
424    my @aResult;
425    my $sSQL = "SELECT documentpoolpath,documentpool,dbdistinct2,sourceversion,destinationversion,mailfeedback,parentdistinct FROM documentcompare $sWhereClause";
426    @aResult = ExecSQL($sSQL);
427
428    my $aValue;
429    foreach $aValue (@aResult)
430    {
431        # print "# $nValue\n";
432        createDBEntriesForEveryDocument($aValue);
433    }
434    if ($test)
435    {
436        last;
437    }
438
439    # wait 30sec.
440    # wait30seconds();
441    waitAMinute();
442    checkForStop("stop_fill_documents_loop");
443}
444