Commit 2a9024fc authored by Yingjie Xu's avatar Yingjie Xu

Extend methods to use binary cache.

parent ec6baeee
...@@ -24,7 +24,8 @@ import tempfile ...@@ -24,7 +24,8 @@ import tempfile
import traceback import traceback
import urllib2 import urllib2
import urlparse import urlparse
import ctypes
from ctypes import util
class NetworkcacheClient(object): class NetworkcacheClient(object):
''' '''
...@@ -112,7 +113,7 @@ class NetworkcacheClient(object): ...@@ -112,7 +113,7 @@ class NetworkcacheClient(object):
self.shadir_cert_file = shadir_cert_file self.shadir_cert_file = shadir_cert_file
def upload(self, file_descriptor, key=None, urlmd5=None, file_name=None, def upload(self, file_descriptor, key=None, urlmd5=None, file_name=None,
valid_until=None, architecture=None): valid_until=None, architecture=None, kernel=None, libc_version=None):
''' Upload the file to the server. ''' Upload the file to the server.
If urlmd5 is None it must only upload to SHACACHE. If urlmd5 is None it must only upload to SHACACHE.
Otherwise, it must create a new entry on SHADIR. Otherwise, it must create a new entry on SHADIR.
...@@ -161,6 +162,10 @@ class NetworkcacheClient(object): ...@@ -161,6 +162,10 @@ class NetworkcacheClient(object):
kw['valid-until'] = valid_until kw['valid-until'] = valid_until
if architecture is not None: if architecture is not None:
kw['architecture'] = architecture kw['architecture'] = architecture
if kernel is not None:
kw['kernel'] = kernel
if libc_version is not None:
kw['libc-version'] = libc_version
sha_entry = json.dumps(kw) sha_entry = json.dumps(kw)
try: try:
...@@ -200,7 +205,7 @@ class NetworkcacheClient(object): ...@@ -200,7 +205,7 @@ class NetworkcacheClient(object):
headers=self.shadir_header_dict) headers=self.shadir_header_dict)
return urllib2.urlopen(request) return urllib2.urlopen(request)
def select(self, key): def select(self, key, binary_mode=False):
''' Download a file from shacache by selecting the entry in shadir ''' Download a file from shacache by selecting the entry in shadir
Raise DirectoryNotFound if multiple files are found. Raise DirectoryNotFound if multiple files are found.
''' '''
...@@ -218,7 +223,8 @@ class NetworkcacheClient(object): ...@@ -218,7 +223,8 @@ class NetworkcacheClient(object):
if self.signature_certificate_list is not None: if self.signature_certificate_list is not None:
for data in data_list: for data in data_list:
if len(data[1]): if len(data[1]):
if self._verifySignatureInCertificateList(data[0], data[1]): if self._verifySignatureInCertificateList(data[0], data[1]) and \
self._isCompatible(data[0], binary_mode=binary_mode):
filtered_data_list.append(data) filtered_data_list.append(data)
else: else:
filtered_data_list = data_list filtered_data_list = data_list
...@@ -318,6 +324,26 @@ class NetworkcacheClient(object): ...@@ -318,6 +324,26 @@ class NetworkcacheClient(object):
content_file.close() content_file.close()
pubkey_file.close() pubkey_file.close()
def _isCompatible(self, information_json, binary_mode=False):
if not binary_mode:
return True
try:
information_dict = json.loads(information_json)
except Exception:
raise DirectoryNotFound('It was impossible to parse json-in-json '
'response:\n%s' % traceback.format_exc())
_, _, kernel, _, _ = os.uname()
LIBC = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c'))
libc_version = LIBC.gnu_get_libc_version
libc_version.restype = ctypes.c_char_p
try:
if kernel != information_dict.get('kernel'):
return False
if ("libc-%s" % libc_version()) != information_dict.get('libc-version'):
return False
except Exception:
return False
return True
class DirectoryNotFound(Exception): class DirectoryNotFound(Exception):
pass pass
......
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