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

Commit f0460d2

Browse files
committed
A lot of fixes, now this kernel is able to execute code
1 parent cf611b2 commit f0460d2

File tree

10 files changed

+55
-30
lines changed

10 files changed

+55
-30
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
* text=auto
2+
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
5+
/.scrutinizer.yml export-ignore
6+
/.travis.yml export-ignore

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
"license": "MIT",
66
"require": {
77
"php": ">=5.5.9",
8+
"composer/composer": "1.0.0-alpha11",
89
"ext-zmq": "*",
10+
"monolog/monolog": "^1.9",
911
"psy/psysh": "0.7.*",
10-
"composer/composer": "1.0.0-alpha11",
12+
"ramsey/uuid": "^3.2",
1113
"react/zmq": "^0.3.0",
12-
"ramsey/uuid": "^3.2"
14+
"symfony/polyfill-php70": "^1.1"
1315
},
1416
"require-dev": {
1517

src/Actions/Action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface Action
88
{
9-
public function call($header, $content);
9+
public function call(array $header, array $content);
1010
}

src/Actions/ExecuteAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(JupyterBroker $broker, SocketWrapper $iopubSocket, S
3333
$this->shellSocket = $shellSocket;
3434
}
3535

36-
public function call($header, $content)
36+
public function call(array $header, array $content)
3737
{
3838
$this->broker->send(
3939
$this->iopubSocket, 'status', ['execution_state' => 'busy'], $header
@@ -44,7 +44,7 @@ public function call($header, $content)
4444
// TODO: Here is where PsySH goes
4545
$vars_before = get_defined_vars();
4646
ob_start();
47-
$result = eval($content->code);
47+
$result = eval($content['code']);
4848
$stdOut = ob_get_contents();
4949
ob_end_clean();
5050
$vars_after = get_defined_vars();

src/Actions/HistoryAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public function __construct(JupyterBroker $broker, SocketWrapper $shellSocket)
2828
$this->shellSocket = $shellSocket;
2929
}
3030

31-
public function call($header, $content)
31+
public function call(array $header, array $content)
3232
{
33-
$this->broker->send($this->shellSocket, 'history_reply', ['history' => array()], $header);
33+
$this->broker->send($this->shellSocket, 'history_reply', ['history' => []], $header);
3434
}
3535
}

src/Actions/KernelInfoAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
final class KernelInfoAction implements Action
88
{
9-
public function call($header, $content)
9+
public function call(array $header, array $content)
1010
{
1111
// TODO: Implement call() method.
1212
}

src/Actions/ShutdownAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
final class ShutdownAction implements Action
88
{
9-
public function call($header, $content)
9+
public function call(array $header, array $content)
1010
{
1111
// TODO: Implement call() method.
1212
}

src/ConnectionSettings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public static function get()
1717
}
1818

1919
if (is_array($argv) && count($argv) > 1) {
20-
$connectionFileContents = file_get_contents($argv[1], false, true, 0, 2048);
20+
$connectionFileContents = file_get_contents($argv[1], null, null, 0, 2048);
2121

2222
if (false === $connectionFileContents) {
2323
throw new \RuntimeException('Connection Settings: Unable to open the connection file.');
2424
}
2525

26-
$connectionSettings = json_decode($connectionFileContents);
26+
$connectionSettings = json_decode($connectionFileContents, true);
2727

2828
if (json_last_error() !== JSON_ERROR_NONE) {
2929
throw new \RuntimeException('Connection Settings: Corrupted connection file.');

src/Handlers/ShellMessagesHandler.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ public function __invoke(array $msg)
4848
{
4949
list($zmqId, $delim, $hmac, $header, $parentHeader, $metadata, $content) = $msg;
5050

51-
$header = json_decode($header);
52-
$content = json_decode($content);
51+
$header = json_decode($header, true);
52+
$content = json_decode($content, true);
5353

54-
if ('kernel_info_request' === $header->msg_type) {
54+
if ('kernel_info_request' === $header['msg_type']) {
5555
$this->kernelInfoAction->call($header, $content);
56-
} elseif ('execute_request' === $header->msg_type) {
56+
} elseif ('execute_request' === $header['msg_type']) {
5757
$this->executeAction->call($header, $content);
58-
} elseif ('history_request' === $header->msg_type) {
58+
} elseif ('history_request' === $header['msg_type']) {
5959
$this->historyAction->call($header, $content);
60-
} elseif ('shutdown_request' === $header->msg_type) {
60+
} elseif ('shutdown_request' === $header['msg_type']) {
6161
$this->shutdownAction->call($header, $content);
6262
} else {
6363
// TODO: Add logger!

src/kernel.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,38 @@
55
namespace Litipk\JupyterPHP;
66

77

8-
require (__DIR__.'/../vendor/autoload.php');
8+
require (__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php');
99

1010

1111
use Ramsey\Uuid\Uuid;
1212

1313

14-
// Obtain settings
15-
$connectionSettings = ConnectionSettings::get();
16-
$connUris = ConnectionSettings::getConnectionUris($connectionSettings);
17-
18-
$kernelCore = new KernelCore(
19-
new JupyterBroker(
20-
$connectionSettings['key'], $connectionSettings['signature_scheme'], Uuid::uuid4()
21-
),
22-
$connUris
23-
);
24-
25-
$kernelCore->run();
14+
try {
15+
// Obtain settings
16+
$connectionSettings = ConnectionSettings::get();
17+
$connUris = ConnectionSettings::getConnectionUris($connectionSettings);
18+
19+
$kernelCore = new KernelCore(
20+
new JupyterBroker(
21+
$connectionSettings['key'], $connectionSettings['signature_scheme'], Uuid::uuid4()
22+
),
23+
$connUris
24+
);
25+
26+
$kernelCore->run();
27+
} 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+
}
42+
}

0 commit comments

Comments
 (0)