Source code for temple.setup


Creates and initializes a project from a template
import subprocess
import unittest.mock

import cookiecutter.generate as cc_generate
import cookiecutter.hooks as cc_hooks

import temple.check
import temple.constants
import temple.utils

def _patched_run_hook(hook_name, project_dir, context):
    """Used to patch cookiecutter's ``run_hook`` function.

    This patched version ensures that the temple.yaml file is created before
    any cookiecutter hooks are executed
    if hook_name == 'post_gen_project':
    return cc_hooks.run_hook(hook_name, project_dir, context)

def _generate_files(repo_dir, config, template, version):
    """Uses cookiecutter to generate files for the project.

    Monkeypatches cookiecutter's "run_hook" to ensure that the temple.yaml file is
    generated before any hooks run. This is important to ensure that hooks can also
    perform any actions involving temple.yaml
    with unittest.mock.patch('cookiecutter.generate.run_hook', side_effect=_patched_run_hook):
                                   context={'cookiecutter': config,
                                            'template': template,
                                            'version': version},

[docs]@temple.utils.set_cmd_env_var('setup') def setup(template, version=None): """Sets up a new project from a template Note that the `temple.constants.TEMPLE_ENV_VAR` is set to 'setup' during the duration of this function. Args: template (str): The git SSH path to a template version (str, optional): The version of the template to use when updating. Defaults to the latest version """ temple.check.is_git_ssh_path(template) temple.check.not_in_git_repo() repo_path = temple.utils.get_repo_path(template) msg = ( 'You will be prompted for the parameters of your new project.' ' Please read the docs at{} before entering parameters.' ).format(repo_path) print(msg) cc_repo_dir, config = temple.utils.get_cookiecutter_config(template, version=version) if not version: with ret ='git rev-parse HEAD', stdout=subprocess.PIPE) version = ret.stdout.decode('utf-8').strip() _generate_files(repo_dir=cc_repo_dir, config=config, template=template, version=version)