Skip to content

Commit 7838146

Browse files
authored
Fix logs name colon symbol, fix binary hash check, fix musllinux detection. (#49)
* Fix log file name escape colon symbol. Fix musllinux detect. Minor changes in pre-compiled binary hash check * Fix test * Fix params to binary check function
1 parent f4f2f1c commit 7838146

File tree

3 files changed

+31
-74
lines changed

3 files changed

+31
-74
lines changed

lib/Service/PythonService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ public function run(
113113
}
114114
if ($nonBlocking) {
115115
if ($binary) {
116-
$logFile = $cwd . 'logs/' . date('d-m-Y_H:i:s', time()) . '.log';
116+
$logFile = $cwd . 'logs/' . date('d-m-Y_H-i-s', time()) . '.log';
117117
} else {
118118
$appDataDir = $this->ncDataFolder . '/appdata_' . $this->ncInstanceId . '/' . $appId . '/';
119119
$pyBitecodeEnvVar = 'PYTHONBYTECODEBASE="' . $appDataDir . '" ';
120120
$envVariables = $pyBitecodeEnvVar . $envVariables;
121-
$logFile = $appDataDir . 'logs/' . date('d-m-Y_H:i:s', time()) . '.log';
121+
$logFile = $appDataDir . 'logs/' . date('d-m-Y_H-i-s', time()) . '.log';
122122
}
123123
$cmd = $envVariables . 'nohup ' . $cmd . ' > ' . $logFile . ' 2>' . $logFile . ' &';
124124
exec($cmd);

lib/Service/UtilsService.php

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ public function isVideosSupported(): bool {
190190
}
191191

192192
public function isMusliLinux(): bool {
193-
exec('ldd --version', $output, $result_code);
194-
if ($result_code == 0 && count($output) > 0 && str_contains($output[0], 'musl')) {
193+
exec('ldd --version 2>&1', $output, $result_code);
194+
if (count($output) > 0 && str_contains($output[0], 'musl')) {
195195
return true;
196196
}
197197
return false;
@@ -274,9 +274,7 @@ public function downloadPythonBinary(
274274
}
275275
$file_name = $filename . '.gz';
276276
$save_file_loc = $dir . $file_name;
277-
$shouldDownloadBinary = $this->compareBinaryHash(
278-
$url, $dir . $filename, $binariesFolder, $filename
279-
);
277+
$shouldDownloadBinary = $this->compareBinaryHash($url, $dir . $filename);
280278
if (!file_exists($dir . $filename) || ($update && $shouldDownloadBinary)) {
281279
$cURL = curl_init($url);
282280
$fp = fopen($save_file_loc, 'wb');
@@ -314,87 +312,46 @@ public function downloadPythonBinary(
314312
/**
315313
* @codeCoverageIgnore
316314
*
315+
* Compare binary hash from release. If hash not exists return `true` (download anyway)
316+
*
317+
* @param string $url
317318
* @param string $binaryPath
318-
* @param array $binariesFolder,
319-
* @param string $filanem
320319
*
321320
* @return bool
322321
*/
323-
public function compareBinaryHash(
324-
string $url,
325-
string $binaryPath,
326-
array $binariesFolder,
327-
string $filename
328-
) {
322+
public function compareBinaryHash(string $url, string $binaryPath) {
329323
if (file_exists($binaryPath)) {
330-
// get current binary hash (from .sha256 file or directly from existing binary)
331-
if (file_exists($binaryPath . '.sha256')) {
332-
$currentBinaryHash = file_get_contents(
333-
$binaryPath . '.sha256', false, null, 0, 64
334-
);
335-
} else {
336-
$binaryData = file_get_contents($binaryPath);
337-
$currentBinaryHash = hash('sha256', $binaryData);
338-
}
339-
// download new binary sha256 hash from attached file to release
340-
copy($binaryPath . '.sha256', $binaryPath . '.sha256.old');
341-
$newBinaryHash = $this->downloadBinaryHash(
342-
str_replace('.gz', '.sha256', $url), $binariesFolder, $filename
343-
);
344-
// should update binary if hashes not equial
345-
if ($newBinaryHash['success']) {
324+
$binaryData = file_get_contents($binaryPath);
325+
$currentBinaryHash = hash('sha256', $binaryData);
326+
$newBinaryHash = $this->downloadBinaryHash(str_replace('.gz', '.sha256', $url));
327+
if ($newBinaryHash['success'] && strlen($newBinaryHash['binaryHash']) == 64) {
346328
return $currentBinaryHash != $newBinaryHash['binaryHash'];
347-
} else {
348-
// revert back old hash file
349-
copy($binaryPath . '.sha256.old', $binaryPath . '.sha256');
350-
unlink($binaryPath . '.sha256.old');
351329
}
352330
}
353-
return false;
331+
return true;
354332
}
355333

356334
/**
357-
* Perform cURL download binary's sha256 sum file
335+
* Perform cURL to get binary's sha256 sum
358336
*
359337
* @codeCoverageIgnore
360338
*
361339
* @param string $url url to the binary hashsum file
362-
* @param array $binariesFolder appdata binaries folder
363-
* @param string $filename downloaded checksum filename
364340
*
365341
* @return array
366342
*/
367-
public function downloadBinaryHash(
368-
string $url,
369-
array $binariesFolder,
370-
string $filename
371-
): array {
372-
if (isset($binariesFolder['success']) && $binariesFolder['success']) {
373-
$dir = $binariesFolder['path'] . '/';
374-
} else {
375-
return $binariesFolder; // Return getAppDataFolder result
376-
}
377-
$file_name = $filename . '.sha256';
378-
$save_file_loc = $dir . $file_name;
343+
public function downloadBinaryHash(string $url): array {
379344
$cURL = curl_init($url);
380-
$fp = fopen($save_file_loc, 'w');
381-
if ($fp) {
382-
curl_setopt_array($cURL, [
383-
CURLOPT_RETURNTRANSFER => true,
384-
CURLOPT_FILE => $fp,
385-
CURLOPT_FOLLOWLOCATION => true,
386-
CURLOPT_RANGE => 64,
387-
]);
388-
$binaryHash = curl_exec($cURL);
389-
curl_close($cURL);
390-
fclose($fp);
391-
return [
392-
'success' => true,
393-
'binaryHash' => $binaryHash,
394-
'binaryHashFilePath' => $save_file_loc,
395-
];
396-
}
397-
return ['success' => false];
345+
curl_setopt_array($cURL, [
346+
CURLOPT_RETURNTRANSFER => true,
347+
CURLOPT_RANGE => 64,
348+
]);
349+
$binaryHash = curl_exec($cURL);
350+
curl_close($cURL);
351+
return [
352+
'success' => $binaryHash != false,
353+
'binaryHash' => $binaryHash,
354+
];
398355
}
399356

400357
/**

tests/Unit/Service/UtilsServiceTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ public function testIsMusliLinux() {
192192
$exec->expects($this->any())
193193
->willReturnCallback(
194194
function ($command, &$output, &$result_code) {
195-
$this->assertEquals('ldd --version', $command);
195+
$this->assertEquals('ldd --version 2>&1', $command);
196196
$output = ['manylinux'];
197-
$result_code = 0;
197+
$result_code = 1;
198198
}
199199
);
200200
$result = $this->utils->isMusliLinux();
@@ -207,9 +207,9 @@ public function testIsMusliLinuxWithOverride() {
207207
$exec->expects($this->any())
208208
->willReturnCallback(
209209
function ($command, &$output, &$result_code) {
210-
$this->assertEquals('ldd --version', $command);
210+
$this->assertEquals('ldd --version 2>&1', $command);
211211
$output = ['musl linux'];
212-
$result_code = 0;
212+
$result_code = 1;
213213
}
214214
);
215215
$result = $this->utils->isMusliLinux();
@@ -356,7 +356,7 @@ public function testGetBinaryNameMuslLinux() {
356356
$exec->expects($this->any())
357357
->willReturnCallback(
358358
function ($command, &$output, &$result_code) {
359-
$this->assertEquals('ldd --version', $command);
359+
$this->assertEquals('ldd --version 2>&1', $command);
360360
$output = ['musl linux'];
361361
$result_code = 0;
362362
}

0 commit comments

Comments
 (0)