-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
307 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,288 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2c184729", | ||
"metadata": {}, | ||
"source": [ | ||
"# install\n", | ||
"\n", | ||
"> nbstata kernel install script\n", | ||
"- order: 15" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c7fb586a", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"The autoreload extension is already loaded. To reload it, use:\n", | ||
" %reload_ext autoreload\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"#| default_exp install\n", | ||
"%load_ext autoreload\n", | ||
"%autoreload 2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "026b00b7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| hide\n", | ||
"from nbdev.showdoc import *" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "4b35edd5", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"import argparse\n", | ||
"import json\n", | ||
"import os\n", | ||
"import sys\n", | ||
"\n", | ||
"from jupyter_client.kernelspec import KernelSpecManager\n", | ||
"from IPython.utils.tempdir import TemporaryDirectory\n", | ||
"import importlib.resources as resources\n", | ||
"from shutil import copyfile\n", | ||
"from pathlib import Path\n", | ||
"from textwrap import dedent\n", | ||
"from fastcore.basics import IN_NOTEBOOK" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "dd9d03cf", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"kernel_json = {\n", | ||
" \"argv\": [sys.executable, \"-m\", \"nbstata\", \"-f\", \"{connection_file}\"],\n", | ||
" \"display_name\": \"Stata (nbstata)\",\n", | ||
" \"language\": \"stata\",\n", | ||
"}\n", | ||
"\n", | ||
"def install_my_kernel_spec(user=True, prefix=None):\n", | ||
" with TemporaryDirectory() as td:\n", | ||
" os.chmod(td, 0o755) # Starts off as 700, not user readable\n", | ||
" with open(os.path.join(td, 'kernel.json'), 'w') as f:\n", | ||
" json.dump(kernel_json, f, sort_keys=True)\n", | ||
"\n", | ||
" # Copy logo to tempdir to be installed with kernelspec\n", | ||
" with resources.path('nbstata', 'logo-64x64.png') as logo_path:\n", | ||
" copyfile(logo_path, os.path.join(td, 'logo-64x64.png'))\n", | ||
"\n", | ||
" print('Installing Jupyter kernel spec')\n", | ||
" KernelSpecManager().install_kernel_spec(td, 'nbstata', user=user, prefix=prefix)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "17baa321", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"def install_conf(conf_file,gen_file=False):\n", | ||
" \"\"\"\n", | ||
" From stata_kernel, but the conf here is much simplier.\n", | ||
" \"\"\"\n", | ||
"\n", | ||
" # By avoiding an import of .config until we need it, we can\n", | ||
" # complete the installation process in virtual environments\n", | ||
" # without needing this submodule nor its downstream imports.\n", | ||
" from .config import find_dir_edition\n", | ||
" try:\n", | ||
" stata_dir,stata_ed = find_dir_edition()\n", | ||
" except OSError as err:\n", | ||
" stata_dir,stata_ed = \"\", \"\"\n", | ||
" gen_file = True\n", | ||
" msg = \"\"\"\\\n", | ||
" WARNING: Could not find Stata path.\n", | ||
" Please specify it manually in configuration file.\n", | ||
" \"\"\"\n", | ||
" print(dedent(msg))\n", | ||
"\n", | ||
" conf_default = dedent(\n", | ||
" \"\"\"\\\n", | ||
" [nbstata]\n", | ||
" stata_dir = {}\n", | ||
" edition = {}\n", | ||
" graph_format = png\n", | ||
" echo = None\n", | ||
" splash = False\n", | ||
" \"\"\".format(stata_dir,stata_ed))\n", | ||
"\n", | ||
" if gen_file:\n", | ||
" print(\"Creating configuration file at:\")\n", | ||
" print(str(conf_file))\n", | ||
" try:\n", | ||
" with conf_file.open('w') as f:\n", | ||
" f.write(conf_default)\n", | ||
" except:\n", | ||
" print(\"Actually, due to an error, configuration file was not created.\")\n", | ||
"\n", | ||
"def _is_root():\n", | ||
" try:\n", | ||
" return os.geteuid() == 0\n", | ||
" except AttributeError:\n", | ||
" return False # assume not an admin on non-Unix platforms" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "66170ae7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"def main(argv=None):\n", | ||
" ap = argparse.ArgumentParser()\n", | ||
" ap.add_argument('--user', action='store_true',\n", | ||
" help=\"Install to the per-user kernels registry. Default if not root.\")\n", | ||
" ap.add_argument('--sys-prefix', action='store_true',\n", | ||
" help=\"Install to sys.prefix (e.g. a virtualenv or conda env)\")\n", | ||
" ap.add_argument('--prefix',\n", | ||
" help=\"Install to the given prefix. \"\n", | ||
" \"Kernelspec will be installed in {PREFIX}/share/jupyter/kernels/\")\n", | ||
" ap.add_argument(\n", | ||
" '--conf-file', action='store_true',\n", | ||
" help=\"Create a configuration file.\")\n", | ||
" args = ap.parse_args(argv)\n", | ||
"\n", | ||
" if args.sys_prefix:\n", | ||
" args.prefix = sys.prefix\n", | ||
" if not args.prefix and not _is_root():\n", | ||
" args.user = True\n", | ||
"\n", | ||
" install_my_kernel_spec(user=args.user, prefix=args.prefix)\n", | ||
" if args.user:\n", | ||
" conf_file = Path('~/.nbstata.conf').expanduser()\n", | ||
" else:\n", | ||
" conf_dir = os.path.join(args.prefix,'etc')\n", | ||
" if not Path(os.path.join(args.prefix,'etc')).is_dir():\n", | ||
" os.mkdir(conf_dir)\n", | ||
" conf_file = Path(os.path.join(conf_dir,'nbstata.conf'))\n", | ||
" if not conf_file.is_file():\n", | ||
" install_conf(conf_file,args.conf_file)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a1cc02dd-3e38-4324-866e-f19a130e38fd", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| export\n", | ||
"if __name__ == \"__main__\" and not IN_NOTEBOOK:\n", | ||
" main()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2c1dedf6-cda9-41e1-9eb9-34a78f22b305", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#| hide\n", | ||
"IN_NOTEBOOK" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a42239e6-298c-4c1f-acf3-80c3fd143919", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"'__main__'" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#| hide\n", | ||
"__name__" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "34bf788c-34bb-49a3-8eee-7768c2091eb4", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"False" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"#| hide\n", | ||
"__name__ == \"__main__\" and not IN_NOTEBOOK" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1c6dd777", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#| hide\n", | ||
"import nbdev; nbdev.nbdev_export()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "python3", | ||
"language": "python", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters