install.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. jupyter-mysql-kernel
  3. author:rabin
  4. """
  5. import json
  6. import os
  7. import sys
  8. import argparse
  9. from jupyter_client.kernelspec import KernelSpecManager
  10. from IPython.utils.tempdir import TemporaryDirectory
  11. kernel_json = {"argv":[sys.executable,"-m","jupyter-mysql-kernel", "-f", "{connection_file}"],
  12. "display_name":"Mysql",
  13. "language":"mysql"
  14. }
  15. def install_my_kernel_spec(user=True, prefix=None):
  16. with TemporaryDirectory() as td:
  17. os.chmod(td, 0o755)
  18. with open(os.path.join(td, 'kernel.json'), 'w') as f:
  19. json.dump(kernel_json, f, sort_keys=True)
  20. print('Installing IPython kernel spec')
  21. KernelSpecManager().install_kernel_spec(td, 'mysql', user=user, replace=True, prefix=prefix)
  22. def _is_root():
  23. try:
  24. return os.geteuid() == 0
  25. except AttributeError:
  26. return False
  27. def main(argv=None):
  28. parser = argparse.ArgumentParser(
  29. description='Install KernelSpec for Mysql Kernel'
  30. )
  31. prefix_locations = parser.add_mutually_exclusive_group()
  32. prefix_locations.add_argument(
  33. '--user',
  34. help='Install KernelSpec in user homedirectory',
  35. action='store_true'
  36. )
  37. prefix_locations.add_argument(
  38. '--sys-prefix',
  39. help='Install KernelSpec in sys.prefix. Useful in conda / virtualenv',
  40. action='store_true',
  41. dest='sys_prefix'
  42. )
  43. prefix_locations.add_argument(
  44. '--prefix',
  45. help='Install KernelSpec in this prefix',
  46. default=None
  47. )
  48. args = parser.parse_args(argv)
  49. user = False
  50. prefix = None
  51. if args.sys_prefix:
  52. prefix = sys.prefix
  53. elif args.prefix:
  54. prefix = args.prefix
  55. elif args.user or not _is_root():
  56. user = True
  57. install_my_kernel_spec(user=user, prefix=prefix)
  58. if __name__ == '__main__':
  59. main()