-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathpolynote.py
More file actions
executable file
·55 lines (42 loc) · 2.19 KB
/
polynote.py
File metadata and controls
executable file
·55 lines (42 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
import sys
from pathlib import Path
import os
import shlex
# Depending on how Python is installed, sometimes it's hard to find jep or other important libraries (e.g., libpython).
# The sys module contains a bunch of *_prefix attributes that point to various locations where these libraries might be,
# such as https://docs.python.org/3/library/sys.html#sys.exec_prefix
# We set both the PYTHONPATH and the LD_LIBRARY_PATH just in case
is_valid_prefix = lambda name: "prefix" in name and name != "pycache_prefix"
sys_prefixes = {getattr(sys, sys_prefix) for sys_prefix in filter(is_valid_prefix, dir(sys))}
if not os.environ.get('PYTHONPATH'):
os.environ['PYTHONPATH'] = os.pathsep.join(sys_prefixes)
else:
print("Using user-provided PYTHONPATH")
if not os.environ.get('LD_LIBRARY_PATH'):
os.environ['LD_LIBRARY_PATH'] = os.pathsep.join([os.path.join(path, "lib") for path in sys_prefixes])
else:
print("Using user-provided LD_LIBRARY_PATH")
scala_version = os.environ.get('POLYNOTE_SCALA_VERSION', '2.12')
polynote_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(polynote_dir)
paths = [ Path(p) for p in sys.path if Path(p).exists() ]
jep_paths = [ p.joinpath("jep") for p in paths if p.joinpath("jep").exists() ]
if len(jep_paths) >= 1:
jep_path = jep_paths[0]
else:
raise Exception("Couldn't find jep library. Try running `pip3 install jep` first.")
plugins_path = Path(polynote_dir).joinpath("plugins.d", scala_version)
plugins = []
if plugins_path.exists():
plugins = list(plugins_path.glob("*.jar"))
deps_path = Path(polynote_dir).joinpath("deps", scala_version)
if not(deps_path.exists()):
raise Exception("Couldn't find the deps directory. Are we in the polynote installation directory?")
deps = Path(polynote_dir).joinpath("deps", scala_version).glob("*.jar")
classpath = ":".join([":".join([ f'"{d}"' for d in deps ]), ":".join([ f'"{p}"' for p in plugins ])])
java_executable = os.path.join(os.environ['JAVA_HOME'], 'bin', 'java') if os.environ.get('JAVA_HOME') else 'java'
cmd = f"{java_executable} -cp {classpath} -Djava.library.path={jep_path} polynote.Main {' '.join(sys.argv[1:])}"
cmd = shlex.split(cmd)
print(cmd)
os.execvp(cmd[0], cmd)