Skip to content

Commit 4c8cf5f

Browse files
committed
Add JARs path detection for Kotlin Notebook plugin
1 parent ea59662 commit 4c8cf5f

File tree

9 files changed

+128
-46
lines changed

9 files changed

+128
-46
lines changed

build-plugin/src/build/InstallTasksConfigurator.kt

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ class InstallTasksConfigurator(
1818
project.tasks.register<Copy>(COPY_RUN_KERNEL_PY_TASK) {
1919
group = LOCAL_INSTALL_GROUP
2020
dependsOn(makeTaskName(settings.cleanInstallDirTaskPrefix, true))
21-
from(settings.runKernelDir.resolve(settings.runKernelPy))
22-
from(settings.distributionDir.resolve(settings.kotlinKernelModule)) {
23-
into(settings.kotlinKernelModule)
24-
}
21+
moduleFromDistributionDir(settings.runKotlinKernelModule)
22+
moduleFromDistributionDir(settings.kotlinKernelModule)
2523
into(settings.localInstallDir)
24+
from(settings.distributionDir.resolve(settings.localRunPy))
2625
}
2726

2827
project.tasks.register<Copy>(COPY_NB_EXTENSION_TASK) {
@@ -49,21 +48,21 @@ class InstallTasksConfigurator(
4948
dependsOn(cleanDirTask, updateLibrariesTask)
5049
group = groupName
5150
from(settings.librariesDir)
52-
into(mainInstallPath.resolve(settings.librariesDir))
51+
into(mainInstallPath.librariesDir)
5352
}
5453

5554
project.tasks.register<Copy>(makeTaskName(settings.installLibsTaskPrefix, local)) {
5655
dependsOn(cleanDirTask)
5756
group = groupName
5857
from(project.configurations["deploy"])
59-
into(mainInstallPath.resolve(settings.jarsPath))
58+
into(mainInstallPath.localJarsDir)
6059
}
6160

6261
project.tasks.register<Copy>(makeTaskName(settings.installKernelTaskPrefix, local)) {
6362
dependsOn(cleanDirTask, shadowJar)
6463
group = groupName
6564
from(shadowJar.get().outputs)
66-
into(mainInstallPath.resolve(settings.jarsPath))
65+
into(mainInstallPath.localJarsDir)
6766
}
6867

6968
listOf(true, false).forEach { debug ->
@@ -72,8 +71,16 @@ class InstallTasksConfigurator(
7271
}
7372
}
7473

75-
private fun registerTaskForSpecs(debug: Boolean, local: Boolean, group: String, cleanDir: TaskProvider<*>, shadowJar: TaskProvider<*>, specPath: File, mainInstallPath: File): String {
76-
val taskName = makeTaskName(if (debug) "createDebugSpecs" else "createSpecs", local)
74+
private fun registerTaskForSpecs(
75+
debug: Boolean,
76+
local: Boolean,
77+
group: String,
78+
cleanDir: TaskProvider<*>,
79+
shadowJar: TaskProvider<*>,
80+
specPath: File,
81+
mainInstallPath: File
82+
): String {
83+
val taskName = makeTaskName("create${debugStr(debug)}Specs", local)
7784
project.tasks.register(taskName) {
7885
this.group = group
7986
dependsOn(cleanDir, shadowJar)
@@ -82,8 +89,8 @@ class InstallTasksConfigurator(
8289

8390
val libsCp = project.files(project.configurations["deploy"]).files.map { it.name }
8491

85-
makeDirs(mainInstallPath.resolve(settings.jarsPath))
86-
makeDirs(mainInstallPath.resolve(settings.configDir))
92+
makeDirs(mainInstallPath.localJarsDir)
93+
makeDirs(mainInstallPath.configDir)
8794
makeDirs(specPath)
8895

8996
writeJson(
@@ -93,7 +100,7 @@ class InstallTasksConfigurator(
93100
"classPath" to libsCp,
94101
"debuggerConfig" to if (debug) settings.debuggerConfig else ""
95102
),
96-
mainInstallPath.resolve(settings.jarArgsFile)
103+
mainInstallPath.jarArgsFile
97104
)
98105
makeKernelSpec(specPath, local)
99106
}
@@ -103,7 +110,7 @@ class InstallTasksConfigurator(
103110

104111
private fun registerMainInstallTask(debug: Boolean, local: Boolean, group: String, specsTaskName: String) {
105112
val taskNamePrefix = if (local) "install" else "prepare"
106-
val taskNameMiddle = if (debug) "Debug" else ""
113+
val taskNameMiddle = debugStr(debug)
107114
val taskNameSuffix = if (local) "" else "Package"
108115
val taskName = "$taskNamePrefix$taskNameMiddle$taskNameSuffix"
109116

@@ -121,24 +128,21 @@ class InstallTasksConfigurator(
121128
}
122129

123130
private fun makeKernelSpec(installPath: File, localInstall: Boolean) {
124-
val argv = if (localInstall) {
125-
listOf(
126-
"python",
127-
installPath.resolve(settings.runKernelPy).toString(),
128-
"{connection_file}",
129-
installPath.resolve(settings.jarArgsFile).toString(),
130-
installPath.toString()
131-
)
132-
} else {
133-
listOf("python", "-m", "run_kotlin_kernel", "{connection_file}")
134-
}
131+
val firstArg = if (localInstall) installPath.resolve(settings.localRunPy).toString() else "-m"
132+
fun execPythonArgs(vararg args: String) = listOf("python", firstArg, *args)
133+
134+
val argv = execPythonArgs(settings.runKotlinKernelModule, "{connection_file}")
135+
val jarsPathDetectorArgv = execPythonArgs(settings.kotlinKernelModule, "detect-jars-location")
135136

136137
writeJson(
137138
mapOf(
138139
"display_name" to "Kotlin",
139140
"language" to "kotlin",
140141
"interrupt_mode" to "message",
141-
"argv" to argv
142+
"argv" to argv,
143+
"metadata" to mapOf(
144+
"jar_path_detect_command" to jarsPathDetectorArgv,
145+
),
142146
),
143147
installPath.resolve(settings.kernelFile)
144148
)
@@ -148,4 +152,17 @@ class InstallTasksConfigurator(
148152
into(installPath)
149153
}
150154
}
155+
156+
private val File.localJarsDir get() = resolve(settings.runKotlinKernelModule).resolve(settings.jarsPath)
157+
private val File.librariesDir get() = resolve(settings.runKotlinKernelModule).resolve(settings.librariesDir)
158+
private val File.configDir get() = resolve(settings.runKotlinKernelModule).resolve(settings.configDir)
159+
private val File.jarArgsFile get() = resolve(settings.runKotlinKernelModule).resolve(settings.jarArgsFile)
160+
161+
private fun debugStr(isDebug: Boolean) = if (isDebug) "Debug" else ""
162+
163+
private fun Copy.moduleFromDistributionDir(moduleName: String) {
164+
from(settings.distributionDir.resolve(moduleName)) {
165+
into(moduleName)
166+
}
167+
}
151168
}

build-plugin/src/build/KernelBuildConfigurator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal class KernelBuildConfigurator(private val project: Project) {
8383

8484
/****** Distribution ******/
8585
registerDistributionTasks()
86-
installTasksConfigurator.registerInstallTasks(false, settings.distribKernelDir, settings.runKernelDir)
86+
installTasksConfigurator.registerInstallTasks(false, settings.distribKernelDir, settings.distribBuildDir)
8787
registerPythonPackageTasks()
8888
registerAggregateUploadTasks()
8989

build-plugin/src/build/RootSettingsExtension.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class RootSettingsExtension(
7373
val resourcesDir: File = project.file("resources")
7474
val distribBuildDir: File = project.buildDir.resolve("distrib-build")
7575
val distribKernelDir: File = distribBuildDir.resolve("kernel")
76-
val runKernelDir: File = distribBuildDir.resolve("run_kotlin_kernel")
7776
val logosDir: File = resourcesDir.resolve("logos")
7877
val nbExtensionDir: File = resourcesDir.resolve("notebook-extension")
7978
val distributionDir: File = project.file("distrib")
@@ -83,10 +82,11 @@ class RootSettingsExtension(
8382
val jarsPath: String = "jars"
8483
val configDir: String = "config"
8584
val jarArgsFile: String = "$configDir/jar_args.json"
86-
val runKernelPy: String = "run_kernel.py"
85+
val runKotlinKernelModule = "run_kotlin_kernel"
8786
val kotlinKernelModule: String = "kotlin_kernel"
8887
val kernelFile: String = "kernel.json"
8988
val setupPy: String = "setup.py"
89+
val localRunPy: String = "local_run.py"
9090

9191
val installKernelTaskPrefix: String = "installKernel"
9292
val cleanInstallDirTaskPrefix: String = "cleanInstallDir"

distrib/kotlin_kernel/__main__.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
from kotlin_kernel.install_user import install_user
1+
from typing import Dict, List
2+
from typing import Callable
3+
24
from kotlin_kernel.add_kernel import add_kernel
5+
from kotlin_kernel.detect_jars_location import detect_jars_location
6+
from kotlin_kernel.install_user import install_user
37

48
import sys
59

6-
if __name__ == "__main__":
7-
if len(sys.argv) == 2 and sys.argv[1] == "fix-kernelspec-location":
8-
install_user()
9-
elif len(sys.argv) >= 2 and sys.argv[1] == "add-kernel":
10-
add_kernel()
10+
commands: Dict[str, Callable[[List[str]], None]] = {
11+
"add-kernel": lambda x: add_kernel(x),
12+
"detect-jars-location": lambda x: detect_jars_location(),
13+
"fix-kernelspec-location": lambda x: install_user(),
14+
}
15+
16+
17+
def show_help(args):
18+
if len(args) < 2:
19+
print("Must specify a command", file=sys.stderr)
1120
else:
12-
if len(sys.argv) < 2:
13-
print("Must specify a command", file=sys.stderr)
21+
commands_str = ", ".join(commands.keys())
22+
print("Unknown command " + args[1] + ", known commands: " + commands_str + ".",
23+
file=sys.stderr)
24+
exit(1)
25+
26+
27+
def main(args):
28+
if len(args) >= 2:
29+
command = args[1]
30+
if command in commands:
31+
commands[command](args)
1432
else:
15-
print("Unknown command " + sys.argv[1] + ", known commands are fix-kernelspec-location and add-kernel.",
16-
file=sys.stderr)
17-
exit(1)
33+
show_help(args)
34+
else:
35+
show_help(args)
36+
37+
38+
if __name__ == "__main__":
39+
main(sys.argv)

distrib/kotlin_kernel/add_kernel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from kotlin_kernel.install_user import install_base_kernel
1212

1313

14-
def add_kernel():
14+
def add_kernel(sys_args):
1515
parser = argparse.ArgumentParser(
1616
prog="add-kernel",
1717
description="Add a kernel with specified JDK, JVM args, and environment",
@@ -31,11 +31,11 @@ def add_kernel():
3131
parser.add_argument("--force", action="store_true", default=False,
3232
help="Overwrite an existing kernel with the same name.")
3333

34-
if len(sys.argv) == 2:
34+
if len(sys_args) == 2:
3535
parser.print_usage()
3636
exit(0)
3737

38-
args = parser.parse_args(sys.argv[2:])
38+
args = parser.parse_args(sys_args[2:])
3939

4040
jdk = args.jdk
4141
if jdk is not None:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
from typing import AnyStr
3+
4+
from run_kotlin_kernel.run_kernel import module_install_path
5+
6+
7+
def detect_jars_location() -> None:
8+
run_kernel_path = module_install_path()
9+
jars_dir: AnyStr = os.path.join(run_kernel_path, "jars")
10+
print(str(jars_dir))
11+
12+
13+
if __name__ == "__main__":
14+
detect_jars_location()

distrib/local_run.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
3+
import kotlin_kernel.__main__
4+
import run_kotlin_kernel.__main__
5+
6+
7+
def main():
8+
module_name = sys.argv[1]
9+
args = sys.argv[1:]
10+
if module_name == "kotlin_kernel":
11+
kotlin_kernel.__main__.main(args)
12+
elif module_name == "run_kotlin_kernel":
13+
run_kotlin_kernel.__main__.main(args)
14+
15+
16+
if __name__ == "__main__":
17+
main()

distrib/run_kotlin_kernel/__main__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
from sys import argv
1+
import sys
2+
23
from run_kotlin_kernel.run_kernel import run_kernel
34

5+
6+
def main(args):
7+
run_kernel(*(args[1:]))
8+
9+
410
if __name__ == "__main__":
5-
run_kernel(*(argv[1:]))
11+
main(sys.argv)

distrib/run_kotlin_kernel/run_kernel.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shlex
44
import subprocess
55
import sys
6-
from typing import List
6+
from typing import List, AnyStr
77

88
from kotlin_kernel import env_names
99

@@ -16,10 +16,16 @@ def run_kernel(*args) -> None:
1616
try:
1717
sys.exit(130)
1818
except SystemExit:
19-
# noinspection PyProtectedMember
19+
# noinspection PyProtectedMember,PyUnresolvedReferences
2020
os._exit(130)
2121

2222

23+
def module_install_path() -> str:
24+
abspath: AnyStr = os.path.abspath(__file__)
25+
current_dir: AnyStr = os.path.dirname(abspath)
26+
return str(current_dir)
27+
28+
2329
def run_kernel_impl(connection_file: str, jar_args_file: str = None, executables_dir: str = None) -> None:
2430
abspath = os.path.abspath(__file__)
2531
current_dir = os.path.dirname(abspath)

0 commit comments

Comments
 (0)