From 0b6b20804af389764b1216d3e0b4735304fcd06a Mon Sep 17 00:00:00 2001 From: Elena.Budnik Date: Mon, 13 Feb 2017 14:57:03 +0100 Subject: [PATCH] TT download --- src/InputOutput/IHMImpl/Config/IHMConfigClass.php | 13 ++++++++++--- src/InputOutput/IHMImpl/IHMInputOutputClass.php | 3 +++ src/InputOutput/IHMImpl/TimeTables/IHMInputOutputConvertTTClass.php | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/InputOutput/IHMImpl/TimeTables/IHMInputOutputTTClass.php | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Request/ProcessRequestImpl/Process/ProcessManagerClass.php | 2 +- src/Request/TTRequestImpl/PostProcessTTClass.php | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Request/TTRequestImpl/TTRequestClass.php | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Request/TTRequestImpl/TTRequestDataClass.php | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/RequestManager.php | 50 +++++++++++++++++++++++++++----------------------- 9 files changed, 520 insertions(+), 27 deletions(-) create mode 100644 src/InputOutput/IHMImpl/TimeTables/IHMInputOutputConvertTTClass.php create mode 100644 src/InputOutput/IHMImpl/TimeTables/IHMInputOutputTTClass.php create mode 100644 src/Request/TTRequestImpl/PostProcessTTClass.php create mode 100644 src/Request/TTRequestImpl/TTRequestClass.php create mode 100644 src/Request/TTRequestImpl/TTRequestDataClass.php diff --git a/src/InputOutput/IHMImpl/Config/IHMConfigClass.php b/src/InputOutput/IHMImpl/Config/IHMConfigClass.php index c305c03..6f988fe 100644 --- a/src/InputOutput/IHMImpl/Config/IHMConfigClass.php +++ b/src/InputOutput/IHMImpl/Config/IHMConfigClass.php @@ -34,6 +34,8 @@ class IHMConfigClass private static $ttDir = "TT/"; private static $wsDir = "WS/"; + + private static $downloadTmp = "DOWNLOAD.TEMP/"; private static $jobsDir = "JOBS/"; @@ -94,14 +96,19 @@ class IHMConfigClass return self::getUserPath().self::$paramTemplateGenerateDir; } + public static function getDownloadTmpPath() + { + return self::getUserPath().self::$downloadTmp; + } + public static function getParamTemplateListFilePath() { return IHM_SRC_DIR.self::$genericDataDir.self::$paramTemplateDir.self::$paramTemplateFile; } - public static function getUserParamManagerFilePath() - { - return self::getUserWSPath().self::$userParamMgrFile; + public static function getUserParamManagerFilePath() + { + return self::getUserWSPath().self::$userParamMgrFile; } public static function getUserDerivedParamFilePath($paramId) diff --git a/src/InputOutput/IHMImpl/IHMInputOutputClass.php b/src/InputOutput/IHMImpl/IHMInputOutputClass.php index 5d758e9..b3f3ec7 100644 --- a/src/InputOutput/IHMImpl/IHMInputOutputClass.php +++ b/src/InputOutput/IHMImpl/IHMInputOutputClass.php @@ -88,6 +88,9 @@ class IHMInputOutputClass implements InputOutputInterface case FunctionTypeEnumClass::TTUNION : $this->inputOutput = new IHMInputOutputUnionTTClass(); break;*/ + case FunctionTypeEnumClass::TTCONVERT : + $this->inputOutput = new IHMInputOutputTTClass(); + break; case FunctionTypeEnumClass::PARAMINFO : $this->inputOutput = new IHMInputOutputParamInfoClass(); break; diff --git a/src/InputOutput/IHMImpl/TimeTables/IHMInputOutputConvertTTClass.php b/src/InputOutput/IHMImpl/TimeTables/IHMInputOutputConvertTTClass.php new file mode 100644 index 0000000..ed610cd --- /dev/null +++ b/src/InputOutput/IHMImpl/TimeTables/IHMInputOutputConvertTTClass.php @@ -0,0 +1,69 @@ +jobsManager = new IHMJobsManagerClass(); + } + + /* + * @brief translate input data from IHM client to AMDA_Integration module for a tt process request + */ + public function getInputData($input, $function, $requestId="") + { + if (isset($this->processDatas)) + unset($this->processDatas); + + if (!$res['success']) + throw new Exception($res['message']); + + + foreach($input->list as $tt) + { + //create a list of data to treat + $processData = new TTRequestDataClass(); + + $processData->setInputTimeTable($tt); + $processData->setInputFormat($input); + $processData->setOutputDir(); + $processData->setOutputTimeTable($tt); + $processData->setOutputFormat(); + + $processData->setCompression(); + + $this->processDatas[] = $processData; + } + + return $this->processDatas; + } + + /* + * @brief translate output data from AMDA_Integration module to IHM client request + */ + public function getOutput($data) + { + $result = array(); + + foreach ($data as $d) + { + if (!$d->getSuccess()) + continue; + + //$res = $this->jobsManager->deleteJob($d->getId()); + } + + return $result; + } +} + +} +?> diff --git a/src/InputOutput/IHMImpl/TimeTables/IHMInputOutputTTClass.php b/src/InputOutput/IHMImpl/TimeTables/IHMInputOutputTTClass.php new file mode 100644 index 0000000..33168c8 --- /dev/null +++ b/src/InputOutput/IHMImpl/TimeTables/IHMInputOutputTTClass.php @@ -0,0 +1,122 @@ +processDatas)) + unset($this->processDatas); + + $final = ""; + + if (is_dir(IHMConfigClass::getDownloadTmpPath())) { + foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.zip') as $filename) unlink($filename); + foreach (glob(IHMConfigClass::getDownloadTmpPath().'*.tar.gz') as $filename) unlink($filename); + } + + foreach($input->list as $tt) + { + //create a list of data to treat + $processData = new TTRequestDataClass(); + $ttName = IHMConfigClass::getUserTTPath().$tt->id.'.xml'; + if (!file_exists($ttName)) + throw new Exception('TimeTable '.$tt->name.' not found'); + $processData->setInputFileName($ttName); + $processData->setInputFileFormat(TTRequestFileFormatEnum::INTERNAL); + $processData->setOutputDir(IHMConfigClass::getDownloadTmpPath()); + + switch ($input->fileformat) { + case 'vot' : + $processData->setOutputFileFormat(TTRequestFileFormatEnum::VOTABLE); + $outputName = $tt->name.'.xml'; + break; + default : + $processData->setOutputFileFormat(TTRequestFileFormatEnum::ASCII); + $outputName = $tt->name; + } + + // $processData->setOutputTimeFormat($input->timeformat); + $processData->setOutputFileName($outputName); + $final .= " ".$outputName; + $processData->setType($function); + $processData->setCmd("ttConversion"); + + $this->processDatas[] = $processData; + } + + $this->postProcessTT = new PostProcessTTClass(); + $this->postProcessTT->setOutputDirAlias($this->processDatas[0]->getOutputDir()); + + if ($input->compression !== 'none') { + $this->postProcessTT->compression = true; + $archive = "Timetable_archive_".date('Ymd_Hi'); + switch ($input->compression) { + case 'zip' : + $postCmd = "zip -Dj ".$archive.".zip ".$final." 1> /dev/null 2> /dev/null"; + $outputFileName = $archive.".zip"; + break; + case 'tar+gzip': + default : + $postCmd="tar cfz ".$archive.".tar.gz --directory=".$this->processDatas[0]->getOutputDir()." ".$final; + $outputFileName = $archive.".tar.gz"; + break; + } + $this->postProcessTT->setPostCmd($postCmd); + $this->postProcessTT->setOutputFileName($outputFileName); + $this->postProcessTT->setOutputDir($this->processDatas[0]->getOutputDir()); + } + + return $this->processDatas; + } + +/* + * @brief translate output data from AMDA_Integration module to IHM client request +*/ + public function getOutput($data) + { + if ( $this->postProcessTT->compression ) { + + $cmd = $this->postProcessTT->getPostCmd(); + system($cmd); + sleep(1); // if immediately - raw files are not deleted (?) + + foreach ($data as $d) + { + $rawFileName = $d->getOutputDir().$d->getOutputFileName(); + if (file_exists($rawFileName)) + unlink($rawFileName); + } + + $result = array('success'=>true, + 'download'=>$this->postProcessTT->getOutputDirAlias().$this->postProcessTT->getOutputFileName(), + 'compression' => $this->postProcessTT->compression); + + + } + else { + $result = array('success'=>$data[0]->getSuccess(), + 'download' => $this->postProcessTT->getOutputDirAlias().$data[0]->getOutputFileName()); + } + + return $result; + } +} + +?> diff --git a/src/Request/ProcessRequestImpl/Process/ProcessManagerClass.php b/src/Request/ProcessRequestImpl/Process/ProcessManagerClass.php index 39f5fb9..b1fc8f4 100644 --- a/src/Request/ProcessRequestImpl/Process/ProcessManagerClass.php +++ b/src/Request/ProcessRequestImpl/Process/ProcessManagerClass.php @@ -24,7 +24,7 @@ class ProcessManagerClass $process = new ProcessClass($cmd, $postProcessCmd); if (!$process->run($runningPath, $envArray)) - return array("success" => false, "message" => "Cannot run the process");; + return array("success" => false, "message" => "Cannot run the process"); $res = $this->concurrentAccessProcessManagerFile(array($this,'addProcessInProcessManagerFile'),$process); diff --git a/src/Request/TTRequestImpl/PostProcessTTClass.php b/src/Request/TTRequestImpl/PostProcessTTClass.php new file mode 100644 index 0000000..18e21e8 --- /dev/null +++ b/src/Request/TTRequestImpl/PostProcessTTClass.php @@ -0,0 +1,63 @@ +outputFileName; + } + + public function setOutputFileName($outputFileName) + { + $this->outputFileName = $outputFileName; + } + + public function getPostCmd() + { + return $this->postCmd; + } + + public function setPostCmd($postCmd) + { + $this->postCmd = $postCmd; + } + + public function getOutputDir() + { + return $this->outputDir."/"; + } + + public function setOutputDir($outputDir) + { + $this->outputDir = $outputDir; + } + + public function getOutputDirAlias() + { + return $this->outputDirAlias; + } + + public function setOutputDirAlias($outputDir) + { + $pos = strpos($outputDir, '/data/'); + + if ($pos > 0) + $this->outputDirAlias = substr($outputDir,$pos + 1); + } + +} + +?> \ No newline at end of file diff --git a/src/Request/TTRequestImpl/TTRequestClass.php b/src/Request/TTRequestImpl/TTRequestClass.php new file mode 100644 index 0000000..5b37b62 --- /dev/null +++ b/src/Request/TTRequestImpl/TTRequestClass.php @@ -0,0 +1,96 @@ +requestData)) + return false; + + if ($this->requestData->getCmd() == '') + { + $this->requestData->setLastErrorMessage('No process command defined'); + return false; + } + + if ($this->requestData->getOutputDir() == '') + { + $this->requestData->setLastErrorMessage('No output dir defined'); + return false; + } + + if (!is_dir($this->requestData->getOutputDir())) + { + if (!mkdir($this->requestData->getOutputDir(),0777) || !is_dir($this->requestData->getOutputDir())) + { + $this->requestData->setLastErrorMessage('Cannot create output dir'); + return false; + } + } + + if (!file_exists($this->requestData->getOutputDir()."app.properties")) + KernelConfigClass::write($this->requestData->getOutputDir(), "", ""); + + $this->requestData->setEnvVars(KernelConfigClass::getExecEnvVarArray()); + + return true; + } + + /* + * @brief Run a process request + */ + + public function run() + { + if (!isset($this->requestData)) + return false; + + $this->requestData->setSuccess(false); + $this->requestData->setLastErrorMessage('Cannot run process request'); + + switch ($this->requestData->getType()) + { + case FunctionTypeEnumClass::TTCONVERT : + //run process + + $args = $this->requestData->getInputFileName()." ".$this->requestData->getInputFileFormat() + ." ".$this->requestData->getOutputDir()." ".$this->requestData->getOutputFileName() + ." ".$this->requestData->getOutputFileFormat(); + + $cmd = $this->requestData->getCmd()." ".$args; + + $process = new ProcessClass($cmd); //, $postProcessCmd); + + if (!$process->run($this->requestData->getOutputDir(), $this->requestData->getEnvVars())) + return array("success" => false, "message" => "Cannot run the TT process"); + + //TBD + while ($process->isRunning() && time() - $process->getRunningStart() > KernelConfigClass::getTimeToBatchMode()) { + sleep(1); + } + + $result = array('success' => true); + + break; + default : + $this->requestData->setLastErrorMessage("TT process method not implemented"); + return false; + } + + $this->requestData->setSuccess($result['success']); + + if (!$result['success']) + $this->requestData->setLastErrorMessage($result['message']); + + return ($result['success']); + } + +} +?> \ No newline at end of file diff --git a/src/Request/TTRequestImpl/TTRequestDataClass.php b/src/Request/TTRequestImpl/TTRequestDataClass.php new file mode 100644 index 0000000..1201e22 --- /dev/null +++ b/src/Request/TTRequestImpl/TTRequestDataClass.php @@ -0,0 +1,129 @@ +inputTimeTable; + } + + public function setInputFileName($inputTimeTable) + { + $this->inputTimeTable = $inputTimeTable; + } + + public function getOutputFileName() + { + return $this->outputTimeTable; + } + + public function setOutputFileName($outputTimeTable) + { + $this->outputTimeTable = $outputTimeTable; + } + + public function getOutputFileFormat() + { + return $this->outputFileFormat; + } + + public function setOutputFileFormat($outputFileFormat) + { + $this->outputFileFormat = $outputFileFormat; + } + + public function getInputFileFormat() + { + return $this->inputFileFormat; + } + + public function setInputFileFormat($inputFileFormat) + { + $this->inputFileFormat = $inputFileFormat; + } + + public function getOutputDir() + { + return $this->outputDir."/"; + } + + public function setOutputDir($outputDir) + { + $this->outputDir = $outputDir; + } + + public function getCmd() + { + return $this->cmd; + } + + public function setCmd($cmd) + { + $this->cmd = $cmd; + } + + public function getEnvVars() + { + return $this->envVars; + } + + public function setEnvVars($envVars) + { + $this->envVars = $envVars; + } + + public function getType() + { + return $this->type; + } + + public function setType($type) + { + $this->type = $type; + } + + + public function getErrorCode() + { + return $this->exitCode; + } + + public function setErrorCode($errorCode) + { + $this->exitCode = $errorCode; + } +} + +?> \ No newline at end of file diff --git a/src/RequestManager.php b/src/RequestManager.php index 0c6ff0b..a5ab10e 100644 --- a/src/RequestManager.php +++ b/src/RequestManager.php @@ -16,6 +16,7 @@ function amdaintegration_autoload($class_name) 'InputOutput/IHMImpl/Process', 'InputOutput/IHMImpl/ParamInfo', 'InputOutput/IHMImpl/Tools', + 'InputOutput/IHMImpl/TimeTables', 'InputOutput/TestImpl', 'Request', 'Request/Config', @@ -25,7 +26,8 @@ function amdaintegration_autoload($class_name) 'Request/ParamsRequestImpl/Nodes/Params', 'Request/ParamsRequestImpl/Nodes/Requests', 'Request/ProcessRequestImpl', - 'Request/ProcessRequestImpl/Process' + 'Request/ProcessRequestImpl/Process', + 'Request/TTRequestImpl' ); foreach($dirs as $dir) @@ -57,6 +59,7 @@ abstract class FunctionTypeEnumClass const PROCESSCLEAN = "process_clean"; const TTMERGE = "tt_merge"; const TTUNION = "tt_union"; + const TTCONVERT = "tt_convert"; const PARAMINFO = "param_info"; } @@ -80,33 +83,33 @@ Class RequestManagerClass { public static $version = "1.5.0"; - /* - * @brief Constructor - */ +/* + * @brief Constructor +*/ function __construct() { } - /* - * @brief Treat a request that's come from the IHM - */ +/* + * @brief Treat a request that's come from the IHM +*/ public function runIHMRequest($user, $userHost, $function, $input) { return $this->runGenericRequest(ClientTypeEnumClass::IHM,$user,$userHost,$function,$input); } - /* - * @brief Treat a request that's come from a test script - */ +/* + * @brief Treat a request that's come from a test script +*/ public function runTestRequest($user, $function, $input) { $userHost = ""; return $this->runGenericRequest(ClientTypeEnumClass::TEST,$user,$userHost,$function,$input); } - /* - * @brief Create the request instance in relation with the function type - */ +/* + * @brief Create the request instance in relation with the function type +*/ private function createRequest($user, $userHost, $function) { switch ($function) @@ -122,6 +125,7 @@ Class RequestManagerClass return new ProcessRequestClass($user, $userHost); case FunctionTypeEnumClass::TTMERGE : case FunctionTypeEnumClass::TTUNION : + case FunctionTypeEnumClass::TTCONVERT : return new TTRequestClass($user, $userHost); case FunctionTypeEnumClass::PARAMINFO : return new ParamInfoRequestClass($user, $userHost); @@ -130,9 +134,9 @@ Class RequestManagerClass } } - /* - * @brief Create an instance of the InputOutput interface in relation with the client - */ +/* + * @brief Create an instance of the InputOutput interface in relation with the client +*/ private function createInputOutput($user,$client) { switch ($client) @@ -146,9 +150,9 @@ Class RequestManagerClass } } - /* - * @brief Sequence used to run a request - */ +/* + * @brief Sequence used to run a request +*/ private function runGenericRequest($client,$user,$userHost,$function,$input) { //create an instance of the InputOutput interface @@ -169,14 +173,14 @@ Class RequestManagerClass } else $outputdata = $this->runSingleRequest($request,$inputdata); - + //get the request output data from the output data by using the InputOutput interface return $inputOutput->getOutput($outputdata); } - /* - * @brief Run a single request - */ +/* + * @brief Run a single request +*/ private function runSingleRequest($request,$data) { //link the request to the input data -- libgit2 0.21.2