Completed
Pull Request — master (#1008)
by
unknown
30s
created

cookiecutter()   C

Complexity

Conditions 7

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
c 2
b 0
f 0
dl 0
loc 82
rs 5.3686

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
3
"""
4
Main entry point for the `cookiecutter` command.
5
6
The code in this module is also a good example of how to use Cookiecutter as a
7
library rather than a script.
8
"""
9
10
from __future__ import unicode_literals
11
import logging
12
import os
13
14
from .config import get_user_config
15
from .generate import generate_context, generate_files
16
from .exceptions import InvalidModeException
17
from .prompt import prompt_for_config
18
from .replay import dump, load
19
from .repository import determine_repo_dir
20
from .utils import rmtree
21
22
from .context import context_is_version_2, load_context
23
24
logger = logging.getLogger(__name__)
25
26
27
def cookiecutter(
28
        template, checkout=None, no_input=False, extra_context=None,
29
        replay=False, overwrite_if_exists=False, output_dir='.',
30
        config_file=None, default_config=False, password=None):
31
    """
32
    API equivalent to using Cookiecutter at the command line.
33
34
    :param template: A directory containing a project template directory,
35
        or a URL to a git repository.
36
    :param checkout: The branch, tag or commit ID to checkout after clone.
37
    :param no_input: Prompt the user at command line for manual configuration?
38
    :param extra_context: A dictionary of context that overrides default
39
        and user configuration.
40
    :param: overwrite_if_exists: Overwrite the contents of output directory
41
        if it exists
42
    :param output_dir: Where to output the generated project dir into.
43
    :param config_file: User configuration file path.
44
    :param default_config: Use default values rather than a config file.
45
    :param password: The password to use when extracting the repository.
46
    """
47
    if replay and ((no_input is not False) or (extra_context is not None)):
48
        err_msg = (
49
            "You can not use both replay and no_input or extra_context "
50
            "at the same time."
51
        )
52
        raise InvalidModeException(err_msg)
53
54
    config_dict = get_user_config(
55
        config_file=config_file,
56
        default_config=default_config,
57
    )
58
59
    repo_dir, cleanup = determine_repo_dir(
60
        template=template,
61
        abbreviations=config_dict['abbreviations'],
62
        clone_to_dir=config_dict['cookiecutters_dir'],
63
        checkout=checkout,
64
        no_input=no_input,
65
        password=password
66
    )
67
68
    template_name = os.path.basename(os.path.abspath(repo_dir))
69
70
    if replay:
71
        context = load(config_dict['replay_dir'], template_name)
72
    else:
73
        context_file = os.path.join(repo_dir, 'cookiecutter.json')
74
        logger.debug('context_file is {}'.format(context_file))
75
76
        context = generate_context(
77
            context_file=context_file,
78
            default_context=config_dict['default_context'],
79
            extra_context=extra_context,
80
        )
81
82
        # prompt the user to manually configure at the command line.
83
        # except when 'no-input' flag is set
84
        if context_is_version_2(context['cookiecutter']):
85
            context['cookiecutter'] = load_context(context[u'cookiecutter'],
86
                                                   no_input=no_input,
87
                                                   verbose=True)
88
        else:
89
            context['cookiecutter'] = prompt_for_config(context, no_input)
90
91
        # include template dir or url in the context dict
92
        context['cookiecutter']['_template'] = template
93
94
        dump(config_dict['replay_dir'], template_name, context)
95
96
    # Create project from local context and project template.
97
    result = generate_files(
98
        repo_dir=repo_dir,
99
        context=context,
100
        overwrite_if_exists=overwrite_if_exists,
101
        output_dir=output_dir
102
    )
103
104
    # Cleanup (if required)
105
    if cleanup:
106
        rmtree(repo_dir)
107
108
    return result
109