import json import os import os.path as osp import shutil import subprocess import sys from pathlib import Path import pkg_resources try: from cookiecutter.main import cookiecutter except ImportError: raise RuntimeError("Please install cookiecutter") DEFAULT_COOKIECUTTER_BRANCH = "3.0" def update_extension(target, branch=DEFAULT_COOKIECUTTER_BRANCH, interactive=True): """Update an extension to the current JupyterLab target: str Path to the extension directory containing the extension branch: str [default: DEFAULT_COOKIECUTTER_BRANCH] Template branch to checkout interactive: bool [default: true] Whether to ask before overwriting content """ # Input is a directory with a package.json or the current directory # Use the cookiecutter as the source # Pull in the relevant config # Pull in the Python parts if possible # Pull in the scripts if possible target = osp.abspath(target) package_file = osp.join(target, 'package.json') setup_file = osp.join(target, 'setup.py') if not osp.exists(package_file): raise RuntimeError('No package.json exists in %s' % target) # Infer the options from the current directory with open(package_file) as fid: data = json.load(fid) if osp.exists(setup_file): python_name = subprocess.check_output([sys.executable, 'setup.py', '--name'], cwd=target).decode('utf8').strip() else: python_name = data['name'] if '@' in python_name: python_name = python_name[1:].replace('/', '_').replace('-', '_') output_dir = osp.join(target, '_temp_extension') if osp.exists(output_dir): shutil.rmtree(output_dir) # Build up the cookiecutter args and run the cookiecutter extra_context = dict( author_name = data.get('author', ''), labextension_name = data['name'], project_short_description = data.get('description', ''), has_server_extension = 'y' if osp.exists(osp.join(target, 'jupyter-config')) else 'n', has_binder = 'y' if osp.exists(osp.join(target, 'binder')) else 'n', repository = data.get('repository', {}).get('url', '