[PATCH] D154351: [llvm][TableGen][Jupyter] Record current python when kernel is installed

David Spickett via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 5 00:59:00 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG535693f8f74d: [llvm][TableGen][Jupyter] Record current python when kernel is installed (authored by DavidSpickett).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154351/new/

https://reviews.llvm.org/D154351

Files:
  llvm/utils/TableGen/jupyter/README.md
  llvm/utils/TableGen/jupyter/tablegen_kernel/assets/kernel.json
  llvm/utils/TableGen/jupyter/tablegen_kernel/install.py


Index: llvm/utils/TableGen/jupyter/tablegen_kernel/install.py
===================================================================
--- llvm/utils/TableGen/jupyter/tablegen_kernel/install.py
+++ llvm/utils/TableGen/jupyter/tablegen_kernel/install.py
@@ -3,17 +3,39 @@
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
 import os
+import json
+import sys
 import argparse
+from tempfile import TemporaryDirectory
 from jupyter_client.kernelspec import KernelSpecManager
 
 
 def install_my_kernel_spec(user=True, prefix=None):
     """Install the kernel spec for user in given prefix."""
     print("Installing llvm-tblgen IPython kernel spec")
-    pkgroot = os.path.dirname(__file__)
-    KernelSpecManager().install_kernel_spec(
-        os.path.join(pkgroot, "assets"), "tablegen", user=user, prefix=prefix
-    )
+
+    kernel_json = {
+        "argv": [
+            sys.executable, "-m", "tablegen_kernel", "-f", "{connection_file}"
+        ],
+        "display_name": "LLVM TableGen",
+        "language": "tablegen",
+        "language_info": {
+            "name": "tablegen",
+            "codemirror_mode": "tablegen",
+            "mimetype": "text/x-tablegen",
+            "file_extension": ".td",
+            "pygments_lexer": "text"
+        }
+    }
+
+    with TemporaryDirectory() as tmpdir:
+      json_path = os.path.join(tmpdir, "kernel.json")
+      with open(json_path, 'w') as json_file:
+        json.dump(kernel_json, json_file)
+      KernelSpecManager().install_kernel_spec(
+          tmpdir, "tablegen", user=user, prefix=prefix
+      )
 
 
 def _is_root():
Index: llvm/utils/TableGen/jupyter/tablegen_kernel/assets/kernel.json
===================================================================
--- llvm/utils/TableGen/jupyter/tablegen_kernel/assets/kernel.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
-    "argv": [
-        "python3", "-m", "tablegen_kernel", "-f", "{connection_file}"
-    ],
-    "display_name": "LLVM TableGen",
-    "language": "tablegen",
-    "language_info": {
-        "name": "tablegen",
-        "codemirror_mode": "tablegen",
-        "mimetype": "text/x-tablegen",
-        "file_extension": ".td",
-        "pygments_lexer": "text"
-    }
-}
Index: llvm/utils/TableGen/jupyter/README.md
===================================================================
--- llvm/utils/TableGen/jupyter/README.md
+++ llvm/utils/TableGen/jupyter/README.md
@@ -15,15 +15,13 @@
 
 ## TableGen Kernel
 
-To use the kernel, first install it into jupyter:
+To use the kernel, first install it into jupyter.
 
-    python3 -m tablegen_kernel.install
-
-Then put this folder on your PYTHONPATH so jupyter can find it:
+If you have installed Jupyter into a virtual environment, adjust `python3` to
+be the interpreter for that environment. This will ensure that tools run the
+kernel in the correct context.
 
-```shell
-    export PYTHONPATH=$PYTHONPATH:<path to this dir>
-```
+    python3 -m tablegen_kernel.install
 
 Then run one of:
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154351.537251.patch
Type: text/x-patch
Size: 2986 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230705/f233ed7a/attachment.bin>


More information about the llvm-commits mailing list