diff --git a/lib/Service/PythonService.php b/lib/Service/PythonService.php index 2e1a6717..5a247771 100644 --- a/lib/Service/PythonService.php +++ b/lib/Service/PythonService.php @@ -113,12 +113,12 @@ public function run( } if ($nonBlocking) { if ($binary) { - $logFile = $cwd . 'logs/' . date('d-m-Y_H:i:s', time()) . '.log'; + $logFile = $cwd . 'logs/' . date('d-m-Y_H-i-s', time()) . '.log'; } else { $appDataDir = $this->ncDataFolder . '/appdata_' . $this->ncInstanceId . '/' . $appId . '/'; $pyBitecodeEnvVar = 'PYTHONBYTECODEBASE="' . $appDataDir . '" '; $envVariables = $pyBitecodeEnvVar . $envVariables; - $logFile = $appDataDir . 'logs/' . date('d-m-Y_H:i:s', time()) . '.log'; + $logFile = $appDataDir . 'logs/' . date('d-m-Y_H-i-s', time()) . '.log'; } $cmd = $envVariables . 'nohup ' . $cmd . ' > ' . $logFile . ' 2>' . $logFile . ' &'; exec($cmd); diff --git a/lib/Service/UtilsService.php b/lib/Service/UtilsService.php index c8b7d0f3..59ea9e6b 100644 --- a/lib/Service/UtilsService.php +++ b/lib/Service/UtilsService.php @@ -190,8 +190,8 @@ public function isVideosSupported(): bool { } public function isMusliLinux(): bool { - exec('ldd --version', $output, $result_code); - if ($result_code == 0 && count($output) > 0 && str_contains($output[0], 'musl')) { + exec('ldd --version 2>&1', $output, $result_code); + if (count($output) > 0 && str_contains($output[0], 'musl')) { return true; } return false; @@ -274,9 +274,7 @@ public function downloadPythonBinary( } $file_name = $filename . '.gz'; $save_file_loc = $dir . $file_name; - $shouldDownloadBinary = $this->compareBinaryHash( - $url, $dir . $filename, $binariesFolder, $filename - ); + $shouldDownloadBinary = $this->compareBinaryHash($url, $dir . $filename); if (!file_exists($dir . $filename) || ($update && $shouldDownloadBinary)) { $cURL = curl_init($url); $fp = fopen($save_file_loc, 'wb'); @@ -314,87 +312,46 @@ public function downloadPythonBinary( /** * @codeCoverageIgnore * + * Compare binary hash from release. If hash not exists return `true` (download anyway) + * + * @param string $url * @param string $binaryPath - * @param array $binariesFolder, - * @param string $filanem * * @return bool */ - public function compareBinaryHash( - string $url, - string $binaryPath, - array $binariesFolder, - string $filename - ) { + public function compareBinaryHash(string $url, string $binaryPath) { if (file_exists($binaryPath)) { - // get current binary hash (from .sha256 file or directly from existing binary) - if (file_exists($binaryPath . '.sha256')) { - $currentBinaryHash = file_get_contents( - $binaryPath . '.sha256', false, null, 0, 64 - ); - } else { - $binaryData = file_get_contents($binaryPath); - $currentBinaryHash = hash('sha256', $binaryData); - } - // download new binary sha256 hash from attached file to release - copy($binaryPath . '.sha256', $binaryPath . '.sha256.old'); - $newBinaryHash = $this->downloadBinaryHash( - str_replace('.gz', '.sha256', $url), $binariesFolder, $filename - ); - // should update binary if hashes not equial - if ($newBinaryHash['success']) { + $binaryData = file_get_contents($binaryPath); + $currentBinaryHash = hash('sha256', $binaryData); + $newBinaryHash = $this->downloadBinaryHash(str_replace('.gz', '.sha256', $url)); + if ($newBinaryHash['success'] && strlen($newBinaryHash['binaryHash']) == 64) { return $currentBinaryHash != $newBinaryHash['binaryHash']; - } else { - // revert back old hash file - copy($binaryPath . '.sha256.old', $binaryPath . '.sha256'); - unlink($binaryPath . '.sha256.old'); } } - return false; + return true; } /** - * Perform cURL download binary's sha256 sum file + * Perform cURL to get binary's sha256 sum * * @codeCoverageIgnore * * @param string $url url to the binary hashsum file - * @param array $binariesFolder appdata binaries folder - * @param string $filename downloaded checksum filename * * @return array */ - public function downloadBinaryHash( - string $url, - array $binariesFolder, - string $filename - ): array { - if (isset($binariesFolder['success']) && $binariesFolder['success']) { - $dir = $binariesFolder['path'] . '/'; - } else { - return $binariesFolder; // Return getAppDataFolder result - } - $file_name = $filename . '.sha256'; - $save_file_loc = $dir . $file_name; + public function downloadBinaryHash(string $url): array { $cURL = curl_init($url); - $fp = fopen($save_file_loc, 'w'); - if ($fp) { - curl_setopt_array($cURL, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_FILE => $fp, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_RANGE => 64, - ]); - $binaryHash = curl_exec($cURL); - curl_close($cURL); - fclose($fp); - return [ - 'success' => true, - 'binaryHash' => $binaryHash, - 'binaryHashFilePath' => $save_file_loc, - ]; - } - return ['success' => false]; + curl_setopt_array($cURL, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_RANGE => 64, + ]); + $binaryHash = curl_exec($cURL); + curl_close($cURL); + return [ + 'success' => $binaryHash != false, + 'binaryHash' => $binaryHash, + ]; } /** diff --git a/tests/Unit/Service/UtilsServiceTest.php b/tests/Unit/Service/UtilsServiceTest.php index 8eae0f9f..e99ddf16 100644 --- a/tests/Unit/Service/UtilsServiceTest.php +++ b/tests/Unit/Service/UtilsServiceTest.php @@ -192,9 +192,9 @@ public function testIsMusliLinux() { $exec->expects($this->any()) ->willReturnCallback( function ($command, &$output, &$result_code) { - $this->assertEquals('ldd --version', $command); + $this->assertEquals('ldd --version 2>&1', $command); $output = ['manylinux']; - $result_code = 0; + $result_code = 1; } ); $result = $this->utils->isMusliLinux(); @@ -207,9 +207,9 @@ public function testIsMusliLinuxWithOverride() { $exec->expects($this->any()) ->willReturnCallback( function ($command, &$output, &$result_code) { - $this->assertEquals('ldd --version', $command); + $this->assertEquals('ldd --version 2>&1', $command); $output = ['musl linux']; - $result_code = 0; + $result_code = 1; } ); $result = $this->utils->isMusliLinux(); @@ -356,7 +356,7 @@ public function testGetBinaryNameMuslLinux() { $exec->expects($this->any()) ->willReturnCallback( function ($command, &$output, &$result_code) { - $this->assertEquals('ldd --version', $command); + $this->assertEquals('ldd --version 2>&1', $command); $output = ['musl linux']; $result_code = 0; }