Completed
Pull Request — master (#885)
by
unknown
01:11 queued 12s
created

generate_file()   C

Complexity

Conditions 7

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
dl 0
loc 69
rs 5.7452
c 0
b 0
f 0

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
"""Functions for generating a project from a project template."""
4
from __future__ import unicode_literals
5
from collections import OrderedDict
6
import fnmatch
7
import io
8
import json
9
import logging
10
import os
11
import shutil
12
13
from jinja2 import FileSystemLoader
14
from cookiecutter.environment import StrictEnvironment
15
from jinja2.exceptions import TemplateSyntaxError, UndefinedError
16
from binaryornot.check import is_binary
17
18
from .exceptions import (
19
    NonTemplatedInputDirException,
20
    ContextDecodingException,
21
    FailedHookException,
22
    OutputDirExistsException,
23
    UndefinedVariableInTemplate,
24
    OutputFileExistsException,
25
)
26
from .find import find_template
27
from .utils import make_sure_path_exists, work_in, rmtree
28
from .hooks import run_hook
29
30
logger = logging.getLogger(__name__)
31
32
class ExistsAction(object):
33
    LEAVE = 'exists'
34
    OVERWRITE = 'overwrite'
35
    ERROR = 'error'
36
37
38
def is_copy_only_path(path, context):
39
    """Check whether the given `path` should only be copied as opposed to being
40
    rendered.
41
42
    Returns True if `path` matches a pattern in the given `context` dict,
43
    otherwise False.
44
45
    :param path: A file-system path referring to a file or dir that
46
        should be rendered or just copied.
47
    :param context: cookiecutter context.
48
    """
49
    try:
50
        for dont_render in context['cookiecutter']['_copy_without_render']:
51
            if fnmatch.fnmatch(path, dont_render):
52
                return True
53
    except KeyError:
54
        return False
55
56
    return False
57
58
59
def apply_overwrites_to_context(context, overwrite_context):
60
    """Modify the given context in place based on the overwrite_context."""
61
    for variable, overwrite in overwrite_context.items():
62
        if variable not in context:
63
            # Do not include variables which are not used in the template
64
            continue
65
66
        context_value = context[variable]
67
68
        if isinstance(context_value, list):
69
            # We are dealing with a choice variable
70
            if overwrite in context_value:
71
                # This overwrite is actually valid for the given context
72
                # Let's set it as default (by definition first item in list)
73
                # see ``cookiecutter.prompt.prompt_choice_for_config``
74
                context_value.remove(overwrite)
75
                context_value.insert(0, overwrite)
76
        else:
77
            # Simply overwrite the value for this variable
78
            context[variable] = overwrite
79
80
81
def generate_context(context_file='cookiecutter.json', default_context=None,
82
                     extra_context=None):
83
    """Generate the context for a Cookiecutter project template.
84
85
    Loads the JSON file as a Python object, with key being the JSON filename.
86
87
    :param context_file: JSON file containing key/value pairs for populating
88
        the cookiecutter's variables.
89
    :param default_context: Dictionary containing config to take into account.
90
    :param extra_context: Dictionary containing configuration overrides
91
    """
92
    context = {}
93
94
    try:
95
        with open(context_file) as file_handle:
96
            obj = json.load(file_handle, object_pairs_hook=OrderedDict)
97
    except ValueError as e:
98
        # JSON decoding error.  Let's throw a new exception that is more
99
        # friendly for the developer or user.
100
        full_fpath = os.path.abspath(context_file)
101
        json_exc_message = str(e)
102
        our_exc_message = (
103
            'JSON decoding error while loading "{0}".  Decoding'
104
            ' error details: "{1}"'.format(full_fpath, json_exc_message))
105
        raise ContextDecodingException(our_exc_message)
106
107
    # Add the Python object to the context dictionary
108
    file_name = os.path.split(context_file)[1]
109
    file_stem = file_name.split('.')[0]
110
    context[file_stem] = obj
111
112
    # Overwrite context variable defaults with the default context from the
113
    # user's global config, if available
114
    if default_context:
115
        apply_overwrites_to_context(obj, default_context)
116
    if extra_context:
117
        apply_overwrites_to_context(obj, extra_context)
118
119
    logger.debug('Context generated is {}'.format(context))
120
    return context
121
122
123
def generate_file(project_dir, infile, context, env,
124
        overwrite_if_exists=False):
125
    """Render filename of infile as name of outfile, handle infile correctly.
126
127
    Dealing with infile appropriately:
128
129
        a. If infile is a binary file, copy it over without rendering.
130
        b. If infile is a text file, render its contents and write the
131
           rendered infile to outfile.
132
133
    Precondition:
134
135
        When calling `generate_file()`, the root template dir must be the
136
        current working directory. Using `utils.work_in()` is the recommended
137
        way to perform this directory change.
138
139
    :param project_dir: Absolute path to the resulting generated project.
140
    :param infile: Input file to generate the file from. Relative to the root
141
        template dir.
142
    :param context: Dict for populating the cookiecutter's variables.
143
    :param env: Jinja2 template execution environment.
144
    """
145
    logger.debug('Processing file {}'.format(infile))
146
147
    # Render the path to the output file (not including the root project dir)
148
    outfile_tmpl = env.from_string(infile)
149
150
    outfile = os.path.join(project_dir, outfile_tmpl.render(**context))
151
152
    if not overwrite_if_exists and os.path.isfile(outfile):
153
        raise OutputFileExistsException(outfile)
154
155
    file_name_is_empty = os.path.isdir(outfile)
156
    if file_name_is_empty:
157
        logger.debug('The resulting file name is empty: {0}'.format(outfile))
158
        return
159
160
    logger.debug('Created file at {0}'.format(outfile))
161
162
    # Just copy over binary files. Don't render.
163
    logger.debug("Check {} to see if it's a binary".format(infile))
164
    if is_binary(infile):
165
        logger.debug(
166
            'Copying binary {} to {} without rendering'
167
            ''.format(infile, outfile)
168
        )
169
        shutil.copyfile(infile, outfile)
170
    else:
171
        # Force fwd slashes on Windows for get_template
172
        # This is a by-design Jinja issue
173
        infile_fwd_slashes = infile.replace(os.path.sep, '/')
174
175
        # Render the file
176
        try:
177
            tmpl = env.get_template(infile_fwd_slashes)
178
        except TemplateSyntaxError as exception:
179
            # Disable translated so that printed exception contains verbose
180
            # information about syntax error location
181
            exception.translated = False
182
            raise
183
        rendered_file = tmpl.render(**context)
184
185
        logger.debug('Writing contents to file {}'.format(outfile))
186
187
        with io.open(outfile, 'w', encoding='utf-8') as fh:
188
            fh.write(rendered_file)
189
190
    # Apply file permissions to output file
191
    shutil.copymode(infile, outfile)
192
193
194
def render_and_create_dir(dirname, context, output_dir, environment,
195
                          exists_action):
196
    """Render name of a directory, create the directory, return its path."""
197
    name_tmpl = environment.from_string(dirname)
198
    rendered_dirname = name_tmpl.render(**context)
199
200
    dir_to_create = os.path.normpath(
201
        os.path.join(output_dir, rendered_dirname)
202
    )
203
204
    logger.debug('Rendered dir {} must exist in output_dir {}'.format(
205
        dir_to_create,
206
        output_dir
207
    ))
208
209
    output_dir_exists = os.path.exists(dir_to_create)
210
211
    if exists_action == ExistsAction.OVERWRITE:
212
        if output_dir_exists:
213
            logger.debug(
214
                'Output directory {} already exists,'
215
                'overwriting it'.format(dir_to_create)
216
            )
217
    elif exists_action == ExistsAction.LEAVE:
218
        pass
219
    elif exists_action == ExistsAction.ERROR:
220
        if output_dir_exists:
221
            msg = 'Error: "{}" directory already exists'.format(dir_to_create)
222
            raise OutputDirExistsException(msg)
223
    else:
224
        raise ValueError(exists_action)
225
226
227
    make_sure_path_exists(dir_to_create)
228
    return dir_to_create
229
230
231
def ensure_dir_is_templated(dirname):
232
    """Ensure that dirname is a templated directory name."""
233
    if '{{' in dirname and '}}' in dirname:
234
        return True
235
    else:
236
        raise NonTemplatedInputDirException
237
238
239
def _run_hook_from_repo_dir(repo_dir, hook_name, project_dir, context):
240
    """Run hook from repo directory, clean project directory if hook fails.
241
242
    :param repo_dir: Project template input directory.
243
    :param hook_name: The hook to execute.
244
    :param project_dir: The directory to execute the script from.
245
    :param context: Cookiecutter project context.
246
    """
247
    with work_in(repo_dir):
248
        try:
249
            run_hook(hook_name, project_dir, context)
250
        except FailedHookException:
251
            rmtree(project_dir)
252
            logger.error(
253
                "Stopping generation because {} hook "
254
                "script didn't exit successfully".format(hook_name)
255
            )
256
            raise
257
258
259
def generate_files(repo_dir, context=None, output_dir='.',
260
                   overwrite_if_exists=False):
261
    """Render the templates and saves them to files.
262
263
    :param repo_dir: Project template input directory.
264
    :param context: Dict for populating the template's variables.
265
    :param output_dir: Where to output the generated project dir into.
266
    :param overwrite_if_exists: Overwrite the contents of the output directory
267
        if it exists.
268
    """
269
    template_dir = find_template(repo_dir)
270
    logger.debug('Generating project from {}...'.format(template_dir))
271
    context = context or {}
272
273
    try:
274
        target_dir = context['cookiecutter']['_target']
275
    except KeyError:
276
        target_dir = None
277
278
    env = StrictEnvironment(
279
        context=context,
280
        keep_trailing_newline=True,
281
    )
282
    unrendered_dir = os.path.split(template_dir)[1]
283
    ensure_dir_is_templated(unrendered_dir)
284
285
    if target_dir:
286
        exists_action = ExistsAction.LEAVE
287
    elif overwrite_if_exists:
288
        exists_action = ExistsAction.OVERWRITE
289
    else:
290
        exists_action = ExistsAction.ERROR
291
292
    try:
293
        project_dir = render_and_create_dir(
294
            target_dir if target_dir is not None else unrendered_dir,
295
            context,
296
            output_dir,
297
            env,
298
            exists_action,
299
        )
300
    except UndefinedError as err:
301
        msg = "Unable to create project directory '{}'".format(unrendered_dir)
302
        raise UndefinedVariableInTemplate(msg, err, context)
303
304
    # We want the Jinja path and the OS paths to match. Consequently, we'll:
305
    #   + CD to the template folder
306
    #   + Set Jinja's path to '.'
307
    #
308
    #  In order to build our files to the correct folder(s), we'll use an
309
    # absolute path for the target folder (project_dir)
310
311
    project_dir = os.path.abspath(project_dir)
312
    logger.debug('Project directory is {}'.format(project_dir))
313
314
    _run_hook_from_repo_dir(repo_dir, 'pre_gen_project', project_dir, context)
315
316
    with work_in(template_dir):
317
        env.loader = FileSystemLoader('.')
318
319
        for root, dirs, files in os.walk('.'):
320
            # We must separate the two types of dirs into different lists.
321
            # The reason is that we don't want ``os.walk`` to go through the
322
            # unrendered directories, since they will just be copied.
323
            copy_dirs = []
324
            render_dirs = []
325
326
            for d in dirs:
327
                d_ = os.path.normpath(os.path.join(root, d))
328
                # We check the full path, because that's how it can be
329
                # specified in the ``_copy_without_render`` setting, but
330
                # we store just the dir name
331
                if is_copy_only_path(d_, context):
332
                    copy_dirs.append(d)
333
                else:
334
                    render_dirs.append(d)
335
336
            for copy_dir in copy_dirs:
337
                indir = os.path.normpath(os.path.join(root, copy_dir))
338
                outdir = os.path.normpath(os.path.join(project_dir, indir))
339
                logger.debug(
340
                    'Copying dir {} to {} without rendering'
341
                    ''.format(indir, outdir)
342
                )
343
                shutil.copytree(indir, outdir)
344
345
            # We mutate ``dirs``, because we only want to go through these dirs
346
            # recursively
347
            dirs[:] = render_dirs
348
            for d in dirs:
349
                unrendered_dir = os.path.join(project_dir, root, d)
350
                try:
351
                    render_and_create_dir(
352
                        unrendered_dir,
353
                        context,
354
                        output_dir,
355
                        env,
356
                        exists_action,
357
                    )
358
                except UndefinedError as err:
359
                    rmtree(project_dir)
360
                    _dir = os.path.relpath(unrendered_dir, output_dir)
361
                    msg = "Unable to create directory '{}'".format(_dir)
362
                    raise UndefinedVariableInTemplate(msg, err, context)
363
364
            for f in files:
365
                infile = os.path.normpath(os.path.join(root, f))
366
                if is_copy_only_path(infile, context):
367
                    outfile_tmpl = env.from_string(infile)
368
                    outfile_rendered = outfile_tmpl.render(**context)
369
                    outfile = os.path.join(project_dir, outfile_rendered)
370
                    logger.debug(
371
                        'Copying file {} to {} without rendering'
372
                        ''.format(infile, outfile)
373
                    )
374
375
                    if os.path.exists(outfile):
376
                        raise OutputFileExistsException(outfile)
377
                    else:
378
                        shutil.copyfile(infile, outfile)
379
                        shutil.copymode(infile, outfile)
380
                    continue
381
                try:
382
                    generate_file(project_dir, infile, context, env, overwrite_if_exists)
383
                except UndefinedError as err:
384
                    # Don't delete pre-existing directories
385
                    if not target_dir:
386
                        rmtree(project_dir)
387
                    msg = "Unable to create file '{}'".format(infile)
388
                    raise UndefinedVariableInTemplate(msg, err, context)
389
390
    _run_hook_from_repo_dir(repo_dir, 'post_gen_project', project_dir, context)
391
392
    return project_dir
393