Gitlab Community Edition Instance

Commit c464f9a8 authored by mhellka's avatar mhellka
Browse files

Use ExitStack instead of nested try-finally

parent 463e64fe
......@@ -14,6 +14,7 @@ you can simply re-run the same command.
"""
import os
from contextlib import ExitStack
from pycdstar3 import FormUpdate, ApiError
from pycdstar3.cli import CliError
......@@ -135,7 +136,9 @@ def command(ctx, args): # noqa: C901
for key, values in acl.items():
form.acl(key, *values)
with client.begin(autocommit=True):
with ExitStack() as stack:
stack.enter_context(client.begin(autocommit=True))
if archive == 'new':
archive = client.create_archive(vault, form=form)['id']
ctx.print("Created new archive: /{}/{} ", vault, archive)
......@@ -153,31 +156,28 @@ def command(ctx, args): # noqa: C901
total = sum(stat.st_size for (file, stat) in uploads.values())
pbar = tqdm(total=total, unit='b', unit_scale=True, unit_divisor=1024, dynamic_ncols=True,
file=ctx.print.file)
try:
for i, target in enumerate(sorted(uploads)):
file, stat = uploads[target]
with open(file, 'rb') as fp:
line = "[{}/{}] {} ({})".format(i + 1, len(uploads), target, hbytes(stat.st_size))
if pbar:
pbar.write(line)
read = fp.read
chunks = iter(lambda: read(1024 * 64), b'')
chunks = (chunk for chunk in chunks if not pbar.update(len(chunk)))
fp = chunks
else:
ctx.print(line)
try:
client.put_file(vault, archive, target, fp, replace=force)
except ApiError as e:
if e.status == 412:
# TODO: Make more specific as soon as CDSTAR returns a proper error code.
ctx.print.warn("Upload failed (file exists): {}", target)
continue
raise
finally:
if pbar:
pbar.close()
stack.enter_context(pbar)
for i, target in enumerate(sorted(uploads)):
file, stat = uploads[target]
with open(file, 'rb') as fp:
line = "[{}/{}] {} ({})".format(i + 1, len(uploads), target, hbytes(stat.st_size))
if pbar:
pbar.write(line)
read = fp.read
chunks = iter(lambda: read(1024 * 64), b'')
chunks = (chunk for chunk in chunks if not pbar.update(len(chunk)))
fp = chunks
else:
ctx.print(line)
try:
client.put_file(vault, archive, target, fp, replace=force)
except ApiError as e:
if e.status == 412:
# TODO: Make more specific as soon as CDSTAR returns a proper error code.
ctx.print.warn("Upload failed (file exists): {}", target)
continue
raise
ctx.print("Done! Uploaded {} files ({}) to archive: /{}/{}", len(uploads), hbytes(total), vault, archive)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment