2015-06-17 15:11:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import hashlib
|
|
|
|
|
2015-07-16 16:56:36 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2015-06-17 15:11:25 +00:00
|
|
|
|
2015-11-09 11:28:00 +00:00
|
|
|
|
2015-06-17 15:11:25 +00:00
|
|
|
def md5sum(path):
|
|
|
|
"""
|
|
|
|
Return the md5sum of an image and cache it on disk
|
|
|
|
|
|
|
|
:param path: Path to the image
|
|
|
|
:returns: Digest of the image
|
|
|
|
"""
|
|
|
|
|
2015-06-19 14:36:25 +00:00
|
|
|
if path is None or len(path) == 0 or not os.path.exists(path):
|
2015-06-17 15:11:25 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(path + '.md5sum') as f:
|
|
|
|
return f.read()
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2015-06-19 14:36:25 +00:00
|
|
|
try:
|
|
|
|
m = hashlib.md5()
|
|
|
|
with open(path, 'rb') as f:
|
|
|
|
while True:
|
|
|
|
buf = f.read(128)
|
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
m.update(buf)
|
|
|
|
digest = m.hexdigest()
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Can't create digest of %s: %s", path, str(e))
|
|
|
|
return None
|
2015-06-17 15:11:25 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
with open('{}.md5sum'.format(path), 'w+') as f:
|
|
|
|
f.write(digest)
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Can't write digest of %s: %s", path, str(e))
|
|
|
|
|
|
|
|
return digest
|
|
|
|
|
|
|
|
|
|
|
|
def remove_checksum(path):
|
|
|
|
"""
|
|
|
|
Remove the checksum of an image from cache if exists
|
|
|
|
"""
|
|
|
|
|
|
|
|
path = '{}.md5sum'.format(path)
|
|
|
|
if os.path.exists(path):
|
|
|
|
os.remove(path)
|