Пример #1
0
 /**
  * exec method with the same interface as that in KWUtils, will check the
  * command and intercept it if it is a known command with a Mock object
  * that should be used for testing, otherwise it will pass it on to KWUtils.
  *
  * @param type $command
  * @param type $output
  * @param type $chdir
  * @param type $return_val
  */
 public function exec($command, &$output = null, $chdir = '', &$return_val = null)
 {
     $matched = false;
     foreach ($this->mockExes as $exeName => $mockExe) {
         if (preg_match('/' . $exeName . '/', $command)) {
             $matched = true;
             $mockExe->exec($command, $output, $chdir, $return_val);
         }
     }
     if (!$matched) {
         KWUtils::exec($command, $output, $chdir, $return_val);
     }
 }
Пример #2
0
 /**
  * function will create a temporary batchmake config, copying over test data
  * to the the locations in that config needed for the tests, returning an
  * array of config property names to directory locations.
  *
  * @todo figure out a way to copy over Batchmake or else mock it
  * @return array
  * @throws Zend_Exception
  */
 public function setupAndGetConfig()
 {
     // create a test batchmake setup in the temp dir
     // and initialize test data
     $tmpDir = $this->getTempDirectory();
     $subDirs = array('batchmake', 'tests');
     KWUtils::createSubDirectories($tmpDir . '/', $subDirs);
     $configProps = array(MIDAS_BATCHMAKE_TMP_DIR_PROPERTY => $tmpDir . '/batchmake/tests/tmp', MIDAS_BATCHMAKE_BIN_DIR_PROPERTY => $tmpDir . '/batchmake/tests/bin', MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY => $tmpDir . '/batchmake/tests/script', MIDAS_BATCHMAKE_APP_DIR_PROPERTY => $tmpDir . '/batchmake/tests/bin', MIDAS_BATCHMAKE_DATA_DIR_PROPERTY => $tmpDir . '/batchmake/tests/data', MIDAS_BATCHMAKE_CONDOR_BIN_DIR_PROPERTY => $tmpDir . '/batchmake/tests/condorbin');
     // now make sure these dirs exist
     // later can actually add some stuff to these dirs
     foreach ($configProps as $dir) {
         if (!file_exists($dir) && !KWUtils::mkDir($dir)) {
             throw new Zend_Exception("couldn't create dir " . $dir);
         }
     }
     // now copy over the bms files
     $srcDir = BASE_PATH . 'modules/batchmake/tests/testfiles/script';
     $targetDir = $configProps[MIDAS_BATCHMAKE_SCRIPT_DIR_PROPERTY];
     $extension = '.bms';
     $this->symlinkFileset($srcDir, $targetDir, $extension);
     // and now the bmms
     $srcDir = BASE_PATH . 'modules/batchmake/tests/testfiles/bin';
     $targetDir = $configProps[MIDAS_BATCHMAKE_APP_DIR_PROPERTY];
     $extension = '.bmm';
     $this->symlinkFileset($srcDir, $targetDir, $extension);
     // the mock object strategy requires both an interface and for
     // executable files to exist on disk in a particular location,
     // so here we will create symlinks to a known executable
     // ls
     // which should be on most systems
     $params = array('ls');
     $cmd = KWUtils::prepareExeccommand('which', $params);
     // dir doesn't matter, just send in srcDir as it is convenient
     KWUtils::exec($cmd, $output, $srcDir, $returnVal);
     if ($returnVal !== 0 || !isset($output) || !isset($output[0])) {
         throw new Zend_Exception('Problem finding ls on your system, used for testing');
     }
     $pathToLs = $output[0];
     // get the applications and their path properties from the component that
     // expects them
     $applicationsPaths = Batchmake_KWBatchmakeComponent::getApplicationsPaths();
     foreach ($applicationsPaths as $application => $pathProperty) {
         // now in the place of each executable, symlink the ls exe
         $link = $configProps[$pathProperty] . '/' . $application;
         if (!file_exists($link) && !symlink($pathToLs, $link)) {
             throw new Zend_Exception($pathToLs . ' could not be sym-linked to ' . $link);
         }
     }
     return $configProps;
 }
Пример #3
0
 /**
  * Stop DICOM server.
  *
  * @path /dicomserver/server/stop
  * @http POST
  * @param storescp_cmd (Optional) The command to run storescp
  * @param dcmqrscp_cmd (Optional) The command to run dcmqrscp
  * @param incoming_dir (Optional) The incoming directory to receive and process DICOM files
  * @param get_command (Optional) If set, will not stop DICOM server, but only get command used to stop DICOM server in command line.
  * @return
  */
 public function stop($args)
 {
     // Only administrator can call this api
     $userDao = Zend_Registry::get('userSession')->Dao;
     if (!$userDao || !$userDao->isAdmin()) {
         throw new Exception('Only administrator can stop DICOM server', MIDAS_INVALID_POLICY);
     }
     $ret = array();
     $status_args = array();
     if (!empty($args['storescp_cmd'])) {
         $status_args['storescp_cmd'] = $args['storescp_cmd'];
     }
     if (!empty($args['dcmqrscp_cmd'])) {
         $status_args['dcmqrscp_cmd'] = $args['dcmqrscp_cmd'];
     }
     $running_status = $this->status($status_args);
     if ($running_status['status'] == MIDAS_DICOMSERVER_SERVER_NOT_RUNNING && !array_key_exists('get_command', $args)) {
         $ret['message'] = 'DICOM server is not running now!';
         return $ret;
     }
     $storescp_cmd = 'storescp';
     if (!empty($args['storescp_cmd'])) {
         $storescp_cmd = $args['storescp_cmd'];
     }
     $dcmqrscp_cmd = 'dcmqrscp';
     if (!empty($args['dcmqrscp_cmd'])) {
         $dcmqrscp_cmd = $args['dcmqrscp_cmd'];
     }
     if (!empty($args['incoming_dir'])) {
         $incoming_dir = $args['incoming_dir'];
     } else {
         /** @var Dicomserver_ServerComponent $serverComponent */
         $serverComponent = MidasLoader::loadComponent('Server', 'dicomserver');
         $incoming_dir = $serverComponent->getDefaultReceptionDir();
     }
     $log_dir = $incoming_dir . MIDAS_DICOMSERVER_LOGS_DIRECTORY;
     if (!file_exists($log_dir)) {
         KWUtils::mkDir($log_dir, 0777);
     }
     $python_cmd = 'python';
     $python_params = array();
     $python_params[] = BASE_PATH . '/modules/dicomserver/library/serverWrapper.py';
     $python_params[] = '--stop';
     $python_params[] = '-i ' . $incoming_dir;
     $python_params[] = '-s ' . $storescp_cmd;
     $python_params[] = '-q ' . $dcmqrscp_cmd;
     $stop_server_command = KWUtils::prepareExeccommand($python_cmd, $python_params);
     if (array_key_exists('get_command', $args)) {
         $stop_server_command_string = str_replace("'", '', $stop_server_command);
         return escapeshellarg($stop_server_command_string);
     }
     KWUtils::exec($stop_server_command, $output, '', $returnVal);
     $ret['message'] = 'Succeeded to stop DICOM C-STORE receiver and Query-Retrieve services!';
     if ($returnVal) {
         $exception_string = "Failed to stop DICOM server! \n Reason:" . implode("\n", $output);
         throw new Zend_Exception(htmlspecialchars($exception_string, ENT_QUOTES), MIDAS_INVALID_POLICY);
     }
     return $ret;
 }
Пример #4
0
 /**
  * Use thumbnailer to pre-process a bitstream to generate a jpeg file.
  * Echoes an error message if a problem occurs (for the scheduler log).
  *
  * @param string $name name of the image to be pre-processed
  * @param string $fullPath absolute path to the image to be pre-processed
  * @return string
  * @throws Zend_Exception
  */
 public function preprocessByThumbnailer($name, $fullPath)
 {
     $tmpPath = UtilityComponent::getTempDirectory('thumbnailcreator');
     if (!file_exists($tmpPath)) {
         throw new Zend_Exception('Temporary thumbnail dir does not exist: ' . $tmpPath);
     }
     $copyDestination = $tmpPath . '/' . $name;
     copy($fullPath, $copyDestination);
     $jpegDestination = $tmpPath . '/' . $name . '.jpg';
     /** @var RandomComponent $randomComponent */
     $randomComponent = MidasLoader::loadComponent('Random');
     while (file_exists($jpegDestination)) {
         $jpegDestination = $tmpPath . '/' . $name . $randomComponent->generateInt() . '.jpg';
     }
     /** @var SettingModel $settingModel */
     $settingModel = MidasLoader::loadModel('Setting');
     $thumbnailerPath = $settingModel->getValueByName(MIDAS_THUMBNAILCREATOR_THUMBNAILER_KEY, $this->moduleName);
     $thumbnailerParams = array($copyDestination, $jpegDestination);
     $thumbnailerCmd = KWUtils::prepareExeccommand($thumbnailerPath, $thumbnailerParams);
     if (KWUtils::isExecutable($thumbnailerPath)) {
         KWUtils::exec($thumbnailerCmd);
     } else {
         unlink($copyDestination);
         throw new Zend_Exception('Thumbnailer does not exist or you do not have execute permission. Please check the configuration of thumbnailcreator module.');
     }
     if (!file_exists($jpegDestination)) {
         unlink($copyDestination);
         throw new Zend_Exception('Problem executing thumbnailer on your system');
     } else {
         unlink($copyDestination);
         return $jpegDestination;
     }
 }
Пример #5
0
 /**
  * Register DICOM image files (bitstreams).
  */
 public function register($revision)
 {
     $bitstreams = $revision->getBitstreams();
     if (count($bitstreams) < 1) {
         return;
     }
     /** @var SettingModel $settingModel */
     $settingModel = MidasLoader::loadModel('Setting');
     $command = $settingModel->getValueByName(MIDAS_DICOMSERVER_DCMQRIDX_COMMAND_KEY, $this->moduleName);
     $command = str_replace("'", '', $command);
     $commandParams = array();
     $receptionDirectory = $settingModel->getValueByName(MIDAS_DICOMSERVER_RECEPTION_DIRECTORY_KEY, $this->moduleName);
     if (!is_writable($receptionDirectory)) {
         throw new Zend_Exception('Please configure Dicom Server module correctly. Its reception directory is NOT writable!', MIDAS_INVALID_POLICY);
     }
     $aeStorage = $receptionDirectory . MIDAS_DICOMSERVER_PACS_DIRECTORY;
     $aeStorage = str_replace("'", '', $aeStorage);
     $commandParams[] = $aeStorage;
     foreach ($bitstreams as $bitstream) {
         $commandParams[] = $bitstream->getFullPath();
         $registerCommand = KWUtils::prepareExeccommand($command, $commandParams);
         array_pop($command_params);
         // prepare for next iteration in the loop
         KWUtils::exec($registerCommand, $output, '', $returnVal);
         if ($returnVal) {
             $exceptionString = "Failed to register DICOM images! \n Reason:" . implode("\n", $output);
             throw new Zend_Exception(htmlspecialchars($exceptionString, ENT_QUOTES), MIDAS_INVALID_POLICY);
         }
     }
     /** @var Dicomserver_RegistrationModel $registrationModel */
     $registrationModel = MidasLoader::loadModel('Registration', 'dicomserver');
     $itemId = $revision->getItemId();
     if (!$registrationModel->checkByItemId($itemId)) {
         $registrationModel->createRegistration($itemId);
     }
 }
Пример #6
0
 /** tests exec function */
 public function testExec()
 {
     // Do not run this on Windows until can figure out how to test it
     if (KWUtils::isWindows()) {
         return;
     }
     // not sure how to test this exactly, for now create a tmp dir, check
     // the value of pwd in it
     $initialCwd = getcwd();
     $output = null;
     $returnVal = null;
     // create a tmp dir for this test
     $execDir = UtilityComponent::getTempDirectory() . '/KWUtilsTest';
     mkdir($execDir);
     $cmd = 'pwd';
     $chdir = $execDir;
     KWUtils::exec($cmd, $output, $chdir, $returnVal);
     // $output should have one value, the same as execDir
     $postCwd = getcwd();
     // check that we are back to the original directory after the exec
     // we are already checking with this test that we chdir correctly because
     // the test changes into a dir and then performs pwd there
     $this->assertEquals($initialCwd, $postCwd, "exec didn't correctly return to the origin directory");
     // yuck, need to do a bit of munging to get around tests/.. in BASE_PATH
     $execDir = str_replace('tests/../', '', $execDir);
     // and now replace any // with /,
     // the // doesn't affect functionality on the
     // filesystem, but will cause string inequality
     $execDir = str_replace('//', '/', $execDir);
     $this->assertEquals($execDir, $output[0]);
     // returnVal should be 0
     $this->assertEquals($returnVal, 0);
     // now clean up the tmp dir
     rmdir($execDir);
     // pass in a bad cwd, be sure that we get an exception
     $chdir = '/this/dir/probably/will/not/exist/anywhere';
     $output = null;
     $returnVal = null;
     try {
         KWUtils::exec($cmd, $output, $chdir, $returnVal);
         $this->fail();
     } catch (Zend_Exception $ze) {
         // this is the correct behavior
         $this->assertTrue(true);
     }
     $postCwd = getcwd();
     // ensure we are still in the same directory
     $this->assertEquals($initialCwd, $postCwd, "exec didn't correctly return to the origin directory");
     // now try to run pwd passing in a null chdir
     // should return an output the same as the initialCwd
     $chdir = null;
     $output = null;
     $returnVal = null;
     KWUtils::exec($cmd, $output, $chdir, $returnVal);
     // ensure that it ran in the initialCwd
     $this->assertEquals($initialCwd, $output[0]);
     // check that we are still in the output dir
     $postCwd = getcwd();
     $this->assertEquals($initialCwd, $postCwd);
 }
Пример #7
0
 /**
  * forwards a call to this method on to KWUtils.exec, with the same
  * method signature.
  *
  * @param string $command
  * @param null|mixed $output
  * @param string $chdir
  * @param null|mixed $return_val
  * @throws Zend_Exception
  */
 public function exec($command, &$output = null, $chdir = '', &$return_val = null)
 {
     KWUtils::exec($command, $output, $chdir, $return_val);
 }