1
|
3 |
|
import textwrap |
2
|
3 |
|
from collections import OrderedDict |
3
|
|
|
from os.path import exists, expanduser, expandvars, join, curdir |
4
|
3 |
|
import io |
5
|
3 |
|
import os |
6
|
3 |
|
import sys |
7
|
|
|
|
8
|
3 |
|
import click |
|
|
|
|
9
|
3 |
|
from pathlib import Path |
10
|
3 |
|
|
11
|
|
|
import toml |
|
|
|
|
12
|
3 |
|
import attr |
|
|
|
|
13
|
3 |
|
|
14
|
|
|
import changes |
15
|
|
|
from changes.models import GitRepository |
16
|
|
|
from .commands import info, note |
17
|
|
|
|
18
|
3 |
|
|
19
|
|
|
AUTH_TOKEN_ENVVAR = 'GITHUB_AUTH_TOKEN' |
20
|
|
|
|
21
|
|
|
# via https://github.com/jakubroztocil/httpie/blob/6bdfc7a/httpie/config.py#L9 |
22
|
3 |
|
IS_WINDOWS = 'win32' in str(sys.platform).lower() |
23
|
|
|
DEFAULT_CONFIG_FILE = str(os.environ.get( |
24
|
|
|
'CHANGES_CONFIG_FILE', |
25
|
|
|
expanduser('~/.changes') if not IS_WINDOWS else |
26
|
|
|
expandvars(r'%APPDATA%\\.changes') |
27
|
|
|
)) |
28
|
|
|
|
29
|
3 |
|
PROJECT_CONFIG_FILE = '.changes.toml' |
30
|
3 |
|
DEFAULT_RELEASES_DIRECTORY = 'docs/releases' |
31
|
3 |
|
|
32
|
3 |
|
|
33
|
3 |
|
@attr.s |
34
|
3 |
|
class Changes(object): |
35
|
|
|
auth_token = attr.ib() |
36
|
3 |
|
|
37
|
|
|
|
38
|
3 |
|
def load_settings(): |
39
|
|
|
tool_config_path = Path(str(os.environ.get( |
40
|
3 |
|
'CHANGES_CONFIG_FILE', |
41
|
3 |
|
expanduser('~/.changes') if not IS_WINDOWS else |
42
|
3 |
|
expandvars(r'%APPDATA%\\.changes') |
43
|
3 |
|
))) |
44
|
3 |
|
|
45
|
|
|
tool_settings = None |
46
|
|
|
if tool_config_path.exists(): |
47
|
|
|
tool_settings = Changes( |
48
|
|
|
**(toml.load(tool_config_path.open())['changes']) |
49
|
3 |
|
) |
50
|
|
|
|
51
|
|
|
if not (tool_settings and tool_settings.auth_token): |
52
|
3 |
|
# prompt for auth token |
53
|
|
|
auth_token = os.environ.get(AUTH_TOKEN_ENVVAR) |
54
|
3 |
|
if auth_token: |
55
|
|
|
info('Found Github Auth Token in the environment') |
56
|
3 |
|
|
57
|
|
|
while not auth_token: |
58
|
3 |
|
info('No auth token found, asking for it') |
59
|
3 |
|
# to interact with the Git*H*ub API |
60
|
3 |
|
note('You need a Github Auth Token for changes to create a release.') |
|
|
|
|
61
|
|
|
click.pause('Press [enter] to launch the GitHub "New personal access ' |
|
|
|
|
62
|
3 |
|
'token" page, to create a token for changes.') |
63
|
|
|
click.launch('https://github.com/settings/tokens/new') |
64
|
|
|
auth_token = click.prompt('Enter your changes token') |
65
|
3 |
|
|
66
|
3 |
|
if not tool_settings: |
67
|
|
|
tool_settings = Changes(auth_token=auth_token) |
68
|
|
|
|
69
|
3 |
|
tool_config_path.write_text( |
|
|
|
|
70
|
3 |
|
toml.dumps({ |
71
|
|
|
'changes': attr.asdict(tool_settings) |
72
|
3 |
|
}) |
73
|
3 |
|
) |
74
|
|
|
|
75
|
|
|
return tool_settings |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
@attr.s |
79
|
|
|
class Project(object): |
80
|
|
|
releases_directory = attr.ib() |
81
|
|
|
repository = attr.ib(default=None) |
82
|
|
|
bumpversion = attr.ib(default=None) |
83
|
|
|
labels = attr.ib(default=attr.Factory(dict)) |
84
|
|
|
|
85
|
|
|
@property |
86
|
|
|
def bumpversion_configured(self): |
87
|
|
|
return isinstance(self.bumpversion, BumpVersion) |
88
|
|
|
|
89
|
|
|
@property |
90
|
|
|
def labels_selected(self): |
91
|
|
|
return len(self.labels) > 0 |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
@attr.s |
95
|
|
|
class BumpVersion(object): |
96
|
|
|
DRAFT_OPTIONS = [ |
97
|
|
|
'--dry-run', '--verbose', |
98
|
|
|
'--no-commit', '--no-tag', |
99
|
|
|
'--allow-dirty', |
100
|
|
|
] |
101
|
|
|
STAGE_OPTIONS = [ |
102
|
|
|
'--verbose', |
103
|
|
|
'--no-commit', '--no-tag', |
104
|
|
|
] |
105
|
|
|
|
106
|
|
|
current_version = attr.ib() |
107
|
|
|
version_files_to_replace = attr.ib(default=attr.Factory(list)) |
108
|
|
|
|
109
|
|
|
def write_to_file(self, config_path: Path): |
110
|
|
|
bumpversion_cfg = textwrap.dedent( |
111
|
|
|
"""\ |
112
|
|
|
[bumpversion] |
113
|
|
|
current_version = {current_version} |
114
|
|
|
|
115
|
|
|
""" |
116
|
|
|
).format(**attr.asdict(self)) |
117
|
|
|
|
118
|
|
|
bumpversion_files = '\n\n'.join([ |
119
|
|
|
'[bumpversion:file:{}]'.format(file_name) |
120
|
|
|
for file_name in self.version_files_to_replace |
121
|
|
|
]) |
122
|
|
|
|
123
|
|
|
config_path.write_text( |
124
|
|
|
bumpversion_cfg + bumpversion_files |
125
|
|
|
) |
126
|
|
|
|
127
|
|
|
def load_project_settings(): |
128
|
|
|
project_settings = configure_changes() |
129
|
|
|
|
130
|
|
|
info('Indexing repository') |
131
|
|
|
project_settings.repository = GitRepository( |
132
|
|
|
auth_token=changes.settings.auth_token |
133
|
|
|
) |
134
|
|
|
|
135
|
|
|
project_settings.bumpversion = configure_bumpversion(project_settings) |
136
|
|
|
|
137
|
|
|
project_settings.labels = configure_labels(project_settings) |
138
|
|
|
|
139
|
|
|
return project_settings |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
def configure_labels(project_settings): |
143
|
|
|
if project_settings.labels_selected: |
144
|
|
|
return project_settings.labels |
145
|
|
|
|
146
|
|
|
github_labels = project_settings.repository.github_labels |
147
|
|
|
|
148
|
|
|
# since there are no labels defined |
149
|
|
|
# let's ask which github tags they want to track |
150
|
|
|
# TODO: streamlined support for github defaults: enhancement, bug |
151
|
|
|
changelog_worthy_labels = read_user_choices( |
152
|
|
|
'labels', |
153
|
|
|
[ |
154
|
|
|
properties['name'] |
155
|
|
|
for label, properties in github_labels.items() |
156
|
|
|
] |
157
|
|
|
) |
158
|
|
|
|
159
|
|
|
# TODO: if not project_settings.labels_have_descriptions: |
160
|
|
|
described_labels = {} |
161
|
|
|
# auto-generate label descriptions |
162
|
|
|
for label_name in changelog_worthy_labels: |
163
|
|
|
label_properties = github_labels[label_name] |
164
|
|
|
# Auto-generate description as titlecase label name |
165
|
|
|
label_properties['description'] = label_name.title() |
166
|
|
|
described_labels[label_name] = label_properties |
167
|
|
|
|
168
|
|
|
return described_labels |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
def configure_changes(): |
172
|
|
|
changes_project_config_path = Path(PROJECT_CONFIG_FILE) |
173
|
|
|
project_settings = None |
174
|
|
|
if changes_project_config_path.exists(): |
175
|
|
|
project_settings = Project( |
176
|
|
|
**(toml.load(changes_project_config_path.open())['changes']) |
177
|
|
|
) |
178
|
|
|
if not project_settings: |
179
|
|
|
project_settings = Project( |
180
|
|
|
releases_directory=str(Path(click.prompt( |
181
|
|
|
'Enter the directory to store your releases notes', |
182
|
|
|
DEFAULT_RELEASES_DIRECTORY, |
183
|
|
|
type=click.Path(exists=True, dir_okay=True) |
184
|
|
|
))) |
185
|
|
|
) |
186
|
|
|
# write config file |
187
|
|
|
changes_project_config_path.write_text( |
|
|
|
|
188
|
|
|
toml.dumps({ |
189
|
|
|
'changes': attr.asdict(project_settings) |
190
|
|
|
}) |
191
|
|
|
) |
192
|
|
|
|
193
|
|
|
return project_settings |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
def configure_bumpversion(project_settings): |
197
|
|
|
# TODO: look in other supported bumpversion config locations |
198
|
|
|
bumpversion = None |
199
|
|
|
bumpversion_config_path = Path('.bumpversion.cfg') |
200
|
|
|
if not bumpversion_config_path.exists(): |
201
|
|
|
user_supplied_versioned_file_paths = [] |
|
|
|
|
202
|
|
|
|
203
|
|
|
version_file_path = None |
204
|
|
|
while not version_file_path == Path('.'): |
205
|
|
|
version_file_path = Path(click.prompt( |
206
|
|
|
'Enter a path to a file that contains a version number ' |
207
|
|
|
"(enter a path of '.' when you're done selecting files)", |
208
|
|
|
type=click.Path( |
209
|
|
|
exists=True, |
210
|
|
|
dir_okay=True, |
211
|
|
|
file_okay=True, |
212
|
|
|
readable=True |
213
|
|
|
) |
214
|
|
|
)) |
215
|
|
|
|
216
|
|
|
if version_file_path != Path('.'): |
217
|
|
|
user_supplied_versioned_file_paths.append(version_file_path) |
218
|
|
|
|
219
|
|
|
bumpversion = BumpVersion( |
220
|
|
|
current_version=project_settings.repository.latest_version, |
221
|
|
|
version_files_to_replace=user_supplied_versioned_file_paths, |
222
|
|
|
) |
223
|
|
|
bumpversion.write_to_file(bumpversion_config_path) |
224
|
|
|
else: |
225
|
|
|
raise NotImplemented('') |
|
|
|
|
226
|
|
|
|
227
|
|
|
return bumpversion |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
def read_user_choices(var_name, options): |
231
|
|
|
"""Prompt the user to choose from several options for the given variable. |
232
|
|
|
|
233
|
|
|
# cookiecutter/cookiecutter/prompt.py |
234
|
|
|
The first item will be returned if no input happens. |
235
|
|
|
|
236
|
|
|
:param str var_name: Variable as specified in the context |
237
|
|
|
:param list options: Sequence of options that are available to select from |
238
|
|
|
:return: Exactly one item of ``options`` that has been chosen by the user |
239
|
|
|
""" |
240
|
|
|
raise NotImplementedError() |
241
|
|
|
# |
242
|
|
|
|
243
|
|
|
# Please see http://click.pocoo.org/4/api/#click.prompt |
244
|
|
|
if not isinstance(options, list): |
245
|
|
|
raise TypeError |
246
|
|
|
|
247
|
|
|
if not options: |
248
|
|
|
raise ValueError |
249
|
|
|
|
250
|
|
|
choice_map = OrderedDict( |
251
|
|
|
(u'{}'.format(i), value) for i, value in enumerate(options, 1) |
252
|
|
|
) |
253
|
|
|
choices = choice_map.keys() |
254
|
|
|
default = u'1' |
255
|
|
|
|
256
|
|
|
choice_lines = [u'{} - {}'.format(*c) for c in choice_map.items()] |
257
|
|
|
prompt = u'\n'.join(( |
258
|
|
|
u'Select {}:'.format(var_name), |
259
|
|
|
u'\n'.join(choice_lines), |
260
|
|
|
u'Choose from {}'.format(u', '.join(choices)) |
261
|
|
|
)) |
262
|
|
|
|
263
|
|
|
# TODO: multi-select |
264
|
|
|
user_choice = click.prompt( |
265
|
|
|
prompt, type=click.Choice(choices), default=default |
266
|
|
|
) |
267
|
|
|
return choice_map[user_choice] |
268
|
|
|
|
269
|
|
|
DEFAULTS = { |
270
|
|
|
'changelog': 'CHANGELOG.md', |
271
|
|
|
'readme': 'README.md', |
272
|
|
|
'github_auth_token': None, |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
class Config: |
277
|
|
|
test_command = None |
278
|
|
|
pypi = None |
279
|
|
|
skip_changelog = None |
280
|
|
|
changelog_content = None |
281
|
|
|
repo = None |
282
|
|
|
|
283
|
|
|
def __init__(self, module_name, dry_run, debug, no_input, requirements, |
284
|
|
|
new_version, current_version, repo_url, version_prefix): |
285
|
|
|
self.module_name = module_name |
286
|
|
|
# module_name => project_name => curdir |
287
|
|
|
self.dry_run = dry_run |
288
|
|
|
self.debug = debug |
289
|
|
|
self.no_input = no_input |
290
|
|
|
self.requirements = requirements |
291
|
|
|
self.new_version = ( |
292
|
|
|
version_prefix + new_version |
293
|
|
|
if version_prefix |
294
|
|
|
else new_version |
295
|
|
|
) |
296
|
|
|
self.current_version = current_version |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
def project_config(): |
300
|
|
|
"""Deprecated""" |
301
|
|
|
project_name = curdir |
302
|
|
|
|
303
|
|
|
config_path = Path(join(project_name, PROJECT_CONFIG_FILE)) |
304
|
|
|
|
305
|
|
|
if not exists(config_path): |
306
|
|
|
store_settings(DEFAULTS.copy()) |
307
|
|
|
return DEFAULTS |
308
|
|
|
|
309
|
|
|
return toml.load(io.open(config_path)) or {} |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
def store_settings(settings): |
313
|
|
|
pass |
314
|
|
|
|
315
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.