Exemplo n.º 1
0
def init(force, silent):
    """Updates the template of a kedro project.
    Running this command is mandatory to use kedro-mlflow.
    2 actions are performed :
        1. Add "conf/base/mlflow.yml": This is a configuration file
         used for run parametrization when calling "kedro run" command.
         See INSERT_DOC_URL for further details.
        2. Modify "src/YOUR_PACKAGE_NAME/run.py" to add mlflow hooks
         to the ProjectContext. This will erase your current "run.py"
         script and all your modifications will be lost.
         If you do not want to erase "run.py", insert the hooks manually
    """

    # get constants
    project_path = Path().cwd()
    project_globals = get_static_project_data(project_path)
    context = load_context(project_path)
    conf_root = context.CONF_ROOT

    # mlflow.yml is just a static file,
    # but the name of the experiment is set to be the same as the project
    mlflow_yml = "mlflow.yml"
    write_jinja_template(
        src=TEMPLATE_FOLDER_PATH / mlflow_yml,
        is_cookiecutter=False,
        dst=project_path / conf_root / "base" / mlflow_yml,
        python_package=project_globals["package_name"],
    )
    if not silent:
        click.secho(
            click.style(
                f"'{conf_root}/base/mlflow.yml' successfully updated.", fg="green"
            )
        )
Exemplo n.º 2
0
def test_write_jinja_template(tmp_path, template_path, jinja_template):
    rendered_path = tmp_path / "rendered.py"
    write_jinja_template(src=template_path,
                         dst=rendered_path,
                         fake_tag="'Hello world!'")
    with open(rendered_path, "r") as file_handler:
        rendered = file_handler.read()
    assert rendered == jinja_template
Exemplo n.º 3
0
def kedro_project_with_mlflow_conf(kedro_project):
    write_jinja_template(
        src=TEMPLATE_FOLDER_PATH / "mlflow.yml",
        is_cookiecutter=False,
        dst=kedro_project / "conf" / "local" / "mlflow.yml",
        python_package="fake_project",
    )

    return kedro_project
Exemplo n.º 4
0
def test_write_jinja_template_with_cookiecutter_tags(tmp_path, template_path,
                                                     cookiecutter_template):
    rendered_path = tmp_path / "rendered.py"
    write_jinja_template(
        src=template_path,
        dst=rendered_path,
        is_cookiecutter=True,
        fake_tag="'Hello world!'",
    )
    with open(rendered_path, "r") as file_handler:
        rendered = file_handler.read()
    assert rendered == cookiecutter_template
Exemplo n.º 5
0
def init(env, force, silent):
    """Updates the template of a kedro project.
    Running this command is mandatory to use kedro-mlflow.
    This adds "conf/base/mlflow.yml": This is a configuration file
    used for run parametrization when calling "kedro run" command.
    See INSERT_DOC_URL for further details.
    """

    # get constants
    mlflow_yml = "mlflow.yml"
    project_path = Path().cwd()
    project_metadata = _get_project_metadata(project_path)
    _add_src_to_path(project_metadata.source_dir, project_path)
    configure_project(project_metadata.package_name)
    session = KedroSession.create(
        project_metadata.package_name, project_path=project_path
    )
    context = session.load_context()
    mlflow_yml_path = project_path / context.CONF_ROOT / env / mlflow_yml

    # mlflow.yml is just a static file,
    # but the name of the experiment is set to be the same as the project
    if mlflow_yml_path.is_file() and not force:
        click.secho(
            click.style(
                f"A 'mlflow.yml' already exists at '{mlflow_yml_path}' You can use the ``--force`` option to override it.",
                fg="red",
            )
        )
    else:
        try:
            write_jinja_template(
                src=TEMPLATE_FOLDER_PATH / mlflow_yml,
                is_cookiecutter=False,
                dst=mlflow_yml_path,
                python_package=project_metadata.package_name,
            )
        except FileNotFoundError:
            click.secho(
                click.style(
                    f"No env '{env}' found. Please check this folder exists inside '{context.CONF_ROOT}' folder.",
                    fg="red",
                )
            )
        if not silent:
            click.secho(
                click.style(
                    f"'{context.CONF_ROOT}/{env}/{mlflow_yml}' successfully updated.",
                    fg="green",
                )
            )
Exemplo n.º 6
0
def template_mlflowyml(tmp_path):
    # the goal is to discover all potential ".py" files
    # but for now there is only "run.py"
    # # this is rather a safeguard for further add
    raw_template_path = TEMPLATE_FOLDER_PATH / "mlflow.yml"
    rendered_template_path = tmp_path / raw_template_path.name
    tags = {
        "project_name": "This is a fake project",
        "python_package": "fake_project",
        "kedro_version": "0.16.0",
    }

    write_jinja_template(src=raw_template_path, dst=rendered_template_path, **tags)
    return rendered_template_path.as_posix()
Exemplo n.º 7
0
def init(env, force, silent):
    """Updates the template of a kedro project.
    Running this command is mandatory to use kedro-mlflow. This
    adds a configuration file used for run parametrization when
    calling "kedro run" command.
    """

    # get constants
    mlflow_yml = "mlflow.yml"
    project_path = Path().cwd()
    project_globals = get_static_project_data(project_path)
    context = load_context(project_path)
    mlflow_yml_path = project_path / context.CONF_ROOT / env / mlflow_yml

    # mlflow.yml is just a static file
    if mlflow_yml_path.is_file() and not force:
        click.secho(
            click.style(
                f"A 'mlflow.yml' already exists at '{mlflow_yml_path}' You can use the ``--force`` option to override it.",
                fg="red",
            ))
        return 1
    else:
        try:
            write_jinja_template(
                src=TEMPLATE_FOLDER_PATH / mlflow_yml,
                is_cookiecutter=False,
                dst=mlflow_yml_path,
                python_package=project_globals["package_name"],
            )
        except FileNotFoundError:
            click.secho(
                click.style(
                    f"No env '{env}' found. Please check this folder exists inside '{context.CONF_ROOT}' folder.",
                    fg="red",
                ))
            return 1
        if not silent:
            click.secho(
                click.style(
                    f"'{context.CONF_ROOT}/{env}/{mlflow_yml}' successfully updated.",
                    fg="green",
                ))
        return 0
Exemplo n.º 8
0
def init(env: str, force: bool, silent: bool):
    """Updates the template of a kedro project.
    Running this command is mandatory to use kedro-mlflow.
    This adds "conf/base/mlflow.yml": This is a configuration file
    used for run parametrization when calling "kedro run" command.
    """

    # get constants
    mlflow_yml = "mlflow.yml"
    project_path = Path().cwd()
    project_metadata = bootstrap_project(project_path)
    mlflow_yml_path = project_path / settings.CONF_SOURCE / env / mlflow_yml

    # mlflow.yml is just a static file,
    # but the name of the experiment is set to be the same as the project
    if mlflow_yml_path.is_file() and not force:
        click.secho(
            click.style(
                f"A 'mlflow.yml' already exists at '{mlflow_yml_path}' You can use the ``--force`` option to override it.",
                fg="red",
            ))
    else:
        try:
            write_jinja_template(
                src=TEMPLATE_FOLDER_PATH / mlflow_yml,
                is_cookiecutter=False,
                dst=mlflow_yml_path,
                python_package=project_metadata.package_name,
            )
            if not silent:
                click.secho(
                    click.style(
                        f"'{settings.CONF_SOURCE}/{env}/{mlflow_yml}' successfully updated.",
                        fg="green",
                    ))
        except FileNotFoundError:
            click.secho(
                click.style(
                    f"No env '{env}' found. Please check this folder exists inside '{settings.CONF_SOURCE}' folder.",
                    fg="red",
                ))
Exemplo n.º 9
0
def init(force, silent):
    """Updates the template of a kedro project.
    Running this command is mandatory to use kedro-mlflow.
    2 actions are performed :
        1. Add "conf/base/mlflow.yml": This is a configuration file
         used for run parametrization when calling "kedro run" command.
         See INSERT_DOC_URL for further details.
        2. Modify "src/YOUR_PACKAGE_NAME/run.py" to add mlflow hooks
         to the ProjectContext. This will erase your current "run.py"
         script and all your modifications will be lost.
         If you do not want to erase "run.py", insert the hooks manually
    """

    # get constants
    project_path = Path().cwd()
    project_globals = _get_project_globals()

    # mlflow.yml is just a static file,
    # but the name of the experiment is set to be the same as the project
    mlflow_yml = "mlflow.yml"
    write_jinja_template(
        src=TEMPLATE_FOLDER_PATH / mlflow_yml,
        is_cookiecutter=False,
        dst=project_path / "conf" / "base" / mlflow_yml,
        python_package=project_globals["python_package"],
    )
    if not silent:
        click.secho(
            click.style("'conf/base/mlflow.yml' successfully updated.",
                        fg="green"))
    # make a check whether the project run.py is strictly identical to the template
    # if yes, replace the script by the template silently
    # if no, raise a warning and send a message to INSERT_DOC_URL
    flag_erase_runpy = force
    runpy_project_path = (
        project_path / "src" /
        (Path(project_globals["context_path"]).parent.as_posix() + ".py"))
    if not force:
        kedro_path = Path(KEDRO_PATH).parent
        runpy_template_path = (kedro_path / "templates" / "project" /
                               "{{ cookiecutter.repo_name }}" / "src" /
                               "{{ cookiecutter.python_package }}" / "run.py")
        kedro_runpy_template = render_jinja_template(
            src=runpy_template_path,
            is_cookiecutter=True,
            python_package=project_globals["python_package"],
            project_name=project_globals["project_name"],
            kedro_version=project_globals["kedro_version"],
        )

        with open(runpy_project_path, mode="r") as file_handler:
            kedro_runpy_project = file_handler.read()

        # beware : black formatting could change slightly this test which is very strict
        if kedro_runpy_project == kedro_runpy_template:
            flag_erase_runpy = True

    if flag_erase_runpy:
        os.remove(runpy_project_path)
        write_jinja_template(
            src=TEMPLATE_FOLDER_PATH / "run.py",
            dst=runpy_project_path,
            is_cookiecutter=True,
            python_package=project_globals["python_package"],
            project_name=project_globals["project_name"],
            kedro_version=project_globals["kedro_version"],
        )
        if not silent:
            click.secho(
                click.style("'run.py' successfully updated", fg="green"))
    else:
        click.secho(
            click.style(
                "You have modified your 'run.py' since project creation.\n" +
                "In order to use kedro-mlflow, you must either:\n" +
                "    -  set up your run.py with the following instructions :\n"
                + "INSERT_DOC_URL\n" + "    - call the following command:\n" +
                "$ kedro mlflow init --force",
                fg="yellow",
            ))