Commit 12a7d752 by Guba Sándor

clean up get method (now using json output)

parent 02f8e3c3
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.
......
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 sign in to comment