Gitlab Community Edition Instance
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cdstar
pycdstar3
Commits
c464f9a8
Commit
c464f9a8
authored
Oct 28, 2019
by
mhellka
Browse files
Use ExitStack instead of nested try-finally
parent
463e64fe
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/pycdstar3/cli/commands/put.py
View file @
c464f9a8
...
...
@@ -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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment