From: karim hamidou Date: Mon, 23 Jul 2012 08:09:48 +0000 (+0200) Subject: WIP - using base64 to transmit binary data X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=af35d3db9a8150713da3bc483ec0ef38e8264fb2;p=cagibi.git WIP - using base64 to transmit binary data --- diff --git a/cagibi/cagibi.py b/cagibi/cagibi.py index f9c99ad..1bdf92e 100644 --- a/cagibi/cagibi.py +++ b/cagibi/cagibi.py @@ -18,6 +18,7 @@ import json import time import os import threading +import base64 client_config = {} @@ -51,11 +52,12 @@ def upload_local_changes(): while not mqueue["modified"].empty(): file = mqueue["modified"].get() print "Detect %s modified" % file + url = "%s/files/%s/hashes" % (server_url, file) fd = urllib2.urlopen(url) hashes = json.load(fd) patchedfile = open(secure_path(cagibi_folder, file), "rb") - deltas = list(rsyncdelta(patchedfile, hashes)) + deltas = encode_deltas(rsyncdelta(patchedfile, hashes)) patchedfile.close() # Send the deltas to the server. @@ -74,6 +76,8 @@ def upload_local_changes(): while not mqueue["added"].empty(): file = mqueue["added"].get() + print "Detected %s added" % file + put_data = {} fd = open(secure_path(cagibi_folder, file), "r") put_data["contents"] = fd.read() @@ -88,6 +92,8 @@ def upload_local_changes(): while not mqueue["removed"].empty(): file = mqueue["removed"].get() + print "Detect %s removed" % file + url = "%s/files/%s" % (server_url, file) request = urllib2.Request(url) request.get_method = lambda: 'DELETE' @@ -117,7 +123,7 @@ def checkout_upstream_changes(): if server_files[file]["rev"] > local_files[file]["rev"]: # Get it too, but using the rsync algo - unpatched = open(secure_path(cagibi_folder, file).encode('ascii', 'ignore'), "rb") + unpatched = open(secure_path(cagibi_folder, file), "rb") hashes = list(blockchecksums(unpatched)) json_hashes = json.dumps(hashes) post_data = {} @@ -126,7 +132,7 @@ def checkout_upstream_changes(): url = "%s/files/%s/deltas" % (server_url, file) fd = urllib2.urlopen(url, post_string) - json_response = json.load(fd) + json_response = decode_deltas(json.load(fd)) unpatched.seek(0) save_to = os.tmpfile() @@ -136,7 +142,7 @@ def checkout_upstream_changes(): save_to.seek(0) # FIXME: rename instead of copying ? - file_copy = open(secure_path(cagibi_folder, file).encode('ascii', 'ignore'), "w+b") + file_copy = open(secure_path(cagibi_folder, file), "w+b") file_copy.write(save_to.read()) file_copy.close() save_to.close() diff --git a/cagibi/rsync.py b/cagibi/rsync.py index 0ef4fb0..fbcd1d3 100644 --- a/cagibi/rsync.py +++ b/cagibi/rsync.py @@ -26,6 +26,7 @@ http://samba.anu.edu.au/rsync/. import collections import hashlib +import base64 if not(hasattr(__builtins__, "bytes")) or str is bytes: # Python 2.x compatibility @@ -36,7 +37,7 @@ if not(hasattr(__builtins__, "bytes")) or str is bytes: return map(ord, var) __all__ = ["rollingchecksum", "weakchecksum", "patchstream", "rsyncdelta", - "blockchecksums"] + "blockchecksums", "encode_deltas", "decode_deltas"] def rsyncdelta(datastream, remotesignatures, blocksize=4096): @@ -174,3 +175,27 @@ def weakchecksum(data): b += (l - i)*data[i] return (b << 16) | a, a, b + +def encode_deltas(deltas): + """ + Returns base64-encoded rsync deltas + """ + def discriminator(element): + if isinstance(element, unicode): + return base64.b64encode(element) + else: + return element + + return map(discriminator, deltas) + +def decode_deltas(deltas): + """ + Return normal unicode strings in base64-encoded rsync deltas + """ + def discriminator(element): + if isinstance(element, str): + return base64.b64decode(element) + else: + return element + + return map(discriminator, deltas) diff --git a/cagibi/server.py b/cagibi/server.py index 21c8a72..f138241 100644 --- a/cagibi/server.py +++ b/cagibi/server.py @@ -2,8 +2,9 @@ from bottle import Bottle, route, run, post, get, put, delete, request, abort, static_file import os, time import json +import base64 from config import load_config, save_config -from rsync import rsyncdelta, blockchecksums, patchstream +from rsync import rsyncdelta, blockchecksums, patchstream, encode_deltas, decode_deltas from util import secure_path cagibi_folder = "." @@ -87,7 +88,8 @@ def file_hashes(filename): @post('/files//hashes') def update_file_hashes(filename): """Updates a file using the deltas received by a client""" - deltas = json.loads(request.forms.get("deltas")) + deltas = decode_deltas(json.loads(request.forms.get("deltas"))) + print deltas unpatched = open(secure_path(cagibi_folder, filename), "rb") save_to = os.tmpfile() @@ -97,7 +99,7 @@ def update_file_hashes(filename): save_to.seek(0) # FIXME: rename instead of copying ? - file_copy = open(secure_path(cagibi_folder, filename).encode('ascii', 'ignore'), "w+b") + file_copy = open(secure_path(cagibi_folder, filename), "w+b") file_copy.write(save_to.read()) file_copy.close() save_to.close() @@ -112,7 +114,7 @@ def return_deltas(filename): """return the deltas corresponding to the file. The client must send in its request the hashes of the file""" hashes = json.loads(request.forms.get("hashes")) patchedfile = open(secure_path(cagibi_folder, filename), "rb") - deltas = rsyncdelta(patchedfile, hashes) + deltas = encode_deltas(rsyncdelta(patchedfile, hashes)) patchedfile.close() return json.dumps(deltas)