-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtasks.py
318 lines (253 loc) · 9.43 KB
/
tasks.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
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
#!/usr/bin/env python3.8
"""Tasks file used by the *invoke* command.
This simplifies some common development tasks.
Run these tasks with the `invoke` tool.
"""
from __future__ import annotations
import sys
import os
import shutil
import getpass
from glob import glob
from shutil import copy2
import semver
import keyring
from invoke import task, run, Exit
# local environment
LIBC = os.confstr('CS_GNU_LIBC_VERSION').replace(" ", "")
LIBSSH2_REMOTE = "https://github.com/kdart/libssh2.git"
# local user account name
SIGNERS = ["keithdart", "keith"]
CURRENT_USER = getpass.getuser()
PYTHONBIN = os.environ.get("PYTHONBIN", sys.executable)
# Put the path in quotes in case there is a space in it.
PYTHONBIN = f'"{PYTHONBIN}"'
GPG = "gpg2"
# Package repo location. Putting info here eliminates the need for user-private ~/.pypirc file.
# You can also set them in your shell environment.
REPO_HOST = os.environ.get("PYPI_REPO_HOST", "upload.pypi.org")
REPOSITORY_URL = os.environ.get("PYPI_REPOSITORY_URL", f"https://{REPO_HOST}/legacy/")
REPO_USERNAME = os.environ.get("PYPI_REPO_USERNAME", "__token__")
@task
def info(ctx):
"""Show information about the current Python and environment."""
suffix = get_suffix()
version = ctx.run(f"{PYTHONBIN} setup.py --version", hide="out").stdout.strip()
print(f"Project version: {version}")
print(f"Python being used: {PYTHONBIN}")
print(f"Python extension suffix: {suffix}")
print(f"repo URL: {REPOSITORY_URL}")
@task
def build(ctx):
"""Build the intermediate package components."""
ctx.run(f"{PYTHONBIN} setup.py build")
@task
def dev_requirements(ctx):
"""Install development requirements."""
user = "" if os.environ.get("VIRTUAL_ENV") else "--user"
ctx.run(f"{PYTHONBIN} -m pip install -r requirements_dev.txt {user}")
@task
def clean(ctx):
"""Clean out build and cache files. Remove built extension modules."""
ctx.run(f"{PYTHONBIN} setup.py clean")
ctx.run(r"find . -depth -type d -name __pycache__ -exec rm -rf {} \;")
ctx.run('find ssh2 -name "*.so*" -delete')
if os.path.isdir("build"):
shutil.rmtree("build", ignore_errors=True)
with ctx.cd("doc"):
ctx.run(f"{PYTHONBIN} -m sphinx.cmd.build -M clean . _build")
@task
def cleandist(ctx):
"""Clean out dist subdirectory."""
if os.path.isdir("dist"):
shutil.rmtree("dist", ignore_errors=True)
os.mkdir("dist")
if os.path.isdir("wheelhouse"):
shutil.rmtree("wheelhouse", ignore_errors=True)
@task
def test(ctx, testfile=None, ls=False):
"""Run unit tests. Use ls option to only list them."""
if ls:
ctx.run(f"{PYTHONBIN} -m pytest --collect-only -qq tests")
elif testfile:
ctx.run(f"{PYTHONBIN} -m pytest -s {testfile}")
else:
ctx.run(f"{PYTHONBIN} -m pytest tests", hide=False, in_stream=False)
@task(cleandist)
def sdist(ctx):
"""Build source distribution."""
ctx.run(f"{PYTHONBIN} setup.py sdist")
@task
def build_ext(ctx):
"""Build compiled extension modules, in place."""
ctx.run(f"{PYTHONBIN} setup.py build_ext --inplace")
@task(sdist)
def wheels(ctx):
"""Build standard wheel files, an installable format, for manylinux_2_27_x86_64 platform."""
cwd = os.getcwd()
uid = os.getuid()
gid = os.getgid()
cmd = (f'docker run -it -e PLAT=manylinux_2_27_x86_64 '
f'-e USER_ID={uid} -e GROUP_ID={gid} '
f'--mount type=bind,source={cwd},target=/io '
f'wheel_builder /io/building/build-wheels.sh')
ctx.run(cmd, hide=False, pty=True)
@task
def build_libssh2(ctx):
"""Build the embedded libssh2 library."""
builddir = f"build/{LIBC}"
if not os.path.exists(builddir):
os.makedirs(builddir)
with ctx.cd(builddir):
ctx.run('cmake ../../libssh2 -DBUILD_SHARED_LIBS=ON '
'-DENABLE_ZLIB_COMPRESSION=ON -DENABLE_CRYPT_NONE=ON '
'-DBUILD_EXAMPLES=OFF -DBUILD_TESTING=OFF '
'-DENABLE_MAC_NONE=ON -DCRYPTO_BACKEND=OpenSSL')
ctx.run('cmake --build . --config Release')
os.environ["LD_LIBRARY_PATH"] = os.path.join(os.path.abspath(builddir), "src")
@task
def update_libssh2(ctx):
"""Pull the latest libssh2 from origin."""
ctx.run(f'git subtree pull -P libssh2 "{LIBSSH2_REMOTE}" master --squash')
@task(pre=[build_libssh2, sdist])
def bdist_native(ctx):
"""Build native wheel file."""
cmd = f"{PYTHONBIN} setup.py bdist_wheel"
ctx.run(cmd)
@task(pre=[wheels, bdist_native])
def sign(ctx):
"""Cryptographically sign dist with your default GPG key."""
if CURRENT_USER in SIGNERS:
distfiles = glob("dist/*.tar.gz")
distfiles.extend(glob("dist/*.whl"))
distfiles.extend(glob("wheelhouse/*.whl"))
for distfile in distfiles:
ctx.run(f"{GPG} --detach-sign -a {distfile}")
else:
print("Not signing.")
@task(pre=[sign])
def publish(ctx, wheels=False):
"""Publish built wheel files to package repo."""
token = get_repo_token(REPO_HOST, REPO_USERNAME)
distfiles = glob("dist/*.tar.gz") # source dist
# pypi.org only accepts manylinux wheel builds.
if "pypi.org" in REPOSITORY_URL:
distfiles.extend(glob("wheelhouse/*.whl"))
else:
# add native wheel for non-pypi repos, optionally all wheel builds.
distfiles.extend(glob("dist/*.whl"))
if wheels:
distfiles.extend(glob("wheelhouse/*.whl"))
if not distfiles:
raise Exit("Nothing to publish!")
distfiles = " ".join(distfiles)
ctx.run(f'{PYTHONBIN} -m twine upload --repository-url \"{REPOSITORY_URL}\" '
f'--username {REPO_USERNAME} --password {token} {distfiles}')
@task(pre=[dev_requirements, build_libssh2])
def develop(ctx, uninstall=False):
"""Start developing in developer mode.
That means setting import paths to use this workspace.
"""
user = "" if os.environ.get("VIRTUAL_ENV") else "--user"
if uninstall:
ctx.run(f"{PYTHONBIN} setup.py develop --uninstall {user}")
else:
copy2(os.path.join(os.environ["LD_LIBRARY_PATH"], "libssh2.so.1.0.1"), "ssh2/libssh2.so.1")
ctx.run(f'{PYTHONBIN} setup.py develop {user}')
for shared in glob("ssh2/*.so"):
ctx.run(f"patchelf --set-rpath '$ORIGIN' {shared}")
@task(pre=[clean])
def undevelop(ctx):
"""Stop developing in developer mode.
"""
ctx.run(f"{PYTHONBIN} setup.py develop --uninstall --user")
@task
def docs(ctx):
"""Build the HTML documentation."""
with ctx.cd("doc"):
ctx.run(f"{PYTHONBIN} -m sphinx.cmd.build -M html . _build")
if os.environ.get("DISPLAY"):
ctx.run("xdg-open docs/_build/html/index.html")
@task
def branch(ctx, name=None):
"""start a new branch, both local and remote tracking."""
if name:
ctx.run(f"git checkout -b {name}")
ctx.run(f"git push -u origin {name}")
else:
ctx.run("git --no-pager branch")
@task
def tag(ctx, tag=None, major=False, minor=False, patch=False):
"""Tag or bump release with a semver tag, prefixed with 'v'. Makes a signed tag."""
latest = None
if tag is None:
tags = get_tags()
if not tags:
latest = semver.VersionInfo(0, 0, 0)
else:
latest = tags[-1]
if patch:
nextver = latest.bump_patch()
elif minor:
nextver = latest.bump_minor()
elif major:
nextver = latest.bump_major()
else:
nextver = latest.bump_patch()
else:
if tag.startswith("v"):
tag = tag[1:]
try:
nextver = semver.parse_version_info(tag)
except ValueError:
raise Exit("Invalid semver tag.", 2)
print(latest, "->", nextver)
tagopt = "-s" if CURRENT_USER in SIGNERS else "-a"
ctx.run(f'git tag {tagopt} -m "Release v{nextver}" v{nextver}')
@task
def tag_delete(ctx, tag=None):
"""Delete a tag, both local and remote."""
if tag:
ctx.run(f"git tag -d {tag}")
ctx.run(f"git push origin :refs/tags/{tag}")
@task
def branch_delete(ctx, name=None):
"""Delete local, remote and tracking branch by name."""
if name:
ctx.run(f"git branch -d {name}", warn=True) # delete local branch
ctx.run(f"git branch -d -r origin/{name}", warn=True) # delete local tracking info
ctx.run(f"git push origin --delete {name}", warn=True) # delete remote (origin) branch.
else:
print("Supply a branch name: --name <name>")
@task
def set_token(ctx):
"""Set the password in the local key ring for the pypi account used as the package repo.
"""
pw = getpass.getpass(f"token/password for account on {REPO_HOST} for user {REPO_USERNAME}? ")
if pw:
keyring.set_password(REPO_HOST, REPO_USERNAME, pw)
else:
raise Exit("No token entered.", 3)
# Helper functions follow.
def get_tags():
rv = run('git tag -l "v*"', hide="out")
vilist = []
for line in rv.stdout.split():
try:
vi = semver.parse_version_info(line[1:])
except ValueError:
pass
else:
vilist.append(vi)
vilist.sort()
return vilist
def get_repo_token(host, username):
cred = keyring.get_credential(host, username)
if not cred:
raise Exit(f"You must set the token for {REPO_HOST} first with the set-token task.", 1)
return cred.password
def get_suffix():
return run(
f'{PYTHONBIN} -c \'import sysconfig; print(sysconfig.get_config_vars()["EXT_SUFFIX"])\'',
hide=True,
).stdout.strip() # noqa