FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
libTestDB.php
1 <?php
2 /*
3  Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
27 function _modFossConf($sysConfPath, $repoPath)
28 {
29  if(!file_exists($sysConfPath . '/fossology.conf'))
30  {
31  echo "ERROR! can't find fossology.conf at:\n$sysConfPath/fossology.conf\n";
32  return FALSE;
33  }
34 
35  $fossConf = file_get_contents($sysConfPath . '/fossology.conf');
36  if($fossConf === FALSE)
37  {
38  echo "ERROR! could not read\n$sysConfPath/fossology.conf\n";
39  return FALSE;
40  }
41  $pat = '!/srv/fossology/repository!';
42  $testConf = preg_replace($pat, $repoPath, $fossConf);
43  $stat = file_put_contents("$sysConfPath/fossology.conf",$testConf);
44  if($stat === FALSE)
45  {
46  echo "ERROR! could not write\n$sysConfPath/fossology.conf\n";
47  return FALSE;
48  }
49  return TRUE;
50 }
51 
64 function createTestDB($name)
65 {
66  if(empty($name))
67  {
68  return("Error, no DB name supplied\n");
69  }
70  // figure out TESTROOT and export it to the environment so the shell scripts
71  // can use it. We live in testing/lib, so just remove /lib.
72 
73  $path = __DIR__;
74  $TESTROOT = dirname($path);
75  $_ENV['TESTROOT'] = $TESTROOT;
76  putenv("TESTROOT=$TESTROOT");
77 
78  if(chdir($TESTROOT . '/db') === FALSE)
79  {
80  return("FATAL! could no cd to $TESTROOT/db\n");
81  }
82  $cmd = "sudo ./ftdbcreate.sh $name 2>&1";
83  exec($cmd, $cmdOut, $cmdRtn);
84  if($cmdRtn != 0)
85  {
86  $err = "Error could not create Data Base $name\n";
87  return($err);
88  }
89  return(NULL);
90 } // CreateTestDB
91 
101 function RestoreFile($filename)
102 {
103  global $SYSCONFDIR;
104 
105  if(empty($filename))
106  {
107  return(FALSE);
108  }
109  // cp is used instead of copy so the caller doesn't have to run as sudo
110  $lastCp = system("sudo cp $SYSCONFDIR/orig.$filename " .
111  "$SYSCONFDIR/$filename", $rtn);
112  if($lastCp === FALSE)
113  {
114  return(FALSE);
115  }
116  // clean up the orig file
117  $lasRm = exec("sudo rm $SYSCONFDIR/orig.$filename", $rmOut, $rmRtn);
118  if($rmRtn != 0)
119  {
120  echo "Trouble removing $SYSCONFDIR/orig.$filename, please " .
121  "investigate and remove by hand\n";
122  return(FALSE);
123  }
124  return(TRUE);
125 }
126 
137 function SetRepo($sysConfPath,$repoPath)
138 {
139  if(empty($repoPath) || empty($sysConfPath))
140  {
141  return FALSE;
142  }
143  return _modFossConf($sysConfPath,$repoPath);
144 }
145 
157 function TestDBInit($path=NULL, $dbName)
158 {
159  if(empty($path))
160  {
161  $path = __DIR__ . '/../../www/ui/core-schema.dat';
162  }
163  if (!file_exists($path))
164  {
165  return("FAILED: Schema data file ($path) not found.\n");
166  }
167  if(empty($dbName))
168  {
169  return("Error!, no catalog supplied\n");
170  }
171 
172  // run ApplySchema via fossinit
173  $result = NULL;
174  $lastUp = NULL;
175  $sysc = getenv('SYSCONFDIR');
176  $fossInit = __DIR__ . '/../../../install/fossinit.php';
177  $upOut = array();
178  $cmd="$fossInit -d $dbName -f $path";
179  $last = exec($cmd, $upOut, $upRtn);
180  //echo "DB: schema up output is:\n" . implode("\n",$upOut) . "\n";
181 
182  if($upRtn != 0)
183  {
184  return(implode("\n", $upOut) . "\n");
185  }
186  else
187  {
188  return(NULL);
189  }
190 }