forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl_template.py
More file actions
47 lines (38 loc) · 1.57 KB
/
repl_template.py
File metadata and controls
47 lines (38 loc) · 1.57 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
import os
import runpy
import sys
from pathlib import Path
from python.runfiles import runfiles
# runfiles.py will reject paths which aren't normalized, which can happen when the REPL rules are
# used from a remote module.
STUB_PATH = os.path.normpath("%stub_path%")
def start_repl():
if sys.stdin.isatty():
# Print the banner similar to how python does it on startup when running interactively.
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
sys.stderr.write("Python %s on %s\n%s\n" % (sys.version, sys.platform, cprt))
# If there's a PYTHONSTARTUP script, we need to capture the new variables
# that it defines.
new_globals = {}
# Simulate Python's behavior when a valid startup script is defined by the
# PYTHONSTARTUP variable. If this file path fails to load, print the error
# and revert to the default behavior.
#
# See upstream for more information:
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP
if startup_file := os.getenv("PYTHONSTARTUP"):
try:
source_code = Path(startup_file).read_text()
except Exception as error:
print(f"{type(error).__name__}: {error}")
else:
compiled_code = compile(source_code, filename=startup_file, mode="exec")
eval(compiled_code, new_globals)
bazel_runfiles = runfiles.Create()
runpy.run_path(
bazel_runfiles.Rlocation(STUB_PATH),
init_globals=new_globals,
run_name="__main__",
)
if __name__ == "__main__":
start_repl()