Skip to content

Run kernel with launcher #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions assembly/zip/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,23 @@ def __call__(self, parser, namespace, value, option_string=None):
replace=args.replace
)

# Connect the self referencing token left in the kernel.json to point to it's install location.

# Prepare the token replacement string which should be properly escaped for use in a JSON string
# Prepare token replacement strings which should be properly escaped for use in a JSON string
# The [1:-1] trims the first and last " json.dumps adds for strings.
install_dest_json_fragment = json.dumps(install_dest)[1:-1]
executable_path = json.dumps(sys.executable)[1:-1]
launcher_path = json.dumps(os.path.join(install_dest, 'launcher.py'))[1:-1]
kernel_path = json.dumps(os.path.join(install_dest, '${project.build.finalName}.jar'))[1:-1]

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

# Replace the @KERNEL_INSTALL_DIRECTORY@ token with the path to where the kernel was installed
# in the installed kernel.json from the local template.
# Replace tokens in the installed kernel.json.
with open(local_kernel_json_path, 'r') as template_kernel_json_file:
template_kernel_json_contents = template_kernel_json_file.read()
kernel_json_contents = template_kernel_json_contents.replace(
'@KERNEL_INSTALL_DIRECTORY@',
install_dest_json_fragment
)
kernel_json_contents = template_kernel_json_contents.replace('@PY_EXECUTABLE@', executable_path)
kernel_json_contents = kernel_json_contents.replace('@LAUNCHER_PATH@', launcher_path)
kernel_json_contents = kernel_json_contents.replace('@KERNEL_PATH@', kernel_path)
print(kernel_json_contents)
kernel_json_json_contents = json.loads(kernel_json_contents)
kernel_env = kernel_json_json_contents.setdefault('env', {})
for k, v in args.env.items():
Expand Down
8 changes: 3 additions & 5 deletions assembly/zip/kernel.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"argv": [
"java",
"--add-opens",
"jdk.jshell/jdk.jshell=ALL-UNNAMED",
"-jar",
"@KERNEL_INSTALL_DIRECTORY@/${project.build.finalName}.jar",
"@PY_EXECUTABLE@",
"@LAUNCHER_PATH@",
"@KERNEL_PATH@",
"{connection_file}"
],
"display_name": "Java",
Expand Down
21 changes: 21 additions & 0 deletions assembly/zip/launcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys
import os
import subprocess


def launch_kernel():
kernel_path = sys.argv[1]
connection_file = sys.argv[2]
jvm_options = os.getenv('JJ_JVM_OPTS', '')

subprocess.run(['java',
jvm_options,
'--add-opens',
'jdk.jshell/jdk.jshell=ALL-UNNAMED',
'-jar',
kernel_path,
connection_file])


if __name__ == '__main__':
launch_kernel()
6 changes: 5 additions & 1 deletion assembly/zip/zip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
<source>${project.build.directory}/${project.build.finalName}.jar</source>
<outputDirectory>java</outputDirectory>
</file>
<file>
<source>assembly/zip/launcher.py</source>
<outputDirectory>java</outputDirectory>
</file>
<file>
<source>assembly/zip/kernel.json</source>
<outputDirectory>java</outputDirectory>
<filtered>true</filtered>
</file>
<file>
<source>assembly/zip/install.py</source>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>