1
-
2
1
package org.jetbrains.kotlin.jupyter
3
2
4
3
import com.beust.klaxon.JsonObject
5
4
import com.beust.klaxon.Parser
6
5
import com.beust.klaxon.int
7
6
import com.beust.klaxon.string
8
- import org.jetbrains.kotlin.cli.common.repl.ReplCheckResult
9
- import org.jetbrains.kotlin.cli.common.repl.ReplEvalResult
10
- import org.jetbrains.kotlin.config.KotlinCompilerVersion
11
7
import java.io.File
12
8
import java.util.concurrent.atomic.AtomicLong
13
9
import kotlin.concurrent.thread
@@ -18,18 +14,20 @@ data class KernelArgs(val cfgFile: File,
18
14
private fun parseCommandLine (vararg args : String ): KernelArgs {
19
15
var cfgFile: File ? = null
20
16
var classpath: List <File >? = null
21
- args.forEach { when {
22
- it.startsWith(" -cp=" ) || it.startsWith(" -classpath=" ) -> {
23
- if (classpath != null ) throw IllegalArgumentException (" classpath already set to ${classpath!! .joinToString(File .pathSeparator)} " )
24
- classpath = it.substringAfter(' =' ).split(File .pathSeparator).map { File (it) }
25
- }
26
- else -> {
27
- if (cfgFile != null ) throw IllegalArgumentException (" config file already set to $cfgFile " )
28
- cfgFile = File (it)
17
+ args.forEach {
18
+ when {
19
+ it.startsWith(" -cp=" ) || it.startsWith(" -classpath=" ) -> {
20
+ if (classpath != null ) throw IllegalArgumentException (" classpath already set to ${classpath!! .joinToString(File .pathSeparator)} " )
21
+ classpath = it.substringAfter(' =' ).split(File .pathSeparator).map { File (it) }
22
+ }
23
+ else -> {
24
+ if (cfgFile != null ) throw IllegalArgumentException (" config file already set to $cfgFile " )
25
+ cfgFile = File (it)
26
+ }
29
27
}
30
- } }
28
+ }
31
29
if (cfgFile == null ) throw IllegalArgumentException (" config file is not provided" )
32
- if (! cfgFile!! .exists() || ! cfgFile!! .isFile ) throw IllegalArgumentException (" invalid config file $cfgFile " )
30
+ if (! cfgFile!! .exists() || ! cfgFile!! .isFile) throw IllegalArgumentException (" invalid config file $cfgFile " )
33
31
return KernelArgs (cfgFile!! , classpath ? : emptyList())
34
32
}
35
33
@@ -49,8 +47,7 @@ fun main(vararg args: String) {
49
47
signatureKey = if (sigScheme == null || key == null ) " " else key,
50
48
classpath = classpath
51
49
))
52
- }
53
- catch (e: Exception ) {
50
+ } catch (e: Exception ) {
54
51
log.error(" exception running kernel with args: \" ${args.joinToString()} \" " , e)
55
52
}
56
53
}
@@ -75,8 +72,7 @@ fun kernelServer(config: KernelConfig) {
75
72
conn.control.onMessage { shellMessagesHandler(it, null , executionCount) }
76
73
77
74
Thread .sleep(config.pollingIntervalMillis)
78
- }
79
- catch (e: InterruptedException ) {
75
+ } catch (e: InterruptedException ) {
80
76
log.debug(" Control: Interrupted" )
81
77
mainThread.interrupt()
82
78
break
@@ -89,8 +85,7 @@ fun kernelServer(config: KernelConfig) {
89
85
conn.shell.onMessage { shellMessagesHandler(it, repl, executionCount) }
90
86
91
87
Thread .sleep(config.pollingIntervalMillis)
92
- }
93
- catch (e: InterruptedException ) {
88
+ } catch (e: InterruptedException ) {
94
89
log.debug(" Main: Interrupted" )
95
90
controlThread.interrupt()
96
91
break
@@ -99,84 +94,9 @@ fun kernelServer(config: KernelConfig) {
99
94
100
95
try {
101
96
controlThread.join()
97
+ } catch (e: InterruptedException ) {
102
98
}
103
- catch (e: InterruptedException ) {}
104
99
105
100
log.info(" Shutdown server" )
106
101
}
107
102
}
108
-
109
- fun JupyterConnection.Socket.shellMessagesHandler (msg : Message , repl : ReplForJupyter ? , executionCount : AtomicLong ) {
110
- when (msg.header!! [" msg_type" ]) {
111
- " kernel_info_request" ->
112
- send(makeReplyMessage(msg, " kernel_info_reply" ,
113
- content = jsonObject(
114
- " protocol_version" to protocolVersion,
115
- " language" to " Kotlin" ,
116
- " language_version" to KotlinCompilerVersion .VERSION ,
117
- " language_info" to jsonObject(" name" to " kotlin" , " file_extension" to " kt" )
118
- )))
119
- " history_request" ->
120
- send(makeReplyMessage(msg, " history_reply" ,
121
- content = jsonObject(
122
- " history" to listOf<String >() // not implemented
123
- )))
124
- " shutdown_request" -> {
125
- send(makeReplyMessage(msg, " shutdown_reply" , content = msg.content))
126
- Thread .currentThread().interrupt()
127
- }
128
- " connect_request" ->
129
- send(makeReplyMessage(msg, " connection_reply" ,
130
- content = jsonObject(JupyterSockets .values()
131
- .map { Pair (" ${it.name} _port" , connection.config.ports[it.ordinal]) })))
132
- " execute_request" -> {
133
- connection.contextMessage = msg
134
- val count = executionCount.getAndIncrement()
135
- val startedTime = ISO8601DateNow
136
- with (connection.iopub) {
137
- send(makeReplyMessage(msg, " status" , content = jsonObject(" execution_state" to " busy" )))
138
- val code = msg.content[" code" ]
139
- send(makeReplyMessage(msg, " execute_input" , content = jsonObject(
140
- " execution_count" to count,
141
- " code" to code)))
142
- val res = if (isCommand(code.toString())) runCommand(code.toString(), repl)
143
- else (connection.evalWithIO { repl?.eval(count, code.toString()) ? : ReplEvalResult .Error .Runtime (emptyList(), " no repl!" ) }).asResult
144
- send(makeReplyMessage(msg, " execute_result" , content = jsonObject(
145
- " execution_count" to count,
146
- " data" to res,
147
- " metadata" to emptyJsonObject)))
148
- send(makeReplyMessage(msg, " status" , content = jsonObject(" execution_state" to " idle" )))
149
- }
150
- send(makeReplyMessage(msg, " execute_reply" ,
151
- metadata = jsonObject(
152
- " dependencies_met" to true ,
153
- " engine" to msg.header[" session" ],
154
- " status" to " ok" ,
155
- " started" to startedTime),
156
- content = jsonObject(
157
- " status" to " ok" ,
158
- " execution_count" to count,
159
- " user_variables" to JsonObject (),
160
- " payload" to listOf<String >(),
161
- " user_expressions" to JsonObject ())
162
- ))
163
- connection.contextMessage = null
164
- }
165
- " is_complete_request" -> {
166
- val code = msg.content[" code" ].toString()
167
- val resStr = if (isCommand(code)) " complete" else {
168
- val res = repl?.checkComplete(executionCount.get(), code)
169
- when (res) {
170
- is ReplCheckResult .Error -> " invalid"
171
- is ReplCheckResult .Incomplete -> " incomplete"
172
- is ReplCheckResult .Ok -> " complete"
173
- null -> " error: no repl"
174
- else -> throw Exception (" unexpected result from checkComplete call: $res " )
175
- }
176
- }
177
- send(makeReplyMessage(msg, " is_complete_reply" , content = jsonObject(" status" to resStr)))
178
- }
179
- else -> send(makeReplyMessage(msg, " unsupported_message_reply" ))
180
- }
181
- }
182
-
0 commit comments