-
Notifications
You must be signed in to change notification settings - Fork 7
/
fabfile.py
211 lines (152 loc) · 4.82 KB
/
fabfile.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 importlib
import os
import sys
from fabric import task
import version
# this must be before importing from secrets.
sys.path = ["."] + sys.path
from secrets import pypi_auth
# Initialise project directory and name
project_dir = os.path.abspath(os.path.dirname(__file__))
project_name = os.path.basename(project_dir)
# Change directory to directory containing this script
os.chdir(project_dir)
def local(ctx, *args, **kwargs):
s = "Executing: {} {}".format(args, kwargs)
if len(s) > 70:
s = s[:70] + f".. ({len(s[70:])})"
print(s)
# http://docs.pyinvoke.org/en/stable/api/runners.html#invoke.runners.Runner.run
with ctx.prefix("PATH={}".format(os.environ["PATH"])):
with ctx.prefix("source ~/.zshrc"):
with ctx.prefix("pyenv activate pynb"):
return ctx.run(*args, **kwargs, pty=True, shell="/bin/zsh")
@task
def test_pep8(ctx):
"""
Run only pep8 test
:return:
"""
local(ctx, 'py.test tests/test_pep8.py')
@task
def test(ctx):
"""
Run all tests
:param params: parameters to py.test
:return:
"""
local(ctx, f'pytest')
@task
def test_sx(ctx):
"""
Run all tests
:param params: parameters to py.test
:return:
"""
local(ctx, f'py.test -sx tests')
@task
def test_single(ctx, f):
"""
Run all tests
:param params: parameters to py.test
:return:
"""
local(ctx, f'py.test -sx {f}')
@task
def fix_pep8(ctx):
"""
Fix a few common and easy-to-fix PEP8 mistakes
:return:
"""
local(ctx,
'autopep8 --select E265,E225,E302,E222,E251,E303,W293,W291,W391 --aggressive --in-place --recursive .')
def inc_version():
"""
Increment micro release version (in 'major.minor.micro') in version.py and re-import it.
Major and minor versions must be incremented manually in version.py.
:return: list with current version numbers, e.g., [0,1,23].
"""
new_version = version.__version__
values = list(map(lambda x: int(x), new_version.split('.')))
values[2] += 1
with open("version.py", "w") as f:
f.write(f'__version__ = "{values[0]}.{values[1]}.{values[2]}"\n')
f.write(f'__pkgname__ = "{project_name}"\n')
with open(f"{project_name}/version.py", "w") as f:
f.write(f'__version__ = "{values[0]}.{values[1]}.{values[2]}"\n')
f.write(f'__pkgname__ = "{project_name}"\n')
importlib.reload(version)
print(f'Package {version.__pkgname__} current version: {version.__version__}')
return values
@task
def git_check(ctx):
"""
Check that all changes , besides versioning files, are committed
:return:
"""
# check that changes staged for commit are pushed to origin
output = local(ctx, f'git diff --name-only | egrep -v "^({project_name}/version.py)|(version.py)$" | tr "\\n" " "',
hide=True).stdout.strip()
if output:
fatal('Stage for commit and commit all changes first: {}'.format(output))
output = local(ctx,
f'git diff --cached --name-only | egrep -v "^({project_name}/version.py)|(version.py)$" | tr "\\n" " "',
hide=True).stdout.strip()
if output:
fatal('Commit all changes first: {}'.format(output))
def fatal(msg):
print("Fatal error: {}; exiting.".format(msg))
sys.exit(1)
def git_push(ctx):
"""
Push new version and corresponding tag to origin
:return:
"""
# get current version
new_version = version.__version__
values = list(map(lambda x: int(x), new_version.split('.')))
# Push to origin new version and corresponding tag:
# * commit new version
# * create tag
# * push version,tag to origin
local(ctx, f'git add {project_name}/version.py version.py')
local(ctx, 'git commit -m "updated version"')
local(ctx, f'git tag {values[0]}.{values[1]}.{values[2]}')
local(ctx, 'git push origin --tags')
local(ctx, 'git push')
@task
def release(ctx):
"""
Release new package version to pypi
:return:
"""
# Check that all changes are committed before creating a new version
git_check(ctx)
# Test package
test(ctx)
# Increment version
inc_version()
# Commit new version, create tag for version and push everything to origin
git_push(ctx)
# Build and publish package
pkgbuild(ctx)
pkgupload(ctx)
# Remove temporary files
clean(ctx)
@task
def pkgupload(ctx):
pathname = f'dist/{project_name}-{version.__version__}.tar.gz'
local(ctx, 'twine upload -u "{}" -p "{}" "{}"'.format(pypi_auth["user"], pypi_auth["pass"], pathname))
@task
def pkgbuild(ctx):
"""
Build package in docker container
:return:
"""
local(ctx, 'python setup.py sdist bdist_wheel')
@task
def clean(ctx):
"""
Rempove temporary files
"""
local(ctx, 'rm -rf .cache .eggs build dist')