58
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import json
|
|
4 import os
|
|
5 import sys
|
|
6 import argparse
|
|
7
|
|
8 from jupyter_client.kernelspec import KernelSpecManager
|
|
9 from IPython.utils.tempdir import TemporaryDirectory
|
|
10
|
|
11 kernel_json = {
|
|
12 "argv": [
|
|
13 "python3",
|
|
14 "-m",
|
|
15 "jupyter_c_kernel",
|
|
16 "-f",
|
|
17 "{connection_file}"
|
|
18 ],
|
|
19 "display_name": "C",
|
|
20 "language": "c"
|
|
21 }
|
|
22
|
|
23
|
|
24 def install_my_kernel_spec(user=True, prefix=None):
|
|
25 with TemporaryDirectory() as td:
|
|
26 os.chmod(td, 0o755) # Starts off as 700, not user readable
|
|
27 with open(os.path.join(td, 'kernel.json'), 'w') as f:
|
|
28 json.dump(kernel_json, f, sort_keys=True)
|
|
29 # TODO: Copy resources once they're specified
|
|
30
|
|
31 print('Installing IPython kernel spec')
|
|
32 KernelSpecManager().install_kernel_spec(td, 'bash', user=user, replace=True, prefix=prefix)
|
|
33
|
|
34
|
|
35 def _is_root():
|
|
36 try:
|
|
37 return os.geteuid() == 0
|
|
38 except AttributeError:
|
|
39 return False # assume not an admin on non-Unix platforms
|
|
40
|
|
41
|
|
42 def main(argv=[]):
|
|
43 parser = argparse.ArgumentParser(
|
|
44 description='Install KernelSpec for C Kernel'
|
|
45 )
|
|
46 prefix_locations = parser.add_mutually_exclusive_group()
|
|
47
|
|
48 prefix_locations.add_argument(
|
|
49 '--user',
|
|
50 help='Install KernelSpec in user homedirectory',
|
|
51 action='store_false' if _is_root() else 'store_true'
|
|
52 )
|
|
53 prefix_locations.add_argument(
|
|
54 '--sys-prefix',
|
|
55 help='Install KernelSpec in sys.prefix. Useful in conda / virtualenv',
|
|
56 action='store_true',
|
|
57 dest='sys_prefix'
|
|
58 )
|
|
59 prefix_locations.add_argument(
|
|
60 '--prefix',
|
|
61 help='Install KernelSpec in this prefix',
|
|
62 default=None
|
|
63 )
|
|
64
|
|
65 args = parser.parse_args()
|
|
66
|
|
67 if args.sys_prefix:
|
|
68 prefix = sys.prefix
|
|
69 user = None
|
|
70 elif args.user:
|
|
71 prefix = None
|
|
72 user = True
|
|
73 else:
|
|
74 prefix = args.prefix
|
|
75 user = None
|
|
76
|
|
77 install_my_kernel_spec(user=user, prefix=prefix)
|
|
78
|
|
79
|
|
80 if __name__ == '__main__':
|
|
81 main(argv=sys.argv)
|