Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 20f0065

Browse files
committed
Minor fixes
1 parent f0460d2 commit 20f0065

File tree

8 files changed

+352
-18
lines changed

8 files changed

+352
-18
lines changed

src/JupyterBroker.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public function send(
5151
$header = $this->createHeader($msgType);
5252

5353
$msgDef = [
54-
json_encode($header),
55-
json_encode($parentHeader),
56-
json_encode($metadata),
57-
json_encode($content),
54+
json_encode(empty($header) ? new \StdClass : $header),
55+
json_encode(empty($parentHeader) ? new \StdClass : $parentHeader),
56+
json_encode(empty($metadata) ? new \StdClass : $metadata),
57+
json_encode(empty($content) ? new \StdClass : $content),
5858
];
5959

6060
$stream->send(array_merge(

src/System/BsdSystem.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\System;
5+
6+
7+
final class BsdSystem extends UnixSystem
8+
{
9+
/** @return integer */
10+
public function getOperativeSystem()
11+
{
12+
return self::OS_BSD;
13+
}
14+
15+
/** @return string */
16+
public function getAppDataDirectory()
17+
{
18+
return $this->getCurrentUserHome().'/.jupyter-php';
19+
}
20+
}

src/System/LinuxSystem.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\System;
5+
6+
7+
final class LinuxSystem extends UnixSystem
8+
{
9+
/** @return integer */
10+
public function getOperativeSystem()
11+
{
12+
return self::OS_LINUX;
13+
}
14+
15+
/** @return string */
16+
public function getAppDataDirectory()
17+
{
18+
return $this->getCurrentUserHome().'/.jupyter-php';
19+
}
20+
}

src/System/MacSystem.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\System;
5+
6+
7+
final class MacSystem extends UnixSystem
8+
{
9+
/** @return integer */
10+
public function getOperativeSystem()
11+
{
12+
return self::OS_OSX;
13+
}
14+
15+
/** @return string */
16+
public function getAppDataDirectory()
17+
{
18+
return $this->getCurrentUserHome().'/Library/jupyter-php';
19+
}
20+
}

src/System/System.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\System;
5+
6+
7+
abstract class System
8+
{
9+
const OS_UNKNOWN = 0;
10+
const OS_LINUX = 1;
11+
const OS_OSX = 2;
12+
const OS_BSD = 3;
13+
const OS_WIN = 4;
14+
15+
16+
/** @return System */
17+
public static function getSystem()
18+
{
19+
$phpOs = self::guessOperativeSystem();
20+
21+
if (self::OS_LINUX === $phpOs) {
22+
return new LinuxSystem();
23+
} elseif (self::OS_BSD === $phpOs) {
24+
return new BsdSystem();
25+
} elseif (self::OS_OSX === $phpOs) {
26+
return new MacSystem();
27+
} elseif (self::OS_WIN === $phpOs) {
28+
return new WindowsSystem();
29+
} else {
30+
throw new \RuntimeException('This platform is unknown for the installer');
31+
}
32+
}
33+
34+
/** @return integer */
35+
public abstract function getOperativeSystem();
36+
37+
/** @return string */
38+
public abstract function getCurrentUser();
39+
40+
/** @return string */
41+
public abstract function getCurrentUserHome();
42+
43+
/** @return string */
44+
public abstract function getAppDataDirectory();
45+
46+
/**
47+
* Returns true if the path is a "valid" path and is writable (event if the complete path does not yet exist).
48+
* @param string $path
49+
* @return boolean
50+
*/
51+
public abstract function validatePath($path);
52+
53+
/**
54+
* @param string $path
55+
* @return string The "absolute path" version of $path.
56+
*/
57+
public abstract function ensurePath($path);
58+
59+
/**
60+
* @param string $path
61+
* @return boolean
62+
*/
63+
protected abstract function isAbsolutePath($path);
64+
65+
/**
66+
* @param string $path
67+
* @return string
68+
*/
69+
protected abstract function getAbsolutePath($path);
70+
71+
/** @return integer */
72+
private static function guessOperativeSystem()
73+
{
74+
$phpOS = strtolower(PHP_OS);
75+
76+
if ('linux' === $phpOS) {
77+
return self::OS_LINUX;
78+
} elseif ('darwin' === $phpOS) {
79+
return self::OS_OSX;
80+
} elseif (in_array($phpOS, ['windows', 'winnt', 'win32'])) {
81+
return self::OS_WIN;
82+
} elseif (in_array($phpOS, ['freebsd', 'netbsd', 'openbsd'])) {
83+
return self::OS_BSD;
84+
} else {
85+
return self::OS_UNKNOWN;
86+
}
87+
}
88+
}

src/System/UnixSystem.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\System;
5+
6+
7+
abstract class UnixSystem extends System
8+
{
9+
/** @return string */
10+
public function getCurrentUser()
11+
{
12+
if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) {
13+
$pwuData = posix_getpwuid(posix_geteuid());
14+
return $pwuData['name'];
15+
} elseif ($this->checkIfCommandExists('whoami')) {
16+
return exec('whoami');
17+
} else {
18+
throw new \RuntimeException('Unable to obtain the current username.');
19+
}
20+
}
21+
22+
/** @return string */
23+
public function getCurrentUserHome()
24+
{
25+
if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) {
26+
$pwuData = posix_getpwuid(posix_geteuid());
27+
return $pwuData['dir'];
28+
} elseif (function_exists('getenv') && false !== getenv('HOME')) {
29+
return getenv('HOME');
30+
} else {
31+
throw new \RuntimeException('Unable to obtain the current user home directory.');
32+
}
33+
}
34+
35+
/**
36+
* Returns true if the path is a "valid" path and is writable (even if the complete path does not yet exist).
37+
* @param string $path
38+
* @return boolean
39+
*/
40+
public function validatePath($path)
41+
{
42+
$absPath = $this->getAbsolutePath($path);
43+
$absPathParts = preg_split('/\//', preg_replace('/(^\/|\/$)/', '', $absPath));
44+
$nSteps = count($absPath);
45+
46+
$tmpPath = '';
47+
$prevReadable = false;
48+
$prevWritable = false;
49+
50+
for ($i=0; $i<$nSteps; $i++) {
51+
$tmpPath .= '/' . $absPathParts[$i];
52+
53+
if (file_exists($tmpPath)) {
54+
if (!is_dir($tmpPath)) {
55+
if (is_link($tmpPath)) {
56+
$linkPath = readlink($tmpPath);
57+
if (false === $linkPath || !is_dir($linkPath)) {
58+
return false;
59+
}
60+
$tmpPath = $linkPath;
61+
} else {
62+
return false;
63+
}
64+
}
65+
66+
$prevReadable = is_readable($tmpPath);
67+
$prevWritable = is_writable($tmpPath);
68+
} else {
69+
return ($prevReadable && $prevWritable);
70+
}
71+
}
72+
73+
return true;
74+
}
75+
76+
/**
77+
* @param string $path
78+
* @return string The "absolute path" version of $path.
79+
*/
80+
public function ensurePath($path)
81+
{
82+
$absPath = $this->getAbsolutePath($path);
83+
84+
if (!file_exists($absPath) && false === mkdir($absPath, 0755, true)) {
85+
throw new \RuntimeException('Unable to create the specified directory ('.$absPath.').');
86+
}
87+
88+
return $absPath;
89+
}
90+
91+
/**
92+
* @param string $path
93+
* @return bool
94+
*/
95+
protected function isAbsolutePath($path)
96+
{
97+
return (1 === preg_match('#^/#', $path));
98+
}
99+
100+
/**
101+
* @param string $path
102+
* @return string
103+
*/
104+
protected function getAbsolutePath($path)
105+
{
106+
return $this->isAbsolutePath($path) ? $path : (getcwd() . DIRECTORY_SEPARATOR . $path);
107+
}
108+
}

src/System/WindowsSystem.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
4+
namespace Litipk\JupyterPHP\System;
5+
6+
7+
final class WindowsSystem extends System
8+
{
9+
10+
/** @return integer */
11+
public function getOperativeSystem()
12+
{
13+
return self::OS_WIN;
14+
}
15+
16+
/** @return string */
17+
public function getCurrentUser()
18+
{
19+
if (function_exists('getenv') && false !== getenv('username')) {
20+
return getenv('username');
21+
} else {
22+
throw new \RuntimeException('Unable to obtain the current username.');
23+
}
24+
}
25+
26+
/** @return string */
27+
public function getCurrentUserHome()
28+
{
29+
if (function_exists('getenv') && false !== getenv('HOMEDRIVE') && false !== getenv('HOMEPATH')) {
30+
return getenv("HOMEDRIVE") . getenv("HOMEPATH");
31+
} else {
32+
throw new \RuntimeException('Unable to obtain the current user home directory.');
33+
}
34+
}
35+
36+
/**
37+
* Returns true if the path is a "valid" path and is writable (event if the complete path does not yet exist).
38+
* @param string $path
39+
* @return boolean
40+
*/
41+
public function validatePath($path)
42+
{
43+
// TODO: Implement validatePath() method.
44+
}
45+
46+
/**
47+
* @param string $path
48+
* @return string The "absolute path" version of $path.
49+
*/
50+
public function ensurePath($path)
51+
{
52+
// TODO: Implement ensurePath() method.
53+
}
54+
55+
/**
56+
* @param string $path
57+
* @return boolean
58+
*/
59+
protected function isAbsolutePath($path)
60+
{
61+
// TODO: Implement isAbsolutePath() method.
62+
}
63+
64+
/**
65+
* @param string $path
66+
* @return string
67+
*/
68+
protected function getAbsolutePath($path)
69+
{
70+
// TODO: Implement getAbsolutePath() method.
71+
}
72+
}

src/kernel.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,28 @@
88
require (__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php');
99

1010

11+
use Litipk\JupyterPHP\System\System;
12+
use Monolog\Handler\FingersCrossedHandler;
13+
use Monolog\Handler\RotatingFileHandler;
14+
use Monolog\Handler\SyslogHandler;
15+
use Monolog\Logger;
1116
use Ramsey\Uuid\Uuid;
1217

1318

19+
$system = System::getSystem();
20+
21+
$logger = new Logger('main');
22+
23+
if ('root' === $system->getCurrentUser()) {
24+
$logger->pushHandler(new FingersCrossedHandler(new SyslogHandler('jupyter-php'), null, 128));
25+
} else {
26+
$system->ensurePath($system->getAppDataDirectory().'/logs');
27+
$logger->pushHandler(new FingersCrossedHandler(
28+
new RotatingFileHandler($system->getAppDataDirectory().'/logs/error.log', 7), null, 128
29+
));
30+
}
31+
32+
1433
try {
1534
// Obtain settings
1635
$connectionSettings = ConnectionSettings::get();
@@ -25,18 +44,5 @@
2544

2645
$kernelCore->run();
2746
} catch (\Exception $e) {
28-
if (
29-
'linux' === strtolower(PHP_OS) &&
30-
preg_match('#^(/home/([a-zA-Z0-9_]+)/\.jupyter-php)/pkgs#', __DIR__, $execDirMatch) > 0
31-
) {
32-
if (!file_exists($execDirMatch[1])) {
33-
mkdir($execDirMatch[1].'/logs', 0755, true);
34-
}
35-
36-
file_put_contents(
37-
$execDirMatch[1].'/logs/error.log',
38-
$e->getFile().' : '.$e->getLine().' :: '.$e->getMessage()."\n\t".
39-
$e->getTraceAsString()
40-
);
41-
}
47+
$logger->error('Unexpected error', ['exception' => $e]);
4248
}

0 commit comments

Comments
 (0)