github.com/yunabe/lgo@v0.0.0-20190709125917-42c42d410fdf/bin/install_kernel (about)

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # This script is based on https://github.com/jupyter/echo_kernel/blob/master/echo_kernel/install.py
     5  
     6  from __future__ import print_function
     7  
     8  import argparse
     9  import json
    10  import os
    11  import shutil
    12  import sys
    13  
    14  from jupyter_client.kernelspec import KernelSpecManager
    15  from IPython.utils.tempdir import TemporaryDirectory
    16  
    17  if sys.version_info.major <= 2:
    18      import commands
    19  else:
    20      import subprocess as commands
    21  
    22  
    23  def install_kernel_spec(binary, user, prefix):
    24      kernel_json = {
    25          "display_name": "Go (lgo)",
    26          "language": "go",
    27      }
    28      kernel_json["argv"] = [binary, "kernel", "--connection_file={connection_file}"]
    29      with TemporaryDirectory() as td:
    30          os.chmod(td, 0o755) # Starts off as 700, not user readable
    31          with open(os.path.join(td, 'kernel.json'), 'w') as f:
    32              json.dump(kernel_json, f, sort_keys=True)
    33  
    34          resources = os.path.join(os.path.dirname(__file__), 'kernel_resources')
    35          for item in os.listdir(resources):
    36              src = os.path.join(resources, item)
    37              if not os.path.isdir(src):
    38                  shutil.copy(src, os.path.join(td, item))
    39  
    40          print('Installing Jupyter kernel spec')
    41          KernelSpecManager().install_kernel_spec(td, 'lgo', user=user, replace=True, prefix=prefix)
    42  
    43  def _is_root():
    44      try:
    45          return os.geteuid() == 0
    46      except AttributeError:
    47          return False # assume not an admin on non-Unix platforms
    48  
    49  def main(argv=None):
    50      ap = argparse.ArgumentParser()
    51  
    52      ap.add_argument('--user', action='store_true',
    53          help="Install to the per-user kernels registry. Default if not root.")
    54      ap.add_argument('--sys-prefix', action='store_true',
    55          help="Install to sys.prefix (e.g. a virtualenv or conda env)")
    56      ap.add_argument('--prefix',
    57          help="Install to the given prefix. "
    58               "Kernelspec will be installed in {PREFIX}/share/jupyter/kernels/")
    59      ap.add_argument('--lgo-in-path', action='store_true',
    60                      help="Use lgo under $PATH instead of $GOPATH/bin/lgo")
    61      args = ap.parse_args(argv)
    62  
    63      if args.sys_prefix:
    64          args.prefix = sys.prefix
    65      if not args.prefix and not _is_root():
    66          args.user = True
    67  
    68      binary = 'lgo'
    69      if not args.lgo_in_path:
    70          rc, gopath = commands.getstatusoutput('go env GOPATH')
    71          if rc != 0:
    72              print('Failed to get GOPATH: ' + gopath, file=sys.stderr)
    73              sys.exit(1)
    74          binary = os.path.join(gopath, 'bin/lgo')
    75  
    76      install_kernel_spec(binary, args.user, args.prefix)
    77  
    78  if __name__ == '__main__':
    79      main()