Commit 9b1213ff authored by Lucas Carvalho's avatar Lucas Carvalho

Simplify the code to starting to integrate with the new design of networkcache http server.

parent fb3af4fc
...@@ -13,39 +13,36 @@ ...@@ -13,39 +13,36 @@
############################################################################## ##############################################################################
import hashlib
import os import os
import posixpath import posixpath
import hashlib import re
from libnetworkcache import NetworkcacheClient from download import check_md5sum
from slapos.tool.libnetworkcache import NetworkcacheClient
def _get_hash_from_file(path, hash_object): _md5_re = re.compile(r'md5=([a-f0-9]+)')
"""
Calculate the hash from file.
"""
f = open(path, 'rb')
try:
chunk = f.read(2**16)
while chunk:
hash_object.update(chunk)
chunk = f.read(2**16)
return hash_object.hexdigest()
finally:
f.close()
def check_sha256sum(path, sha256sum): def _get_md5_from_url(url):
"""Tell whether the SHA256sum checksum of the file at path matches match = _md5_re.search(url)
""" if match:
return sha256sum == _get_hash_from_file(path, hashlib.sha256()) return match.group(1)
return None
def _update_network_cached_log(key, url): def _update_network_cached_log(path, url):
"""Update the networkcached.log file. """Update the networkcached.log file.
Adding the last key and URL which has been retrieved from networkcached server. Adding the last key and URL which has been retrieved from networkcached server.
""" """
f = open(path, 'r')
try:
key = hashlib.sha512(f.read()).hexdigest()
finally:
f.close()
# Keep the log # Keep the log
map_path = os.path.join(os.environ.get('PWD'), '.networkcached.log') map_path = os.path.join(os.environ.get('PWD'), '.networkcached.log')
mode = 'r+' mode = 'r+'
...@@ -66,7 +63,7 @@ def _update_network_cached_log(key, url): ...@@ -66,7 +63,7 @@ def _update_network_cached_log(key, url):
f.close() f.close()
def download_network_cached(network_cache, path, url, logger): def download_network_cached(network_cache, path, url, logger, md5sum=None):
"""Download from a network cache provider """Download from a network cache provider
If something fail (providor be offline, or hash_string fail), we ignore If something fail (providor be offline, or hash_string fail), we ignore
...@@ -77,19 +74,33 @@ def download_network_cached(network_cache, path, url, logger): ...@@ -77,19 +74,33 @@ def download_network_cached(network_cache, path, url, logger):
if network_cache in (None, '',): if network_cache in (None, '',):
# Not able to use network cache # Not able to use network cache
return False return False
if md5sum is None:
print "URL ", url
md5sum = _get_md5_from_url(url)
# XXX (lucas): This will be removed.
key = hashlib.sha512(url).hexdigest()
key = hashlib.sha256(url).hexdigest()
network_cached_url = os.path.join(network_cache, key) network_cached_url = os.path.join(network_cache, key)
logger.info('Downloading from network cache %s' % network_cached_url) logger.info('Downloading from network cache %s' % network_cached_url)
try: try:
nc = NetworkcacheClient(network_cache) nc = NetworkcacheClient(network_cache)
result, path = nc.get(key, path) result = nc.get(key)
if result.status != 200: if result.status != 200:
logger.info('Fail to download from network cache: File Not Found.') logger.info('Fail to download from network cache: File Not Found.')
return False return False
if not check_sha256sum(path, result.getheader('x-sha256sum')): file_content = result.read()
logger.info('SHA256 checksum mismatch downloading %r' % \ # save the file
f = open(path, 'w+b')
try:
f.write(file_content)
finally:
f.close()
if not check_md5sum(path, md5sum):
logger.info('MD5 checksum mismatch downloading %r' % \
network_cached_url) network_cached_url)
return False return False
except IOError, e: except IOError, e:
...@@ -107,12 +118,11 @@ def upload_network_cached(network_cache, external_url, path, logger): ...@@ -107,12 +118,11 @@ def upload_network_cached(network_cache, external_url, path, logger):
'Upload cache ignored, network-cache was not provided') 'Upload cache ignored, network-cache was not provided')
return False return False
key = hashlib.sha256(external_url).hexdigest()
try: try:
nc = NetworkcacheClient(network_cache) nc = NetworkcacheClient(network_cache)
result = nc.put(key, path) result = nc.put(path)
if result.status == 200: if result.status == 200:
_update_network_cached_log(key, external_url) _update_network_cached_log(path, external_url)
except (IOError, EOFError), e: except (IOError, EOFError), e:
logger.info('Fail to upload cache on %s. %s' % \ logger.info('Fail to upload cache on %s. %s' % \
......
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