Compress binaries before uploading

This commit is contained in:
Gabriel Augendre 2021-08-24 11:16:59 +02:00
parent 4773950a65
commit 339580e36f

View file

@ -31,7 +31,7 @@ def test(context):
"""Run tests""" """Run tests"""
context: Context context: Context
with context.cd(BASE_DIR): with context.cd(BASE_DIR):
context.run(f"go test ./... -race -bench .", echo=True) context.run(f"go test ./... -race .", echo=True)
@task @task
@ -40,12 +40,13 @@ def clean(context):
context.run(f"rm -rf {DIST_DIR}", echo=True) context.run(f"rm -rf {DIST_DIR}", echo=True)
@task(pre=[test], post=[clean]) @task(pre=[clean, test], post=[clean])
def release(context, version_name): def release(context, version_name):
"""Create & push git tag + build binaries""" """Create & push git tag + build binaries"""
tag(context, version_name) tag(context, version_name)
binaries = build(context, version_name) binaries = build(context, version_name)
upload(context, version_name, binaries) archives = compress(context, binaries)
upload(context, version_name, archives)
@task(pre=[test]) @task(pre=[test])
@ -80,7 +81,27 @@ def build(context, version_name):
@task @task
def upload(ctx, version_name, binaries): def compress(context, binaries):
"""Cross-platform build"""
archives = []
with ThreadPoolExecutor() as pool:
for binary in binaries:
binary_name = binary.name
archive_path = DIST_DIR / f"{binary_name}.tar.gz"
archives.append(archive_path)
pool.submit(_compress_single_binary, context, archive_path, binary_name)
return archives
def _compress_single_binary(context, archive_path, binary_name):
with context.cd(DIST_DIR):
context.run(
f"tar czf {archive_path} {binary_name} && rm {binary_name}", echo=True
)
@task
def upload(ctx, version_name, upload_files):
context: Context context: Context
version_name = fix_version_name(version_name) version_name = fix_version_name(version_name)
session = requests.Session() session = requests.Session()
@ -97,17 +118,17 @@ def upload(ctx, version_name, binaries):
print(f"The draft release has been created at {html_url}") print(f"The draft release has been created at {html_url}")
api_url = resp.get("url") + "/assets" api_url = resp.get("url") + "/assets"
with ThreadPoolExecutor() as pool: with ThreadPoolExecutor() as pool:
for binary in binaries: for upload_file in upload_files:
pool.submit(post_attachment, api_url, binary, session) pool.submit(post_attachment, api_url, upload_file, session)
print(f"All uploads are finished. Update & publish your draft: {html_url}") print(f"All uploads are finished. Update & publish your draft: {html_url}")
def post_attachment(api_url, binary, session): def post_attachment(api_url, upload_file, session):
binary = Path(binary) upload_file = Path(upload_file)
name = binary.name name = upload_file.name
url = api_url + f"?name={name}" url = api_url + f"?name={name}"
print(f"Uploading {name}...") print(f"Uploading {name}...")
with open(binary, "rb") as f: with open(upload_file, "rb") as f:
res = session.post(url, files={"attachment": f}) res = session.post(url, files={"attachment": f})
status_code = res.status_code status_code = res.status_code
if status_code != 201: if status_code != 201: