1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
# The MIT License (MIT) |
5
|
|
|
# Copyright © 2016, Jace Browning |
6
|
|
|
# |
7
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
# in the Software without restriction, including without limitation the rights |
10
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
# furnished to do so, subject to the following conditions: |
13
|
|
|
# |
14
|
|
|
# The above copyright notice and this permission notice shall be included in |
15
|
|
|
# all copies or substantial portions of the Software. |
16
|
|
|
# |
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
# SOFTWARE. |
24
|
|
|
# |
25
|
|
|
# Source: https://github.com/jacebrowning/verchew |
26
|
|
|
# Documentation: https://verchew.readthedocs.io |
27
|
|
|
# Package: https://pypi.org/project/verchew |
28
|
|
|
|
29
|
|
|
|
30
|
1 |
|
from __future__ import unicode_literals |
31
|
|
|
|
32
|
1 |
|
import argparse |
33
|
1 |
|
import logging |
34
|
1 |
|
import os |
35
|
1 |
|
import re |
36
|
1 |
|
import sys |
37
|
|
|
from collections import OrderedDict |
38
|
|
|
from subprocess import PIPE, STDOUT, Popen |
39
|
1 |
|
from typing import Any, Dict |
40
|
1 |
|
|
41
|
1 |
|
|
42
|
|
|
PY2 = sys.version_info[0] == 2 |
43
|
1 |
|
|
44
|
|
|
if PY2: |
45
|
1 |
|
import ConfigParser as configparser # pylint: disable=import-error |
46
|
1 |
|
else: |
47
|
1 |
|
import configparser # type: ignore |
48
|
|
|
|
49
|
|
|
__version__ = '3.0b1' |
50
|
|
|
|
51
|
|
|
CONFIG_FILENAMES = ['verchew.ini', '.verchew.ini', '.verchewrc', '.verchew'] |
52
|
|
|
|
53
|
|
|
SAMPLE_CONFIG = """ |
54
|
|
|
[Python] |
55
|
|
|
|
56
|
|
|
cli = python |
57
|
|
|
version = Python 3.5 || Python 3.6 |
58
|
|
|
|
59
|
|
|
[Legacy Python] |
60
|
|
|
|
61
|
|
|
cli = python2 |
62
|
|
|
version = Python 2.7 |
63
|
|
|
|
64
|
|
|
[virtualenv] |
65
|
|
|
|
66
|
|
|
cli = virtualenv |
67
|
|
|
version = 15 |
68
|
|
|
message = Only required with Python 2. |
69
|
|
|
|
70
|
|
|
[Make] |
71
|
|
|
|
72
|
|
|
cli = make |
73
|
1 |
|
version = GNU Make |
74
|
|
|
optional = true |
75
|
|
|
|
76
|
|
|
""".strip() |
77
|
|
|
|
78
|
|
|
STYLE = {"~": "✔", "*": "⭑", "?": "⚠", "x": "✘"} |
79
|
1 |
|
|
80
|
|
|
COLOR = { |
81
|
|
|
"x": "\033[91m", # red |
82
|
|
|
"~": "\033[92m", # green |
83
|
|
|
"?": "\033[93m", # yellow |
84
|
|
|
"*": "\033[94m", # cyan |
85
|
|
|
None: "\033[0m", # reset |
86
|
|
|
} |
87
|
1 |
|
|
88
|
|
|
QUIET = False |
89
|
|
|
|
90
|
1 |
|
log = logging.getLogger(__name__) |
91
|
1 |
|
|
92
|
1 |
|
|
93
|
|
|
def main(): |
94
|
1 |
|
global QUIET |
95
|
1 |
|
|
96
|
|
|
args = parse_args() |
97
|
1 |
|
configure_logging(args.verbose) |
98
|
1 |
|
if args.quiet: |
99
|
|
|
QUIET = True |
100
|
1 |
|
|
101
|
1 |
|
log.debug("PWD: %s", os.getenv('PWD')) |
102
|
|
|
log.debug("PATH: %s", os.getenv('PATH')) |
103
|
|
|
|
104
|
1 |
|
path = find_config(args.root, generate=args.init) |
105
|
1 |
|
config = parse_config(path) |
106
|
|
|
|
107
|
1 |
|
if not check_dependencies(config) and args.exit_code: |
108
|
1 |
|
sys.exit(1) |
109
|
1 |
|
|
110
|
|
|
|
111
|
1 |
|
def parse_args(): |
112
|
|
|
parser = argparse.ArgumentParser() |
113
|
1 |
|
|
114
|
|
|
version = "%(prog)s v" + __version__ |
115
|
1 |
|
parser.add_argument('--version', action='version', version=version) |
116
|
|
|
parser.add_argument( |
117
|
|
|
'-r', '--root', metavar='PATH', help="specify a custom project root directory" |
118
|
1 |
|
) |
119
|
|
|
parser.add_argument( |
120
|
1 |
|
'--init', action='store_true', help="generate a sample configuration file" |
121
|
|
|
) |
122
|
|
|
parser.add_argument( |
123
|
1 |
|
'--exit-code', |
124
|
1 |
|
action='store_true', |
125
|
1 |
|
help="return a non-zero exit code on failure", |
126
|
|
|
) |
127
|
|
|
|
128
|
|
|
group = parser.add_mutually_exclusive_group() |
129
|
|
|
group.add_argument( |
130
|
|
|
'-v', '--verbose', action='count', default=0, help="enable verbose logging" |
131
|
1 |
|
) |
132
|
|
|
group.add_argument( |
133
|
|
|
'-q', '--quiet', action='store_true', help="suppress all output on success" |
134
|
1 |
|
) |
135
|
1 |
|
|
136
|
1 |
|
args = parser.parse_args() |
137
|
|
|
|
138
|
1 |
|
return args |
139
|
1 |
|
|
140
|
1 |
|
|
141
|
1 |
|
def configure_logging(count=0): |
142
|
1 |
|
if count == 0: |
143
|
1 |
|
level = logging.WARNING |
144
|
1 |
|
elif count == 1: |
145
|
1 |
|
level = logging.INFO |
146
|
|
|
else: |
147
|
1 |
|
level = logging.DEBUG |
148
|
1 |
|
|
149
|
1 |
|
logging.basicConfig(level=level, format="%(levelname)s: %(message)s") |
150
|
|
|
|
151
|
1 |
|
|
152
|
1 |
|
def find_config(root=None, filenames=None, generate=False): |
153
|
|
|
root = root or os.getcwd() |
154
|
|
|
filenames = filenames or CONFIG_FILENAMES |
155
|
1 |
|
|
156
|
1 |
|
path = None |
157
|
1 |
|
log.info("Looking for config file in: %s", root) |
158
|
|
|
log.debug("Filename options: %s", ", ".join(filenames)) |
159
|
1 |
|
for filename in os.listdir(root): |
160
|
|
|
if filename in filenames: |
161
|
1 |
|
path = os.path.join(root, filename) |
162
|
1 |
|
log.info("Found config file: %s", path) |
163
|
1 |
|
return path |
164
|
|
|
|
165
|
1 |
|
if generate: |
166
|
|
|
path = generate_config(root, filenames) |
167
|
|
|
return path |
168
|
1 |
|
|
169
|
1 |
|
msg = "No config file found in: {0}".format(root) |
170
|
|
|
raise RuntimeError(msg) |
171
|
1 |
|
|
172
|
1 |
|
|
173
|
1 |
|
def generate_config(root=None, filenames=None): |
174
|
|
|
root = root or os.getcwd() |
175
|
1 |
|
filenames = filenames or CONFIG_FILENAMES |
176
|
1 |
|
|
177
|
1 |
|
path = os.path.join(root, filenames[0]) |
178
|
1 |
|
|
179
|
|
|
log.info("Generating sample config: %s", path) |
180
|
1 |
|
with open(path, 'w') as config: |
181
|
|
|
config.write(SAMPLE_CONFIG + '\n') |
182
|
|
|
|
183
|
1 |
|
return path |
184
|
1 |
|
|
185
|
|
|
|
186
|
1 |
|
def parse_config(path): |
187
|
1 |
|
data: Dict[str, Any] = OrderedDict() |
188
|
1 |
|
|
189
|
1 |
|
log.info("Parsing config file: %s", path) |
190
|
1 |
|
config = configparser.ConfigParser() |
191
|
1 |
|
config.read(path) |
192
|
|
|
|
193
|
1 |
|
for section in config.sections(): |
194
|
1 |
|
data[section] = OrderedDict() |
195
|
1 |
|
for name, value in config.items(section): |
196
|
|
|
data[section][name] = value |
197
|
1 |
|
|
198
|
1 |
|
for name in data: |
199
|
1 |
|
version = data[name].get('version') or "" |
200
|
1 |
|
data[name]['version'] = version |
201
|
|
|
data[name]['patterns'] = [v.strip() for v in version.split('||')] |
202
|
1 |
|
|
203
|
|
|
return data |
204
|
1 |
|
|
205
|
|
|
|
206
|
|
|
def check_dependencies(config): |
207
|
1 |
|
success = [] |
208
|
1 |
|
|
209
|
1 |
|
for name, settings in config.items(): |
210
|
|
|
show("Checking for {0}...".format(name), head=True) |
211
|
1 |
|
output = get_version(settings['cli'], settings.get('cli_version_arg')) |
212
|
1 |
|
|
213
|
1 |
|
for pattern in settings['patterns']: |
214
|
|
|
if match_version(pattern, output): |
215
|
1 |
|
show(_("~") + " MATCHED: {0}".format(pattern)) |
216
|
|
|
success.append(_("~")) |
217
|
|
|
break |
218
|
1 |
|
else: |
219
|
1 |
|
if settings.get('optional'): |
220
|
|
|
show(_("?") + " EXPECTED: {0}".format(settings['version'])) |
221
|
|
|
success.append(_("?")) |
222
|
1 |
|
else: |
223
|
1 |
|
if QUIET: |
224
|
1 |
|
print( |
225
|
1 |
|
"Unmatched {0} version: {1}".format(name, settings['version']) |
226
|
1 |
|
) |
227
|
1 |
|
show(_("x") + " EXPECTED: {0}".format(settings['version'])) |
228
|
|
|
success.append(_("x")) |
229
|
1 |
|
if settings.get('message'): |
230
|
1 |
|
show(_("*") + " MESSAGE: {0}".format(settings['message'])) |
231
|
1 |
|
|
232
|
|
|
show("Results: " + " ".join(success), head=True) |
233
|
1 |
|
|
234
|
|
|
return _("x") not in success |
235
|
|
|
|
236
|
1 |
|
|
237
|
|
|
def get_version(program, argument=None): |
238
|
1 |
|
if argument is None: |
239
|
1 |
|
args = [program, '--version'] |
240
|
1 |
|
elif argument: |
241
|
|
|
args = [program, argument] |
242
|
1 |
|
else: |
243
|
1 |
|
args = [program] |
244
|
|
|
|
245
|
1 |
|
show("$ {0}".format(" ".join(args))) |
246
|
1 |
|
output = call(args) |
247
|
|
|
lines = output.splitlines() |
248
|
1 |
|
show(lines[0] if lines else "<nothing>") |
249
|
1 |
|
|
250
|
|
|
return output |
251
|
|
|
|
252
|
1 |
|
|
253
|
|
|
def match_version(pattern, output): |
254
|
1 |
|
regex = pattern.replace('.', r'\.') + r'(\b|/)' |
255
|
|
|
|
256
|
1 |
|
log.debug("Matching %s: %s", regex, output) |
257
|
1 |
|
match = re.match(regex, output) |
258
|
1 |
|
if match is None: |
259
|
1 |
|
match = re.match(r'.*[^\d.]' + regex, output) |
260
|
1 |
|
|
261
|
1 |
|
return bool(match) |
262
|
|
|
|
263
|
1 |
|
|
264
|
1 |
|
def call(args): |
265
|
|
|
try: |
266
|
1 |
|
process = Popen(args, stdout=PIPE, stderr=STDOUT) |
267
|
1 |
|
except OSError: |
268
|
|
|
log.debug("Command not found: %s", args[0]) |
269
|
1 |
|
output = "sh: command not found: {0}".format(args[0]) |
270
|
1 |
|
else: |
271
|
|
|
raw = process.communicate()[0] |
272
|
1 |
|
output = raw.decode('utf-8').strip() |
273
|
|
|
log.debug("Command output: %r", output) |
274
|
|
|
|
275
|
|
|
return output |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
def show(text, start='', end='\n', head=False): |
279
|
|
|
"""Python 2 and 3 compatible version of print.""" |
280
|
|
|
if QUIET: |
281
|
|
|
return |
282
|
|
|
|
283
|
|
|
if head: |
284
|
|
|
start = '\n' |
285
|
|
|
end = '\n\n' |
286
|
|
|
|
287
|
|
|
if log.getEffectiveLevel() < logging.WARNING: |
288
|
|
|
log.info(text) |
289
|
|
|
else: |
290
|
|
|
formatted = start + text + end |
291
|
|
|
if PY2: |
292
|
|
|
formatted = formatted.encode('utf-8') |
293
|
|
|
sys.stdout.write(formatted) |
294
|
|
|
sys.stdout.flush() |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
def _(word, is_tty=None, supports_utf8=None, supports_ansi=None): |
298
|
|
|
"""Format and colorize a word based on available encoding.""" |
299
|
|
|
formatted = word |
300
|
|
|
|
301
|
|
|
if is_tty is None: |
302
|
|
|
is_tty = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
303
|
|
|
if supports_utf8 is None: |
304
|
|
|
supports_utf8 = sys.stdout.encoding == 'UTF-8' |
305
|
|
|
if supports_ansi is None: |
306
|
|
|
supports_ansi = sys.platform != 'win32' or 'ANSICON' in os.environ |
307
|
|
|
|
308
|
|
|
style_support = supports_utf8 |
309
|
|
|
color_support = is_tty and supports_ansi |
310
|
|
|
|
311
|
|
|
if style_support: |
312
|
|
|
formatted = STYLE.get(word, word) |
313
|
|
|
|
314
|
|
|
if color_support and COLOR.get(word): |
315
|
|
|
formatted = COLOR[word] + formatted + COLOR[None] |
316
|
|
|
|
317
|
|
|
return formatted |
318
|
|
|
|
319
|
|
|
|
320
|
|
|
if __name__ == '__main__': # pragma: no cover |
321
|
|
|
main() |
322
|
|
|
|