Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
default_config: bool = False,
extra_context: Optional[dict] = None,
) -> 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
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,
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),
cruft_state = json.loads(cruft_file.read_text())
skip_cruft = cruft_state.get("skip", [])
if toml and pyproject_file.is_file():
pyproject_cruft = toml.loads(pyproject_file.read_text()).get("tool", {}).get("cruft", {})
skip_cruft.extend(pyproject_cruft.get("skip", []))
with TemporaryDirectory() as compare_directory_str:
compare_directory = Path(compare_directory_str)
template_dir = compare_directory / "template"
try:
repo = Repo.clone_from(cruft_state["template"], template_dir)
last_commit = repo.head.object.hexsha
except Exception as e: # pragma: no cover
raise InvalidCookiecutterRepository(e)
if last_commit == cruft_state["commit"] or not repo.index.diff(cruft_state["commit"]):
return False
context_file = template_dir / "cookiecutter.json"
new_output_dir = compare_directory / "new_output"
new_context = _generate_output(
context_file=str(context_file),
cruft_state=cruft_state,
cookiecutter_input=cookiecutter_input,
template_dir=str(template_dir),
output_dir=str(new_output_dir),
)