#!/usr/bin/python
+import hashlib
import logging
import os
import socket
import pakfire.packages
+CHUNK_SIZE = 2097152 # 2M
+
class MasterSlave(object):
@property
def hostname(self):
"""
return socket.gethostname()
- def upload_package_file(self, source_id, pkg_id, pkg):
- logging.info("Adding package file: %s" % pkg.filename)
+ def _chunked_upload(self, filename):
+ # Update the amount of chunks that there will be to be uploaded.
+ chunks = (os.path.getsize(filename) / CHUNK_SIZE) + 1
+
+ # Open the file for read.
+ f = open(filename, "rb")
+
+ chunk, id = 0, ""
+ while True:
+ # Read a chunk and break if we reached the end of the file.
+ data = f.read(CHUNK_SIZE)
+ if not data:
+ break
+
+ chunk += 1
+ logging.info("Uploading chunk %s/%s of %s." % (chunk, chunks, filename))
+
+ # Calc the hash of the chunk.
+ hash = hashlib.sha1(data)
+
+ # Actually do the upload and make sure we got an ID.
+ id = self.conn.chunk_upload(id, hash.hexdigest(), xmlrpclib.Binary(data))
+ assert id
- # Read-in the package payload.
- f = open(pkg.filename, "rb")
- payload = xmlrpclib.Binary(f.read())
f.close()
+ return id
+
+ def upload_package_file(self, source_id, pkg_id, pkg):
+ logging.info("Uploading package file: %s" % pkg.filename)
+
+ # Upload the file at first to the server.
+ file_id = self._chunked_upload(pkg.filename)
+
info = {
"filename" : os.path.basename(pkg.filename),
"source_id" : source_id,
"build_id" : pkg.build_id,
"build_time" : pkg.build_time,
"uuid" : pkg.uuid,
- "payload" : payload,
}
if isinstance(pkg, pakfire.packages.BinaryPackage):
"conflicts" : " ".join(pkg.conflicts),
})
- return self.conn.upload_package_file(pkg_id, info)
+ return self.conn.package_add_file(pkg_id, file_id, info)
def upload_log_file(self):
pass # XXX TO BE DONE