Commit dac81d22 authored by Łukasz Nowak's avatar Łukasz Nowak

Follow specification.

Required parameters are:

 * file: the name of the file
 * urlmd5: mdsum of orginal url
 * sha512: the hash (sha512) of the file content

Optional ones:

 * valid-until: the date which the file must be expired
 * architecture: computer architecture
parent 921ae014
......@@ -79,15 +79,12 @@ class NetworkcacheClient(object):
elif value.startswith('http'):
self.signature_certificate_url_list.append(value)
def upload(self, file_descriptor, directory_key=None, **kw):
def upload(self, file_descriptor, urlmd5=None, file_name=None,
valid_until=None, architecture=None):
''' Upload the file to the server.
If directory_key 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.
'''
if directory_key is not None and 'urlmd5' not in kw:
msg = 'The parameter "urlmd5" is required once you set directory_key.'
raise ValueError(msg)
sha512sum = hashlib.sha512()
# do not trust, go to beginning of opened file
file_descriptor.seek(0)
......@@ -114,16 +111,18 @@ class NetworkcacheClient(object):
'URL: %s. Response code: %s. Response data: %s' % \
(self.shacache_host, result.status, data))
if directory_key is not None:
path = os.path.join(self.shadir_path, directory_key)
file_name = kw.get('file', None)
if urlmd5 is not None:
if file_name is None:
kw['file'] = os.path.basename(file_descriptor.name)
raise ValueError('In case if urlmd5 is given file_name is required.')
kw = dict()
kw['file'] = file_name
kw['urlmd5'] = urlmd5
kw['sha512'] = sha512sum
sha512 = kw.get('sha512', None)
if sha512 is None:
kw['sha512'] = sha512sum
if valid_until is not None:
kw['valid-until'] = valid_until
if architecture is not None:
kw['architecture'] = architecture
signature = self._getSignatureString()
data = [kw, signature]
......@@ -131,7 +130,7 @@ class NetworkcacheClient(object):
shadir_connection = httplib.HTTPConnection(self.shadir_host,
self.shadir_port)
try:
shadir_connection.request('POST', path, json.dumps(data),
shadir_connection.request('POST', self.shadir_path, json.dumps(data),
self.shadir_header_dict)
result = shadir_connection.getresponse()
data = result.read()
......
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