Example #1
0
def create(
    template_git_url: str,
    output_dir: str = ".",
    config_file: Optional[str] = None,
    default_config: bool = False,
    extra_context: Optional[dict] = None,
    no_input: bool = False,
    overwrite_if_exists: bool = False,
) -> str:
    """Expand a Git based Cookiecutter template into a new project on disk."""
    with TemporaryDirectory() as cookiecutter_template_dir_str:
        cookiecutter_template_dir = Path(cookiecutter_template_dir_str)
        try:
            repo = Repo.clone_from(template_git_url, cookiecutter_template_dir)
            last_commit = repo.head.object.hexsha
        except Exception as e:
            raise InvalidCookiecutterRepository(e)

        main_cookiecutter_directory: Optional[Path] = None
        for dir_item in cookiecutter_template_dir.glob("*cookiecutter.*"):
            if dir_item.is_dir(
            ) and "{{" in dir_item.name and "}}" in dir_item.name:
                main_cookiecutter_directory = dir_item
                break

        if not main_cookiecutter_directory:  # pragma: no cover
            raise UnableToFindCookiecutterTemplate(cookiecutter_template_dir)

        context_file = cookiecutter_template_dir / "cookiecutter.json"

        config_dict = get_user_config(config_file=config_file,
                                      default_config=default_config)

        context = generate_context(
            context_file=str(context_file),
            default_context=config_dict["default_context"],
            extra_context=extra_context,
        )

        # prompt the user to manually configure at the command line.
        # except when 'no-input' flag is set
        context["cookiecutter"] = prompt_for_config(context, no_input)
        context["cookiecutter"]["_template"] = template_git_url

        (main_cookiecutter_directory / ".cruft.json").write_text(
            json_dumps({
                "template": template_git_url,
                "commit": last_commit,
                "context": context
            }))

        return generate_files(
            repo_dir=cookiecutter_template_dir,
            context=context,
            overwrite_if_exists=overwrite_if_exists,
            output_dir=output_dir,
        )
Example #2
0
def _validate_cookiecutter(cookiecutter_template_dir: Path):
    main_cookiecutter_directory: Optional[Path] = None

    for dir_item in cookiecutter_template_dir.glob("*cookiecutter.*"):
        if dir_item.is_dir(
        ) and "{{" in dir_item.name and "}}" in dir_item.name:
            main_cookiecutter_directory = dir_item
            break

    if not main_cookiecutter_directory:
        raise UnableToFindCookiecutterTemplate(cookiecutter_template_dir)
Example #3
0
def link(template_git_url: str,
         project_dir: str = ".",
         use_latest: bool = False,
         no_input: bool = False,
         config_file: Optional[str] = None,
         default_config: bool = False,
         extra_context: Optional[dict] = None,
         directory: str = "") -> bool:
    """Links an existing project created from a template, to the template it was created from."""
    project_dir_path = Path(project_dir)
    cruft_file = project_dir_path / ".cruft.json"
    if cruft_file.is_file():
        raise CruftAlreadyPresent(cruft_file)

    with TemporaryDirectory() as cookiecutter_template_dir_str:
        cookiecutter_template_dir = Path(cookiecutter_template_dir_str)
        try:
            repo = Repo.clone_from(template_git_url, cookiecutter_template_dir)
            last_commit = repo.head.object.hexsha
        except Exception as e:  # pragma: no cover
            raise InvalidCookiecutterRepository(e)

        main_cookiecutter_directory: Optional[Path] = None
        if directory:
            cookiecutter_template_dir = cookiecutter_template_dir / directory

        for dir_item in cookiecutter_template_dir.glob("*cookiecutter.*"):
            if dir_item.is_dir(
            ) and "{{" in dir_item.name and "}}" in dir_item.name:
                main_cookiecutter_directory = dir_item
                break

        if not main_cookiecutter_directory:  # pragma: no cover
            raise UnableToFindCookiecutterTemplate(cookiecutter_template_dir)

        context_file = cookiecutter_template_dir / "cookiecutter.json"

        config_dict = get_user_config(config_file=config_file,
                                      default_config=default_config)

        context = generate_context(
            context_file=context_file,
            default_context=config_dict["default_context"],
            extra_context=extra_context,
        )

        # prompt the user to manually configure at the command line.
        # except when 'no-input' flag is set
        context["cookiecutter"] = prompt_for_config(context, no_input)
        context["cookiecutter"]["_template"] = template_git_url

        if use_latest or no_input:
            use_commit = last_commit
        else:  # pragma: no cover
            print("")
            print(f"The latest commit to the template is {last_commit}")
            print(
                "Press enter to link against this commit or provide an alternative commit."
            )
            print("")
            use_commit = input(
                f"Link to template at commit [{last_commit}]: ")  # nosec
            use_commit = use_commit if use_commit.strip() else last_commit

        cruft_file.write_text(
            json_dumps({
                "template": template_git_url,
                "commit": use_commit,
                "context": context,
                "directory": directory
            }))

    return True
Example #4
0
def create(
    template_git_url: str,
    output_dir: str = ".",
    config_file: Optional[str] = None,
    default_config: bool = False,
    extra_context: Optional[dict] = None,
    no_input: bool = False,
    overwrite_if_exists: bool = False,
) -> str:
    """Expand a Git based Cookiecutter template into a new project on disk."""
    with TemporaryDirectory() as cookiecutter_template_dir:
        try:
            repo = Repo.clone_from(template_git_url, cookiecutter_template_dir)
            last_commit = repo.head.object.hexsha
        except Exception as e:
            raise InvalidCookiecutterRepository(e)

        main_cookiecutter_directory: str = ""
        for file_name in os.listdir(cookiecutter_template_dir):
            file_path = os.path.join(cookiecutter_template_dir, file_name)
            if (os.path.isdir(file_path) and "{{" in file_name
                    and "}}" in file_name and "cookiecutter." in file_name):
                main_cookiecutter_directory = file_path
                break

        if not main_cookiecutter_directory:
            raise UnableToFindCookiecutterTemplate(cookiecutter_template_dir)

        context_file = os.path.join(cookiecutter_template_dir,
                                    "cookiecutter.json")

        config_dict = get_user_config(config_file=config_file,
                                      default_config=default_config)

        context = generate_context(
            context_file=context_file,
            default_context=config_dict["default_context"],
            extra_context=extra_context,
        )

        # prompt the user to manually configure at the command line.
        # except when 'no-input' flag is set
        context["cookiecutter"] = prompt_for_config(context, no_input)
        context["cookiecutter"]["_template"] = template_git_url

        with open(os.path.join(main_cookiecutter_directory, ".cruft.json"),
                  "w") as cruft_file:
            json_dump(
                {
                    "template": template_git_url,
                    "commit": last_commit,
                    "context": context
                },
                cruft_file,
            )

        return generate_files(
            repo_dir=cookiecutter_template_dir,
            context=context,
            overwrite_if_exists=overwrite_if_exists,
            output_dir=output_dir,
        )