Commit 257c6fbb authored by Nikos Skalkotos's avatar Nikos Skalkotos

Add an Image method for computing the MD5 checksum

This used to be a class in the util module, but since this is only used
on the Image, it suits better there.
parent b680d3a9
...@@ -27,7 +27,7 @@ import re ...@@ -27,7 +27,7 @@ import re
import time import time
from image_creator import __version__ as version from image_creator import __version__ as version
from image_creator.util import MD5, FatalError, virtio_versions from image_creator.util import FatalError, virtio_versions
from image_creator.output.dialog import GaugeOutput, InfoBoxOutput from image_creator.output.dialog import GaugeOutput, InfoBoxOutput
from image_creator.kamaki_wrapper import Kamaki, ClientError from image_creator.kamaki_wrapper import Kamaki, ClientError
from image_creator.help import get_help_file from image_creator.help import get_help_file
...@@ -149,8 +149,7 @@ def upload_image(session): ...@@ -149,8 +149,7 @@ def upload_image(session):
kamaki.out = out kamaki.out = out
try: try:
if 'checksum' not in session: if 'checksum' not in session:
md5 = MD5(out) session['checksum'] = image.md5()
session['checksum'] = md5.compute(image.device, image.size)
try: try:
# Upload image file # Upload image file
......
...@@ -24,7 +24,6 @@ import stat ...@@ -24,7 +24,6 @@ import stat
import re import re
import json import json
from image_creator.output.dialog import GaugeOutput from image_creator.output.dialog import GaugeOutput
from image_creator.util import MD5
from image_creator.kamaki_wrapper import Kamaki from image_creator.kamaki_wrapper import Kamaki
SMALL_WIDTH = 60 SMALL_WIDTH = 60
...@@ -198,8 +197,7 @@ def extract_image(session): ...@@ -198,8 +197,7 @@ def extract_image(session):
out.add(gauge) out.add(gauge)
try: try:
if "checksum" not in session: if "checksum" not in session:
md5 = MD5(out) session['checksum'] = image.md5()
session['checksum'] = md5.compute(image.device, image.size)
# Extract image file # Extract image file
image.dump(path) image.dump(path)
......
...@@ -25,7 +25,7 @@ import json ...@@ -25,7 +25,7 @@ import json
import re import re
from image_creator.kamaki_wrapper import Kamaki, ClientError from image_creator.kamaki_wrapper import Kamaki, ClientError
from image_creator.util import MD5, FatalError, virtio_versions from image_creator.util import FatalError, virtio_versions
from image_creator.output.cli import OutputWthProgress from image_creator.output.cli import OutputWthProgress
from image_creator.dialog_util import extract_image, update_background_title, \ from image_creator.dialog_util import extract_image, update_background_title, \
add_cloud, edit_cloud, update_sysprep_param add_cloud, edit_cloud, update_sysprep_param
...@@ -461,7 +461,7 @@ def create_image(session, answers): ...@@ -461,7 +461,7 @@ def create_image(session, answers):
metadata['DESCRIPTION'] = answers['ImageDescription'] metadata['DESCRIPTION'] = answers['ImageDescription']
# MD5 # MD5
session['checksum'] = MD5(image.out).compute(image.device, image.size) session['checksum'] = image.md5()
image.out.output() image.out.output()
try: try:
......
...@@ -21,6 +21,7 @@ from image_creator.os_type import os_cls ...@@ -21,6 +21,7 @@ from image_creator.os_type import os_cls
import re import re
import guestfs import guestfs
import hashlib
from sendfile import sendfile from sendfile import sendfile
...@@ -391,14 +392,13 @@ class Image(object): ...@@ -391,14 +392,13 @@ class Image(object):
partition table. Empty space in the end of the device will be ignored. partition table. Empty space in the end of the device will be ignored.
""" """
MB = 2 ** 20 MB = 2 ** 20
blocksize = 4 * MB # 4MB blocksize = 2 ** 22 # 4MB
size = self.size progr_size = (self.size + MB - 1) // MB # in MB
progr_size = (size + MB - 1) // MB # in MB
progressbar = self.out.Progress(progr_size, "Dumping image file", 'mb') progressbar = self.out.Progress(progr_size, "Dumping image file", 'mb')
with open(self.device, 'r') as src: with open(self.device, 'rb') as src:
with open(outfile, "w") as dst: with open(outfile, "wb") as dst:
left = size left = self.size
offset = 0 offset = 0
progressbar.next() progressbar.next()
while left > 0: while left > 0:
...@@ -414,7 +414,31 @@ class Image(object): ...@@ -414,7 +414,31 @@ class Image(object):
offset += sent offset += sent
left -= sent left -= sent
progressbar.goto((size - left) // MB) progressbar.goto((self.size - left) // MB)
progressbar.success('image file %s was successfully created' % outfile) progressbar.success('image file %s was successfully created' % outfile)
def md5(self):
"""Computes the MD5 checksum of the image"""
MB = 2 ** 20
blocksize = 2 ** 22 # 4MB
progr_size = ((self.size + MB - 1) // MB) # in MB
progressbar = self.out.Progress(progr_size, "Calculating md5sum", 'mb')
md5 = hashlib.md5()
with open(self.device, "rb") as src:
left = self.size
while left > 0:
length = min(left, blocksize)
data = src.read(length)
md5.update(data)
left -= length
progressbar.goto((self.size - left) // MB)
checksum = md5.hexdigest()
progressbar.success(checksum)
return checksum
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
...@@ -22,7 +22,7 @@ snf-image-creator program. ...@@ -22,7 +22,7 @@ snf-image-creator program.
from image_creator import __version__ as version from image_creator import __version__ as version
from image_creator.disk import Disk from image_creator.disk import Disk
from image_creator.util import FatalError, MD5 from image_creator.util import FatalError
from image_creator.output.cli import SilentOutput, SimpleOutput, \ from image_creator.output.cli import SilentOutput, SimpleOutput, \
OutputWthProgress OutputWthProgress
from image_creator.kamaki_wrapper import Kamaki, ClientError from image_creator.kamaki_wrapper import Kamaki, ClientError
...@@ -314,8 +314,7 @@ def image_creator(): ...@@ -314,8 +314,7 @@ def image_creator():
# Add command line metadata to the collected ones... # Add command line metadata to the collected ones...
metadata.update(options.metadata) metadata.update(options.metadata)
md5 = MD5(out) checksum = image.md5()
checksum = md5.compute(image.device, image.size)
metastring = unicode(json.dumps( metastring = unicode(json.dumps(
{'properties': metadata, {'properties': metadata,
......
...@@ -20,7 +20,6 @@ the package. ...@@ -20,7 +20,6 @@ the package.
""" """
import sh import sh
import hashlib
import time import time
import os import os
import re import re
...@@ -97,35 +96,6 @@ def free_space(dirname): ...@@ -97,35 +96,6 @@ def free_space(dirname):
return stat.f_bavail * stat.f_frsize return stat.f_bavail * stat.f_frsize
class MD5:
"""Represents MD5 computations"""
def __init__(self, output):
"""Create an MD5 instance"""
self.out = output
def compute(self, filename, size):
"""Compute the MD5 checksum of a file"""
MB = 2 ** 20
BLOCKSIZE = 4 * MB # 4MB
prog_size = ((size + MB - 1) // MB) # in MB
progressbar = self.out.Progress(prog_size, "Calculating md5sum", 'mb')
md5 = hashlib.md5()
with open(filename, "r") as src:
left = size
while left > 0:
length = min(left, BLOCKSIZE)
data = src.read(length)
md5.update(data)
left -= length
progressbar.goto((size - left) // MB)
checksum = md5.hexdigest()
progressbar.success(checksum)
return checksum
def virtio_versions(virtio_state): def virtio_versions(virtio_state):
"""Returns the versions of the drivers defined by the virtio state""" """Returns the versions of the drivers defined by the virtio state"""
......
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