-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
276 lines (240 loc) · 8.75 KB
/
__main__.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# pylint: disable=invalid-name
# pylint: disable=misplaced-comparison-constant
# pylint: disable=wrong-import-position
"""
This file provides a procedural script to generate shortcodes for all available
versions of pylint
"""
import os
import re
import shutil
import subprocess
import time
import htmlmin
from jinja2 import Environment, FileSystemLoader
import virtualenv
ROOT_DIR = os.path.join(
os.getcwd(),
os.path.dirname(__file__)
)
VIRTUALENV_DIR = os.path.join(ROOT_DIR, '.venv')
virtualenv.create_environment(VIRTUALENV_DIR)
if 'nt' == os.name:
activate_path = os.path.join(VIRTUALENV_DIR, "Scripts", "activate_this.py")
else:
activate_path = os.path.join(VIRTUALENV_DIR, "bin", "activate_this.py")
execfile(activate_path, dict(__file__=activate_path))
PIP_PATH = os.path.join(VIRTUALENV_DIR, 'bin', 'pip')
PYLINT_PATH = os.path.join(VIRTUALENV_DIR, 'bin', 'pylint')
VERSION_NUMBER_LIST = os.path.join(ROOT_DIR, 'version_numbers')
WORKING_FILES_DIR = os.path.join(ROOT_DIR, 'raw')
if os.path.isdir(WORKING_FILES_DIR):
shutil.rmtree(WORKING_FILES_DIR)
os.makedirs(WORKING_FILES_DIR)
VERSION_FILES_DIR = os.path.join(ROOT_DIR, 'dist', 'versions')
if os.path.isdir(VERSION_FILES_DIR):
shutil.rmtree(VERSION_FILES_DIR)
os.makedirs(VERSION_FILES_DIR)
TEMPLATE_DIR = os.path.join(ROOT_DIR, 'templates')
JINJA_ENV = Environment(
loader=FileSystemLoader(TEMPLATE_DIR)
)
SHORTCODE_PATTERN = re.compile(r'''
\s* # include potential initial space
\: # begins with a colon
(?P<wordy> # descriptive-name-of-code
[\w\-]+ # is word characters and dash
)
\s+ # spacing
\( # open bracket
(?P<number> # code number
\w+ # letter and some digits
)
\) # close bracket
: # descriptor ends
\s+ # spacing
\* # parsed message wrapped in asterisks
(?P<message> # parsed message
[^*]+ # everything until the next asterisk
)
\* # close parsed message
\s+ # spacing including newlines
(?P<details> # pull the full detail
( # capture single character
[\s\S] # can be anything
(?<!\n\:) # so long as newline colon is not following
)+ # repeat greedily
)
\s+ # include final space
''', re.VERBOSE)
MAX_VERSION_LIST_AGE = 60 * 60 * 24 * 7
MAX_SHORTCODE_FILE_AGE = 60 * 60 * 24 * 7 * 30
def get_cached_version_numbers():
"""Loads the version_numbers cache file"""
with open(VERSION_NUMBER_LIST, 'r+') as cache_file:
version_numbers = cache_file.read().strip().split('\n')
return version_numbers
def check_list_cache_age():
"""Checks the age of the included version list"""
try:
assert (
os.path.getmtime(VERSION_NUMBER_LIST)
>=
(int(time.time()) - MAX_VERSION_LIST_AGE)
)
except (AssertionError, OSError):
return get_fresh_version_numbers()
else:
return get_cached_version_numbers()
def get_fresh_version_numbers():
"""Gets a full list of pylint versions"""
print 'Installing base pylint to get version list'
version_results = subprocess.check_output(
[
PIP_PATH,
'install',
'--upgrade',
'-v',
'pylint'
]
)
print 'Finished installing; parsing log'
version_numbers = list()
for version_line_match in re.finditer(
r'^\s*Using.+?versions:(?P<version_list>.+)\)$',
version_results, re.MULTILINE
):
for version_number_match in re.finditer(
r'(?P<full_semver>(\d+\.?)+)',
version_line_match.group('version_list')
):
version_numbers.append(
version_number_match.group('full_semver')
)
version_numbers = sorted(set(version_numbers))[::-1]
with open(VERSION_NUMBER_LIST, 'w+') as cache_file:
for version_number in version_numbers:
cache_file.write(version_number + '\n')
print "Finished parsing; discovered these versions: %s" % (', '.join(version_numbers))
# remove_pylint()
return version_numbers
def install_specific_pylint_version(version_number):
"""Installs a specific version of pylint"""
print "Installing pylint==%s" % (version_number)
# print ' '.join([
# PIP_PATH,
# 'install',
# '--quiet',
# "pylint==%s" % (version_number)
# ])
print subprocess.check_output(
[
PIP_PATH,
'install',
'--quiet',
"pylint==%s" % (version_number)
]
)
print "Finished installing %s" % (version_number)
def convert_version_number_to_raw_filename(version_number):
"""Simplifies finding a version number's raw output"""
return os.path.join(
WORKING_FILES_DIR,
("raw_%s.txt" % (version_number.replace('.', '_')))
)
def get_version_shortcodes(version_number):
"""Attempts to run the --list-msgs command"""
print "Attempting to find shortcodes for %s" % (version_number)
try:
shortcodes = subprocess.check_output([PYLINT_PATH, '--list-msgs'])
except subprocess.CalledProcessError:
print(
'ERROR: pylint --list-msgs raised an error; '
'writing nothing to its output'
)
shortcodes = 'NOPE'
with open(
convert_version_number_to_raw_filename(version_number),
'w+'
) as raw_output:
raw_output.write(shortcodes)
return shortcodes
def parse_raw_shortcodes(version_number, raw_shortcodes): # pylint: disable=unused-argument
"""Parses the full list of shortcodes"""
list_of_shortcodes = list()
for shortcode_match in re.finditer(SHORTCODE_PATTERN, raw_shortcodes):
shortcode = type('', (), {})()
for key in ['wordy', 'number', 'message', 'details']:
setattr(
shortcode,
key,
re.sub(r'\s+', ' ', shortcode_match.group(key))
)
list_of_shortcodes.append(shortcode)
return list_of_shortcodes
def parse_shortcodes_from_working_files(version_number):
"""Loads the shortcodes from the working directory"""
with open(
convert_version_number_to_raw_filename(version_number),
'r+'
) as raw_file:
raw_shortcodes = raw_file.read()
return parse_raw_shortcodes(version_number, raw_shortcodes)
def convert_version_number_to_out_filename(version_number):
"""Converts the version number to its out filename"""
return (
os.path.join(
VERSION_FILES_DIR,
"%s.html" % (version_number.replace('.', '_'))
)
)
def compile_version_template(version_number, shortcodes):
"""Compiles the finishes template per version number"""
template = JINJA_ENV.get_template('version.html.j2')
rendered_template = template.render(
version_number=version_number,
shortcodes=shortcodes
)
minified = htmlmin.minify(rendered_template)
with open(
convert_version_number_to_out_filename(version_number),
'w+'
) as final_page:
final_page.write(minified)
def parse_a_version(version_number):
"""Runs all the necessary methods for a specific version number"""
print "Parsing version %s" % (version_number)
install_specific_pylint_version(version_number)
raw_shortcodes = get_version_shortcodes(version_number)
# cleaned_shortcodes = parse_shortcodes_from_working_files(version_number)
cleaned_shortcodes = parse_raw_shortcodes(version_number, raw_shortcodes)
compile_version_template(version_number, cleaned_shortcodes)
def compile_index_template(version_numbers):
"""Compiles the index"""
template = JINJA_ENV.get_template('index.html.j2')
rendered_template = template.render(
version_numbers=version_numbers
)
with open(
os.path.join(VERSION_FILES_DIR, '..', 'index.html'),
'w+'
) as final_page:
final_page.write(rendered_template)
def commit_and_push():
"""Commits changes and pushes the repo"""
subprocess.check_output(['git', 'add', '--all'])
subprocess.check_output(['git', 'commit', '-m', 'script update'])
subprocess.check_output(['git', 'push'])
subprocess.check_output(
['git', 'subtree', 'split', '--prefix', 'dist', '-b', 'gh-pages'])
subprocess.check_output(
['git', 'push', '-f', 'origin', 'gh-pages:gh-pages'])
subprocess.check_output(['git', 'branch', '-D', 'gh-pages'])
def cli_runner():
"""Runs the functions in the proper order"""
version_numbers = get_fresh_version_numbers()
for version_number in version_numbers:
parse_a_version(version_number)
compile_index_template(version_numbers)
commit_and_push()
cli_runner()