Example #1
0
def lint(files, check_only):
    """Run flake8, isort and black."""
    static_data = get_static_project_data(Path.cwd())
    source_path = static_data["source_dir"]
    package_name = (static_data.get("package_name")
                    or _load_project_context().package_name)
    files = files or (str(
        source_path / "tests"), str(source_path / package_name))

    if "PYTHONPATH" not in os.environ:
        # isort needs the source path to be in the 'PYTHONPATH' environment
        # variable to treat it as a first-party import location
        os.environ["PYTHONPATH"] = str(source_path)  # pragma: no cover

    for module_name in ("flake8", "isort", "black"):
        try:
            _check_module_importable(module_name)
        except KedroCliError as exc:
            raise KedroCliError(
                NO_DEPENDENCY_MESSAGE.format(module=module_name,
                                             src=str(source_path))) from exc

    python_call("black", ("--check", ) + files if check_only else files)
    python_call("flake8", ("--max-line-length=88", ) + files)

    check_flag = ("-c", ) if check_only else ()
    python_call("isort",
                (*check_flag, "-rc", "-tc", "-up", "-fgw=0", "-m=3", "-w=88") +
                files)
Example #2
0
def jupyter_lab(
    metadata: ProjectMetadata,
    ip_address,
    all_kernels,
    env,
    idle_timeout,
    args,
    **kwargs,
):  # pylint: disable=unused-argument,too-many-arguments
    """Open Jupyter Lab with project specific variables loaded."""
    _check_module_importable("jupyter_core")

    if "-h" not in args and "--help" not in args:
        ipython_message(all_kernels)

    _update_ipython_dir(metadata.project_path)
    arguments = _build_jupyter_command(
        "lab",
        ip_address=ip_address,
        all_kernels=all_kernels,
        args=args,
        idle_timeout=idle_timeout,
        project_name=metadata.project_name,
    )

    python_call_kwargs = _build_jupyter_env(env)
    python_call("jupyter", arguments, **python_call_kwargs)
Example #3
0
def activate_nbstripout():
    """Install the nbstripout git hook to automatically clean notebooks."""
    source_path = _get_source_path()
    secho(
        ("Notebook output cells will be automatically cleared before committing"
         " to git."),
        fg="yellow",
    )

    try:
        _check_module_importable("nbstripout")
    except KedroCliError as exc:
        raise KedroCliError(
            NO_DEPENDENCY_MESSAGE.format(module="nbstripout",
                                         src=str(source_path))) from exc

    try:
        res = subprocess.run(  # pylint: disable=subprocess-run-check
            ["git", "rev-parse", "--git-dir"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )
        if res.returncode:
            raise KedroCliError("Not a git repository. Run `git init` first.")
    except FileNotFoundError as exc:
        raise KedroCliError(
            "Git executable not found. Install Git first.") from exc

    call(["nbstripout", "--install"])
Example #4
0
def lint(
    metadata: ProjectMetadata, files, check_only, **kwargs
):  # pylint: disable=unused-argument
    """Run flake8, isort and black."""
    source_path = metadata.source_dir
    package_name = metadata.package_name
    files = files or (str(source_path / "tests"), str(source_path / package_name))

    if "PYTHONPATH" not in os.environ:
        # isort needs the source path to be in the 'PYTHONPATH' environment
        # variable to treat it as a first-party import location
        os.environ["PYTHONPATH"] = str(source_path)  # pragma: no cover

    for module_name in ("flake8", "isort", "black"):
        try:
            _check_module_importable(module_name)
        except KedroCliError as exc:
            raise KedroCliError(
                NO_DEPENDENCY_MESSAGE.format(module=module_name, src=str(source_path))
            ) from exc

    python_call("black", ("--check",) + files if check_only else files)
    python_call("flake8", files)

    check_flag = ("-c",) if check_only else ()
    python_call("isort", (*check_flag, "-rc") + files)  # type: ignore
Example #5
0
def ipython(metadata: ProjectMetadata, env, args, **kwargs):  # pylint: disable=unused-argument
    """Open IPython with project specific variables loaded."""
    _check_module_importable("IPython")

    os.environ["IPYTHONDIR"] = str(metadata.project_path / ".ipython")
    if env:
        os.environ["KEDRO_ENV"] = env
    if "-h" not in args and "--help" not in args:
        ipython_message()
    call(["ipython"] + list(args))
Example #6
0
def ipython(env, args):
    """Open IPython with project specific variables loaded."""
    context = _load_project_context(env=env)
    _check_module_importable("IPython")
    os.environ["IPYTHONDIR"] = str(context.project_path / ".ipython")
    if env:
        os.environ["KEDRO_ENV"] = env
    if "-h" not in args and "--help" not in args:
        ipython_message()
    call(["ipython"] + list(args))
Example #7
0
def test(args):
    """Run the test suite."""
    try:
        _check_module_importable("pytest")
    except KedroCliError as exc:
        source_path = _get_source_path()
        raise KedroCliError(
            NO_DEPENDENCY_MESSAGE.format(module="pytest",
                                         src=str(source_path))) from exc
    else:
        python_call("pytest", args)
Example #8
0
def test(metadata: ProjectMetadata, args, **kwargs):  # pylint: disable=unused-argument
    """Run the test suite."""
    try:
        _check_module_importable("pytest")
    except KedroCliError as exc:
        source_path = metadata.source_dir
        raise KedroCliError(
            NO_DEPENDENCY_MESSAGE.format(module="pytest", src=str(source_path))
        ) from exc
    else:
        python_call("pytest", args)
Example #9
0
def jupyter_lab(ip_address, all_kernels, env, idle_timeout, args):
    """Open Jupyter Lab with project specific variables loaded."""
    _check_module_importable("jupyter_core")
    context = _load_project_context(env=env)
    if "-h" not in args and "--help" not in args:
        ipython_message(all_kernels)

    _update_ipython_dir(context.project_path)
    arguments = _build_jupyter_command(
        "lab",
        ip_address=ip_address,
        all_kernels=all_kernels,
        args=args,
        idle_timeout=idle_timeout,
        project_name=context.project_name,
    )

    python_call_kwargs = _build_jupyter_env(env)
    python_call("jupyter", arguments, **python_call_kwargs)