Skip to content

Commit ef72a1a

Browse files
authored
Merge pull request #18 from m-dzianishchyts/dynamic-startup
Run kernel with launcher
2 parents 71eae55 + 25b6a8b commit ef72a1a

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

assembly/zip/install.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,23 @@ def __call__(self, parser, namespace, value, option_string=None):
169169
replace=args.replace
170170
)
171171

172-
# Connect the self referencing token left in the kernel.json to point to it's install location.
173-
174-
# Prepare the token replacement string which should be properly escaped for use in a JSON string
172+
# Prepare token replacement strings which should be properly escaped for use in a JSON string
175173
# The [1:-1] trims the first and last " json.dumps adds for strings.
176-
install_dest_json_fragment = json.dumps(install_dest)[1:-1]
174+
executable_path = json.dumps(sys.executable)[1:-1]
175+
launcher_path = json.dumps(os.path.join(install_dest, 'launcher.py'))[1:-1]
176+
kernel_path = json.dumps(os.path.join(install_dest, '${project.build.finalName}.jar'))[1:-1]
177177

178178
# Prepare the paths to the installed kernel.json and the one bundled with this installer.
179179
local_kernel_json_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'java', 'kernel.json')
180180
installed_kernel_json_path = os.path.join(install_dest, 'kernel.json')
181181

182-
# Replace the @KERNEL_INSTALL_DIRECTORY@ token with the path to where the kernel was installed
183-
# in the installed kernel.json from the local template.
182+
# Replace tokens in the installed kernel.json.
184183
with open(local_kernel_json_path, 'r') as template_kernel_json_file:
185184
template_kernel_json_contents = template_kernel_json_file.read()
186-
kernel_json_contents = template_kernel_json_contents.replace(
187-
'@KERNEL_INSTALL_DIRECTORY@',
188-
install_dest_json_fragment
189-
)
185+
kernel_json_contents = template_kernel_json_contents.replace('@PY_EXECUTABLE@', executable_path)
186+
kernel_json_contents = kernel_json_contents.replace('@LAUNCHER_PATH@', launcher_path)
187+
kernel_json_contents = kernel_json_contents.replace('@KERNEL_PATH@', kernel_path)
188+
print(kernel_json_contents)
190189
kernel_json_json_contents = json.loads(kernel_json_contents)
191190
kernel_env = kernel_json_json_contents.setdefault('env', {})
192191
for k, v in args.env.items():

assembly/zip/kernel.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
22
"argv": [
3-
"java",
4-
"--add-opens",
5-
"jdk.jshell/jdk.jshell=ALL-UNNAMED",
6-
"-jar",
7-
"@KERNEL_INSTALL_DIRECTORY@/${project.build.finalName}.jar",
3+
"@PY_EXECUTABLE@",
4+
"@LAUNCHER_PATH@",
5+
"@KERNEL_PATH@",
86
"{connection_file}"
97
],
108
"display_name": "Java",

assembly/zip/launcher.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
import os
3+
import subprocess
4+
5+
6+
def launch_kernel():
7+
kernel_path = sys.argv[1]
8+
connection_file = sys.argv[2]
9+
jvm_options = os.getenv('JJ_JVM_OPTS', '')
10+
11+
subprocess.run(['java',
12+
jvm_options,
13+
'--add-opens',
14+
'jdk.jshell/jdk.jshell=ALL-UNNAMED',
15+
'-jar',
16+
kernel_path,
17+
connection_file])
18+
19+
20+
if __name__ == '__main__':
21+
launch_kernel()

assembly/zip/zip.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
<source>${project.build.directory}/${project.build.finalName}.jar</source>
1212
<outputDirectory>java</outputDirectory>
1313
</file>
14+
<file>
15+
<source>assembly/zip/launcher.py</source>
16+
<outputDirectory>java</outputDirectory>
17+
</file>
1418
<file>
1519
<source>assembly/zip/kernel.json</source>
1620
<outputDirectory>java</outputDirectory>
17-
<filtered>true</filtered>
1821
</file>
1922
<file>
2023
<source>assembly/zip/install.py</source>
2124
<outputDirectory>/</outputDirectory>
25+
<filtered>true</filtered>
2226
</file>
2327
</files>
2428
</assembly>

0 commit comments

Comments
 (0)