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