-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrever.xsh
374 lines (300 loc) · 8.46 KB
/
rever.xsh
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Standard library imports
import ast
import os
# Third party imports
from rever.activity import activity
from rever.tools import replace_in_file
$ACTIVITIES = [
'checkout',
'clean_repo',
'update_repo',
'install_deps',
'format_code',
'update_release_version',
'create_python_distributions',
'create_npm_distributions',
'upload_test_distributions',
'install_test_distributions',
'run_tests',
'authors',
'commit_release_version',
'add_tag',
'upload_python_distributions',
'upload_npm_distributions',
'update_dev_version',
'commit_dev_version',
'push',
]
$PROJECT = "ibis-vega-transform"
$MODULE = "ibis_vega_transform"
$GITHUB_ORG = 'Quansight'
$GITHUB_REPO = $PROJECT
$VERSION_BUMP_PATTERNS = [
# These note where/how to find the version numbers
($MODULE + '/__init__.py', r'__version__\s*=.*', '__version__ = "$VERSION"'),
('package.json', r'"version":\s.*', '"version": "$VERSION",'),
]
$AUTHORS_FILENAME = "AUTHORS.md"
$AUTHORS_TEMPLATE = """
The $PROJECT project has some great contributors! They are:
{authors}
These have been sorted {sorting_text}.
"""
$AUTHORS_FORMAT= "- [{name}](https://github.com/{github})\n"
$AUTHORS_SORTBY = "alpha"
$TEMP_ENV = 'tmp-' + $PROJECT
$CONDA_ACTIVATE_SCRIPT = 'activate.xsh'
$HERE = os.path.abspath(os.path.dirname(__file__))
# --- Helpers
# ----------------------------------------------------------------------------
def get_version(version_type, module=$MODULE):
"""
Get version info. Tuple with three items, major.minor.patch
"""
with open(os.path.join($HERE, module, "__init__.py")) as fh:
data = fh.read()
major, minor, patch = 'MAJOR', 'MINOR', 'PATCH'
lines = data.split("\n")
for line in lines:
if line.startswith("__version__"):
version = ast.literal_eval(line.split("=")[-1].strip())
major, minor, patch = [int(v) for v in version.split('.')[:3]]
version_type = version_type.lower()
if version_type == 'major':
major += 1
minor = 0
patch = 0
elif version_type == 'minor':
minor += 1
patch = 0
elif version_type == 'patch':
patch += 1
elif version_type in ['check', 'setup']:
pass
elif len(version_type.split('.')) == 3:
major, minor, patch = version_type.split('.')
else:
raise Exception('Invalid option! Must provide version type: [major|minor|patch]')
major = str(major)
minor = str(minor)
patch = str(patch)
version = '.'.join([major, minor, patch])
if version_type not in ['check', 'setup']:
print('\n\nReleasing version {}\n\n'.format(version))
return version
# Actual versions to use
$NEW_VERSION = get_version($VERSION)
$DEV_VERSION = $NEW_VERSION + '.dev0'
$DEV_NPM_VERSION = $NEW_VERSION + '-dev.0'
def activate(env_name):
"""
Activate a conda environment.
"""
if not os.path.isfile($CONDA_ACTIVATE_SCRIPT):
with open('activate.xsh', 'w') as fh:
fh.write($(conda shell.xonsh hook))
# Activate environment
source activate.xsh
conda activate @(env_name)
$[conda info]
def update_version(python_version, npm_version=None):
"""
Update version patterns.
"""
if npm_version is None:
npm_version = python_version
for fpath, pattern, new_pattern in $VERSION_BUMP_PATTERNS:
if 'package.json' in fpath:
new_pattern = new_pattern.replace('$VERSION', npm_version)
elif '__init__.py' in fpath:
new_pattern = new_pattern.replace('$VERSION', python_version)
replace_in_file(pattern, new_pattern, fpath)
# --- Activities
# ----------------------------------------------------------------------------
@activity
def checkout(branch='master'):
"""
Checkout master branch.
"""
git checkout @(branch)
@activity
def clean_repo():
"""
Clean the repo from build/dist and other files.
"""
import pathlib
# Remove python files
for p in pathlib.Path('.').rglob('*.py[co]'):
p.unlink()
for p in pathlib.Path('.').rglob('__pycache__'):
p.rmdir()
rm -rf .pytest_cache/
rm -rf build/
rm -rf dist/
rm -rf examples/.ipynb_checkpoints/
rm -rf examples/population.db
rm -rf activate.xsh
rm -rf out.tgz
rm -rf $MODULE.egg-info
rm -rf lib/
rm -rf node_modules/
rm -rf tsconfig.tsbuildinfo
rm -rf yarn.lock
# Delete files not tracked by git?
# git clean -xfd
@activity
def update_repo(branch='master'):
"""
Stash any current changes and ensure you have the latest version from origin.
"""
git stash
git pull origin @(branch)
@activity
def install_deps():
"""
Install release and test dependencies.
"""
try:
conda env remove --name $TEMP_ENV --yes
except:
pass
conda env create --file binder/environment.yml --name $TEMP_ENV
conda install --name $TEMP_ENV black twine yarn wheel --channel conda-forge --yes --quiet
activate($TEMP_ENV)
jlpm
@activity
def format_code():
"""
Create distributions.
"""
activate($TEMP_ENV)
black ibis_vega_transform
jlpm run prettier
@activity
def update_release_version():
"""
Update version in `__init__.py` (set release version, remove 'dev0').
and on the package.json file.
"""
update_version($NEW_VERSION)
@activity
def create_python_distributions():
"""
Create distributions.
"""
activate($TEMP_ENV)
python setup.py sdist bdist_wheel
@activity
def create_npm_distributions():
"""
Create npm distributions.
"""
activate($TEMP_ENV)
yarn run build
yarn pack --filename out.tgz
@activity
def upload_test_distributions():
"""
Upload test distributions.
"""
activate($TEMP_ENV)
# The file might be already there
try:
python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*
except Exception as err:
print(err)
@activity
def install_test_distributions():
"""
Upload test distributions.
"""
activate($TEMP_ENV)
# Python package
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple $PROJECT==$NEW_VERSION
# Npm package
jupyter labextension install out.tgz
@activity
def run_tests():
"""
Run tests before cleaning repository.
"""
# Python tests
# pytest $MODULE
# Npm tests
npm test
while True:
result = input(
'\n\nPlease open a new console and type:\n\n'
' conda activate ' + $TEMP_ENV + '\n'
' cd "' + os.path.join($HERE, 'examples') + '"\n'
' jupyter lab\n\n'
'And test that all notebooks work as expected.\n\n'
'Once finished write "ok" and <enter>.\nIf notebooks are not '
'working as expected write "cancel" and <enter>:\n\n'
)
if result == 'ok':
git stash
break
elif result == 'cancel':
git stash
clean_repo()
rm -rf rever/
raise Exception('The package cannot be released yet!')
@activity
def commit_release_version():
"""
Commit release version.
"""
git add .
git commit -m @('Set release version to ' + $NEW_VERSION + ' [ci skip]') --no-verify
@activity
def add_tag():
"""
Add release tag.
"""
# TODO: Add check to see if tag already exists?
git tag -a @('v' + $NEW_VERSION) -m @('Tag version ' + $NEW_VERSION + ' [ci skip]')
@activity
def upload_python_distributions():
"""
Upload the distributions to pypi production environment.
"""
activate($TEMP_ENV)
# The file might be already there
try:
twine upload dist/*
except Exception as err:
print(err)
@activity
def upload_npm_distributions():
"""
Upload the distributions to npm.
"""
activate($TEMP_ENV)
# Ask for credentials
npm login
# The file might be already there
try:
npm publish out.tgz
except Exception as err:
print(err)
@activity
def update_dev_version():
"""
Update `__init__.py` (add 'dev0' and increment minor).
"""
update_version(python_version=$DEV_VERSION, npm_version=$DEV_NPM_VERSION)
@activity
def commit_dev_version():
""""
Commit dev changes.
"""
git add .
git commit -m "Restore dev version [ci skip]" --no-verify
@activity
def push(branch='master'):
"""
Push changes.
"""
git push origin @(branch)
git push --tags