def create_adhoc_commit(project):
"""
Create an ad-hoc tarball and commit of the project directory.
:param project: Project
:type project: valohai_cli.models.project.Project
:return: Commit response object from API
:rtype: dict[str, object]
"""
tarball = None
try:
click.echo('Packaging {dir}...'.format(dir=project.directory))
tarball = package_directory(project.directory, progress=True)
# TODO: We could check whether the commit is known already
size = os.stat(tarball).st_size
click.echo('Uploading {size:.2f} KiB...'.format(size=size / 1024.))
upload = MultipartEncoder({'data': ('data.tgz', open(tarball, 'rb'), 'application/gzip')})
prog = click.progressbar(length=upload.len, width=0)
prog.is_hidden = (size < 524288) # Don't bother with the bar if the upload is small
with prog:
def callback(upload):
prog.pos = upload.bytes_read
prog.update(0) # Step is 0 because we set pos above
monitor = MultipartEncoderMonitor(upload, callback)
resp = request(
'post',
'/api/v0/projects/{id}/import-package/'.format(id=project.id),
data=monitor,
headers={'Content-Type': monitor.content_type},
).json()
success('Uploaded ad-hoc code {identifier}'.format(identifier=resp['identifier']))
finally:
if tarball:
os.unlink(tarball)
return resp
评论列表
文章目录