Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
storagedriver
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f3a27e66
authored
Jun 25, 2014
by
Guba Sándor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
disk: wrong status codes raise exceptions
parent
cde8c7b2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
60 deletions
+62
-60
disk.py
+62
-60
No files found.
disk.py
View file @
f3a27e66
...
@@ -138,67 +138,69 @@ class Disk(object):
...
@@ -138,67 +138,69 @@ class Disk(object):
disk_path
=
self
.
get_path
()
disk_path
=
self
.
get_path
()
logger
.
info
(
"Downloading image from
%
s to
%
s"
,
url
,
disk_path
)
logger
.
info
(
"Downloading image from
%
s to
%
s"
,
url
,
disk_path
)
r
=
requests
.
get
(
url
,
stream
=
True
)
r
=
requests
.
get
(
url
,
stream
=
True
)
if
r
.
status_code
==
200
:
if
r
.
status_code
!=
200
:
class
AbortException
(
Exception
):
raise
Exception
(
"Invalid response status code:
%
s"
%
r
.
status_code
)
pass
if
task
.
is_aborted
():
class
AbortException
(
Exception
):
raise
AbortException
()
pass
if
parent_id
is
None
:
if
task
.
is_aborted
():
parent_id
=
task
.
request
.
id
raise
AbortException
()
chunk_size
=
256
*
1024
if
parent_id
is
None
:
ext
=
url
.
split
(
'.'
)[
-
1
]
.
lower
()
parent_id
=
task
.
request
.
id
if
ext
==
'gz'
:
chunk_size
=
256
*
1024
decompressor
=
decompressobj
(
16
+
MAX_WBITS
)
ext
=
url
.
split
(
'.'
)[
-
1
]
.
lower
()
# undocumented zlib feature http://stackoverflow.com/a/2424549
if
ext
==
'gz'
:
elif
ext
==
'bz2'
:
decompressor
=
decompressobj
(
16
+
MAX_WBITS
)
decompressor
=
BZ2Decompressor
()
# undocumented zlib feature http://stackoverflow.com/a/2424549
clen
=
max
(
int
(
r
.
headers
.
get
(
'content-length'
,
700000000
)),
1
)
elif
ext
==
'bz2'
:
percent
=
0
decompressor
=
BZ2Decompressor
()
try
:
clen
=
max
(
int
(
r
.
headers
.
get
(
'content-length'
,
700000000
)),
1
)
with
open
(
disk_path
,
'wb'
)
as
f
:
percent
=
0
for
chunk
in
r
.
iter_content
(
chunk_size
=
chunk_size
):
try
:
if
ext
in
(
'gz'
,
'bz'
):
with
open
(
disk_path
,
'wb'
)
as
f
:
chunk
=
decompressor
.
decompress
(
chunk
)
for
chunk
in
r
.
iter_content
(
chunk_size
=
chunk_size
):
f
.
write
(
chunk
)
if
ext
in
(
'gz'
,
'bz'
):
actsize
=
f
.
tell
()
chunk
=
decompressor
.
decompress
(
chunk
)
new_percent
=
min
(
100
,
round
(
actsize
*
100.0
/
clen
))
f
.
write
(
chunk
)
if
new_percent
>
percent
:
actsize
=
f
.
tell
()
percent
=
new_percent
new_percent
=
min
(
100
,
round
(
actsize
*
100.0
/
clen
))
if
not
task
.
is_aborted
():
if
new_percent
>
percent
:
task
.
update_state
(
percent
=
new_percent
task_id
=
parent_id
,
if
not
task
.
is_aborted
():
state
=
task
.
AsyncResult
(
parent_id
)
.
state
,
task
.
update_state
(
meta
=
{
'size'
:
actsize
,
'percent'
:
percent
})
task_id
=
parent_id
,
else
:
state
=
task
.
AsyncResult
(
parent_id
)
.
state
,
raise
AbortException
()
meta
=
{
'size'
:
actsize
,
'percent'
:
percent
})
if
ext
==
'gz'
:
else
:
f
.
write
(
decompressor
.
flush
())
raise
AbortException
()
f
.
flush
()
if
ext
==
'gz'
:
self
.
size
=
os
.
path
.
getsize
(
disk_path
)
f
.
write
(
decompressor
.
flush
())
logger
.
debug
(
"Download finished
%
s (
%
s bytes)"
,
f
.
flush
()
self
.
name
,
self
.
size
)
self
.
size
=
os
.
path
.
getsize
(
disk_path
)
except
AbortException
:
logger
.
debug
(
"Download finished
%
s (
%
s bytes)"
,
# Cleanup file:
self
.
name
,
self
.
size
)
os
.
unlink
(
disk_path
)
except
AbortException
:
logger
.
info
(
"Download
%
s aborted
%
s removed."
,
# Cleanup file:
url
,
disk_path
)
os
.
unlink
(
disk_path
)
except
:
logger
.
info
(
"Download
%
s aborted
%
s removed."
,
url
,
disk_path
)
except
:
os
.
unlink
(
disk_path
)
logger
.
error
(
"Download
%
s failed,
%
s removed."
,
url
,
disk_path
)
raise
else
:
if
ext
==
'zip'
and
is_zipfile
(
disk_path
):
task
.
update_state
(
task_id
=
parent_id
,
state
=
task
.
AsyncResult
(
parent_id
)
.
state
,
meta
=
{
'size'
:
actsize
,
'extracting'
:
'zip'
,
'percent'
:
99
})
self
.
extract_iso_from_zip
(
disk_path
)
if
not
self
.
check_valid_image
():
os
.
unlink
(
disk_path
)
os
.
unlink
(
disk_path
)
logger
.
error
(
"Download
%
s failed,
%
s removed."
,
raise
Exception
(
"Invalid file format. Only qcow and "
url
,
disk_path
)
"iso files are allowed."
)
raise
else
:
if
ext
==
'zip'
and
is_zipfile
(
disk_path
):
task
.
update_state
(
task_id
=
parent_id
,
state
=
task
.
AsyncResult
(
parent_id
)
.
state
,
meta
=
{
'size'
:
actsize
,
'extracting'
:
'zip'
,
'percent'
:
99
})
self
.
extract_iso_from_zip
(
disk_path
)
if
not
self
.
check_valid_image
():
os
.
unlink
(
disk_path
)
raise
Exception
(
"Invalid file format. Only qcow and "
"iso files are allowed."
)
def
extract_iso_from_zip
(
self
,
disk_path
):
def
extract_iso_from_zip
(
self
,
disk_path
):
with
ZipFile
(
disk_path
,
'r'
)
as
z
:
with
ZipFile
(
disk_path
,
'r'
)
as
z
:
...
...
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