FOSSology  4.4.0
Open Source License Compliance by Open Source Software
libTestDB.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
16 function _modFossConf($sysConfPath, $repoPath)
17 {
18  if(!file_exists($sysConfPath . '/fossology.conf'))
19  {
20  echo "ERROR! can't find fossology.conf at:\n$sysConfPath/fossology.conf\n";
21  return FALSE;
22  }
23 
24  $fossConf = file_get_contents($sysConfPath . '/fossology.conf');
25  if($fossConf === FALSE)
26  {
27  echo "ERROR! could not read\n$sysConfPath/fossology.conf\n";
28  return FALSE;
29  }
30  $pat = '!/srv/fossology/repository!';
31  $testConf = preg_replace($pat, $repoPath, $fossConf);
32  $stat = file_put_contents("$sysConfPath/fossology.conf",$testConf);
33  if($stat === FALSE)
34  {
35  echo "ERROR! could not write\n$sysConfPath/fossology.conf\n";
36  return FALSE;
37  }
38  return TRUE;
39 }
40 
53 function createTestDB($name)
54 {
55  if(empty($name))
56  {
57  return("Error, no DB name supplied\n");
58  }
59  // figure out TESTROOT and export it to the environment so the shell scripts
60  // can use it. We live in testing/lib, so just remove /lib.
61 
62  $path = __DIR__;
63  $TESTROOT = dirname($path);
64  $_ENV['TESTROOT'] = $TESTROOT;
65  putenv("TESTROOT=$TESTROOT");
66 
67  if(chdir($TESTROOT . '/db') === FALSE)
68  {
69  return("FATAL! could no cd to $TESTROOT/db\n");
70  }
71  $cmd = "sudo ./ftdbcreate.sh $name 2>&1";
72  exec($cmd, $cmdOut, $cmdRtn);
73  if($cmdRtn != 0)
74  {
75  $err = "Error could not create Data Base $name\n";
76  return($err);
77  }
78  return(NULL);
79 } // CreateTestDB
80 
90 function RestoreFile($filename)
91 {
92  global $SYSCONFDIR;
93 
94  if(empty($filename))
95  {
96  return(FALSE);
97  }
98  // cp is used instead of copy so the caller doesn't have to run as sudo
99  $lastCp = system("sudo cp $SYSCONFDIR/orig.$filename " .
100  "$SYSCONFDIR/$filename", $rtn);
101  if($lastCp === FALSE)
102  {
103  return(FALSE);
104  }
105  // clean up the orig file
106  $lasRm = exec("sudo rm $SYSCONFDIR/orig.$filename", $rmOut, $rmRtn);
107  if($rmRtn != 0)
108  {
109  echo "Trouble removing $SYSCONFDIR/orig.$filename, please " .
110  "investigate and remove by hand\n";
111  return(FALSE);
112  }
113  return(TRUE);
114 }
115 
126 function SetRepo($sysConfPath,$repoPath)
127 {
128  if(empty($repoPath) || empty($sysConfPath))
129  {
130  return FALSE;
131  }
132  return _modFossConf($sysConfPath,$repoPath);
133 }
134 
146 function TestDBInit($path, $dbName)
147 {
148  if(empty($path))
149  {
150  $path = __DIR__ . '/../../www/ui/core-schema.dat';
151  }
152  if (!file_exists($path))
153  {
154  return("FAILED: Schema data file ($path) not found.\n");
155  }
156  if(empty($dbName))
157  {
158  return("Error!, no catalog supplied\n");
159  }
160 
161  // run ApplySchema via fossinit
162  $result = NULL;
163  $lastUp = NULL;
164  $sysc = getenv('SYSCONFDIR');
165  $fossInit = __DIR__ . '/../../../install/fossinit.php';
166  $upOut = array();
167  $cmd="$fossInit -d $dbName -f $path";
168  $last = exec($cmd, $upOut, $upRtn);
169  //echo "DB: schema up output is:\n" . implode("\n",$upOut) . "\n";
170 
171  if($upRtn != 0)
172  {
173  return(implode("\n", $upOut) . "\n");
174  }
175  else
176  {
177  return(NULL);
178  }
179 }