Completed
Push — master ( 454794...ea987b )
by Michael
01:15
created

is_copy_only_path()   A

Complexity

Conditions 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
dl 0
loc 19
rs 9.2
c 1
b 0
f 0
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
)
25
from .find import find_template
26
from .utils import make_sure_path_exists, work_in, rmtree
27
from .hooks import run_hook
28
29
logger = logging.getLogger(__name__)
30
31
32
def is_copy_only_path(path, context):
33
    """Check whether the given `path` should only be copied as opposed to being
34
    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
    """Render filename of infile as name of outfile, handle infile correctly.
119
120
    Dealing with infile appropriately:
121
122
        a. If infile is a binary file, copy it over without rendering.
123
        b. If infile is a text file, render its contents and write the
124
           rendered infile to outfile.
125
126
    Precondition:
127
128
        When calling `generate_file()`, the root template dir must be the
129
        current working directory. Using `utils.work_in()` is the recommended
130
        way to perform this directory change.
131
132
    :param project_dir: Absolute path to the resulting generated project.
133
    :param infile: Input file to generate the file from. Relative to the root
134
        template dir.
135
    :param context: Dict for populating the cookiecutter's variables.
136
    :param env: Jinja2 template execution environment.
137
    """
138
    logger.debug('Processing file {}'.format(infile))
139
140
    # Render the path to the output file (not including the root project dir)
141
    outfile_tmpl = env.from_string(infile)
142
143
    outfile = os.path.join(project_dir, outfile_tmpl.render(**context))
144
    file_name_is_empty = os.path.isdir(outfile)
145
    if file_name_is_empty:
146
        logger.debug('The resulting file name is empty: {0}'.format(outfile))
147
        return
148
149
    logger.debug('Created file at {0}'.format(outfile))
150
151
    # Just copy over binary files. Don't render.
152
    logger.debug("Check {} to see if it's a binary".format(infile))
153
    if is_binary(infile):
154
        logger.debug(
155
            'Copying binary {} to {} without rendering'
156
            ''.format(infile, outfile)
157
        )
158
        shutil.copyfile(infile, outfile)
159
    else:
160
        # Force fwd slashes on Windows for get_template
161
        # This is a by-design Jinja issue
162
        infile_fwd_slashes = infile.replace(os.path.sep, '/')
163
164
        # Render the file
165
        try:
166
            tmpl = env.get_template(infile_fwd_slashes)
167
        except TemplateSyntaxError as exception:
168
            # Disable translated so that printed exception contains verbose
169
            # information about syntax error location
170
            exception.translated = False
171
            raise
172
        rendered_file = tmpl.render(**context)
173
174
        logger.debug('Writing contents to file {}'.format(outfile))
175
176
        with io.open(outfile, 'w', encoding='utf-8') as fh:
177
            fh.write(rendered_file)
178
179
    # Apply file permissions to output file
180
    shutil.copymode(infile, outfile)
181
182
183
def render_and_create_dir(dirname, context, output_dir, environment,
184
                          overwrite_if_exists=False):
185
    """Render name of a directory, create the directory, return its path."""
186
    name_tmpl = environment.from_string(dirname)
187
    rendered_dirname = name_tmpl.render(**context)
188
189
    dir_to_create = os.path.normpath(
190
        os.path.join(output_dir, rendered_dirname)
191
    )
192
193
    logger.debug('Rendered dir {} must exist in output_dir {}'.format(
194
        dir_to_create,
195
        output_dir
196
    ))
197
198
    output_dir_exists = os.path.exists(dir_to_create)
199
200
    if overwrite_if_exists:
201
        if output_dir_exists:
202
            logger.debug(
203
                'Output directory {} already exists,'
204
                'overwriting it'.format(dir_to_create)
205
            )
206
    else:
207
        if output_dir_exists:
208
            msg = 'Error: "{}" directory already exists'.format(dir_to_create)
209
            raise OutputDirExistsException(msg)
210
211
    make_sure_path_exists(dir_to_create)
212
    return dir_to_create
213
214
215
def ensure_dir_is_templated(dirname):
216
    """Ensure that dirname is a templated directory name."""
217
    if '{{' in dirname and '}}' in dirname:
218
        return True
219
    else:
220
        raise NonTemplatedInputDirException
221
222
223
def _run_hook_from_repo_dir(repo_dir, hook_name, project_dir, context):
224
    """Run hook from repo directory, clean project directory if hook fails."""
225
    with work_in(repo_dir):
226
        try:
227
            run_hook(hook_name, project_dir, context)
228
        except FailedHookException:
229
            rmtree(project_dir)
230
            logger.error(
231
                "Stopping generation because {} hook "
232
                "script didn't exit successfully".format(hook_name)
233
            )
234
            raise
235
236
237
def generate_files(repo_dir, context=None, output_dir='.',
238
                   overwrite_if_exists=False):
239
    """Render the templates and saves them to files.
240
241
    :param repo_dir: Project template input directory.
242
    :param context: Dict for populating the template's variables.
243
    :param output_dir: Where to output the generated project dir into.
244
    :param overwrite_if_exists: Overwrite the contents of the output directory
245
        if it exists.
246
    """
247
    template_dir = find_template(repo_dir)
248
    logger.debug('Generating project from {}...'.format(template_dir))
249
    context = context or {}
250
251
    unrendered_dir = os.path.split(template_dir)[1]
252
    ensure_dir_is_templated(unrendered_dir)
253
    env = StrictEnvironment(
254
        context=context,
255
        keep_trailing_newline=True,
256
    )
257
    try:
258
        project_dir = render_and_create_dir(
259
            unrendered_dir,
260
            context,
261
            output_dir,
262
            env,
263
            overwrite_if_exists
264
        )
265
    except UndefinedError as err:
266
        msg = "Unable to create project directory '{}'".format(unrendered_dir)
267
        raise UndefinedVariableInTemplate(msg, err, context)
268
269
    # We want the Jinja path and the OS paths to match. Consequently, we'll:
270
    #   + CD to the template folder
271
    #   + Set Jinja's path to '.'
272
    #
273
    #  In order to build our files to the correct folder(s), we'll use an
274
    # absolute path for the target folder (project_dir)
275
276
    project_dir = os.path.abspath(project_dir)
277
    logger.debug('Project directory is {}'.format(project_dir))
278
279
    _run_hook_from_repo_dir(repo_dir, 'pre_gen_project', project_dir, context)
280
281
    with work_in(template_dir):
282
        env.loader = FileSystemLoader('.')
283
284
        for root, dirs, files in os.walk('.'):
285
            # We must separate the two types of dirs into different lists.
286
            # The reason is that we don't want ``os.walk`` to go through the
287
            # unrendered directories, since they will just be copied.
288
            copy_dirs = []
289
            render_dirs = []
290
291
            for d in dirs:
292
                d_ = os.path.normpath(os.path.join(root, d))
293
                # We check the full path, because that's how it can be
294
                # specified in the ``_copy_without_render`` setting, but
295
                # we store just the dir name
296
                if is_copy_only_path(d_, context):
297
                    copy_dirs.append(d)
298
                else:
299
                    render_dirs.append(d)
300
301
            for copy_dir in copy_dirs:
302
                indir = os.path.normpath(os.path.join(root, copy_dir))
303
                outdir = os.path.normpath(os.path.join(project_dir, indir))
304
                logger.debug(
305
                    'Copying dir {} to {} without rendering'
306
                    ''.format(indir, outdir)
307
                )
308
                shutil.copytree(indir, outdir)
309
310
            # We mutate ``dirs``, because we only want to go through these dirs
311
            # recursively
312
            dirs[:] = render_dirs
313
            for d in dirs:
314
                unrendered_dir = os.path.join(project_dir, root, d)
315
                try:
316
                    render_and_create_dir(
317
                        unrendered_dir,
318
                        context,
319
                        output_dir,
320
                        env,
321
                        overwrite_if_exists
322
                    )
323
                except UndefinedError as err:
324
                    rmtree(project_dir)
325
                    _dir = os.path.relpath(unrendered_dir, output_dir)
326
                    msg = "Unable to create directory '{}'".format(_dir)
327
                    raise UndefinedVariableInTemplate(msg, err, context)
328
329
            for f in files:
330
                infile = os.path.normpath(os.path.join(root, f))
331
                if is_copy_only_path(infile, context):
332
                    outfile_tmpl = env.from_string(infile)
333
                    outfile_rendered = outfile_tmpl.render(**context)
334
                    outfile = os.path.join(project_dir, outfile_rendered)
335
                    logger.debug(
336
                        'Copying file {} to {} without rendering'
337
                        ''.format(infile, outfile)
338
                    )
339
                    shutil.copyfile(infile, outfile)
340
                    shutil.copymode(infile, outfile)
341
                    continue
342
                try:
343
                    generate_file(project_dir, infile, context, env)
344
                except UndefinedError as err:
345
                    rmtree(project_dir)
346
                    msg = "Unable to create file '{}'".format(infile)
347
                    raise UndefinedVariableInTemplate(msg, err, context)
348
349
    _run_hook_from_repo_dir(repo_dir, 'post_gen_project', project_dir, context)
350
351
    return project_dir
352