-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
211 lines (172 loc) · 5.54 KB
/
utils.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
import os
import re
from textwrap import dedent
## Compiled regular expression pattern
## setup.py
RE_DEV_STATUS = re.compile(r"(.*)('Development Status ::.*')(.*)")
RE_INVENIO_DEV_DEP = re.compile(r"(?P<prefix>.*)(?P<invenio>invenio-.*)(?P<ver>1.0.0)(?P<devver>\.?([ab]|dev)+[0-9]+)(?P<suffix>.*)")
RE_INVENIO_SEARCH_DEV_DEP = re.compile(r"(invenio_)(.*)( = )(.*)(1.0.0)([ab]+[0-9]{1,2})(.*)")
## version.py
RE_INVENIO_VERSION_PY = re.compile(r"(__version__)( = )(.*)(1.0.0)(.*)")
## README.rst
RE_README_TAG_BADGE = re.compile(r"(.*)(img.shields.io/pypi/dm/)(.*)")
RE_README_TAG_URL = re.compile(r"(.*)(pypi.python.org/pypi/)(.*)")
## MANIFEST.in
RE_MANIFEST_LINE = re.compile(r"(?P<prefix>^(recursive-)?(in)?(ex)?clude )(?P<files>.*)")
# Constants
TROVE_DEV_STATUS = "Development Status :: 5 - Production/Stable"
def filter_text(text, fn):
return '\n'.join(filter(fn, text.split('\n')))
def map_text(text, fn):
return '\n'.join(map(fn, text.split('\n')))
def remove_setup_py_pypi_classifier(text):
def pypy_trove(line):
return 'Implementation :: PyPy' not in line
return filter_text(text, pypy_trove)
def update_setup_py_development_status(text):
def dev_status(line):
m = re.match(RE_DEV_STATUS, line)
if m:
return "{0}'{trove}'{2}".format(*m.groups(), trove=TROVE_DEV_STATUS)
return line
return map_text(text, dev_status)
def update_setup_py_invenio_dev_deps(text):
def dev_dep(line):
m = re.match(RE_INVENIO_DEV_DEP, line)
if m:
# Skip the 'devver' groups corresponding to 'a3' 'b3' '.dev'
return "{prefix}{invenio}{ver}{suffix}".format(**m.groupdict())
return line
def dev_dep_search(line):
m = re.match(RE_INVENIO_SEARCH_DEV_DEP, line)
if m:
return "{0}{1}{2}{3}{4}{6}".format(*m.groups()) # Skip the 'a3' 'b3', 'a13' etc.
return line
text = map_text(text, dev_dep)
text = map_text(text, dev_dep_search)
return text
def update_setup_py(text):
functs = [
remove_setup_py_pypi_classifier,
update_setup_py_development_status,
update_setup_py_invenio_dev_deps,
]
for fn in functs:
text = fn(text)
return text
def remove_readme_rst_badges(text):
matching = [
'shields.io/github/tag',
'shields.io/github/license',
]
cnt = 0
new_out = []
for line in text.split('\n'):
if any(m in line for m in matching):
cnt = 3 # skip next 3 lines
if cnt == 0:
new_out.append(line)
else:
cnt -= 1
return "\n".join(new_out)
def change_readme_rst_tag_badge(text):
def badge_img(line):
m = re.match(RE_README_TAG_BADGE, line)
if m:
return "{0}img.shields.io/pypi/v/{2}".format(*m.groups())
return line
def badge_target(line):
m = re.match(RE_README_TAG_URL, line)
if m:
return "{0}pypi.org/pypi/{2}".format(*m.groups()) # Skip the 'a3' 'b3', 'a13' etc.
return line
text = map_text(text, badge_img)
text = map_text(text, badge_target)
return text
def update_readme_rst(text):
functs = [
remove_readme_rst_badges,
change_readme_rst_tag_badge,
]
for fn in functs:
text = fn(text)
return text
def remove_pypy_from_travis_yml(text):
"Remove pypy dependencies from travis.yml file."
def pypy_trove(line):
return '- "pypy"' not in line
matches = [
(
("matrix:\n"
" fast_finish: true\n"
" allow_failures:\n"
" - python: pypy\n"
"\n"),
("matrix:\n"
" fast_finish: true\n"
"\n")
),
(
("matrix:\n"
" allow_failures:\n"
" - python: pypy\n"
" include:\n"
" - python: pypy\n"
" env:"
),
("matrix:\n"
" include:\n"
" env:"),
),
(
("matrix:\n"
" fast_finish: true\n"
" allow_failures:\n"
" - python: pypy\n"
" include:\n"
" - python: pypy\n"
" env:"
),
("matrix:\n"
" fast_finish: true\n"
" include:\n"
" env:"),
)
]
text = filter_text(text, pypy_trove)
for old, new in matches:
text = text.replace(old, new)
return text
def change_version_py(text):
"Change the version to '1.0.0' in every version.py file"
new_text = []
for line in text.split('\n'):
m = re.search(RE_INVENIO_VERSION_PY, line)
if m:
new_text.append("__version__ = '1.0.0'")
else:
new_text.append(line)
return '\n'.join(new_text)
def update_manifest_in(text):
"""Remove some files from the manifest and the repository/"""
files_to_delete = set([
'.lgtm',
'MAINTAINERS',
'RELEASE-NOTES.rst'
])
for file in files_to_delete:
try:
os.remove(file)
except FileNotFoundError:
pass
new_text = []
for line in text.split('\n'):
m = re.search(RE_MANIFEST_LINE, line)
if m:
files = m.group('files').split(' ')
result = [file for file in files if file not in files_to_delete]
if result:
new_text.append(m.group('prefix') + ' '.join(result))
else:
new_text.append(line)
return '\n'.join(new_text)