<commit_before># coding: utf-8
"""JupyterLab entry points"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
from jinja2 import Environment, FileSystemLoader
import json
import os
from os import path as osp
from os.path import join as pjoin
from subprocess import check_call, check_output
import shutil
import tarfile

from jupyter_core.paths import ENV_JUPYTER_PATH, ENV_CONFIG_PATH


here = osp.dirname(osp.abspath(__file__))
build_dir = pjoin(ENV_JUPYTER_PATH[0], 'lab')
cache_dir = pjoin(build_dir, 'cache')
pkg_path = pjoin(build_dir, 'package.json')
config_dir = pjoin(ENV_CONFIG_PATH[0], 'labconfig')
TEMPLATE_ENVIRONMENT = Environment(
    autoescape=False,
    loader=FileSystemLoader(pjoin(here, 'src')),
    trim_blocks=False)


def install_extension(extension):
    """Install an extension package into JupyterLab.

    Follows the semantics of https://docs.npmjs.com/cli/install.

    The extension is first validated.
    """
    tar_name = validate_extension(extension)
    path = pjoin(cache_dir, tar_name)
    check_call(['npm', 'install', '--save', path], cwd=build_dir)


def uninstall_extension(extension):
    """Uninstall an extension by name.
    """
    pkg = _read_package()
    del pkg['dependencies'][extension]
    with open(pkg_path, 'w') as fid:
        json.dump(pkg, fid)


def list_extensions():
    """List installed extensions.
    """
    pkg = _read_package()
    for ext in sorted(pkg['dependencies']):
        print(ext)


def validate_extension(extension):
    """Verify that a JupyterLab extension is valid.
    """
    extension = osp.expanduser(extension)
    if osp.exists(extension):
        extension = osp.abspath(extension)
    _ensure_package()
    # npm pack the extension
    output = check_output(['npm', 'pack', extension], cwd=cache_dir)
    name = output.decode('utf8').splitlines()[-1]
    # read the package.json data from the file
    tar = tarfile.open(pjoin(cache_dir, name), "r:gz")
    f = tar.extractfile('package/package.json')
    data = json.loads(f.read().decode('utf8'))
    msg = '%s is not a valid JupyterLab extension' % extension
    if 'jupyterlab' not in data:
        raise ValueError(msg)
    if not isinstance(data['jupyterlab'], dict):
        raise ValueError(msg)
    if not data['jupyterlab'].get('extension', False):
        raise ValueError(msg)
    return name


def link_extension(package):
    """Link a package into JupyterLab

    Follows the semantics of https://docs.npmjs.com/cli/link.
    """
    check_call(['npm', 'link', package], cwd=build_dir)


def build():
    """Build the JupyterLab application."""
    pkg = _read_package()
    # Template and write the index.js
    names = list(pkg['dependencies'].keys())
    context = dict(jupyterlab_extensions=names)
    with open(pjoin(build_dir, 'index.js'), 'w') as fid:
        fid.write(_render_template('index.js', context))
    check_call(['npm', 'run', 'build'], cwd=build_dir)


def set_config(name, value):
    """Set a configuration value in the JupyterLab application."""
    pass


def get_config(name):
    """Get a configuration value in the JupyterLab application."""
    pass


def list_config():
    """Show all config settings."""
    pass


def delete_config(name):
    """Deletes the key from the JupyterLab configuration."""
    pass


def _ensure_package():
    """Make sure there is a package.json file."""
    if osp.exists(pkg_path):
        return
    if not osp.exists(build_dir):
        os.makedirs(build_dir)
    if not osp.exists(cache_dir):
        os.makedirs(cache_dir)
    shutil.copy2(pjoin(here, 'src', 'package.json'),
                 pjoin(build_dir, 'package.json'))
    shutil.copy2(pjoin(here, 'src', 'webpack.config.js'),
                 pjoin(build_dir, 'webpack.config.js'))
    check_call(['npm', 'install'], cwd=build_dir)


def _read_package():
    """Read the JupyterLab package.json data.
    """
    _ensure_package()
    with open(pkg_path) as fid:
        return json.load(fid)


def _render_template(template_filename, context):
    """Render a jinja template"""
    return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)


if __name__ == '__main__':
    #install_extension('~/workspace/jupyter/jupyterlab_geojson/labextension')
    build()
<commit_msg>Update entries to use new build method<commit_after># coding: utf-8
"""JupyterLab entry points"""

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import os
from os import path as osp
from os.path import join as pjoin
from subprocess import check_call, check_output
import shutil
import tarfile

from jupyter_core.paths import ENV_JUPYTER_PATH, ENV_CONFIG_PATH


here = osp.dirname(osp.abspath(__file__))
build_dir = pjoin(ENV_JUPYTER_PATH[0], 'lab')
cache_dir = pjoin(build_dir, 'cache')
pkg_path = pjoin(build_dir, 'package.json')
config_dir = pjoin(ENV_CONFIG_PATH[0], 'labconfig')


def install_extension(extension):
    """Install an extension package into JupyterLab.

    Follows the semantics of https://docs.npmjs.com/cli/install.

    The extension is first validated.
    """
    tar_name, pkg_name = validate_extension(extension)
    path = pjoin(cache_dir, tar_name)
    check_call(['npm', 'install', '--save', path], cwd=build_dir)
    data = _read_package()
    data['jupyterlab']['extensions'].push(pkg_name)
    data['jupyterlab']['extensions'].sort()
    with open(pkg_path, 'w') as fid:
        json.dump(data, fid)


def uninstall_extension(extension):
    """Uninstall an extension by name.
    """
    data = _read_package()
    data['jupyterlab']['extensions'].remove(extension)
    del data['dependencies'][extension]
    with open(pkg_path, 'w') as fid:
        json.dump(data, fid)


def list_extensions():
    """List installed extensions.
    """
    data = _read_package()
    for ext in sorted(data['jupyterlab']['extensions']):
        print(ext)


def validate_extension(extension):
    """Verify that a JupyterLab extension is valid.
    """
    extension = osp.expanduser(extension)
    if osp.exists(extension):
        extension = osp.abspath(extension)
    _ensure_package()
    # npm pack the extension
    output = check_output(['npm', 'pack', extension], cwd=cache_dir)
    name = output.decode('utf8').splitlines()[-1]
    # read the package.json data from the file
    tar = tarfile.open(pjoin(cache_dir, name), "r:gz")
    f = tar.extractfile('package/package.json')
    data = json.loads(f.read().decode('utf8'))
    msg = '%s is not a valid JupyterLab extension' % extension
    if 'jupyterlab' not in data:
        raise ValueError(msg)
    if not isinstance(data['jupyterlab'], dict):
        raise ValueError(msg)
    if not data['jupyterlab'].get('extension', False):
        raise ValueError(msg)
    return name, data['name']


def link_extension(package):
    """Link a package into JupyterLab

    Follows the semantics of https://docs.npmjs.com/cli/link.
    """
    check_call(['npm', 'link', package], cwd=build_dir)


def build():
    """Build the JupyterLab application."""
    check_call(['npm', 'run', 'build'], cwd=build_dir)


def set_config(name, value):
    """Set a configuration value in the JupyterLab application."""
    pass


def get_config(name):
    """Get a configuration value in the JupyterLab application."""
    pass


def list_config():
    """Show all config settings."""
    pass


def delete_config(name):
    """Deletes the key from the JupyterLab configuration."""
    pass


def _ensure_package():
    """Make sure there is a package.json file."""
    run = False
    if not osp.exists(build_dir):
        os.makedirs(build_dir)
        run = True
    if not osp.exists(cache_dir):
        os.makedirs(cache_dir)
        run = True
    for name in ['package.json', 'index.template.js', 'webpack.config.js']:
        dest = pjoin(build_dir, name)
        if not osp.exists(dest):
            shutil.copy2(pjoin(here, name), dest)
            run = True
    if run:
        check_call(['npm', 'install'], cwd=build_dir)


def _read_package():
    """Read the JupyterLab package.json data.
    """
    _ensure_package()
    with open(pkg_path) as fid:
        return json.load(fid)


if __name__ == '__main__':
    #install_extension('~/workspace/jupyter/jupyterlab_geojson/labextension')
    build()

{'fraction_non_alphanumeric': 0.1285236502627807, 'fraction_numerical': 0.0019111323459149545, 'mean_word_length': 3.6828859060402683, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1510469', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2687, 'n_words': 695}