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