-
Notifications
You must be signed in to change notification settings - Fork 28
/
plugin.py
207 lines (160 loc) · 5.8 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# -*- coding: utf-8 -*-
import os
import pathlib
import warnings
import py
import pytest
from cookiecutter.generate import generate_context
from cookiecutter.main import cookiecutter
from cookiecutter.prompt import prompt_for_config
USER_CONFIG = u"""
cookiecutters_dir: "{cookiecutters_dir}"
replay_dir: "{replay_dir}"
"""
class Result(object):
"""Holds the captured result of the cookiecutter project generation."""
def __init__(self, exception=None, exit_code=0, project_dir=None, context=None):
self.exception = exception
self.exit_code = exit_code
self.context = context
self._project_dir = project_dir
@property
def project(self):
"""Return a py.path.local object if no exception occurred."""
warning_message = (
"project is deprecated and will be removed in a future release, "
"please use project_path instead."
)
warnings.warn(
warning_message,
DeprecationWarning,
stacklevel=1,
)
if self.exception is None:
return py.path.local(self._project_dir)
return None
@property
def project_path(self):
"""Return a pathlib.Path object if no exception occurred."""
if self.exception is None:
return pathlib.Path(self._project_dir)
return None
def __repr__(self):
if self.exception:
return "<Result {!r}>".format(self.exception)
return "<Result {}>".format(self.project)
class Cookies(object):
"""Class to provide convenient access to the cookiecutter API."""
def __init__(self, template, output_factory, config_file):
self._default_template = template
self._output_factory = output_factory
self._config_file = config_file
self._counter = 0
def _new_output_dir(self):
dirname = "bake{:02d}".format(self._counter)
output_dir = self._output_factory(dirname)
self._counter += 1
return output_dir
def bake(self, extra_context=None, template=None):
exception = None
exit_code = 0
project_dir = None
context = None
if template is None:
template = self._default_template
context_file = pathlib.Path(template) / "cookiecutter.json"
try:
# Render the context, so that we can store it on the Result
context = prompt_for_config(
generate_context(
context_file=str(context_file), extra_context=extra_context
),
no_input=True,
)
# Run cookiecutter to generate a new project
project_dir = cookiecutter(
template,
no_input=True,
extra_context=extra_context,
output_dir=str(self._new_output_dir()),
config_file=str(self._config_file),
)
except SystemExit as e:
if e.code != 0:
exception = e
exit_code = e.code
except Exception as e:
exception = e
exit_code = -1
return Result(
exception=exception,
exit_code=exit_code,
project_dir=project_dir,
context=context,
)
@pytest.fixture(scope="session")
def _cookiecutter_config_file(tmpdir_factory):
user_dir = tmpdir_factory.mktemp("user_dir")
cookiecutters_dir = user_dir.mkdir("cookiecutters")
replay_dir = user_dir.mkdir("cookiecutter_replay")
config_text = USER_CONFIG.format(
cookiecutters_dir=cookiecutters_dir, replay_dir=replay_dir
)
config_file = user_dir.join("config")
config_file.write_text(config_text, encoding="utf8")
return config_file
@pytest.fixture
def cookies(request, tmpdir, _cookiecutter_config_file):
"""Yield an instance of the Cookies helper class that can be used to
generate a project from a template.
Run cookiecutter:
result = cookies.bake(extra_context={
'variable1': 'value1',
'variable2': 'value2',
})
"""
template_dir = request.config.option.template
output_dir = tmpdir.mkdir("cookies")
output_factory = output_dir.mkdir
yield Cookies(template_dir, output_factory, _cookiecutter_config_file)
# Add option to keep generated output directories.
if not request.config.option.keep_baked_projects:
output_dir.remove()
@pytest.fixture(scope="session")
def cookies_session(request, tmpdir_factory, _cookiecutter_config_file):
"""Yield an instance of the Cookies helper class that can be used to
generate a project from a template.
Run cookiecutter:
result = cookies.bake(extra_context={
'variable1': 'value1',
'variable2': 'value2',
})
"""
template_dir = request.config.option.template
output_dir = tmpdir_factory.mktemp("cookies")
output_factory = output_dir.mkdir
yield Cookies(template_dir, output_factory, _cookiecutter_config_file)
# Add option to keep generated output directories.
if not request.config.option.keep_baked_projects:
output_dir.remove()
def pytest_addoption(parser):
group = parser.getgroup("cookies")
group.addoption(
"--template",
action="store",
default=".",
dest="template",
help="specify the template to be rendered",
type=str,
)
group.addoption(
"--keep-baked-projects",
action="store_true",
default=False,
dest="keep_baked_projects",
help="Keep projects directories generated with 'cookies.bake()'.",
)
def pytest_configure(config):
# To protect ourselves from tests or fixtures changing directories, keep
# an absolute path to the template.
config.option.template = os.path.abspath(config.option.template)