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
12a7d752
authored
Sep 23, 2014
by
Guba Sándor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean up get method (now using json output)
parent
02f8e3c3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
22 deletions
+16
-22
disk.py
+16
-22
No files found.
disk.py
View file @
12a7d752
import
json
import
json
import
os
import
os
import
subprocess
import
subprocess
import
re
import
logging
import
logging
import
magic
import
magic
from
shutil
import
move
,
copyfileobj
from
shutil
import
move
,
copyfileobj
...
@@ -14,10 +13,6 @@ import requests
...
@@ -14,10 +13,6 @@ import requests
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
re_qemu_img
=
re
.
compile
(
r'(file format: (?P<format>(qcow2|raw))|'
r'virtual size: \w+ \((?P<size>[0-9]+) bytes\)|'
r'backing file: \S+ \(actual path: (?P<base>\S+)\))$'
)
class
AbortException
(
Exception
):
class
AbortException
(
Exception
):
pass
pass
...
@@ -33,7 +28,8 @@ class Disk(object):
...
@@ -33,7 +28,8 @@ class Disk(object):
FORMATS
=
[
'qcow2'
,
'raw'
,
'iso'
]
FORMATS
=
[
'qcow2'
,
'raw'
,
'iso'
]
CREATE_FORMATS
=
[
'qcow2'
,
'raw'
]
CREATE_FORMATS
=
[
'qcow2'
,
'raw'
]
def
__init__
(
self
,
dir
,
name
,
format
,
type
,
size
,
base_name
):
def
__init__
(
self
,
dir
,
name
,
format
,
type
,
size
,
base_name
,
actual_size
=
0
):
# TODO: tests
# TODO: tests
self
.
name
=
name
self
.
name
=
name
self
.
dir
=
os
.
path
.
realpath
(
dir
)
self
.
dir
=
os
.
path
.
realpath
(
dir
)
...
@@ -47,6 +43,7 @@ class Disk(object):
...
@@ -47,6 +43,7 @@ class Disk(object):
self
.
size
=
int
(
size
)
self
.
size
=
int
(
size
)
except
:
except
:
self
.
size
=
None
self
.
size
=
None
self
.
actual_size
=
actual_size
self
.
base_name
=
base_name
self
.
base_name
=
base_name
@classmethod
@classmethod
...
@@ -64,6 +61,7 @@ class Disk(object):
...
@@ -64,6 +61,7 @@ class Disk(object):
'dir'
:
self
.
dir
,
'dir'
:
self
.
dir
,
'format'
:
self
.
format
,
'format'
:
self
.
format
,
'size'
:
self
.
size
,
'size'
:
self
.
size
,
'actual_size'
:
self
.
actual_size
,
'base_name'
:
self
.
base_name
,
'base_name'
:
self
.
base_name
,
}
}
...
@@ -83,23 +81,19 @@ class Disk(object):
...
@@ -83,23 +81,19 @@ class Disk(object):
def
get
(
cls
,
dir
,
name
):
def
get
(
cls
,
dir
,
name
):
"""Create disk from path."""
"""Create disk from path."""
path
=
os
.
path
.
realpath
(
dir
+
'/'
+
name
)
path
=
os
.
path
.
realpath
(
dir
+
'/'
+
name
)
output
=
subprocess
.
check_output
([
'qemu-img'
,
'info'
,
path
])
output
=
subprocess
.
check_output
(
[
'qemu-img'
,
'info'
,
'--output=json'
,
path
])
type
=
'normal'
disk_info
=
json
.
loads
(
output
)
base
=
None
name
=
name
for
line
in
output
.
split
(
'
\n
'
):
format
=
disk_info
.
get
(
'format'
)
m
=
re_qemu_img
.
search
(
line
)
size
=
disk_info
.
get
(
'virtual-size'
)
if
m
:
actual_size
=
disk_info
.
get
(
'actual-size'
)
res
=
m
.
groupdict
()
base_name
=
disk_info
.
get
(
'backing-filename'
)
if
res
.
get
(
'format'
,
None
)
is
not
None
:
if
base_name
:
format
=
res
[
'format'
]
if
res
.
get
(
'size'
,
None
)
is
not
None
:
size
=
res
[
'size'
]
if
res
.
get
(
'base'
,
None
)
is
not
None
:
base
=
os
.
path
.
basename
(
res
[
'base'
])
type
=
'snapshot'
type
=
'snapshot'
else
:
return
Disk
(
dir
,
name
,
format
,
size
,
base
,
type
)
type
=
'normal'
return
Disk
(
dir
,
name
,
format
,
type
,
size
,
base_name
,
actual_size
)
def
create
(
self
):
def
create
(
self
):
""" Creating new image format specified at self.format.
""" Creating new image format specified at self.format.
...
...
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